1 package org.opentrafficsim.gui;
2
3 import java.awt.BorderLayout;
4 import java.awt.Container;
5 import java.awt.FlowLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8
9 import javax.swing.JComboBox;
10 import javax.swing.JLabel;
11 import javax.swing.JPanel;
12 import javax.swing.border.Border;
13 import javax.swing.border.LineBorder;
14
15 import org.opentrafficsim.core.gtu.animation.GTUColorer;
16 import org.opentrafficsim.core.gtu.animation.SwitchableGTUColorer;
17
18
19
20
21
22
23
24
25
26
27
28 public class ColorControlPanel extends JPanel implements ActionListener
29 {
30
31 private static final long serialVersionUID = 20150527L;
32
33
34 private JComboBox<GTUColorer> comboBoxGTUColor;
35
36
37 private final JPanel legendPanel;
38
39
40 private GTUColorer gtuColorer;
41
42
43 private final SwitchableGTUColorer switchableGTUColorer;
44
45
46
47
48
49
50 public ColorControlPanel(final GTUColorer gtuColorer)
51 {
52 this.setLayout(new FlowLayout(FlowLayout.LEFT));
53 this.legendPanel = new JPanel(new FlowLayout());
54
55 if (gtuColorer instanceof SwitchableGTUColorer)
56 {
57 this.switchableGTUColorer = (SwitchableGTUColorer) gtuColorer;
58 class AppearanceControlComboBox<T> extends JComboBox<T> implements AppearanceControl
59 {
60
61 private static final long serialVersionUID = 1L;
62
63
64 @Override
65 public boolean isFont()
66 {
67 return true;
68 }
69
70
71 @Override
72 public String toString()
73 {
74 return "AppearanceControlComboBox []";
75 }
76 }
77 this.comboBoxGTUColor = new AppearanceControlComboBox<>();
78 this.add(this.comboBoxGTUColor);
79 this.comboBoxGTUColor.addActionListener(this);
80
81 for (GTUColorer colorer : ((SwitchableGTUColorer) gtuColorer).getColorers())
82 {
83 addItem(colorer);
84 }
85 }
86 else
87 {
88 this.gtuColorer = gtuColorer;
89 this.switchableGTUColorer = null;
90 rebuildLegend();
91 }
92
93 this.add(this.legendPanel);
94 }
95
96
97
98
99
100 public final void addItem(final GTUColorer colorer)
101 {
102 this.comboBoxGTUColor.addItem(colorer);
103
104 }
105
106
107 @Override
108 public final void actionPerformed(final ActionEvent e)
109 {
110 GTUColorer newColorer = (GTUColorer) this.comboBoxGTUColor.getSelectedItem();
111 if (null != newColorer)
112 {
113 this.gtuColorer = newColorer;
114 rebuildLegend();
115
116 if (this.switchableGTUColorer != null)
117 {
118 this.switchableGTUColorer.setGTUColorer(this.comboBoxGTUColor.getSelectedIndex());
119 }
120 }
121 }
122
123
124
125
126 private void rebuildLegend()
127 {
128 this.legendPanel.removeAll();
129 for (GTUColorer.LegendEntry legendEntry : this.gtuColorer.getLegend())
130 {
131 JPanel panel = new JPanel(new BorderLayout());
132 class ColorBox extends JLabel implements AppearanceControl
133 {
134
135 private static final long serialVersionUID = 20180206L;
136
137
138
139
140 public ColorBox()
141 {
142 super(" ");
143 }
144
145
146 @Override
147 public String toString()
148 {
149 return "ColorBox []";
150 }
151 }
152 ColorBox colorBox = new ColorBox();
153 colorBox.setOpaque(true);
154 colorBox.setBackground(legendEntry.getColor());
155 Border border = LineBorder.createBlackLineBorder();
156 colorBox.setBorder(border);
157 panel.add(colorBox, BorderLayout.LINE_START);
158 JLabel name = new JLabel(" " + legendEntry.getName().trim());
159 panel.add(name, BorderLayout.CENTER);
160 name.setOpaque(true);
161 name.setForeground(getForeground());
162 name.setBackground(getBackground());
163 panel.setToolTipText(legendEntry.getDescription());
164 this.legendPanel.add(panel);
165 }
166 this.legendPanel.revalidate();
167
168 Container parentPanel = this.getParent();
169
170 if (parentPanel != null)
171 {
172 parentPanel.repaint();
173 }
174 }
175
176
177 @Override
178 public final String toString()
179 {
180 return "ColorControlPanel [gtuColorer=" + this.gtuColorer + "]";
181 }
182
183 }