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