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  		JTextComponentPopupMenu popupMenu = new JTextComponentPopupMenu( textComponent );
39  		textComponent.setComponentPopupMenu( popupMenu );
40  		return popupMenu;
41  	}
42  	
43  	private JTextComponentPopupMenu(JTextComponent textComponent)
44  	{
45  		super( "Edit" );
46  		this.textComponent = textComponent;
47  		
48  		if( textComponent instanceof Undoable )
49  		{
50  			undoAction = new UndoAction();
51  			add( undoAction );
52  			
53  			redoAction = new RedoAction();
54  			add( redoAction );
55  			
56  			addSeparator();
57  		}
58  		
59  		cutAction = new CutAction();
60  		add( cutAction );
61  		copyAction = new CopyAction();
62  		add( copyAction );
63  		pasteAction = new PasteAction();
64  		add( pasteAction );
65  		clearAction = new ClearAction();
66  		add( clearAction );
67  		addSeparator();
68  		selectAllAction = new SelectAllAction();
69  		add( selectAllAction );
70  		
71  		addPopupMenuListener( this );
72  	}
73  	
74  	private final class CutAction extends AbstractAction
75  	{
76  		public CutAction()
77  		{
78  			super( "Cut" );
79  		}
80  
81  		public void actionPerformed( ActionEvent e )
82  		{
83  			textComponent.cut();
84  		}
85  	}
86  	
87  	private final class CopyAction extends AbstractAction
88  	{
89  		public CopyAction()
90  		{
91  			super( "Copy" );
92  		}
93  
94  		public void actionPerformed( ActionEvent e )
95  		{
96  			textComponent.copy();
97  		}
98  	}
99  	
100 	private final class PasteAction extends AbstractAction
101 	{
102 		public PasteAction()
103 		{
104 			super( "Paste" );
105 		}
106 
107 		public void actionPerformed( ActionEvent e )
108 		{
109 			textComponent.paste();
110 		}
111 	}
112 	
113 	private final class ClearAction extends AbstractAction
114 	{
115 		public ClearAction()
116 		{
117 			super( "Clear" );
118 		}
119 
120 		public void actionPerformed( ActionEvent e )
121 		{
122 			textComponent.setText( "" );
123 		}
124 	}
125 	
126 	private final class SelectAllAction extends AbstractAction
127 	{
128 		public SelectAllAction()
129 		{
130 			super( "Select All" );
131 		}
132 
133 		public void actionPerformed( ActionEvent e )
134 		{
135 			textComponent.selectAll();
136 		}
137 	}
138 	
139 	private final class UndoAction extends AbstractAction
140 	{
141 		public UndoAction()
142 		{
143 			super( "Undo" );
144 		}
145 
146 		public void actionPerformed( ActionEvent e )
147 		{
148 			((Undoable)textComponent).undo();
149 		}
150 	}
151 	
152 	private final class RedoAction extends AbstractAction
153 	{
154 		public RedoAction()
155 		{
156 			super( "Redo" );
157 		}
158 
159 		public void actionPerformed( ActionEvent e )
160 		{
161 			((Undoable)textComponent).redo();
162 		}
163 	}
164 	
165 
166 	public void popupMenuCanceled( PopupMenuEvent e )
167 	{
168 	}
169 
170 	public void popupMenuWillBecomeInvisible( PopupMenuEvent e )
171 	{
172 	}
173 
174 	public void popupMenuWillBecomeVisible( PopupMenuEvent e )
175 	{
176 		if( textComponent instanceof Undoable )
177 		{
178 			undoAction.setEnabled( ((Undoable)textComponent ).canUndo() );
179 			redoAction.setEnabled( ((Undoable)textComponent ).canRedo() );
180 		}
181 		
182 		cutAction.setEnabled( textComponent.getSelectionEnd() !=  textComponent.getSelectionStart() );
183 		copyAction.setEnabled( cutAction.isEnabled() );
184 		clearAction.setEnabled( cutAction.isEnabled() );
185 		selectAllAction.setEnabled( cutAction.isEnabled() );
186 	}
187 }