View Javadoc

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