|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ColorPanel.java | 64.3% | 77.8% | 78.6% | 76% |
|
1 |
/*
|
|
2 |
* SimplyHTML, a word processor based on Java, HTML and CSS
|
|
3 |
* Copyright (C) 2002 Ulrich Hilger
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or
|
|
6 |
* modify it under the terms of the GNU General Public License
|
|
7 |
* as published by the Free Software Foundation; either version 2
|
|
8 |
* of the License, or (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
18 |
*/
|
|
19 |
|
|
20 |
import java.awt.*;
|
|
21 |
import javax.swing.*;
|
|
22 |
import java.awt.event.ActionListener;
|
|
23 |
import java.awt.event.ActionEvent;
|
|
24 |
import javax.swing.text.AttributeSet;
|
|
25 |
import java.util.EventObject;
|
|
26 |
import java.util.EventListener;
|
|
27 |
import java.util.Vector;
|
|
28 |
import java.util.Enumeration;
|
|
29 |
import javax.swing.text.html.CSS;
|
|
30 |
import javax.swing.text.SimpleAttributeSet;
|
|
31 |
import javax.swing.text.html.StyleSheet;
|
|
32 |
|
|
33 |
/**
|
|
34 |
* a panel to display and change a color setting
|
|
35 |
*
|
|
36 |
* @author Ulrich Hilger
|
|
37 |
* @author Light Development
|
|
38 |
* @author <a href="http://www.lightdev.com">http://www.lightdev.com</a>
|
|
39 |
* @author <a href="mailto:info@lightdev.com">info@lightdev.com</a>
|
|
40 |
* @author published under the terms and conditions of the
|
|
41 |
* GNU General Public License,
|
|
42 |
* for details see file gpl.txt in the distribution
|
|
43 |
* package of this software
|
|
44 |
*
|
|
45 |
* @version stage 11, April 27, 2003
|
|
46 |
*/
|
|
47 |
public class ColorPanel extends JPanel implements ActionListener, AttributeComponent { |
|
48 |
|
|
49 |
/** the component showing the chosen color */
|
|
50 |
JTextField colorDisplay = new JTextField();
|
|
51 |
|
|
52 |
/** default color */
|
|
53 |
private Color defaultColor;
|
|
54 |
|
|
55 |
/** the attribute key, this component returns values for */
|
|
56 |
private Object attributeKey;
|
|
57 |
|
|
58 |
/** value to compare for determining changes */
|
|
59 |
private Color originalColor;
|
|
60 |
|
|
61 |
/** indicates if setValue is called initially */
|
|
62 |
private int setValCount = 0; |
|
63 |
|
|
64 |
/**
|
|
65 |
* construct a color panel
|
|
66 |
*
|
|
67 |
* @param title the title of the color panel
|
|
68 |
* @param col the color to be displayed first
|
|
69 |
* @param titleFont font for title
|
|
70 |
* @param key the attribute key this component shall return color values for
|
|
71 |
*/
|
|
72 | 14 |
public ColorPanel(String title, Color col, Object key) {
|
73 | 14 |
super(new BorderLayout(5,5)); |
74 |
|
|
75 | 14 |
this.defaultColor = col;
|
76 | 14 |
this.attributeKey = key;
|
77 |
|
|
78 |
/** adjust the color display */
|
|
79 | 14 |
colorDisplay.setBackground(col); |
80 | 14 |
Dimension dim = new Dimension(20,15);
|
81 | 14 |
colorDisplay.setMinimumSize(dim); |
82 | 14 |
colorDisplay.setMaximumSize(dim); |
83 | 14 |
colorDisplay.setPreferredSize(dim); |
84 | 14 |
colorDisplay.setEditable(false);
|
85 |
|
|
86 |
/** a button to open a color chooser window */
|
|
87 | 14 |
JButton browseButton = new JButton();
|
88 | 14 |
browseButton.setText("...");
|
89 | 14 |
dim = new Dimension(20,15);
|
90 | 14 |
browseButton.setMinimumSize(dim); |
91 | 14 |
browseButton.setMaximumSize(dim); |
92 | 14 |
browseButton.setPreferredSize(dim); |
93 | 14 |
browseButton.addActionListener(this);
|
94 |
|
|
95 |
/** a helper panel for proper component placement */
|
|
96 | 14 |
JPanel eastPanel = new JPanel(new FlowLayout()); |
97 | 14 |
eastPanel.add(colorDisplay); |
98 | 14 |
eastPanel.add(browseButton); |
99 |
|
|
100 |
/** set the title */
|
|
101 | 14 |
if((title != null) && (title.length() > 0)) { |
102 | 8 |
JLabel titleLabel = new JLabel(title);
|
103 | 8 |
titleLabel.setFont(UIManager.getFont("TextField.font"));
|
104 | 8 |
add(titleLabel, BorderLayout.WEST); |
105 | 8 |
add(eastPanel, BorderLayout.EAST); |
106 |
} |
|
107 |
else {
|
|
108 | 6 |
add(eastPanel, BorderLayout.WEST); |
109 |
} |
|
110 |
|
|
111 |
} |
|
112 |
|
|
113 |
/**
|
|
114 |
* get the selected color
|
|
115 |
*
|
|
116 |
* @return the selected color
|
|
117 |
*/
|
|
118 | 20 |
public Color getColor() {
|
119 | 20 |
return colorDisplay.getBackground();
|
120 |
} |
|
121 |
|
|
122 |
/**
|
|
123 |
* set the selected color
|
|
124 |
*
|
|
125 |
* @param color the selected color
|
|
126 |
*/
|
|
127 | 14 |
private void setColor(Color color) { |
128 |
//System.out.println("ColorPanel setColor attributeKey=" + attributeKey + ", color=" + color);
|
|
129 | 14 |
colorDisplay.setBackground(color); |
130 | 14 |
if(++setValCount < 2) {
|
131 | 14 |
originalColor = color; |
132 |
} |
|
133 | 14 |
fireColorChanged(); |
134 |
} |
|
135 |
|
|
136 |
/**
|
|
137 |
* open a color chooser when a 'Browse' button
|
|
138 |
* is clicked and change the associated color
|
|
139 |
* display accordingly, when another color
|
|
140 |
* is selected from the color chooser
|
|
141 |
*/
|
|
142 | 0 |
public void actionPerformed(ActionEvent e) { |
143 | 0 |
JColorChooser cc = new JColorChooser();
|
144 | 0 |
Color color = cc.showDialog(this,
|
145 |
"Select Color",
|
|
146 |
colorDisplay.getBackground()); |
|
147 | 0 |
if(color != null) { |
148 | 0 |
setColor(color); |
149 |
} |
|
150 |
} |
|
151 |
|
|
152 |
/**
|
|
153 |
* set the value of this <code>AttributeComponent</code>
|
|
154 |
*
|
|
155 |
* @param a the set of attributes possibly having an
|
|
156 |
* attribute this component can display
|
|
157 |
*
|
|
158 |
* @return true, if the set of attributes had a matching attribute,
|
|
159 |
* false if not
|
|
160 |
*/
|
|
161 | 10 |
public boolean setValue(AttributeSet a) { |
162 | 10 |
boolean success = false; |
163 | 10 |
if(a.isDefined(getAttributeKey())) {
|
164 |
//System.out.println("ColorPanel setValue attributeKey=" + attributeKey + ", a=" + a.getAttribute(attributeKey));
|
|
165 | 3 |
String value = a.getAttribute(getAttributeKey()).toString(); |
166 | 3 |
setValue(value); |
167 |
//setColor(Util.styleSheet().getForeground(a));
|
|
168 | 3 |
success = true;
|
169 |
} |
|
170 |
else {
|
|
171 | 7 |
setColor(defaultColor); |
172 |
} |
|
173 | 10 |
return success;
|
174 |
} |
|
175 |
|
|
176 | 7 |
public void setValue(String value) { |
177 |
//System.out.println("ColorPanel setValue value=" + value);
|
|
178 | 7 |
try {
|
179 | 7 |
setColor(new Color(Integer.parseInt(
|
180 |
value.toString().substring(1).toUpperCase(), 16))); |
|
181 |
} |
|
182 |
catch(Exception e) {
|
|
183 | 7 |
try {
|
184 |
//setColor(Util.styleSheet().getForeground(a));
|
|
185 |
//System.out.println("ColorPanel setValue value=" + value + "=" + Color.getColor(value));
|
|
186 | 7 |
setColor(Color.getColor(value)); |
187 |
} |
|
188 |
catch(Exception e2) {
|
|
189 | 0 |
Util.errMsg(null, null, e2); |
190 |
} |
|
191 |
} |
|
192 |
} |
|
193 |
|
|
194 | 4 |
public String getAttr() {
|
195 | 4 |
String color = "#" + Integer.toHexString(
|
196 |
getColor().getRGB()).substring(2); |
|
197 | 4 |
return color;
|
198 |
} |
|
199 |
|
|
200 |
/**
|
|
201 |
* get the value of this <code>AttributeComponent</code>
|
|
202 |
*
|
|
203 |
* @return the value selected from this component
|
|
204 |
*/
|
|
205 | 8 |
public AttributeSet getValue() {
|
206 | 8 |
SimpleAttributeSet set = new SimpleAttributeSet();
|
207 | 8 |
Color value = getColor(); |
208 | 8 |
if(value != originalColor) {
|
209 | 2 |
String color = "#" + Integer.toHexString(
|
210 |
value.getRGB()).substring(2); |
|
211 | 2 |
try {
|
212 | 2 |
Util.styleSheet().addCSSAttribute(set, |
213 |
(CSS.Attribute) getAttributeKey(), color); |
|
214 |
} |
|
215 |
catch(Exception e) {
|
|
216 | 0 |
set.addAttribute(getAttributeKey(), color); |
217 |
} |
|
218 |
//System.out.println("ColorPanel getValue color=" + color);
|
|
219 |
} |
|
220 | 8 |
return set;
|
221 |
} |
|
222 |
|
|
223 | 0 |
public AttributeSet getValue(boolean includeUnchanged) { |
224 | 0 |
if(includeUnchanged) {
|
225 | 0 |
SimpleAttributeSet set = new SimpleAttributeSet();
|
226 | 0 |
Color value = getColor(); |
227 | 0 |
String color = "#" + Integer.toHexString(
|
228 |
value.getRGB()).substring(2); |
|
229 | 0 |
try {
|
230 | 0 |
Util.styleSheet().addCSSAttribute(set, |
231 |
(CSS.Attribute) getAttributeKey(), color); |
|
232 |
} |
|
233 |
catch(Exception e) {
|
|
234 | 0 |
set.addAttribute(getAttributeKey(), color); |
235 |
} |
|
236 |
//System.out.println("ColorPanel getValue color=" + color);
|
|
237 | 0 |
return set;
|
238 |
} |
|
239 |
else {
|
|
240 | 0 |
return getValue();
|
241 |
} |
|
242 |
} |
|
243 |
|
|
244 |
/**
|
|
245 |
* get the attribute key this object was created for
|
|
246 |
*
|
|
247 |
* @return the attribute key this ColorPanel returns values for
|
|
248 |
*/
|
|
249 | 27 |
public Object getAttributeKey() {
|
250 | 27 |
return attributeKey;
|
251 |
} |
|
252 |
|
|
253 |
/* -------------- event listener implementation start ----------- */
|
|
254 |
|
|
255 |
/** the listeners for ColorPanelEvents */
|
|
256 |
private Vector listeners = new Vector(0); |
|
257 |
|
|
258 |
/**
|
|
259 |
* add an event listener.
|
|
260 |
*
|
|
261 |
* @param listener the event listener to add
|
|
262 |
*/
|
|
263 | 8 |
public void addColorPanelListener(ColorPanelListener listener) { |
264 | 8 |
listeners.addElement(listener); |
265 |
} |
|
266 |
|
|
267 |
/**
|
|
268 |
* remove an event listener.
|
|
269 |
*
|
|
270 |
* @param listener the event listener to remove
|
|
271 |
*/
|
|
272 | 0 |
public void removeColorPanelListener(ColorPanelListener listener) { |
273 | 0 |
listeners.removeElement(listener); |
274 |
} |
|
275 |
|
|
276 |
/** fire a color changed event to all registered listeners */
|
|
277 | 14 |
void fireColorChanged() {
|
278 | 14 |
Enumeration listenerList = listeners.elements(); |
279 | 14 |
while(listenerList.hasMoreElements()) {
|
280 | 8 |
((ColorPanelListener) listenerList.nextElement()).colorChanged( |
281 |
new ColorPanelEvent(this)); |
|
282 |
} |
|
283 |
} |
|
284 |
|
|
285 |
/** the event object definition for ColorPanels */
|
|
286 |
class ColorPanelEvent extends EventObject { |
|
287 | 8 |
public ColorPanelEvent(Object source) {
|
288 | 8 |
super(source);
|
289 |
} |
|
290 |
} |
|
291 |
|
|
292 |
/** the event listener definition for ColorPanels */
|
|
293 |
interface ColorPanelListener extends EventListener { |
|
294 |
public void colorChanged(ColorPanelEvent e); |
|
295 |
} |
|
296 |
|
|
297 |
/* -------------- event listener implementation end ----------- */
|
|
298 |
|
|
299 |
} |
|