-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathJListMutable.java
More file actions
269 lines (223 loc) · 9.75 KB
/
JListMutable.java
File metadata and controls
269 lines (223 loc) · 9.75 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package dfEditor.CustomComponents;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.beans.*;
import java.util.EventObject;
import java.applet.Applet;
// @author Santhosh Kumar T - santhosh@in.fiorano.com
public class JListMutable<T> extends JList<T> implements CellEditorListener {
private static final long serialVersionUID = -8376374319089296337L;
protected Component editorComp = null;
protected int editingIndex = -1;
protected ListCellEditor<T> editor = null;
private PropertyChangeListener editorRemover = null;
public JListMutable(ListModel<T> dataModel){
super(dataModel);
init();
}
private void init(){
getActionMap().put("startEditing", new StartEditingAction()); //NOI18N
getActionMap().put("cancel", new CancelEditingAction()); //NOI18N
addMouseListener(new MouseListener());
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), "startEditing"); //NOI18N
getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel"); //NOI18N
putClientProperty("terminateEditOnFocusLost", Boolean.TRUE); //NOI18N
}
public void setListCellEditor(ListCellEditor<T> editor){
this.editor = editor;
}
public ListCellEditor<T> getListCellEditor(){
return editor;
}
public boolean isEditing() {
return (editorComp == null)? false : true;
}
public Component getEditorComponent() {
return editorComp;
}
public int getEditingIndex() {
return editingIndex;
}
public Component prepareEditor(int index) {
Object value = getModel().getElementAt(index);
boolean isSelected = isSelectedIndex(index);
Component comp = editor.getListCellEditorComponent(this, value, isSelected, index);
if (comp instanceof JComponent) {
JComponent jComp = (JComponent)comp;
if (jComp.getNextFocusableComponent() == null) {
jComp.setNextFocusableComponent(this);
}
}
return comp;
}
public void removeEditor(){
KeyboardFocusManager.getCurrentKeyboardFocusManager().
removePropertyChangeListener("permanentFocusOwner", editorRemover); //NOI18N
editorRemover = null;
if(editor != null) {
editor.removeCellEditorListener(this);
if (editorComp != null) {
remove(editorComp);
}
Rectangle cellRect = getCellBounds(editingIndex, editingIndex);
editingIndex = -1;
editorComp = null;
repaint(cellRect);
}
}
public boolean editCellAt(int index, EventObject e){
if(editor!=null && !editor.stopCellEditing())
return false;
if(index<0 || index>=getModel().getSize())
return false;
if(!isCellEditable(index))
return false;
if (editorRemover == null){
KeyboardFocusManager fm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
editorRemover = new CellEditorRemover(fm);
fm.addPropertyChangeListener("permanentFocusOwner", editorRemover); //NOI18N
}
if (editor != null && editor.isCellEditable(e)) {
editorComp = prepareEditor(index);
if (editorComp == null) {
removeEditor();
return false;
}
editorComp.setBounds(getCellBounds(index, index));
add(editorComp);
editorComp.validate();
editingIndex = index;
editor.addCellEditorListener(this);
return true;
}
return false;
}
public void removeNotify() {
KeyboardFocusManager.getCurrentKeyboardFocusManager().
removePropertyChangeListener("permanentFocusOwner", editorRemover); //NOI18N
super.removeNotify();
}
// This class tracks changes in the keyboard focus state. It is used
// when the XList is editing to determine when to cancel the edit.
// If focus switches to a component outside of the XList, but in the
// same window, this will cancel editing.
class CellEditorRemover implements PropertyChangeListener {
KeyboardFocusManager focusManager;
public CellEditorRemover(KeyboardFocusManager fm) {
this.focusManager = fm;
}
public void propertyChange(PropertyChangeEvent ev) {
if (!isEditing() || getClientProperty("terminateEditOnFocusLost") != Boolean.TRUE) { //NOI18N
return;
}
Component c = focusManager.getPermanentFocusOwner();
while (c != null) {
if (c == JListMutable.this) {
// focus remains inside the table
return;
} else if ((c instanceof Window) ||
(c instanceof Applet && c.getParent() == null)) {
if (c == SwingUtilities.getRoot(JListMutable.this)) {
if (!getListCellEditor().stopCellEditing()) {
getListCellEditor().cancelCellEditing();
}
}
break;
}
c = c.getParent();
}
}
}
/*-------------------------------------------------[ Model Support ]---------------------------------------------------*/
public boolean isCellEditable(int index){
if(getModel() instanceof MutableListModel)
return ((MutableListModel<T>)getModel()).isCellEditable(index);
return false;
}
public void setValueAt(Object value, int index){
((NamedElement)((MutableListModel<T>)getModel()).getElementAt(index)).setName((String)value);
}
/*-------------------------------------------------[ CellEditorListener ]---------------------------------------------------*/
public void editingStopped(ChangeEvent e) {
if (editor != null) {
Object value = editor.getCellEditorValue();
setValueAt(value, editingIndex);
removeEditor();
}
}
public void editingCanceled(ChangeEvent e) {
removeEditor();
}
/*-------------------------------------------------[ Editing Actions]---------------------------------------------------*/
private static class StartEditingAction extends AbstractAction {
private static final long serialVersionUID = 2378126151125982434L;
public void actionPerformed(ActionEvent e) {
JListMutable<?> list = (JListMutable<?>)e.getSource();
if (!list.hasFocus()) {
CellEditor cellEditor = list.getListCellEditor();
if(cellEditor!=null && !cellEditor.stopCellEditing()) {
return;
}
list.requestFocus();
return;
}
ListSelectionModel rsm = list.getSelectionModel();
int anchorRow = rsm.getAnchorSelectionIndex();
list.editCellAt(anchorRow, null);
Component editorComp = list.getEditorComponent();
if (editorComp != null) {
editorComp.requestFocus();
}
}
}
private class CancelEditingAction extends AbstractAction {
private static final long serialVersionUID = 3414959167369764270L;
public void actionPerformed(ActionEvent e) {
@SuppressWarnings("unchecked")
JListMutable<T> list = (JListMutable<T>)e.getSource();
list.removeEditor();
}
public boolean isEnabled(){
return isEditing();
}
}
private class MouseListener extends MouseAdapter{
private Component dispatchComponent;
private void setDispatchComponent(MouseEvent e) {
Component editorComponent = getEditorComponent();
Point p = e.getPoint();
Point p2 = SwingUtilities.convertPoint(JListMutable.this, p, editorComponent);
dispatchComponent = SwingUtilities.getDeepestComponentAt(editorComponent,
p2.x, p2.y);
}
private boolean repostEvent(MouseEvent e) {
// Check for isEditing() in case another event has
// caused the editor to be removed. See bug #4306499.
if (dispatchComponent == null || !isEditing()) {
return false;
}
MouseEvent e2 = SwingUtilities.convertMouseEvent(JListMutable.this, e, dispatchComponent);
dispatchComponent.dispatchEvent(e2);
return true;
}
private boolean shouldIgnore(MouseEvent e) {
return e.isConsumed() || (!(SwingUtilities.isLeftMouseButton(e) && isEnabled()));
}
public void mousePressed(MouseEvent e){
if(shouldIgnore(e))
return;
Point p = e.getPoint();
int index = locationToIndex(p);
// The autoscroller can generate drag events outside the Table's range.
if(index==-1)
return;
if(editCellAt(index, e)){
setDispatchComponent(e);
repostEvent(e);
} else if(isRequestFocusEnabled())
requestFocus();
}
}
}