1 package com.explosion.utilities.preferences.editandrender.table;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.awt.Color;
24 import java.awt.Component;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27
28 import javax.swing.DefaultCellEditor;
29 import javax.swing.JButton;
30 import javax.swing.JCheckBox;
31 import javax.swing.JColorChooser;
32 import javax.swing.JDialog;
33 import javax.swing.JTable;
34
35 import com.explosion.utilities.GeneralUtils;
36 import com.explosion.utilities.preferences.Preference;
37
38 public class ColorPreferenceEditor extends DefaultCellEditor
39 {
40
41 private Object value = null;
42
43 public ColorPreferenceEditor(JButton b, Component owner)
44 {
45 super(new JCheckBox());
46
47 editorComponent = b;
48 setClickCountToStart(1);
49
50 ((JButton) editorComponent).addActionListener(new ColorButtonActionListener(((JButton) editorComponent), this, owner));
51
52 ((JButton) editorComponent).addActionListener(new ActionListener()
53 {
54
55 public void actionPerformed(ActionEvent e)
56 {
57 fireEditingStopped();
58 }
59 });
60 }
61
62 protected void fireEditingStopped()
63 {
64 super.fireEditingStopped();
65 }
66
67 public Object getCellEditorValue()
68 {
69 return value;
70 }
71
72 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
73 {
74
75 this.value = value;
76 ((JButton) editorComponent).setText("..");
77
78 if (value instanceof Preference)
79 {
80 editorComponent.setBackground((Color) ((Preference)value).getValue());
81 }
82 else if (value instanceof Color)
83 {
84 editorComponent.setBackground((Color) value);
85 }
86 return ((JButton) editorComponent);
87 }
88
89 /***
90 * Returns the colour value for this editor
91 * @return
92 */
93 public Color getColour()
94 {
95 if (value instanceof Preference)
96 {
97 return (Color) ((Preference)value).getValue();
98 }
99 else if (value instanceof Color)
100 {
101 return (Color) value;
102 }
103
104 return null;
105 }
106
107 /***
108 * Sets the color value for this editor
109 * @param colour
110 * @throws Exception
111 */
112 public void setColour(Color colour) throws Exception
113 {
114 if (value instanceof Preference)
115 {
116 ((Preference)value).setValue(colour);
117 }
118 else if (value instanceof Color)
119 {
120 value = colour;
121 }
122 }
123
124 }
125
126 class ColorButtonActionListener implements ActionListener
127 {
128
129 private JButton button = null;
130
131 private JColorChooser colorChooser = new JColorChooser();
132
133 private ColorPreferenceEditor colorEditor = null;
134
135 private JDialog dialog = null;
136
137 public ColorButtonActionListener(JButton button, ColorPreferenceEditor colorEditor, Component owner)
138 {
139 this.button = button;
140 this.colorEditor = colorEditor;
141
142 ColorChooserActionListener okListener = new ColorChooserActionListener(colorEditor, colorChooser);
143 dialog = JColorChooser.createDialog(owner, "Pick a Color", true, colorChooser, okListener, null);
144 GeneralUtils.centreWindowInParent(dialog, owner);
145 }
146
147 public void actionPerformed(ActionEvent e)
148 {
149 button.setBackground(colorEditor.getColour());
150 colorChooser.setColor(colorEditor.getColour());
151 dialog.setLocationRelativeTo(button);
152 dialog.setVisible(true);
153 }
154 }
155
156 /***
157 * This class just sets the currentcolor of the
158 */
159
160 class ColorChooserActionListener implements ActionListener
161 {
162
163 private JColorChooser colorChooser = new JColorChooser();
164
165 private ColorPreferenceEditor colorEditor = null;
166
167 public ColorChooserActionListener(ColorPreferenceEditor colorEditor, JColorChooser colorChooser)
168 {
169 this.colorEditor = colorEditor;
170 this.colorChooser = colorChooser;
171 }
172
173 public void actionPerformed(ActionEvent e)
174 {
175 try
176 {
177 colorEditor.setColour(colorChooser.getColor());
178 } catch (Exception e1)
179 {
180 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e1, null);
181 }
182 }
183
184 }
185