Debian Codegraph @

laf-widget/src/main/java/org/pushingpixels/lafwidget/text/EditContextMenuWidget.java

raw
1 /*
2 * Copyright (c) 2005-2010 Laf-Widget Kirill Grouchnikov. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * o Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * o Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * o Neither the name of Laf-Widget Kirill Grouchnikov nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 package org.pushingpixels.lafwidget.text;
31
32 import java.awt.Point;
33 import java.awt.Toolkit;
34 import java.awt.datatransfer.DataFlavor;
35 import java.awt.datatransfer.Transferable;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.MouseAdapter;
38 import java.awt.event.MouseEvent;
39 import java.awt.event.MouseListener;
40
41 import javax.swing.AbstractAction;
42 import javax.swing.ImageIcon;
43 import javax.swing.JPopupMenu;
44 import javax.swing.SwingUtilities;
45 import javax.swing.text.JTextComponent;
46
47 import org.pushingpixels.lafwidget.LafWidgetAdapter;
48 import org.pushingpixels.lafwidget.LafWidgetUtilities;
49
50 /**
51 * Adds edit context menu on text components.
52 *
53 * @author Kirill Grouchnikov
54 */
55 public class EditContextMenuWidget extends LafWidgetAdapter<JTextComponent> {
56 /**
57 * Mouse listener for showing the edit context menu.
58 */
59 protected MouseListener menuMouseListener;
60
61 /*
62 * (non-Javadoc)
63 *
64 * @see org.pushingpixels.lafwidget.LafWidget#requiresCustomLafSupport()
65 */
66 @Override
67 public boolean requiresCustomLafSupport() {
68 return false;
69 }
70
71 /*
72 * (non-Javadoc)
73 *
74 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#installListeners()
75 */
76 @Override
77 public void installListeners() {
78 this.menuMouseListener = new MouseAdapter() {
79 // fix for issue 8 - use mousePressed instead of
80 // mouseClicked so that it will be triggered on Linux.
81 @Override
82 public void mousePressed(MouseEvent e) {
83 this.handleMouseEvent(e);
84 }
85
86 @Override
87 public void mouseReleased(MouseEvent e) {
88 this.handleMouseEvent(e);
89 }
90
91 private void handleMouseEvent(MouseEvent e) {
92 if (!LafWidgetUtilities.hasTextEditContextMenu(jcomp))
93 return;
94 if (!e.isPopupTrigger())
95 return;
96
97 // request focus
98 jcomp.requestFocus(true);
99
100 JPopupMenu editMenu = new JPopupMenu();
101 editMenu.add(new CutAction());
102 editMenu.add(new CopyAction());
103 editMenu.add(new PasteAction());
104 editMenu.addSeparator();
105 editMenu.add(new DeleteAction());
106 editMenu.add(new SelectAllAction());
107
108 Point pt = SwingUtilities.convertPoint(e.getComponent(), e
109 .getPoint(), jcomp);
110 editMenu.show(jcomp, pt.x, pt.y);
111 }
112 };
113 jcomp.addMouseListener(this.menuMouseListener);
114 }
115
116 /*
117 * (non-Javadoc)
118 *
119 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#uninstallListeners()
120 */
121 @Override
122 public void uninstallListeners() {
123 jcomp.removeMouseListener(this.menuMouseListener);
124 this.menuMouseListener = null;
125 }
126
127 /**
128 * <code>Paste</code> action.
129 *
130 * @author Kirill Grouchnikov
131 */
132 private class PasteAction extends AbstractAction {
133 /**
134 * Creates new <code>Paste</code> action.
135 */
136 public PasteAction() {
137 super(LafWidgetUtilities.getResourceBundle(jcomp).getString(
138 "EditMenu.paste"), new ImageIcon(
139 EditContextMenuWidget.class.getClassLoader().getResource(
140 "org/pushingpixels/lafwidget/text/edit-paste.png")));
141 }
142
143 /*
144 * (non-Javadoc)
145 *
146 * @see
147 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent
148 * )
149 */
150 @Override
151 public void actionPerformed(ActionEvent e) {
152 jcomp.paste();
153 }
154
155 /*
156 * (non-Javadoc)
157 *
158 * @see javax.swing.AbstractAction#isEnabled()
159 */
160 @Override
161 public boolean isEnabled() {
162 if (jcomp.isEditable() && jcomp.isEnabled()) {
163 Transferable contents = Toolkit.getDefaultToolkit()
164 .getSystemClipboard().getContents(this);
165 return contents.isDataFlavorSupported(DataFlavor.stringFlavor);
166 } else
167 return false;
168 }
169 }
170
171 /**
172 * <code>Select All</code> action.
173 *
174 * @author Kirill Grouchnikov
175 */
176 private class SelectAllAction extends AbstractAction {
177 /**
178 * Creates new <code>Select All</code> action.
179 */
180 public SelectAllAction() {
181 super(
182 LafWidgetUtilities.getResourceBundle(jcomp).getString(
183 "EditMenu.selectAll"),
184 new ImageIcon(
185 EditContextMenuWidget.class
186 .getClassLoader()
187 .getResource(
188 "org/pushingpixels/lafwidget/text/edit-select-all.png")));
189 }
190
191 /*
192 * (non-Javadoc)
193 *
194 * @see
195 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent
196 * )
197 */
198 @Override
199 public void actionPerformed(ActionEvent e) {
200 jcomp.selectAll();
201 }
202
203 /*
204 * (non-Javadoc)
205 *
206 * @see javax.swing.AbstractAction#isEnabled()
207 */
208 @Override
209 public boolean isEnabled() {
210 return jcomp.isEnabled() && (jcomp.getDocument().getLength() > 0);
211 }
212 }
213
214 /**
215 * <code>Delete</code> action.
216 *
217 * @author Kirill Grouchnikov
218 */
219 private class DeleteAction extends AbstractAction {
220 /**
221 * Creates new <code>Delete</code> action.
222 */
223 public DeleteAction() {
224 super(
225 LafWidgetUtilities.getResourceBundle(jcomp).getString(
226 "EditMenu.delete"),
227 new ImageIcon(
228 EditContextMenuWidget.class
229 .getClassLoader()
230 .getResource(
231 "org/pushingpixels/lafwidget/text/edit-delete.png")));
232 }
233
234 /*
235 * (non-Javadoc)
236 *
237 * @see
238 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent
239 * )
240 */
241 @Override
242 public void actionPerformed(ActionEvent e) {
243 jcomp.replaceSelection(null);
244 }
245
246 /*
247 * (non-Javadoc)
248 *
249 * @see javax.swing.AbstractAction#isEnabled()
250 */
251 @Override
252 public boolean isEnabled() {
253 return jcomp.isEditable() && jcomp.isEnabled()
254 && (jcomp.getSelectedText() != null);
255 }
256 }
257
258 /**
259 * <code>Cut</code> action.
260 *
261 * @author Kirill Grouchnikov
262 */
263 private class CutAction extends AbstractAction {
264 /**
265 * Creates new <code>Cut</code> action.
266 */
267 public CutAction() {
268 super(LafWidgetUtilities.getResourceBundle(jcomp).getString(
269 "EditMenu.cut"), new ImageIcon(EditContextMenuWidget.class
270 .getClassLoader().getResource(
271 "org/pushingpixels/lafwidget/text/edit-cut.png")));
272 }
273
274 /*
275 * (non-Javadoc)
276 *
277 * @see
278 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent
279 * )
280 */
281 @Override
282 public void actionPerformed(ActionEvent e) {
283 jcomp.cut();
284 }
285
286 /*
287 * (non-Javadoc)
288 *
289 * @see javax.swing.AbstractAction#isEnabled()
290 */
291 @Override
292 public boolean isEnabled() {
293 return jcomp.isEditable() && jcomp.isEnabled()
294 && (jcomp.getSelectedText() != null);
295 }
296 }
297
298 /**
299 * <code>Copy</code> action.
300 *
301 * @author Kirill Grouchnikov
302 */
303 private class CopyAction extends AbstractAction {
304 /**
305 * Creates new <code>Copy</code> action.
306 */
307 public CopyAction() {
308 super(LafWidgetUtilities.getResourceBundle(jcomp).getString(
309 "EditMenu.copy"), new ImageIcon(EditContextMenuWidget.class
310 .getClassLoader().getResource(
311 "org/pushingpixels/lafwidget/text/edit-copy.png")));
312 }
313
314 /*
315 * (non-Javadoc)
316 *
317 * @see
318 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent
319 * )
320 */
321 @Override
322 public void actionPerformed(ActionEvent e) {
323 jcomp.copy();
324 }
325
326 /*
327 * (non-Javadoc)
328 *
329 * @see javax.swing.AbstractAction#isEnabled()
330 */
331 @Override
332 public boolean isEnabled() {
333 return jcomp.isEnabled() && (jcomp.getSelectedText() != null);
334 }
335 }
336 }