2
* Copyright (c) 2005-2010 Laf-Widget Kirill Grouchnikov. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
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.
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.
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.
30
package org.pushingpixels.lafwidget.text;
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;
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;
47
import org.pushingpixels.lafwidget.LafWidgetAdapter;
48
import org.pushingpixels.lafwidget.LafWidgetUtilities;
51
* Adds edit context menu on text components.
53
* @author Kirill Grouchnikov
55
public class EditContextMenuWidget extends LafWidgetAdapter<JTextComponent> {
57
* Mouse listener for showing the edit context menu.
59
protected MouseListener menuMouseListener;
64
* @see org.pushingpixels.lafwidget.LafWidget#requiresCustomLafSupport()
67
public boolean requiresCustomLafSupport() {
74
* @see org.pushingpixels.lafwidget.LafWidgetAdapter#installListeners()
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.
82
public void mousePressed(MouseEvent e) {
83
this.handleMouseEvent(e);
87
public void mouseReleased(MouseEvent e) {
88
this.handleMouseEvent(e);
91
private void handleMouseEvent(MouseEvent e) {
92
if (!LafWidgetUtilities.hasTextEditContextMenu(jcomp))
94
if (!e.isPopupTrigger())
98
jcomp.requestFocus(true);
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());
108
Point pt = SwingUtilities.convertPoint(e.getComponent(), e
110
editMenu.show(jcomp, pt.x, pt.y);
113
jcomp.addMouseListener(this.menuMouseListener);
119
* @see org.pushingpixels.lafwidget.LafWidgetAdapter#uninstallListeners()
122
public void uninstallListeners() {
123
jcomp.removeMouseListener(this.menuMouseListener);
124
this.menuMouseListener = null;
128
* <code>Paste</code> action.
130
* @author Kirill Grouchnikov
132
private class PasteAction extends AbstractAction {
134
* Creates new <code>Paste</code> action.
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")));
147
* java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent
151
public void actionPerformed(ActionEvent e) {
158
* @see javax.swing.AbstractAction#isEnabled()
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);
172
* <code>Select All</code> action.
174
* @author Kirill Grouchnikov
176
private class SelectAllAction extends AbstractAction {
178
* Creates new <code>Select All</code> action.
180
public SelectAllAction() {
182
LafWidgetUtilities.getResourceBundle(jcomp).getString(
183
"EditMenu.selectAll"),
185
EditContextMenuWidget.class
188
"org/pushingpixels/lafwidget/text/edit-select-all.png")));
195
* java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent
199
public void actionPerformed(ActionEvent e) {
206
* @see javax.swing.AbstractAction#isEnabled()
209
public boolean isEnabled() {
210
return jcomp.isEnabled() && (jcomp.getDocument().getLength() > 0);
215
* <code>Delete</code> action.
217
* @author Kirill Grouchnikov
219
private class DeleteAction extends AbstractAction {
221
* Creates new <code>Delete</code> action.
223
public DeleteAction() {
225
LafWidgetUtilities.getResourceBundle(jcomp).getString(
228
EditContextMenuWidget.class
231
"org/pushingpixels/lafwidget/text/edit-delete.png")));
238
* java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent
242
public void actionPerformed(ActionEvent e) {
243
jcomp.replaceSelection(null);
249
* @see javax.swing.AbstractAction#isEnabled()
252
public boolean isEnabled() {
253
return jcomp.isEditable() && jcomp.isEnabled()
254
&& (jcomp.getSelectedText() != null);
259
* <code>Cut</code> action.
261
* @author Kirill Grouchnikov
263
private class CutAction extends AbstractAction {
265
* Creates new <code>Cut</code> action.
268
super(LafWidgetUtilities.getResourceBundle(jcomp).getString(
269
"EditMenu.cut"), new ImageIcon(EditContextMenuWidget.class
270
.getClassLoader().getResource(
271
"org/pushingpixels/lafwidget/text/edit-cut.png")));
278
* java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent
282
public void actionPerformed(ActionEvent e) {
289
* @see javax.swing.AbstractAction#isEnabled()
292
public boolean isEnabled() {
293
return jcomp.isEditable() && jcomp.isEnabled()
294
&& (jcomp.getSelectedText() != null);
299
* <code>Copy</code> action.
301
* @author Kirill Grouchnikov
303
private class CopyAction extends AbstractAction {
305
* Creates new <code>Copy</code> action.
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")));
318
* java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent
322
public void actionPerformed(ActionEvent e) {
329
* @see javax.swing.AbstractAction#isEnabled()
332
public boolean isEnabled() {
333
return jcomp.isEnabled() && (jcomp.getSelectedText() != null);