File Source: ColourChooser.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.components.ColourChooser__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.addons.ui_swing.components;
24
25 import com.dmdirc.ui.messages.ColourManager;
26 import com.dmdirc.addons.ui_swing.UIUtilities;
27
28 import java.awt.Color;
29 import java.awt.Dimension;
30 import java.awt.Insets;
31 import java.awt.Window;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34
35 import javax.swing.BorderFactory;
36 import javax.swing.JButton;
37 import javax.swing.JPanel;
38 import javax.swing.event.EventListenerList;
39
40 import net.miginfocom.swing.MigLayout;
41
42 /**
43 * Colour chooser widget.
44 */
45 public final class ColourChooser extends JPanel implements ActionListener {
46
47 /**
48 * A version number for this class. It should be changed whenever the class
49 * structure is changed (or anything else that would prevent serialized
50 * objects being unserialized with the new class).
51 */
52 private static final long serialVersionUID = 1;
53 /** Edit button. */
54 private JButton editButton;
55 /** Panel to show the colour preview. */
56 private JPanel previewPanel;
57 /** Colours picking dialog. */
58 private ColourPickerDialog cpd;
59 /** show irc colours. */
60 private boolean showIRC;
61 /** show hex colours. */
62 private boolean showHex;
63 /** The value of this component. */
64 private String value;
65 /** Event listeners. */
66 private EventListenerList listeners;
67 /** Action command. */
68 private String command;
69 /** Parent window. */
70 private Window window;
71
72 /** Creates a new instance of ColourChooser. */
73 public ColourChooser() {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.ColourChooser()
*
* Postconditions:
* this.command == &""
* this.editButton == &new JButton(ColourChooser#2)
* this.listeners == &new EventListenerList(ColourChooser#1)
* this.previewPanel == &new JPanel(ColourChooser#5)
* this.showHex == 1
* this.showIRC == 1
* new EventListenerList(ColourChooser#1) num objects == 1
* new JButton(ColourChooser#2) num objects == 1
* new JPanel(ColourChooser#5) num objects == 1
* this.value == &"ffffff"
* ...
*/
74 this("ffffff", true, true);
75 }
76
77 /**
78 * Creates a new instance of ColourChooser.
79 *
80 * @param window Parent window
81 *
82 * @since 0.6
83 */
84 public ColourChooser(final Window window) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.ColourChooser(Window)
*
* Postconditions:
* this.command == &""
* this.editButton == &new JButton(ColourChooser#2)
* this.listeners == &new EventListenerList(ColourChooser#1)
* this.previewPanel == &new JPanel(ColourChooser#5)
* this.showHex == 1
* this.showIRC == 1
* new EventListenerList(ColourChooser#1) num objects == 1
* new JButton(ColourChooser#2) num objects == 1
* new JPanel(ColourChooser#5) num objects == 1
* this.value == &"ffffff"
* ...
*/
85 this("ffffff", true, true, window);
86 }
87
88 /**
89 * Creates a new instance of ColourChooser.
90 *
91 * @param initialColour Snitial colour
92 * @param ircColours Show irc colours
93 * @param hexColours Show hex colours
94 */
95 public ColourChooser(final String initialColour, final boolean ircColours,
96 final boolean hexColours) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.ColourChooser(String, bool, bool)
*
* Postconditions:
* this.command == &""
* this.editButton == &new JButton(ColourChooser#2)
* this.listeners == &new EventListenerList(ColourChooser#1)
* this.previewPanel == &new JPanel(ColourChooser#5)
* this.showHex == hexColours
* init'ed(this.showHex)
* this.showIRC == ircColours
* init'ed(this.showIRC)
* this.value == initialColour
* init'ed(this.value)
* ...
*/
97 this(initialColour, ircColours, hexColours, null);
98 }
99
100 /**
101 * Creates a new instance of ColourChooser.
102 *
103 * @param initialColour initial colour
104 * @param ircColours show irc colours
105 * @param hexColours show hex colours
106 * @param window Parent window
107 *
108 * @since 0.6
109 */
110 public ColourChooser(final String initialColour, final boolean ircColours,
111 final boolean hexColours, final Window window) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.ColourChooser(String, bool, bool, Window)
*
* Presumptions:
* init'ed(java.awt.Color.DARK_GRAY)
*
* Postconditions:
* this.command == &""
* this.editButton == &new JButton(ColourChooser#2)
* this.listeners == &new EventListenerList(ColourChooser#1)
* this.previewPanel == &new JPanel(ColourChooser#5)
* this.showHex == hexColours
* init'ed(this.showHex)
* this.showIRC == ircColours
* init'ed(this.showIRC)
* this.value == initialColour
* init'ed(this.value)
* ...
*/
112 super();
113
114 this.window = window;
115 showIRC = ircColours;
116 showHex = hexColours;
117 value = initialColour;
118 listeners = new EventListenerList();
119 command = "";
120
121 editButton = new JButton("Edit");
122 if (UIUtilities.isWindowsUI()) {
123 editButton.setMargin(new Insets(2, 4, 2, 4));
124 } else {
125 editButton.setMargin(new Insets(0, 2, 0, 2));
126 }
127
128 editButton.addActionListener(this);
129
130 previewPanel = new JPanel();
131 previewPanel.setPreferredSize(new Dimension(40, 10));
132 previewPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
133
134 setLayout(new MigLayout("ins 0, fill"));
135
136 add(previewPanel, "growx, pushx, sgy all");
137 add(editButton, "sgy all");
138
139 updateColour(initialColour);
140 }
141
142 /**
143 * Returns the selected colour from this component.
144 * @return This components colour, as a string
145 */
146 public String getColour() {
/*
P/P * Method: String getColour()
*
* Preconditions:
* init'ed(this.value)
*
* Postconditions:
* return_value == this.value
* init'ed(return_value)
*/
147 return value;
148 }
149
150 /**
151 * Sets the selected colour for this component.
152 * @param newValue New colour
153 */
154 public void setColour(final String newValue) {
/*
P/P * Method: void setColour(String)
*
* Preconditions:
* this.previewPanel != null
*
* Postconditions:
* this.value == newValue
* init'ed(this.value)
*/
155 value = newValue;
156 updateColour(value);
157 }
158
159 /** Sets the colour back to white. */
160 public void clearColour() {
/*
P/P * Method: void clearColour()
*
* Preconditions:
* this.previewPanel != null
*
* Postconditions:
* this.value == &"ffffff"
*/
161 value = "ffffff";
162 previewPanel.setBackground(ColourManager.getColour("ffffff"));
163 previewPanel.setToolTipText("");
164 }
165
166 /**
167 * Updates the colour panel.
168 * @param newColour The new colour to use.
169 */
170 private void updateColour(final String newColour) {
/*
P/P * Method: void updateColour(String)
*
* Preconditions:
* this.previewPanel != null
*
* Test Vectors:
* newColour: Addr_Set{null}, Inverse{null}
* java.lang.String:isEmpty(...)@171: {0}, {1}
*/
171 if (newColour == null || newColour.isEmpty()) {
172 previewPanel.setBackground(ColourManager.getColour("ffffff"));
173 previewPanel.setToolTipText("");
174 } else {
175 previewPanel.setBackground(ColourManager.parseColour(newColour));
176 previewPanel.setToolTipText(newColour);
177 }
178 }
179
180 /**
181 * {@inheritDoc}.
182 *
183 * @param e Action event
184 */
185 @Override
186 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* e != null
* init'ed(this.editButton)
* (soft) this.cpd != null
* (soft) init'ed(this.command)
* (soft) this.listeners != null
* (soft) this.previewPanel != null
* (soft) init'ed(this.showHex)
* (soft) init'ed(this.showIRC)
* (soft) init'ed(this.window)
*
* Postconditions:
* this.cpd == One-of{&new ColourPickerDialog(actionPerformed#1), old this.cpd}
* this.cpd != null
* possibly_updated(this.value)
* new ArrayList(ColourPickerPanel#1) num objects <= 1
* new ColourPickerDialog(actionPerformed#1) num objects <= 1
* init'ed(new ColourPickerDialog(actionPerformed#1).colourChooser)
* init'ed(new ColourPickerDialog(actionPerformed#1).window)
* new ColourPickerPanel(ColourPickerDialog#1) num objects <= 1
* init'ed(new ColourPickerPanel(ColourPickerDialog#1).listeners)
* init'ed(new ColourPickerPanel(ColourPickerDialog#1).saturation)
* ...
*/
187 if (e.getSource() == editButton) {
188 cpd = new ColourPickerDialog(showIRC, showHex, window);
189 cpd.setLocationRelativeTo(editButton);
190 cpd.addActionListener(this);
191 cpd.setVisible(true);
192 } else {
193 value = e.getActionCommand();
194 updateColour(e.getActionCommand());
195 fireActionPerformed();
196 cpd.dispose();
197 }
198 }
199
200 /**
201 * Sets this colour choosers action command.
202 *
203 * @param command New action command
204 */
205 public void setActionCommand(final String command) {
/*
P/P * Method: void setActionCommand(String)
*
* Postconditions:
* this.command == command
* init'ed(this.command)
*/
206 this.command = command;
207 }
208
209 /**
210 * Adds a ActionListener to the listener list.
211 *
212 * @param listener Listener to add
213 */
214 public void addActionListener(final ActionListener listener) {
/*
P/P * Method: void addActionListener(ActionListener)
*
* Preconditions:
* (soft) this.listeners != null
*
* Test Vectors:
* listener: Inverse{null}, Addr_Set{null}
*/
215 synchronized (listeners) {
216 if (listener == null) {
217 return;
218 }
219 listeners.add(ActionListener.class, listener);
220 }
221 }
222
223 /**
224 * Removes a ActionListener from the listener list.
225 *
226 * @param listener Listener to remove
227 */
228 public void removeActionListener(final ActionListener listener) {
/*
P/P * Method: void removeActionListener(ActionListener)
*
* Preconditions:
* this.listeners != null
*/
229 listeners.remove(ActionListener.class, listener);
230 }
231
232 /**
233 * Fires the action performed method on all listeners.
234 */
235 protected void fireActionPerformed() {
/*
P/P * Method: void fireActionPerformed()
*
* Preconditions:
* this.listeners != null
* (soft) init'ed(this.command)
*
* Presumptions:
* javax.swing.event.EventListenerList:getListenerList(...)@236 != null
* localListenerList.length@236 in {0, 2..232-1}
* localListenerList[i + 1]@236 != null
*/
236 final Object[] localListenerList = listeners.getListenerList();
237 for (int i = 0; i < localListenerList.length; i += 2) {
238 if (localListenerList[i] == ActionListener.class) {
239 ((ActionListener) localListenerList[i + 1]).actionPerformed(
240 new ActionEvent(this,
241 ActionEvent.ACTION_PERFORMED, command));
242 }
243 }
244 }
245
246 /**
247 * Sets the Parent window.
248 *
249 * @param window Parent window
250 */
251 public void setWindow(final Window window) {
/*
P/P * Method: void setWindow(Window)
*
* Postconditions:
* this.window == window
* init'ed(this.window)
*/
252 this.window = window;
253 }
254 }
SofCheck Inspector Build Version : 2.17854
| ColourChooser.java |
2009-Jun-25 01:54:24 |
| ColourChooser.class |
2009-Sep-02 17:04:15 |