View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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.event.ActionEvent;
16  
17  import javax.swing.AbstractAction;
18  import javax.swing.Action;
19  import javax.swing.JPopupMenu;
20  import javax.swing.event.PopupMenuEvent;
21  import javax.swing.event.PopupMenuListener;
22  
23  import com.eviware.soapui.support.UISupport;
24  import com.eviware.soapui.support.xml.JXEditTextArea;
25  
26  public final class JXEditAreaPopupMenu extends JPopupMenu implements PopupMenuListener
27  {
28  	private final JXEditTextArea textComponent;
29  	private CutAction cutAction;
30  	private CopyAction copyAction;
31  	private PasteAction pasteAction;
32  	private ClearAction clearAction;
33  	private SelectAllAction selectAllAction;
34  	private UndoAction undoAction;
35  	private RedoAction redoAction;
36  
37  	public static JXEditAreaPopupMenu add( JXEditTextArea textComponent )
38  	{
39  		// double-check
40  		if( textComponent.getRightClickPopup() instanceof JXEditAreaPopupMenu )
41  			return ( JXEditAreaPopupMenu ) textComponent.getRightClickPopup();
42  		
43  		JXEditAreaPopupMenu popupMenu = new JXEditAreaPopupMenu( textComponent );
44  		textComponent.setRightClickPopup( popupMenu );
45  		return popupMenu;
46  	}
47  	
48  	private JXEditAreaPopupMenu(JXEditTextArea textComponent)
49  	{
50  		super( "Edit" );
51  		this.textComponent = textComponent;
52  		
53  		undoAction = new UndoAction();
54  		add( undoAction );
55  		
56  		redoAction = new RedoAction();
57  		add( redoAction );
58  		
59  		addSeparator();
60  		
61  		cutAction = new CutAction();
62  		add( cutAction );
63  		copyAction = new CopyAction();
64  		add( copyAction );
65  		pasteAction = new PasteAction();
66  		add( pasteAction );
67  		clearAction = new ClearAction();
68  		add( clearAction );
69  		addSeparator();
70  		selectAllAction = new SelectAllAction();
71  		add( selectAllAction );
72  		
73  		addPopupMenuListener( this );
74  	}
75  	
76  	private final class CutAction extends AbstractAction
77  	{
78  		public CutAction()
79  		{
80  			super( "Cut" );
81  			putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu X" ));
82  		}
83  
84  		public void actionPerformed( ActionEvent e )
85  		{
86  			textComponent.cut();
87  		}
88  	}
89  	
90  	private final class CopyAction extends AbstractAction
91  	{
92  		public CopyAction()
93  		{
94  			super( "Copy" );
95  			putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu C" ));
96  		}
97  
98  		public void actionPerformed( ActionEvent e )
99  		{
100 			textComponent.copy();
101 		}
102 	}
103 	
104 	private final class PasteAction extends AbstractAction
105 	{
106 		public PasteAction()
107 		{
108 			super( "Paste" );
109 			putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu V" ));
110 		}
111 
112 		public void actionPerformed( ActionEvent e )
113 		{
114 			textComponent.paste();
115 		}
116 	}
117 	
118 	private final class ClearAction extends AbstractAction
119 	{
120 		public ClearAction()
121 		{
122 			super( "Clear" );
123 		}
124 
125 		public void actionPerformed( ActionEvent e )
126 		{
127 			textComponent.setText( "" );
128 		}
129 	}
130 	
131 	private final class SelectAllAction extends AbstractAction
132 	{
133 		public SelectAllAction()
134 		{
135 			super( "Select All" );
136 			putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu A" ));
137 		}
138 
139 		public void actionPerformed( ActionEvent e )
140 		{
141 			textComponent.selectAll();
142 		}
143 	}
144 	
145 	private final class UndoAction extends AbstractAction
146 	{
147 		public UndoAction()
148 		{
149 			super( "Undo" );
150 			putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu Z" ));
151 		}
152 
153 		public void actionPerformed( ActionEvent e )
154 		{
155 			textComponent.undo();
156 		}
157 	}
158 	
159 	private final class RedoAction extends AbstractAction
160 	{
161 		public RedoAction()
162 		{
163 			super( "Redo" );
164 			putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu Y" ));
165 		}
166 
167 		public void actionPerformed( ActionEvent e )
168 		{
169 			textComponent.redo();
170 		}
171 	}
172 	
173 
174 	public void popupMenuCanceled( PopupMenuEvent e )
175 	{
176 	}
177 
178 	public void popupMenuWillBecomeInvisible( PopupMenuEvent e )
179 	{
180 	}
181 
182 	public void popupMenuWillBecomeVisible( PopupMenuEvent e )
183 	{
184 		undoAction.setEnabled( textComponent.canUndo() );
185 		redoAction.setEnabled( textComponent.canRedo() );
186 		
187 		cutAction.setEnabled( textComponent.getSelectionEnd() !=  textComponent.getSelectionStart() );
188 		copyAction.setEnabled( cutAction.isEnabled() );
189 		clearAction.setEnabled( cutAction.isEnabled() );
190 		selectAllAction.setEnabled( textComponent.getText().length() > 0 );
191 	}
192 }