View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    11   */
    12
    13  package com.eviware.soapui.support.swing;
    14
    15  import java.awt.Component;
    16  import java.awt.event.ActionEvent;
    17
    18  import javax.swing.AbstractAction;
    19  import javax.swing.Action;
    20  import javax.swing.JPopupMenu;
    21  import javax.swing.JSeparator;
    22  import javax.swing.event.PopupMenuEvent;
    23  import javax.swing.event.PopupMenuListener;
    24  import javax.swing.text.JTextComponent;
    25
    26  import com.eviware.soapui.support.UISupport;
    27  import com.eviware.soapui.support.components.Undoable;
    28
    29  public final class JTextComponentPopupMenu extends JPopupMenu implements PopupMenuListener
    30  {
    31  	private final JTextComponent textComponent;
    32  	private CutAction cutAction;
    33  	private CopyAction copyAction;
    34  	private PasteAction pasteAction;
    35  	private ClearAction clearAction;
    36  	private SelectAllAction selectAllAction;
    37  	private UndoAction undoAction;
    38  	private RedoAction redoAction;
    39
    40  	public static JTextComponentPopupMenu add( JTextComponent textComponent )
    41  	{
    42  		JPopupMenu componentPopupMenu = textComponent.getComponentPopupMenu();
    43
    44  		// double-check
    45  		if( componentPopupMenu instanceof JTextComponentPopupMenu )
    46  			return ( JTextComponentPopupMenu ) componentPopupMenu;
    47
    48  		JTextComponentPopupMenu popupMenu = new JTextComponentPopupMenu( textComponent );
    49  		if( componentPopupMenu != null && componentPopupMenu.getComponentCount() > 0)
    50  		{
    51  			popupMenu.insert( new JSeparator(), 0 );
    52
    53  			while( componentPopupMenu.getComponentCount() > 0 )
    54  			{
    55  				Component comp = componentPopupMenu.getComponent( componentPopupMenu.getComponentCount()-1 );
    56  				componentPopupMenu.remove( comp );
    57  				popupMenu.insert( comp, 0 );
    58  			}
    59  		}
    60
    61  		if( componentPopupMenu != null )
    62  		{
    63  			for( PopupMenuListener listener : componentPopupMenu.getPopupMenuListeners() )
    64  			{
    65  				popupMenu.addPopupMenuListener( listener );
    66  			}
    67  		}
    68
    69  		textComponent.setComponentPopupMenu( popupMenu );
    70  		return popupMenu;
    71  	}
    72
    73  	private JTextComponentPopupMenu(JTextComponent textComponent)
    74  	{
    75  		super( "Edit" );
    76  		this.textComponent = textComponent;
    77
    78  		if( textComponent instanceof Undoable )
    79  		{
    80  			undoAction = new UndoAction();
    81  			add( undoAction );
    82
    83  			redoAction = new RedoAction();
    84  			add( redoAction );
    85
    86  			addSeparator();
    87  		}
    88
    89  		cutAction = new CutAction();
    90  		add( cutAction );
    91  		copyAction = new CopyAction();
    92  		add( copyAction );
    93  		pasteAction = new PasteAction();
    94  		add( pasteAction );
    95  		clearAction = new ClearAction();
    96  		add( clearAction );
    97  		addSeparator();
    98  		selectAllAction = new SelectAllAction();
    99  		add( selectAllAction );
    100
    101 		addPopupMenuListener( this );
    102 	}
    103
    104 	private final class CutAction extends AbstractAction
    105 	{
    106 		public CutAction()
    107 		{
    108 			super( "Cut" );
    109 			putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu X" ));
    110 		}
    111
    112 		public void actionPerformed( ActionEvent e )
    113 		{
    114 			textComponent.cut();
    115 		}
    116 	}
    117
    118 	private final class CopyAction extends AbstractAction
    119 	{
    120 		public CopyAction()
    121 		{
    122 			super( "Copy" );
    123 			putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu C" ));
    124 		}
    125
    126 		public void actionPerformed( ActionEvent e )
    127 		{
    128 			textComponent.copy();
    129 		}
    130 	}
    131
    132 	private final class PasteAction extends AbstractAction
    133 	{
    134 		public PasteAction()
    135 		{
    136 			super( "Paste" );
    137 			putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu V" ));
    138 		}
    139
    140 		public void actionPerformed( ActionEvent e )
    141 		{
    142 			textComponent.paste();
    143 		}
    144 	}
    145
    146 	private final class ClearAction extends AbstractAction
    147 	{
    148 		public ClearAction()
    149 		{
    150 			super( "Clear" );
    151 		}
    152
    153 		public void actionPerformed( ActionEvent e )
    154 		{
    155 			textComponent.setText( "" );
    156 		}
    157 	}
    158
    159 	private final class SelectAllAction extends AbstractAction
    160 	{
    161 		public SelectAllAction()
    162 		{
    163 			super( "Select All" );
    164 			putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu A" ));
    165 		}
    166
    167 		public void actionPerformed( ActionEvent e )
    168 		{
    169 			textComponent.selectAll();
    170 		}
    171 	}
    172
    173 	private final class UndoAction extends AbstractAction
    174 	{
    175 		public UndoAction()
    176 		{
    177 			super( "Undo" );
    178 			putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu Z" ));
    179 		}
    180
    181 		public void actionPerformed( ActionEvent e )
    182 		{
    183 			((Undoable)textComponent).undo();
    184 		}
    185 	}
    186
    187 	private final class RedoAction extends AbstractAction
    188 	{
    189 		public RedoAction()
    190 		{
    191 			super( "Redo" );
    192 			putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu Y" ));
    193 		}
    194
    195 		public void actionPerformed( ActionEvent e )
    196 		{
    197 			((Undoable)textComponent).redo();
    198 		}
    199 	}
    200
    201
    202 	public void popupMenuCanceled( PopupMenuEvent e )
    203 	{
    204 	}
    205
    206 	public void popupMenuWillBecomeInvisible( PopupMenuEvent e )
    207 	{
    208 	}
    209
    210 	public void popupMenuWillBecomeVisible( PopupMenuEvent e )
    211 	{
    212 		if( textComponent instanceof Undoable )
    213 		{
    214 			undoAction.setEnabled( ((Undoable)textComponent ).canUndo() );
    215 			redoAction.setEnabled( ((Undoable)textComponent ).canRedo() );
    216 		}
    217
    218 		cutAction.setEnabled( textComponent.getSelectionEnd() !=  textComponent.getSelectionStart() );
    219 		copyAction.setEnabled( cutAction.isEnabled() );
    220 		clearAction.setEnabled( cutAction.isEnabled() );
    221 		selectAllAction.setEnabled( textComponent.getText().length() > 0 );
    222 	}
    223 }