1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package de.desy.acop.chart.customizer;
24
25 import java.awt.Color;
26 import java.awt.Component;
27 import java.awt.GridBagConstraints;
28 import java.awt.GridBagLayout;
29 import java.awt.Insets;
30 import java.beans.Customizer;
31 import java.lang.reflect.Method;
32 import javax.swing.DefaultListCellRenderer;
33 import javax.swing.JColorChooser;
34 import javax.swing.JLabel;
35 import javax.swing.JList;
36 import javax.swing.JPanel;
37 import javax.swing.JScrollPane;
38 import javax.swing.event.ListSelectionEvent;
39 import javax.swing.event.ListSelectionListener;
40 import de.desy.acop.chart.Acop;
41
42
43
44
45 public class ColorCustomizerPanel extends JPanel implements Customizer
46 {
47 public static final String[] COLOR_PROPERTIES = {
48 "background", "cursorMarkerColor", "errorColor", "foreground", "frameForeColor", "gridColor", "keepColor", "leadingEdgeColor", "markerColor", "secondaryYTickLabelColor", "tagColor", "XTickLabelColor", "YTickLabelColor"
49 };
50
51 class ColorCellRenderer extends DefaultListCellRenderer
52 {
53 Color[] colors = new Color[AccessColor.values().length];
54 int factor = 64;
55 ColorCellRenderer()
56 {
57 super();
58 }
59 @Override
60 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
61 boolean cellHasFocus)
62 {
63 Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
64 if (colors[index] == null)
65 {
66 if (acopBean != null)
67 {
68 try
69 {
70 Method m = acopBean.getClass().getMethod("get" + value.toString(), null);
71 colors[index] = (Color) m.invoke(acopBean, null);
72
73
74
75
76 }
77 catch (Exception ex)
78 {
79 ex.printStackTrace();
80 }
81 }
82 else
83 {
84 colors[index] = new Color((int) (Integer.MAX_VALUE * Math.random()));
85
86
87
88
89 }
90 }
91 c.setBackground(colors[index]);
92
93
94 c.setForeground(new Color(255 - (255 - colors[index].getRed()) / factor, 255
95 - (255 - colors[index].getGreen()) / factor, 255 - (255 - colors[index].getBlue()) / factor));
96
97
98
99
100
101
102 return c;
103 }
104 }
105 private ColorCellRenderer cellRender;
106 private AccessColor accessColor;
107 private java.awt.Color selectColor, currentColor;
108 private JList listColor = null;
109 private Acop acopBean;
110
111
112
113 public static void main(String[] args)
114 {
115
116 }
117 public ColorCustomizerPanel()
118 {
119 super();
120 initialize();
121 }
122 private void initialize()
123 {
124 setLayout(new GridBagLayout());
125 add(new JLabel("Color Property:"), new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0,
126 GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(4, 4, 4, 4), 0, 0));
127 JScrollPane sc = new JScrollPane(getListColor());
128 sc.setMinimumSize(sc.getPreferredSize());
129 add(sc, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
130 GridBagConstraints.VERTICAL, new Insets(4, 4, 4, 4), 0, 0));
131 }
132 private JList getListColor()
133 {
134 if (listColor == null)
135 {
136 listColor = new JList(AccessColor.values());
137 listColor.addListSelectionListener(new ListSelectionListener()
138 {
139 public void valueChanged(ListSelectionEvent e)
140 {
141 System.out.println("ListCOlor select index: " + listColor.getSelectedIndex());
142
143
144
145
146
147
148 accessColor = (AccessColor) listColor.getSelectedValue();
149 currentColor = (java.awt.Color) cellRender.colors[listColor.getSelectedIndex()];
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 selectColor = JColorChooser.showDialog(ColorCustomizerPanel.this, accessColor.toString(),
166 currentColor);
167
168
169
170
171
172 if (selectColor != null && selectColor != currentColor)
173 {
174 try
175 {
176 Method m = acopBean.getClass().getMethod("set" + accessColor.toString(),
177 new Class[] {Color.class});
178 m.invoke(acopBean, new Color[] {selectColor});
179 firePropertyChange(accessColor.toString(), currentColor, selectColor);
180 }
181 catch (Exception ex)
182 {
183 ex.printStackTrace();
184 }
185
186 cellRender.colors[listColor.getSelectedIndex()] = selectColor;
187
188
189
190
191
192 }
193
194
195
196
197 }
198 });
199
200 cellRender = new ColorCellRenderer();
201 listColor.setCellRenderer(cellRender);
202 }
203 return listColor;
204 }
205
206
207
208 public Acop getAcopBean()
209 {
210 return acopBean;
211 }
212
213
214
215
216 public void setAcopBean(Acop acopBean)
217 {
218 this.acopBean = acopBean;
219 accessColor = AccessColor.Background;
220
221
222 cellRender = new ColorCellRenderer();
223 listColor.setCellRenderer(cellRender);
224 }
225 public void setObject(Object bean) {
226 setAcopBean((Acop)bean);
227
228 }
229 }