1 /*
2 * Copyright (c) 2006 Stiftung Deutsches Elektronen-Synchroton,
3 * Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY.
4 *
5 * THIS SOFTWARE IS PROVIDED UNDER THIS LICENSE ON AN "../AS IS" BASIS.
6 * WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
7 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR PARTICULAR PURPOSE AND
8 * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
9 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
11 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. SHOULD THE SOFTWARE PROVE DEFECTIVE
12 * IN ANY RESPECT, THE USER ASSUMES THE COST OF ANY NECESSARY SERVICING, REPAIR OR
13 * CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
14 * NO USE OF ANY SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
15 * DESY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
16 * OR MODIFICATIONS.
17 * THE FULL LICENSE SPECIFYING FOR THE SOFTWARE THE REDISTRIBUTION, MODIFICATION,
18 * USAGE AND OTHER RIGHTS AND OBLIGATIONS IS INCLUDED WITH THE DISTRIBUTION OF THIS
19 * PROJECT IN THE FILE LICENSE.HTML. IF THE LICENSE IS NOT INCLUDED YOU MAY FIND A COPY
20 * AT HTTP://WWW.DESY.DE/LEGAL/LICENSE.HTM
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 * @author wu
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; // for background color
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 { // initialize color[] array
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 // System.out.println("init CellColor: " + index); // TODO
73 // Auto-generated
74 // Event stub
75 // itemStateChanged()
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 // System.out.println("init random CellColor: " + index); // TODO
86 // Auto-generated
87 // Event stub
88 // itemStateChanged()
89 }
90 }
91 c.setBackground(colors[index]);
92 // c.setForeground(new Color(255-colors[index].getRed(),
93 // 255-colors[index].getGreen(),255-colors[index].getBlue()));
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 // c.setForeground(new Color(~(c.getBackground().getRGB())));
97 // System.out.println("setCellColor: " + index + " " + colors[index]); // TODO
98 // Auto-generated
99 // Event
100 // stub
101 // itemStateChanged()
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; // @jve:decl-index=0:visual-constraint="207,112"
109 private Acop acopBean;
110 /**
111 * @param args
112 */
113 public static void main(String[] args)
114 {
115 // TODO Auto-generated method stub
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()); // TODO
142 // Auto-generated
143 // Event
144 // stub
145 // itemStateChanged()
146 // if ( accessColor.values() != listColor.getSelectedValue() )
147 // {
148 accessColor = (AccessColor) listColor.getSelectedValue();
149 currentColor = (java.awt.Color) cellRender.colors[listColor.getSelectedIndex()];
150 /*
151 * try { Method m = acopBean.getClass().getMethod("get" +
152 * accessColor.toString(),null); currentColor =
153 * (Color)m.invoke(acopBean, null); } catch (Exception ex) {
154 * currentColor =
155 * (java.awt.Color)cellRender.colors[listColor.getSelectedIndex()];
156 * System.out.println("exception set color: " + "currentColor: "
157 * +currentColor); // TODO Auto-generated Event stub
158 * itemStateChanged() ex.printStackTrace(); }
159 */
160 // System.out.println(accessColor.toString() + "currentColor: " + currentColor); // TODO
161 // Auto-generated
162 // Event
163 // stub
164 // itemStateChanged()
165 selectColor = JColorChooser.showDialog(ColorCustomizerPanel.this, accessColor.toString(),
166 currentColor);
167 // System.out.println("selectColor: " + selectColor + "currentColor: " + currentColor); // TODO
168 // Auto-generated
169 // Event
170 // stub
171 // itemStateChanged()
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 // set cellRende display color
186 cellRender.colors[listColor.getSelectedIndex()] = selectColor;
187 // System.out.println("setAccessColor" + listColor.getSelectedValue()); // TODO
188 // Auto-generated
189 // Event
190 // stub
191 // itemStateChanged()
192 }
193 // System.out.println("itemStateChanged()"); // TODO Auto-generated Event
194 // stub itemStateChanged()
195 // listColor.setSelectedIndex(-1);
196 // }
197 }
198 });
199 // init cell render
200 cellRender = new ColorCellRenderer();
201 listColor.setCellRenderer(cellRender);
202 }
203 return listColor;
204 }
205 /**
206 * @return Returns the acopBean.
207 */
208 public Acop getAcopBean()
209 {
210 return acopBean;
211 }
212 /**
213 * @param acopBean
214 * The acopBean to set.
215 */
216 public void setAcopBean(Acop acopBean)
217 {
218 this.acopBean = acopBean;
219 accessColor = AccessColor.Background; // init to first element of
220 // AccessColor enum
221 // re-init cell render after loading acop bean
222 cellRender = new ColorCellRenderer();
223 listColor.setCellRenderer(cellRender);
224 }
225 public void setObject(Object bean) {
226 setAcopBean((Acop)bean);
227
228 }
229 }