1 package org.wcb.common.component;
2
3 import org.wcb.common.*;
4 import org.wcb.util.TableSorter;
5
6 import javax.swing.*;
7 import java.awt.*;
8 import java.awt.event.ActionListener;
9 import java.awt.event.ActionEvent;
10 import java.util.Vector;
11 import java.util.Enumeration;
12
13
14 /***
15 * Copyright (C) 1999 Walter Bogaardt
16 *
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2 of the License, or (at your option) any later version.
21 *
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this library; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 *
31 * Project: Alice X10 Home Automation
32 * Filename: $Id: ColorEditorPanel.java,v 1.3 2004/07/22 03:06:50 wbogaardt Exp $<BR>
33 * Abstract: Used to display Color edititing of all swing colors.
34 *
35 * $Log: ColorEditorPanel.java,v $
36 * Revision 1.3 2004/07/22 03:06:50 wbogaardt
37 * removed deprecated method calls.
38 *
39 * Revision 1.2 2004/01/31 07:40:26 wbogaardt
40 * modified layout managers for the panel so that it uses the java standard layoutsrather than the FormLayout api. The purpose is to reduce dependency on non-standard layout api.
41 *
42 */
43 public class ColorEditorPanel extends JPanel {
44
45 private ColorTableModel uiModel;
46 private Object uiColors[][];
47 private JTable colorTable;
48
49 public ColorEditorPanel()
50 {
51 this.setup();
52 }
53
54 public void saveUIColors(UIDefaultsLoader uiProperties)
55 {
56
57 for (int i = 0; i < uiModel.getRowCount(); i++) {
58 uiProperties.setProperty(uiModel.getValueAt(i, 0).toString(),
59 this.getColorHexString( (Color)uiModel.getValueAt(i, 1) ));
60 UIManager.put(uiModel.getValueAt(i, 0), uiModel.getValueAt(i, 1));
61 }
62 }
63
64 private void setup(){
65 Vector systemColors = this.buildSystemColors();
66 uiColors = this.convertToObjectArray(systemColors);
67 this.setLayout(new BorderLayout());
68 uiModel = new ColorTableModel(uiColors);
69 TableSorter sorter = new TableSorter(uiModel);
70 colorTable = new JTable(sorter);
71 sorter.addMouseListenerToHeaderInTable(colorTable);
72 JScrollPane scrollPane = new JScrollPane(colorTable);
73 setUpColorRenderer(colorTable);
74 setUpColorEditor(colorTable);
75 this.add(new JLabel("Click on color to change."), BorderLayout.NORTH);
76 this.add(scrollPane, BorderLayout.CENTER);
77 }
78
79 private void setUpColorRenderer(JTable table) {
80 table.setDefaultRenderer(Color.class, new ColorCellRender(true));
81 }
82
83 private void setUpColorEditor(JTable table) {
84 final JButton button = new JButton("") {
85 public void setText(String s) {
86 }
87 };
88 button.setBackground(Color.white);
89 button.setBorderPainted(false);
90 button.setMargin(new Insets(0,0,0,0));
91
92
93
94 final ColorCellEditor colorEditor = new ColorCellEditor(button);
95 table.setDefaultEditor(Color.class, colorEditor);
96
97
98 final JColorChooser colorChooser = new JColorChooser();
99 ActionListener okListener = new ActionListener() {
100 public void actionPerformed(ActionEvent e) {
101 colorEditor.currentColor = colorChooser.getColor();
102 }
103 };
104
105 final JDialog dialog = JColorChooser.createDialog(button,
106 "Pick a Color",
107 true,
108 colorChooser,
109 okListener,
110 null);
111
112
113 button.addActionListener(new ActionListener() {
114 public void actionPerformed(ActionEvent e) {
115 button.setBackground(colorEditor.currentColor);
116 colorChooser.setColor(colorEditor.currentColor);
117 dialog.setVisible(true);
118 }
119 });
120 }
121
122 private Vector buildSystemColors(){
123 UIDefaults uidef = UIManager.getDefaults();
124 Enumeration keys = uidef.keys();
125 Vector returnValue = new Vector();
126 Vector nextRow;
127 String nextKey;
128 Color systemValue;
129 while (keys.hasMoreElements()) {
130 nextKey = keys.nextElement().toString();
131 systemValue = uidef.getColor(nextKey);
132 if (systemValue != null) {
133 nextRow = new Vector(2);
134 nextRow.addElement(nextKey);
135 nextRow.addElement(systemValue);
136 returnValue.addElement(nextRow);
137 }
138 }
139 return returnValue;
140 }
141
142 private Object[][] convertToObjectArray(Vector systemValues){
143 Object[] sortArray = new Object[systemValues.size()];
144 systemValues.copyInto(sortArray);
145 Comparator asComparable = new Comparator() {
146 public boolean less(Object lhs, Object rhs)
147 {
148 Vector left = (Vector)lhs;
149 Vector right = (Vector)rhs;
150 int compValue = (left.elementAt(0).toString()).compareTo(right.elementAt(0).toString());
151 return
152 (compValue < 0);
153 }
154 };
155 ArraySorter.sort(sortArray, asComparable);
156 int colorsLength = sortArray.length/2;
157 Object[][] returnValue = new Object[colorsLength][2];
158 Vector nextRow;
159 for (int i=0;i<colorsLength;i++) {
160 nextRow = (Vector)sortArray[i*2];
161 returnValue[i][0] = nextRow.elementAt(0);
162 returnValue[i][1] = nextRow.elementAt(1);
163 }
164 return returnValue;
165 }
166
167 /***
168 * changes the color information back to a hexadecimal
169 * string value to be saved in the properties
170 * file.
171 */
172 private String getColorHexString(Color c)
173 {
174 String returnValue=null;
175 String colString = Integer.toHexString(c.getRGB() & 0xffffff);
176 returnValue="#000000".substring(0,7 - colString.length()).concat(colString);
177 return "Color."+returnValue;
178 }
179
180 }