-
Notifications
You must be signed in to change notification settings - Fork 1
/
ContextMenuMouseListener.java
137 lines (104 loc) · 4.18 KB
/
ContextMenuMouseListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javasvmpredictui;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JPopupMenu;
import javax.swing.text.JTextComponent;
/**
*
* @author Ritesh
*/
public class ContextMenuMouseListener extends MouseAdapter{
private JPopupMenu popup = new JPopupMenu();
private Action cutAction;
private Action copyAction;
private Action pasteAction;
private Action undoAction;
private Action selectAllAction;
private JTextComponent textComponent;
private String savedString = "";
private Actions lastActionSelected;
private enum Actions { UNDO, CUT, COPY, PASTE, SELECT_ALL };
public ContextMenuMouseListener() {
undoAction = new AbstractAction("Undo") {
@Override
public void actionPerformed(ActionEvent ae) {
textComponent.setText("");
textComponent.replaceSelection(savedString);
lastActionSelected = Actions.UNDO;
}
};
popup.add(undoAction);
popup.addSeparator();
cutAction = new AbstractAction("Cut") {
@Override
public void actionPerformed(ActionEvent ae) {
lastActionSelected = Actions.CUT;
savedString = textComponent.getText();
textComponent.cut();
}
};
popup.add(cutAction);
copyAction = new AbstractAction("Copy") {
@Override
public void actionPerformed(ActionEvent ae) {
lastActionSelected = Actions.COPY;
textComponent.copy();
}
};
popup.add(copyAction);
pasteAction = new AbstractAction("Paste") {
@Override
public void actionPerformed(ActionEvent ae) {
lastActionSelected = Actions.PASTE;
savedString = textComponent.getText();
textComponent.paste();
}
};
popup.add(pasteAction);
popup.addSeparator();
selectAllAction = new AbstractAction("Select All") {
@Override
public void actionPerformed(ActionEvent ae) {
lastActionSelected = Actions.SELECT_ALL;
textComponent.selectAll();
}
};
popup.add(selectAllAction);
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
if (!(e.getSource() instanceof JTextComponent)) {
return;
}
textComponent = (JTextComponent) e.getSource();
textComponent.requestFocus();
boolean enabled = textComponent.isEnabled();
boolean editable = textComponent.isEditable();
boolean nonempty = !(textComponent.getText() == null || textComponent.getText().equals(""));
boolean marked = textComponent.getSelectedText() != null;
boolean pasteAvailable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).isDataFlavorSupported(DataFlavor.stringFlavor);
undoAction.setEnabled(enabled && editable && (lastActionSelected == Actions.CUT || lastActionSelected == Actions.PASTE));
cutAction.setEnabled(enabled && editable && marked);
copyAction.setEnabled(enabled && marked);
pasteAction.setEnabled(enabled && editable && pasteAvailable);
selectAllAction.setEnabled(enabled && nonempty);
int nx = e.getX();
if (nx > 500) {
nx = nx - popup.getSize().width;
}
popup.show(e.getComponent(), nx, e.getY() - popup.getSize().height);
}
}
}