1 package com.explosion.utilities.preferences.editandrender.form;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.GridBagConstraints;
6 import java.awt.GridBagLayout;
7 import java.awt.Insets;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10
11 import javax.swing.JButton;
12 import javax.swing.JColorChooser;
13 import javax.swing.JDialog;
14 import javax.swing.JLabel;
15
16 import org.apache.log4j.LogManager;
17 import org.apache.log4j.Logger;
18
19 import com.explosion.utilities.GeneralUtils;
20 import com.explosion.utilities.exception.ExceptionManagerFactory;
21 import com.explosion.utilities.preferences.Preference;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 /***
45 * This class provides a way to edit and rebder text based preferences
46 * @author Stephen Cowx
47 * @version
48 */
49 public class ColorPreferenceEditorAndRenderer extends FormBasedPreferenceRendererAndEditor {
50
51 private JLabel colourLabel = new JLabel();
52 private JButton button = new JButton("...");
53 private JLabel label = new JLabel();
54 private Component owner;
55 private static Logger log = LogManager.getLogger(ColorPreferenceEditorAndRenderer.class);
56
57 /***
58 * Constructs an instance of this class
59 * @param preference
60 */
61 public ColorPreferenceEditorAndRenderer(Preference preference, Component owner)
62 {
63 super(preference);
64 this.owner = owner;
65 init();
66 }
67
68 /***
69 * This method initialises this renderer editor combination
70 */
71 public void init()
72 {
73 this.setLayout(new GridBagLayout());
74
75
76 label.setText(getPreference().getLongName());
77 colourLabel.setBackground((Color)getPreference().getValue());
78 colourLabel.setForeground((Color)getPreference().getValue());
79 colourLabel.setText("colors");
80 colourLabel.setOpaque(true);
81
82
83 this.add(label , new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
84 this.add(colourLabel , new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
85 this.add(button , new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
86
87
88 button.addActionListener(new ColorButtonActionListener(this, owner));
89 }
90
91 /***
92 * @throws Exception
93 */
94 protected void setColor(Color color) {
95 log.debug("setColor called");
96 try {
97 getPreference().setValue(color);
98 colourLabel.setBackground((Color)getPreference().getValue());
99 colourLabel.setForeground((Color)getPreference().getValue());
100 } catch (Exception e) {
101 ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while updating color preference");
102 }
103 }
104
105 /***
106 * Returns the Color for the preference
107 * @return
108 */
109 protected Color getColor()
110 {
111 return (Color) getPreference().getValue();
112 }
113
114 }
115
116 class ColorButtonActionListener implements ActionListener
117 {
118
119 private JColorChooser colorChooser = new JColorChooser();
120
121 private ColorPreferenceEditorAndRenderer colorEditor = null;
122
123 private JDialog dialog = null;
124
125 public ColorButtonActionListener(ColorPreferenceEditorAndRenderer colorEditor, Component owner)
126 {
127 this.colorEditor = colorEditor;
128
129 ColorChooserActionListener okListener = new ColorChooserActionListener(colorEditor, colorChooser);
130 dialog = JColorChooser.createDialog(owner, "Pick a Color", true, colorChooser, okListener, null);
131 GeneralUtils.centreWindowInParent(dialog, owner);
132 }
133
134 public void actionPerformed(ActionEvent e)
135 {
136 colorChooser.setColor(colorEditor.getColor());
137 dialog.setVisible(true);
138 }
139 }
140
141 /***
142 * This class just sets the currentcolor of the
143 */
144
145 class ColorChooserActionListener implements ActionListener
146 {
147
148 private JColorChooser colorChooser = new JColorChooser();
149
150 private ColorPreferenceEditorAndRenderer colorEditor = null;
151
152 public ColorChooserActionListener(ColorPreferenceEditorAndRenderer colorEditor, JColorChooser colorChooser)
153 {
154 this.colorEditor = colorEditor;
155 this.colorChooser = colorChooser;
156 }
157
158 public void actionPerformed(ActionEvent e)
159 {
160 try
161 {
162 colorEditor.setColor(colorChooser.getColor());
163 } catch (Exception e1)
164 {
165 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e1, null);
166 }
167 }
168
169 }