-
Notifications
You must be signed in to change notification settings - Fork 156
/
MainPanel.java
364 lines (324 loc) · 12 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
// -*- 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.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Objects;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.plaf.basic.ComboPopup;
public final class MainPanel extends JPanel {
private MainPanel() {
super(new BorderLayout());
ListModel<ListItem> model = makeModel();
JList<ListItem> list = new NewspaperStyleList<>(model);
JScrollPane scroll = new JScrollPane(list) {
@Override public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.width = 62 * 3 + 10;
d.height = 32;
return d;
}
};
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
scroll.setBorder(BorderFactory.createEmptyBorder());
scroll.setViewportBorder(BorderFactory.createEmptyBorder());
JList<ListItem> list2 = new NewspaperStyleList<>(model);
JScrollPane scroll2 = new JScrollPane(list2) {
@Override public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.width = 62 * 4 + 10;
return d;
}
};
scroll2.setBorder(BorderFactory.createEmptyBorder());
scroll2.setViewportBorder(BorderFactory.createEmptyBorder());
JPopupMenu popup = new JPopupMenu();
popup.setLayout(new BorderLayout());
list2.addMouseListener(new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
if (popup.isVisible() && e.getClickCount() >= 2) {
popup.setVisible(false);
}
}
});
popup.add(scroll2);
popup.setBorder(BorderFactory.createLineBorder(Color.GRAY));
JToggleButton dropDown = new JToggleButton(new DropDownArrowIcon());
popup.addPopupMenuListener(new PopupMenuListener() {
@Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
list2.setSelectedIndex(list.getSelectedIndex());
EventQueue.invokeLater(popup::pack);
}
@Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
dropDown.setSelected(false);
list.requestFocusInWindow();
int i = list2.getSelectedIndex();
if (i >= 0) {
list.setSelectedIndex(i);
list.scrollRectToVisible(list.getCellBounds(i, i));
}
}
@Override public void popupMenuCanceled(PopupMenuEvent e) {
popupMenuWillBecomeInvisible(e);
}
});
dropDown.setBorder(BorderFactory.createEmptyBorder());
dropDown.setContentAreaFilled(false);
dropDown.setFocusPainted(false);
dropDown.setFocusable(false);
dropDown.addItemListener(e -> {
AbstractButton b = (AbstractButton) e.getItemSelectable();
if (e.getStateChange() == ItemEvent.SELECTED) {
popup.show(b, -scroll.getWidth(), b.getHeight());
}
});
JScrollBar verticalScrollBar = scroll.getVerticalScrollBar();
JPanel verticalBox = new JPanel(new BorderLayout()) {
@Override public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.height = 32 + 5 + 5;
return d;
}
};
verticalBox.setOpaque(false);
verticalBox.add(verticalScrollBar);
verticalBox.add(dropDown, BorderLayout.SOUTH);
JPanel panel = new JPanel(new BorderLayout(0, 0));
panel.add(scroll);
panel.add(verticalBox, BorderLayout.EAST);
panel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
JPanel p = new JPanel(new GridLayout(2, 1, 25, 25));
p.add(makeTitledPanel("JList + JPopupMenu", panel));
p.add(makeTitledPanel("JComboBox + ComboBoxEditor", makeListEditorComboBox()));
add(p, BorderLayout.NORTH);
setPreferredSize(new Dimension(320, 240));
}
private JComboBox<ListItem> makeListEditorComboBox() {
ListModel<ListItem> model = makeModel();
JList<ListItem> list = new NewspaperStyleList<>(model);
JScrollPane scroll = new JScrollPane(list) {
@Override public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.width = 62 * 4;
d.height = 40;
return d;
}
};
scroll.setBorder(BorderFactory.createEmptyBorder());
scroll.setViewportBorder(BorderFactory.createEmptyBorder());
DefaultComboBoxModel<ListItem> cm = new DefaultComboBoxModel<>();
for (int i = 0; i < model.getSize(); i++) {
cm.addElement(model.getElementAt(i));
}
JComboBox<ListItem> combo = new JComboBox<ListItem>(cm) {
@Override public void updateUI() {
super.updateUI();
setMaximumRowCount(4);
setPrototypeDisplayValue(new ListItem("red", new ColorIcon(Color.RED)));
EventQueue.invokeLater(() -> {
ComboPopup popup = (ComboPopup) getAccessibleContext().getAccessibleChild(0);
JList<?> lst = popup.getList();
lst.setLayoutOrientation(JList.HORIZONTAL_WRAP);
lst.setVisibleRowCount(0);
lst.setFixedCellWidth(62);
lst.setFixedCellHeight(40);
lst.setOpaque(true);
lst.setBackground(new Color(0x32_32_32));
lst.setForeground(Color.WHITE);
});
}
};
combo.setRenderer(new ListItemListCellRenderer<>());
combo.setEditable(true);
combo.setEditor(new ComboBoxEditor() {
@Override public Component getEditorComponent() {
return scroll;
}
@Override public void setItem(Object anObject) {
combo.setSelectedIndex(list.getSelectedIndex());
}
@Override public Object getItem() {
return list.getSelectedValue();
}
@Override public void selectAll() {
// System.out.println("selectAll");
}
@Override public void addActionListener(ActionListener l) {
// System.out.println("addActionListener");
}
@Override public void removeActionListener(ActionListener l) {
// System.out.println("removeActionListener");
}
});
return combo;
}
private ListModel<ListItem> makeModel() {
DefaultListModel<ListItem> model = new DefaultListModel<>();
model.addElement(new ListItem("red", new ColorIcon(Color.RED)));
model.addElement(new ListItem("green", new ColorIcon(Color.GREEN)));
model.addElement(new ListItem("blue", new ColorIcon(Color.BLUE)));
model.addElement(new ListItem("cyan", new ColorIcon(Color.CYAN)));
model.addElement(new ListItem("darkGray", new ColorIcon(Color.DARK_GRAY)));
model.addElement(new ListItem("gray", new ColorIcon(Color.GRAY)));
model.addElement(new ListItem("lightGray", new ColorIcon(Color.LIGHT_GRAY)));
model.addElement(new ListItem("magenta", new ColorIcon(Color.MAGENTA)));
model.addElement(new ListItem("orange", new ColorIcon(Color.ORANGE)));
model.addElement(new ListItem("pink", new ColorIcon(Color.PINK)));
model.addElement(new ListItem("yellow", new ColorIcon(Color.YELLOW)));
// model.addElement(new ListItem("black", new ColorIcon(Color.BLACK)));
model.addElement(new ListItem("white", new ColorIcon(Color.WHITE)));
return model;
}
private static Component makeTitledPanel(String title, Component c) {
JPanel p = new JPanel(new GridBagLayout());
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);
}
}
class NewspaperStyleList<E extends ListItem> extends JList<E> {
protected NewspaperStyleList(ListModel<E> model) {
super(model);
}
@Override public void updateUI() {
setSelectionForeground(null); // Nimbus
setSelectionBackground(null); // Nimbus
setCellRenderer(null);
super.updateUI();
setLayoutOrientation(HORIZONTAL_WRAP);
setVisibleRowCount(0);
setFixedCellWidth(62);
setFixedCellHeight(40);
setCellRenderer(new ListItemListCellRenderer<>());
setOpaque(true);
setBackground(new Color(0x32_32_32));
setForeground(Color.WHITE);
}
}
class ListItemListCellRenderer<E extends ListItem> implements ListCellRenderer<E> {
protected static final Color SELECTED_COLOR = new Color(0x40_32_64_FF, true);
private final JLabel label = new JLabel("", null, SwingConstants.CENTER) {
@Override protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (SELECTED_COLOR.equals(getBackground())) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setPaint(SELECTED_COLOR);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.dispose();
}
}
};
private final JPanel renderer = new JPanel(new BorderLayout());
private final Border focusBorder = UIManager.getBorder("List.focusCellHighlightBorder");
private final Border noFocusBorder; // = UIManager.getBorder("List.noFocusBorder");
protected ListItemListCellRenderer() {
Border b = UIManager.getBorder("List.noFocusBorder");
if (Objects.isNull(b)) { // Nimbus???
Insets i = focusBorder.getBorderInsets(renderer);
b = BorderFactory.createEmptyBorder(i.top, i.left, i.bottom, i.right);
}
noFocusBorder = b;
label.setVerticalTextPosition(SwingConstants.BOTTOM);
label.setHorizontalTextPosition(SwingConstants.CENTER);
label.setForeground(renderer.getForeground());
label.setBackground(renderer.getBackground());
label.setBorder(noFocusBorder);
label.setOpaque(false);
renderer.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
renderer.add(label);
renderer.setOpaque(false);
}
@Override public Component getListCellRendererComponent(JList<? extends E> list, E value, int index, boolean isSelected, boolean cellHasFocus) {
label.setText(value.getTitle());
label.setBorder(cellHasFocus ? focusBorder : noFocusBorder);
label.setIcon(value.getIcon());
if (isSelected) {
label.setForeground(list.getSelectionForeground());
label.setBackground(SELECTED_COLOR);
} else {
label.setForeground(list.getForeground());
label.setBackground(list.getBackground());
}
return renderer;
}
}
class ListItem {
private final Icon icon;
private final String title;
protected ListItem(String title, Icon icon) {
this.title = title;
this.icon = icon;
}
public String getTitle() {
return title;
}
public Icon getIcon() {
return icon;
}
}
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 32;
}
@Override public int getIconHeight() {
return 12;
}
}
class DropDownArrowIcon implements Icon {
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g.create();
g2.translate(x, y);
if (c instanceof AbstractButton && ((AbstractButton) c).isSelected()) {
g2.setPaint(Color.LIGHT_GRAY);
} else {
g2.setPaint(Color.DARK_GRAY);
}
g2.drawLine(2, 3, 6, 3);
g2.drawLine(3, 4, 5, 4);
g2.drawLine(4, 5, 4, 5);
g2.dispose();
}
@Override public int getIconWidth() {
return 9;
}
@Override public int getIconHeight() {
return 9;
}
}