1 package fr.ifremer.quadrige3.ui.swing.component;
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 import org.jdesktop.swingx.util.PaintUtils;
28
29 import javax.swing.*;
30 import javax.swing.border.EmptyBorder;
31 import java.awt.*;
32
33
34
35
36
37
38 public class ActionListCellRenderer extends DefaultListCellRenderer {
39
40 private final JComboBox comboBox;
41
42 protected final EmptyBorder border = new EmptyBorder(4, 8, 4, 8);
43
44
45
46
47
48
49 public ActionListCellRenderer(JComboBox comboBox) {
50 super();
51 this.comboBox = comboBox;
52 }
53
54
55
56
57 @Override
58 public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
59
60
61 if (list == null) {
62 list = new JList<>();
63 }
64
65 DefaultListCellRenderer result = (DefaultListCellRenderer) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
66
67 if (value instanceof JButton) {
68 JButton button = (JButton) value;
69 result.setEnabled(comboBox.isEnabled() && button.isEnabled() && (button.getAction() == null || button.getAction().isEnabled()));
70 Action action = button.getAction();
71 if (action != null) {
72 result.setIcon((Icon) action.getValue(Action.LARGE_ICON_KEY));
73 result.setText((String) action.getValue(Action.NAME));
74 result.setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION));
75 } else {
76 result.setIcon(button.getIcon());
77 result.setText(button.getText());
78 result.setToolTipText(button.getToolTipText());
79 }
80 if (isSelected) {
81 result.setBackground(UIManager.getColor("nimbusSelection"));
82 result.setForeground(PaintUtils.computeForeground(UIManager.getColor("nimbusSelection")));
83 }
84 result.setBorder(border);
85 return result;
86 }
87 throw new ClassCastException("the value is not an instance of JButton");
88 }
89 }