/
MainPanel.java
430 lines (391 loc) · 14 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.text.ParseException;
import java.util.Date;
import javax.swing.*;
import javax.swing.plaf.ColorUIResource;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
public final class MainPanel extends JPanel {
private MainPanel() {
super(new GridLayout(3, 1));
String[] columnNames = {"Type", "Value"};
@SuppressWarnings("JavaUtilDate")
Object[][] data = {
{"String", "text"},
{"Date", new Date()},
{"Integer", 12},
{"Double", 3.45},
{"Boolean", Boolean.TRUE},
{"Color", Color.RED}
};
TableModel model = new DefaultTableModel(data, columnNames) {
@Override public Class<?> getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
JTable table1 = new PropertyTable(model);
JTable table2 = new PropertyTable(model);
table2.setTransferHandler(new HtmlTableTransferHandler());
add(new JScrollPane(table1));
add(new JScrollPane(table2));
add(new JScrollPane(new JEditorPane("text/html", "")));
setPreferredSize(new Dimension(320, 240));
}
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);
}
}
class PropertyTable extends JTable {
private static final int TARGET_COL_IDX = 1;
private Class<?> editingClass;
protected PropertyTable(TableModel model) {
super(model);
}
// public PropertyTable(Object[][] data, String[] columnNames) {
// super(data, columnNames);
// }
private Class<?> getClassAt(int row, int column) {
int mc = convertColumnIndexToModel(column);
int mr = convertRowIndexToModel(row);
return getModel().getValueAt(mr, mc).getClass();
}
@Override public void updateUI() {
// Changing to Nimbus LAF and back doesn't reset look and feel of JTable completely
// https://bugs.openjdk.org/browse/JDK-6788475
// Set a temporary ColorUIResource to avoid this issue
setSelectionForeground(new ColorUIResource(Color.RED));
setSelectionBackground(new ColorUIResource(Color.RED));
super.updateUI();
setDefaultRenderer(Color.class, new ColorRenderer());
setDefaultEditor(Color.class, new ColorEditor());
setDefaultEditor(Date.class, new DateEditor());
}
@Override public TableCellRenderer getCellRenderer(int row, int column) {
if (convertColumnIndexToModel(column) == TARGET_COL_IDX) {
return getDefaultRenderer(getClassAt(row, column));
} else {
return super.getCellRenderer(row, column);
}
}
// https://stackoverflow.com/questions/1464691/property-list-gui-component-in-swing
// This method is also invoked by the editor when the value in the editor
// component is saved in the TableModel. The class was saved when the
// editor was invoked so the proper class can be created.
@Override public TableCellEditor getCellEditor(int row, int column) {
if (convertColumnIndexToModel(column) == TARGET_COL_IDX) {
editingClass = getClassAt(row, column);
return getDefaultEditor(editingClass);
} else {
editingClass = null;
return super.getCellEditor(row, column);
}
}
@Override public Class<?> getColumnClass(int column) {
if (convertColumnIndexToModel(column) == TARGET_COL_IDX) {
return editingClass;
} else {
return super.getColumnClass(column);
}
}
}
// delegation pattern
class DateEditor extends AbstractCellEditor implements TableCellEditor {
private final JSpinner spinner;
protected DateEditor() {
super();
spinner = new JSpinner(new SpinnerDateModel());
JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner, "yyyy/MM/dd");
spinner.setEditor(editor);
setArrowButtonEnabled(false);
editor.getTextField().setHorizontalAlignment(SwingConstants.LEFT);
editor.getTextField().addFocusListener(new FocusListener() {
@Override public void focusLost(FocusEvent e) {
setArrowButtonEnabled(false);
}
@Override public void focusGained(FocusEvent e) {
setArrowButtonEnabled(true);
EventQueue.invokeLater(() -> {
JTextField field = (JTextField) e.getComponent();
field.setCaretPosition(8);
field.setSelectionStart(8);
field.setSelectionEnd(10);
});
}
});
spinner.setBorder(BorderFactory.createEmptyBorder());
}
protected final void setArrowButtonEnabled(boolean flag) {
for (Component c : spinner.getComponents()) {
if (c instanceof JButton) {
c.setEnabled(flag);
}
}
}
@Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
spinner.setValue(value);
return spinner;
}
@Override public Object getCellEditorValue() {
return spinner.getValue();
}
// // AbstractCellEditor
// @Override public boolean isCellEditable(EventObject e) {
// return true;
// }
// @Override public boolean shouldSelectCell(EventObject anEvent) {
// return true;
// }
@Override public boolean stopCellEditing() {
boolean stopEditing = true;
try {
spinner.commitEdit();
} catch (ParseException ex) {
UIManager.getLookAndFeel().provideErrorFeedback(spinner);
stopEditing = false;
}
return stopEditing && super.stopCellEditing();
}
// @Override public void cancelCellEditing() {
// fireEditingCanceled();
// }
}
// // inheritance to extend a class
// class DateEditor extends JSpinner implements TableCellEditor {
// private transient ChangeEvent changeEvent;
// private final JSpinner.DateEditor editor;
//
// protected DateEditor() {
// super(new SpinnerDateModel());
// editor = new JSpinner.DateEditor(this, "yyyy/MM/dd");
// setEditor(editor);
// setArrowButtonEnabled(false);
// editor.getTextField().setHorizontalAlignment(SwingConstants.LEFT);
//
// editor.getTextField().addFocusListener(new FocusListener() {
// @Override public void focusLost(FocusEvent e) {
// setArrowButtonEnabled(false);
// }
//
// @Override public void focusGained(FocusEvent e) {
// // System.out.println("getTextField");
// setArrowButtonEnabled(true);
// EventQueue.invokeLater(new Runnable() {
// @Override public void run() {
// editor.getTextField().setCaretPosition(8);
// editor.getTextField().setSelectionStart(8);
// editor.getTextField().setSelectionEnd(10);
// }
// });
// }
// });
// setBorder(BorderFactory.createEmptyBorder());
// }
//
// private void setArrowButtonEnabled(boolean flag) {
// for (Component c : getComponents()) {
// if (c instanceof JButton) {
// ((JButton) c).setEnabled(flag);
// }
// }
// }
//
// @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
// setValue(value);
// return this;
// }
//
// @Override public Object getCellEditorValue() {
// return getValue();
// }
//
// // Copied from AbstractCellEditor
// // protected EventListenerList listenerList = new EventListenerList();
// // protected transient ChangeEvent changeEvent;
// @Override public boolean isCellEditable(EventObject e) {
// return true;
// }
//
// @Override public boolean shouldSelectCell(EventObject anEvent) {
// return true;
// }
//
// @Override public boolean stopCellEditing() {
// try {
// commitEdit();
// } catch (ParseException ex) {
// UIManager.getLookAndFeel().provideErrorFeedback(this);
// return false;
// // // Edited value is invalid, spinner.getValue() will return
// // // the last valid value, you could revert the spinner to show that:
// // editor.getTextField().setValue(getValue());
// }
// 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);
// }
// }
// }
// }
class ColorRenderer extends DefaultTableCellRenderer {
@Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
if (value instanceof Color && c instanceof JLabel) {
Color color = (Color) value;
JLabel l = (JLabel) c;
l.setIcon(new ColorIcon(color));
l.setText(String.format("(%d, %d, %d)", color.getRed(), color.getGreen(), color.getBlue()));
}
return c;
}
}
// https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableDialogEditDemoProject/src/components/ColorEditor.java
class ColorEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
private static final String EDIT = "edit";
private final JButton button = new JButton();
private final JColorChooser colorChooser;
private final JDialog dialog;
private Color currentColor;
protected ColorEditor() {
super();
// Set up the editor (from the table's point of view),
// which is a button.
// This button brings up the color chooser dialog,
// which is the editor from the user's point of view.
button.setActionCommand(EDIT);
button.addActionListener(this);
// button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setFocusPainted(false);
button.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
button.setOpaque(false);
button.setHorizontalAlignment(SwingConstants.LEFT);
button.setHorizontalTextPosition(SwingConstants.RIGHT);
// Set up the dialog that the button brings up.
colorChooser = new JColorChooser();
dialog = JColorChooser.createDialog(button, "Pick a Color", true, colorChooser, this, null);
}
/**
* Handles events from the editor button and from
* the dialog's OK button.
*/
@Override public void actionPerformed(ActionEvent e) {
if (EDIT.equals(e.getActionCommand())) {
// The user has clicked the cell, so
// bring up the dialog.
button.setBackground(currentColor);
button.setIcon(new ColorIcon(currentColor));
colorChooser.setColor(currentColor);
dialog.setVisible(true);
// Make the renderer reappear.
fireEditingStopped();
} else { // User pressed dialog's "OK" button.
currentColor = colorChooser.getColor();
}
}
// Implement the one CellEditor method that AbstractCellEditor doesn't.
@Override public Object getCellEditorValue() {
return currentColor;
}
// Implement the one method defined by TableCellEditor.
@Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
currentColor = (Color) value;
button.setIcon(new ColorIcon(currentColor));
int r = currentColor.getRed();
int g = currentColor.getGreen();
int b = currentColor.getBlue();
button.setText(String.format("(%d, %d, %d)", r, g, b));
return button;
}
}
class ColorIcon implements Icon {
private final Color color;
protected ColorIcon(Color color) {
this.color = color;
}
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g.create();
g2.translate(x, y);
g2.setPaint(color);
g2.fillRect(0, 0, getIconWidth(), getIconHeight());
g2.dispose();
}
@Override public int getIconWidth() {
return 10;
}
@Override public int getIconHeight() {
return 10;
}
}