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