/
MainPanel.java
382 lines (348 loc) · 12.5 KB
/
MainPanel.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.EventObject;
import java.util.stream.Stream;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;
public final class MainPanel extends JPanel {
private MainPanel() {
super(new BorderLayout());
Box list1 = Box.createVerticalBox();
DefaultListModel<CheckBoxNode> model = new DefaultListModel<>();
JList<CheckBoxNode> list2 = new CheckBoxList(model);
JTree list3 = new JTree() {
@Override public void updateUI() {
setCellRenderer(null);
setCellEditor(null);
super.updateUI();
setEditable(true);
setRootVisible(false);
setShowsRootHandles(false);
setCellRenderer(new CheckBoxNodeRenderer());
setCellEditor(new CheckBoxNodeEditor());
}
};
JPanel p = new JPanel(new GridLayout(1, 3));
p.add(makeTitledPanel("Box", new JScrollPane(list1)));
p.add(makeTitledPanel("JList", new JScrollPane(list2)));
p.add(makeTitledPanel("JTree", new JScrollPane(list3)));
DefaultMutableTreeNode root = new DefaultMutableTreeNode("JTree");
Stream.of("1", "22", "333", "4444", "55555", "6666", "777", "88", "9", "00")
.forEach(title -> {
boolean isSelected = title.length() % 2 == 0;
JCheckBox c = new JCheckBox(title, isSelected);
c.setAlignmentX(LEFT_ALIGNMENT);
list1.add(c);
model.addElement(new CheckBoxNode(title, isSelected));
root.add(new DefaultMutableTreeNode(new CheckBoxNode(title, isSelected)));
});
list3.setModel(new DefaultTreeModel(root));
add(new JLabel("JCheckBox in ", SwingConstants.CENTER), BorderLayout.NORTH);
add(p);
setPreferredSize(new Dimension(320, 240));
}
private static Component makeTitledPanel(String title, Component c) {
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createTitledBorder(title));
p.add(c);
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(MainPanel::createAndShowGui);
}
private static void createAndShowGui() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException ignored) {
Toolkit.getDefaultToolkit().beep();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
ex.printStackTrace();
return;
}
JFrame frame = new JFrame("@title@");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
final class CheckBoxNode {
private final String text;
private final boolean selected;
/* default */ CheckBoxNode(String text, boolean selected) {
this.text = text;
this.selected = selected;
}
public String getText() {
return text;
}
public boolean isSelected() {
return selected;
}
@Override public String toString() {
return text;
}
}
class CheckBoxList extends JList<CheckBoxNode> {
private transient CheckBoxCellRenderer renderer;
private transient MouseListener handler;
protected CheckBoxList(ListModel<CheckBoxNode> model) {
super(model);
}
@Override public void updateUI() {
setForeground(null);
setBackground(null);
setSelectionForeground(null);
setSelectionBackground(null);
removeMouseListener(handler);
removeMouseListener(renderer);
removeMouseMotionListener(renderer);
super.updateUI();
handler = new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
int index = locationToIndex(e.getPoint());
if (e.getButton() == MouseEvent.BUTTON1 && index >= 0) {
DefaultListModel<CheckBoxNode> m = (DefaultListModel<CheckBoxNode>) getModel();
CheckBoxNode n = m.get(index);
m.set(index, new CheckBoxNode(n.getText(), !n.isSelected()));
repaint(getCellBounds(index, index));
}
}
};
addMouseListener(handler);
renderer = new CheckBoxCellRenderer();
setCellRenderer(renderer);
addMouseListener(renderer);
addMouseMotionListener(renderer);
putClientProperty("List.isFileList", Boolean.TRUE);
}
// @see SwingUtilities2.pointOutsidePrefSize(...)
private boolean pointOutsidePrefSize(Point p) {
int i = locationToIndex(p);
CheckBoxNode cbn = getModel().getElementAt(i);
Component c = getCellRenderer().getListCellRendererComponent(this, cbn, i, false, false);
Rectangle rect = getCellBounds(i, i);
rect.width = c.getPreferredSize().width;
return i < 0 || !rect.contains(p);
}
@Override protected void processMouseEvent(MouseEvent e) {
if (!pointOutsidePrefSize(e.getPoint())) {
super.processMouseEvent(e);
}
}
@Override protected void processMouseMotionEvent(MouseEvent e) {
if (pointOutsidePrefSize(e.getPoint())) {
MouseEvent ev = new MouseEvent(
e.getComponent(), MouseEvent.MOUSE_EXITED, e.getWhen(),
e.getModifiersEx(), e.getX(), e.getY(), e.getXOnScreen(), e.getYOnScreen(),
e.getClickCount(), e.isPopupTrigger(), MouseEvent.NOBUTTON);
super.processMouseEvent(ev);
} else {
super.processMouseMotionEvent(e);
}
}
}
class CheckBoxCellRenderer extends MouseAdapter implements ListCellRenderer<CheckBoxNode> {
private final JCheckBox checkBox = new JCheckBox();
private int rollOverRowIndex = -1;
@Override public Component getListCellRendererComponent(JList<? extends CheckBoxNode> list, CheckBoxNode value, int index, boolean isSelected, boolean cellHasFocus) {
checkBox.setOpaque(true);
if (isSelected) {
checkBox.setBackground(list.getSelectionBackground());
checkBox.setForeground(list.getSelectionForeground());
} else {
checkBox.setBackground(list.getBackground());
checkBox.setForeground(list.getForeground());
}
checkBox.setSelected(value.isSelected());
checkBox.getModel().setRollover(index == rollOverRowIndex);
checkBox.setText(value.getText());
return checkBox;
}
@Override public void mouseExited(MouseEvent e) {
if (rollOverRowIndex >= 0) {
JList<?> l = (JList<?>) e.getComponent();
l.repaint(l.getCellBounds(rollOverRowIndex, rollOverRowIndex));
rollOverRowIndex = -1;
}
}
@Override public void mouseMoved(MouseEvent e) {
JList<?> l = (JList<?>) e.getComponent();
int index = l.locationToIndex(e.getPoint());
if (index != rollOverRowIndex) {
rollOverRowIndex = index;
l.repaint();
}
}
}
class CheckBoxNodeRenderer implements TreeCellRenderer {
private final JCheckBox checkBox = new JCheckBox();
private final TreeCellRenderer renderer = new DefaultTreeCellRenderer();
@Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
Component c;
if (leaf && value instanceof DefaultMutableTreeNode) {
checkBox.setOpaque(false);
Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
if (userObject instanceof CheckBoxNode) {
CheckBoxNode node = (CheckBoxNode) userObject;
checkBox.setText(node.getText());
checkBox.setSelected(node.isSelected());
}
c = checkBox;
} else {
c = renderer.getTreeCellRendererComponent(
tree, value, selected, expanded, leaf, row, hasFocus);
}
return c;
}
}
// delegation pattern
class CheckBoxNodeEditor extends AbstractCellEditor implements TreeCellEditor {
private final JCheckBox checkBox = new JCheckBox() {
private transient ActionListener handler;
@Override public void updateUI() {
removeActionListener(handler);
super.updateUI();
setOpaque(false);
setFocusable(false);
handler = e -> stopCellEditing();
addActionListener(handler);
}
};
@Override public Component getTreeCellEditorComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row) {
if (leaf && value instanceof DefaultMutableTreeNode) {
Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
if (userObject instanceof CheckBoxNode) {
checkBox.setSelected(((CheckBoxNode) userObject).isSelected());
} else {
checkBox.setSelected(false);
}
checkBox.setText(value.toString());
}
return checkBox;
}
@Override public Object getCellEditorValue() {
return new CheckBoxNode(checkBox.getText(), checkBox.isSelected());
}
@Override public boolean isCellEditable(EventObject e) {
return e instanceof MouseEvent;
}
// // AbstractCellEditor
// @Override public boolean shouldSelectCell(EventObject anEvent) {
// return true;
// }
//
// @Override public boolean stopCellEditing() {
// fireEditingStopped();
// return true;
// }
//
// @Override public void cancelCellEditing() {
// fireEditingCanceled();
// }
}
// // inheritance to extend a class
// class CheckBoxNodeEditor extends JCheckBox implements TreeCellEditor {
// private transient ActionListener handler;
// @Override public void updateUI() {
// removeActionListener(handler);
// super.updateUI();
// setOpaque(false);
// setFocusable(false);
// handler = e -> stopCellEditing();
// addActionListener(handler);
// }
//
// @Override public Component getTreeCellEditorComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row) {
// if (leaf && value instanceof DefaultMutableTreeNode) {
// Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
// if (userObject instanceof CheckBoxNode) {
// this.setSelected(((CheckBoxNode) userObject).selected);
// } else {
// this.setSelected(false);
// }
// this.setText(value.toString());
// }
// return this;
// }
//
// @Override public Object getCellEditorValue() {
// return new CheckBoxNode(getText(), isSelected());
// }
//
// @Override public boolean isCellEditable(EventObject e) {
// return e instanceof MouseEvent;
// }
//
// // Copied from AbstractCellEditor
// // protected EventListenerList listenerList = new EventListenerList();
// // protected transient ChangeEvent changeEvent;
// @Override public boolean shouldSelectCell(EventObject anEvent) {
// return true;
// }
//
// @Override public boolean stopCellEditing() {
// fireEditingStopped();
// return true;
// }
//
// @Override public void cancelCellEditing() {
// fireEditingCanceled();
// }
//
// @Override public void addCellEditorListener(CellEditorListener l) {
// listenerList.add(CellEditorListener.class, l);
// }
//
// @Override public void removeCellEditorListener(CellEditorListener l) {
// listenerList.remove(CellEditorListener.class, l);
// }
//
// public CellEditorListener[] getCellEditorListeners() {
// return listenerList.getListeners(CellEditorListener.class);
// }
//
// protected void fireEditingStopped() {
// // Guaranteed to return a non-null array
// Object[] listeners = listenerList.getListenerList();
// // Process the listeners last to first, notifying
// // those that are interested in this event
// for (int i = listeners.length - 2; i >= 0; i -= 2) {
// if (listeners[i] == CellEditorListener.class) {
// // Lazily create the event:
// if (Objects.isNull(changeEvent)) {
// changeEvent = new ChangeEvent(this);
// }
// ((CellEditorListener) listeners[i + 1]).editingStopped(changeEvent);
// }
// }
// }
//
// protected void fireEditingCanceled() {
// // Guaranteed to return a non-null array
// Object[] listeners = listenerList.getListenerList();
// // Process the listeners last to first, notifying
// // those that are interested in this event
// for (int i = listeners.length - 2; i >= 0; i -= 2) {
// if (listeners[i] == CellEditorListener.class) {
// // Lazily create the event:
// if (Objects.isNull(changeEvent)) {
// changeEvent = new ChangeEvent(this);
// }
// ((CellEditorListener) listeners[i + 1]).editingCanceled(changeEvent);
// }
// }
// }
// }