author | malenkov |
Wed, 30 Apr 2014 19:28:05 +0400 | |
changeset 24544 | c0133e7c7162 |
parent 23697 | e556a715949f |
child 24969 | afa6934dd8e8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
2 |
* Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.swing; |
|
27 |
||
28 |
import java.awt.*; |
|
29 |
import java.awt.event.*; |
|
30 |
import java.io.*; |
|
31 |
import java.util.*; |
|
32 |
||
33 |
import javax.swing.colorchooser.*; |
|
34 |
import javax.swing.plaf.ColorChooserUI; |
|
35 |
import javax.accessibility.*; |
|
36 |
||
37 |
import sun.swing.SwingUtilities2; |
|
38 |
||
39 |
||
40 |
/** |
|
41 |
* <code>JColorChooser</code> provides a pane of controls designed to allow |
|
42 |
* a user to manipulate and select a color. |
|
43 |
* For information about using color choosers, see |
|
44 |
* <a |
|
20455
f6f9a0c2796b
8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents:
20169
diff
changeset
|
45 |
href="http://docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html">How to Use Color Choosers</a>, |
2 | 46 |
* a section in <em>The Java Tutorial</em>. |
47 |
* |
|
48 |
* <p> |
|
49 |
* |
|
50 |
* This class provides three levels of API: |
|
51 |
* <ol> |
|
52 |
* <li>A static convenience method which shows a modal color-chooser |
|
53 |
* dialog and returns the color selected by the user. |
|
54 |
* <li>A static convenience method for creating a color-chooser dialog |
|
55 |
* where <code>ActionListeners</code> can be specified to be invoked when |
|
56 |
* the user presses one of the dialog buttons. |
|
57 |
* <li>The ability to create instances of <code>JColorChooser</code> panes |
|
58 |
* directly (within any container). <code>PropertyChange</code> listeners |
|
59 |
* can be added to detect when the current "color" property changes. |
|
60 |
* </ol> |
|
61 |
* <p> |
|
62 |
* <strong>Warning:</strong> Swing is not thread safe. For more |
|
63 |
* information see <a |
|
64 |
* href="package-summary.html#threading">Swing's Threading |
|
65 |
* Policy</a>. |
|
66 |
* <p> |
|
67 |
* <strong>Warning:</strong> |
|
68 |
* Serialized objects of this class will not be compatible with |
|
69 |
* future Swing releases. The current serialization support is |
|
70 |
* appropriate for short term storage or RMI between applications running |
|
71 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 72 |
* of all JavaBeans™ |
2 | 73 |
* has been added to the <code>java.beans</code> package. |
74 |
* Please see {@link java.beans.XMLEncoder}. |
|
75 |
* |
|
76 |
* |
|
77 |
* @beaninfo |
|
78 |
* attribute: isContainer false |
|
79 |
* description: A component that supports selecting a Color. |
|
80 |
* |
|
81 |
* |
|
82 |
* @author James Gosling |
|
83 |
* @author Amy Fowler |
|
84 |
* @author Steve Wilson |
|
85 |
*/ |
|
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
86 |
@SuppressWarnings("serial") // Same-version serialization only |
2 | 87 |
public class JColorChooser extends JComponent implements Accessible { |
88 |
||
89 |
/** |
|
90 |
* @see #getUIClassID |
|
91 |
* @see #readObject |
|
92 |
*/ |
|
93 |
private static final String uiClassID = "ColorChooserUI"; |
|
94 |
||
95 |
private ColorSelectionModel selectionModel; |
|
96 |
||
6512
ce0a2790e09f
6977726: JColorChooser.getPreviewPanel() returnes null starting from jdk7 b105.
malenkov
parents:
5506
diff
changeset
|
97 |
private JComponent previewPanel = ColorChooserComponentFactory.getPreviewPanel(); |
2 | 98 |
|
99 |
private AbstractColorChooserPanel[] chooserPanels = new AbstractColorChooserPanel[0]; |
|
100 |
||
101 |
private boolean dragEnabled; |
|
102 |
||
103 |
/** |
|
104 |
* The selection model property name. |
|
105 |
*/ |
|
106 |
public static final String SELECTION_MODEL_PROPERTY = "selectionModel"; |
|
107 |
||
108 |
/** |
|
109 |
* The preview panel property name. |
|
110 |
*/ |
|
111 |
public static final String PREVIEW_PANEL_PROPERTY = "previewPanel"; |
|
112 |
||
113 |
/** |
|
114 |
* The chooserPanel array property name. |
|
115 |
*/ |
|
116 |
public static final String CHOOSER_PANELS_PROPERTY = "chooserPanels"; |
|
117 |
||
118 |
||
119 |
/** |
|
120 |
* Shows a modal color-chooser dialog and blocks until the |
|
121 |
* dialog is hidden. If the user presses the "OK" button, then |
|
122 |
* this method hides/disposes the dialog and returns the selected color. |
|
123 |
* If the user presses the "Cancel" button or closes the dialog without |
|
124 |
* pressing "OK", then this method hides/disposes the dialog and returns |
|
125 |
* <code>null</code>. |
|
126 |
* |
|
127 |
* @param component the parent <code>Component</code> for the dialog |
|
128 |
* @param title the String containing the dialog's title |
|
129 |
* @param initialColor the initial Color set when the color-chooser is shown |
|
130 |
* @return the selected color or <code>null</code> if the user opted out |
|
131 |
* @exception HeadlessException if GraphicsEnvironment.isHeadless() |
|
132 |
* returns true. |
|
133 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
134 |
*/ |
|
135 |
public static Color showDialog(Component component, |
|
136 |
String title, Color initialColor) throws HeadlessException { |
|
137 |
||
138 |
final JColorChooser pane = new JColorChooser(initialColor != null? |
|
139 |
initialColor : Color.white); |
|
140 |
||
141 |
ColorTracker ok = new ColorTracker(pane); |
|
142 |
JDialog dialog = createDialog(component, title, true, pane, ok, null); |
|
143 |
||
144 |
dialog.addComponentListener(new ColorChooserDialog.DisposeOnClose()); |
|
145 |
||
146 |
dialog.show(); // blocks until user brings dialog down... |
|
147 |
||
148 |
return ok.getColor(); |
|
149 |
} |
|
150 |
||
151 |
||
152 |
/** |
|
153 |
* Creates and returns a new dialog containing the specified |
|
154 |
* <code>ColorChooser</code> pane along with "OK", "Cancel", and "Reset" |
|
155 |
* buttons. If the "OK" or "Cancel" buttons are pressed, the dialog is |
|
156 |
* automatically hidden (but not disposed). If the "Reset" |
|
157 |
* button is pressed, the color-chooser's color will be reset to the |
|
158 |
* color which was set the last time <code>show</code> was invoked on the |
|
159 |
* dialog and the dialog will remain showing. |
|
160 |
* |
|
161 |
* @param c the parent component for the dialog |
|
162 |
* @param title the title for the dialog |
|
163 |
* @param modal a boolean. When true, the remainder of the program |
|
164 |
* is inactive until the dialog is closed. |
|
165 |
* @param chooserPane the color-chooser to be placed inside the dialog |
|
166 |
* @param okListener the ActionListener invoked when "OK" is pressed |
|
167 |
* @param cancelListener the ActionListener invoked when "Cancel" is pressed |
|
168 |
* @return a new dialog containing the color-chooser pane |
|
169 |
* @exception HeadlessException if GraphicsEnvironment.isHeadless() |
|
170 |
* returns true. |
|
171 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
172 |
*/ |
|
173 |
public static JDialog createDialog(Component c, String title, boolean modal, |
|
174 |
JColorChooser chooserPane, ActionListener okListener, |
|
175 |
ActionListener cancelListener) throws HeadlessException { |
|
176 |
||
177 |
Window window = JOptionPane.getWindowForComponent(c); |
|
178 |
ColorChooserDialog dialog; |
|
179 |
if (window instanceof Frame) { |
|
180 |
dialog = new ColorChooserDialog((Frame)window, title, modal, c, chooserPane, |
|
181 |
okListener, cancelListener); |
|
182 |
} else { |
|
183 |
dialog = new ColorChooserDialog((Dialog)window, title, modal, c, chooserPane, |
|
184 |
okListener, cancelListener); |
|
185 |
} |
|
14658 | 186 |
dialog.getAccessibleContext().setAccessibleDescription(title); |
2 | 187 |
return dialog; |
188 |
} |
|
189 |
||
190 |
/** |
|
191 |
* Creates a color chooser pane with an initial color of white. |
|
192 |
*/ |
|
193 |
public JColorChooser() { |
|
194 |
this(Color.white); |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
* Creates a color chooser pane with the specified initial color. |
|
199 |
* |
|
200 |
* @param initialColor the initial color set in the chooser |
|
201 |
*/ |
|
202 |
public JColorChooser(Color initialColor) { |
|
203 |
this( new DefaultColorSelectionModel(initialColor) ); |
|
204 |
||
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
* Creates a color chooser pane with the specified |
|
209 |
* <code>ColorSelectionModel</code>. |
|
210 |
* |
|
211 |
* @param model the <code>ColorSelectionModel</code> to be used |
|
212 |
*/ |
|
213 |
public JColorChooser(ColorSelectionModel model) { |
|
214 |
selectionModel = model; |
|
215 |
updateUI(); |
|
216 |
dragEnabled = false; |
|
217 |
} |
|
218 |
||
219 |
/** |
|
20169 | 220 |
* Returns the L&F object that renders this component. |
2 | 221 |
* |
222 |
* @return the <code>ColorChooserUI</code> object that renders |
|
223 |
* this component |
|
224 |
*/ |
|
225 |
public ColorChooserUI getUI() { |
|
226 |
return (ColorChooserUI)ui; |
|
227 |
} |
|
228 |
||
229 |
/** |
|
20169 | 230 |
* Sets the L&F object that renders this component. |
2 | 231 |
* |
20169 | 232 |
* @param ui the <code>ColorChooserUI</code> L&F object |
2 | 233 |
* @see UIDefaults#getUI |
234 |
* |
|
235 |
* @beaninfo |
|
236 |
* bound: true |
|
237 |
* hidden: true |
|
238 |
* description: The UI object that implements the color chooser's LookAndFeel. |
|
239 |
*/ |
|
240 |
public void setUI(ColorChooserUI ui) { |
|
241 |
super.setUI(ui); |
|
242 |
} |
|
243 |
||
244 |
/** |
|
20169 | 245 |
* Notification from the <code>UIManager</code> that the L&F has changed. |
2 | 246 |
* Replaces the current UI object with the latest version from the |
247 |
* <code>UIManager</code>. |
|
248 |
* |
|
249 |
* @see JComponent#updateUI |
|
250 |
*/ |
|
251 |
public void updateUI() { |
|
252 |
setUI((ColorChooserUI)UIManager.getUI(this)); |
|
253 |
} |
|
254 |
||
255 |
/** |
|
20169 | 256 |
* Returns the name of the L&F class that renders this component. |
2 | 257 |
* |
258 |
* @return the string "ColorChooserUI" |
|
259 |
* @see JComponent#getUIClassID |
|
260 |
* @see UIDefaults#getUI |
|
261 |
*/ |
|
262 |
public String getUIClassID() { |
|
263 |
return uiClassID; |
|
264 |
} |
|
265 |
||
266 |
/** |
|
267 |
* Gets the current color value from the color chooser. |
|
268 |
* By default, this delegates to the model. |
|
269 |
* |
|
270 |
* @return the current color value of the color chooser |
|
271 |
*/ |
|
272 |
public Color getColor() { |
|
273 |
return selectionModel.getSelectedColor(); |
|
274 |
} |
|
275 |
||
276 |
/** |
|
277 |
* Sets the current color of the color chooser to the specified color. |
|
278 |
* The <code>ColorSelectionModel</code> will fire a <code>ChangeEvent</code> |
|
279 |
* @param color the color to be set in the color chooser |
|
280 |
* @see JComponent#addPropertyChangeListener |
|
281 |
* |
|
282 |
* @beaninfo |
|
283 |
* bound: false |
|
284 |
* hidden: false |
|
285 |
* description: The current color the chooser is to display. |
|
286 |
*/ |
|
287 |
public void setColor(Color color) { |
|
288 |
selectionModel.setSelectedColor(color); |
|
289 |
||
290 |
} |
|
291 |
||
292 |
/** |
|
293 |
* Sets the current color of the color chooser to the |
|
294 |
* specified RGB color. Note that the values of red, green, |
|
295 |
* and blue should be between the numbers 0 and 255, inclusive. |
|
296 |
* |
|
297 |
* @param r an int specifying the amount of Red |
|
298 |
* @param g an int specifying the amount of Green |
|
299 |
* @param b an int specifying the amount of Blue |
|
300 |
* @exception IllegalArgumentException if r,g,b values are out of range |
|
301 |
* @see java.awt.Color |
|
302 |
*/ |
|
303 |
public void setColor(int r, int g, int b) { |
|
304 |
setColor(new Color(r,g,b)); |
|
305 |
} |
|
306 |
||
307 |
/** |
|
308 |
* Sets the current color of the color chooser to the |
|
309 |
* specified color. |
|
310 |
* |
|
311 |
* @param c an integer value that sets the current color in the chooser |
|
312 |
* where the low-order 8 bits specify the Blue value, |
|
313 |
* the next 8 bits specify the Green value, and the 8 bits |
|
314 |
* above that specify the Red value. |
|
315 |
*/ |
|
316 |
public void setColor(int c) { |
|
317 |
setColor((c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF); |
|
318 |
} |
|
319 |
||
320 |
/** |
|
321 |
* Sets the <code>dragEnabled</code> property, |
|
322 |
* which must be <code>true</code> to enable |
|
323 |
* automatic drag handling (the first part of drag and drop) |
|
324 |
* on this component. |
|
325 |
* The <code>transferHandler</code> property needs to be set |
|
326 |
* to a non-<code>null</code> value for the drag to do |
|
327 |
* anything. The default value of the <code>dragEnabled</code> |
|
328 |
* property |
|
329 |
* is <code>false</code>. |
|
330 |
* |
|
331 |
* <p> |
|
332 |
* |
|
333 |
* When automatic drag handling is enabled, |
|
334 |
* most look and feels begin a drag-and-drop operation |
|
335 |
* when the user presses the mouse button over the preview panel. |
|
336 |
* Some look and feels might not support automatic drag and drop; |
|
337 |
* they will ignore this property. You can work around such |
|
338 |
* look and feels by modifying the component |
|
339 |
* to directly call the <code>exportAsDrag</code> method of a |
|
340 |
* <code>TransferHandler</code>. |
|
341 |
* |
|
342 |
* @param b the value to set the <code>dragEnabled</code> property to |
|
343 |
* @exception HeadlessException if |
|
344 |
* <code>b</code> is <code>true</code> and |
|
345 |
* <code>GraphicsEnvironment.isHeadless()</code> |
|
346 |
* returns <code>true</code> |
|
347 |
* |
|
348 |
* @since 1.4 |
|
349 |
* |
|
350 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
351 |
* @see #getDragEnabled |
|
352 |
* @see #setTransferHandler |
|
353 |
* @see TransferHandler |
|
354 |
* |
|
355 |
* @beaninfo |
|
356 |
* description: Determines whether automatic drag handling is enabled. |
|
357 |
* bound: false |
|
358 |
*/ |
|
359 |
public void setDragEnabled(boolean b) { |
|
360 |
if (b && GraphicsEnvironment.isHeadless()) { |
|
361 |
throw new HeadlessException(); |
|
362 |
} |
|
363 |
dragEnabled = b; |
|
364 |
} |
|
365 |
||
366 |
/** |
|
367 |
* Gets the value of the <code>dragEnabled</code> property. |
|
368 |
* |
|
369 |
* @return the value of the <code>dragEnabled</code> property |
|
370 |
* @see #setDragEnabled |
|
371 |
* @since 1.4 |
|
372 |
*/ |
|
373 |
public boolean getDragEnabled() { |
|
374 |
return dragEnabled; |
|
375 |
} |
|
376 |
||
377 |
/** |
|
378 |
* Sets the current preview panel. |
|
379 |
* This will fire a <code>PropertyChangeEvent</code> for the property |
|
380 |
* named "previewPanel". |
|
381 |
* |
|
382 |
* @param preview the <code>JComponent</code> which displays the current color |
|
383 |
* @see JComponent#addPropertyChangeListener |
|
384 |
* |
|
385 |
* @beaninfo |
|
386 |
* bound: true |
|
387 |
* hidden: true |
|
388 |
* description: The UI component which displays the current color. |
|
389 |
*/ |
|
390 |
public void setPreviewPanel(JComponent preview) { |
|
391 |
||
392 |
if (previewPanel != preview) { |
|
393 |
JComponent oldPreview = previewPanel; |
|
394 |
previewPanel = preview; |
|
395 |
firePropertyChange(JColorChooser.PREVIEW_PANEL_PROPERTY, oldPreview, preview); |
|
396 |
} |
|
397 |
} |
|
398 |
||
399 |
/** |
|
400 |
* Returns the preview panel that shows a chosen color. |
|
401 |
* |
|
402 |
* @return a <code>JComponent</code> object -- the preview panel |
|
403 |
*/ |
|
404 |
public JComponent getPreviewPanel() { |
|
405 |
return previewPanel; |
|
406 |
} |
|
407 |
||
408 |
/** |
|
409 |
* Adds a color chooser panel to the color chooser. |
|
410 |
* |
|
411 |
* @param panel the <code>AbstractColorChooserPanel</code> to be added |
|
412 |
*/ |
|
413 |
public void addChooserPanel( AbstractColorChooserPanel panel ) { |
|
414 |
AbstractColorChooserPanel[] oldPanels = getChooserPanels(); |
|
415 |
AbstractColorChooserPanel[] newPanels = new AbstractColorChooserPanel[oldPanels.length+1]; |
|
416 |
System.arraycopy(oldPanels, 0, newPanels, 0, oldPanels.length); |
|
417 |
newPanels[newPanels.length-1] = panel; |
|
418 |
setChooserPanels(newPanels); |
|
419 |
} |
|
420 |
||
421 |
/** |
|
422 |
* Removes the Color Panel specified. |
|
423 |
* |
|
424 |
* @param panel a string that specifies the panel to be removed |
|
425 |
* @return the color panel |
|
426 |
* @exception IllegalArgumentException if panel is not in list of |
|
427 |
* known chooser panels |
|
428 |
*/ |
|
429 |
public AbstractColorChooserPanel removeChooserPanel( AbstractColorChooserPanel panel ) { |
|
430 |
||
431 |
||
432 |
int containedAt = -1; |
|
433 |
||
434 |
for (int i = 0; i < chooserPanels.length; i++) { |
|
435 |
if (chooserPanels[i] == panel) { |
|
436 |
containedAt = i; |
|
437 |
break; |
|
438 |
} |
|
439 |
} |
|
440 |
if (containedAt == -1) { |
|
441 |
throw new IllegalArgumentException("chooser panel not in this chooser"); |
|
442 |
} |
|
443 |
||
444 |
AbstractColorChooserPanel[] newArray = new AbstractColorChooserPanel[chooserPanels.length-1]; |
|
445 |
||
446 |
if (containedAt == chooserPanels.length-1) { // at end |
|
447 |
System.arraycopy(chooserPanels, 0, newArray, 0, newArray.length); |
|
448 |
} |
|
449 |
else if (containedAt == 0) { // at start |
|
450 |
System.arraycopy(chooserPanels, 1, newArray, 0, newArray.length); |
|
451 |
} |
|
452 |
else { // in middle |
|
453 |
System.arraycopy(chooserPanels, 0, newArray, 0, containedAt); |
|
454 |
System.arraycopy(chooserPanels, containedAt+1, |
|
455 |
newArray, containedAt, (chooserPanels.length - containedAt - 1)); |
|
456 |
} |
|
457 |
||
458 |
setChooserPanels(newArray); |
|
459 |
||
460 |
return panel; |
|
461 |
} |
|
462 |
||
463 |
||
464 |
/** |
|
465 |
* Specifies the Color Panels used to choose a color value. |
|
466 |
* |
|
467 |
* @param panels an array of <code>AbstractColorChooserPanel</code> |
|
468 |
* objects |
|
469 |
* |
|
470 |
* @beaninfo |
|
471 |
* bound: true |
|
472 |
* hidden: true |
|
473 |
* description: An array of different chooser types. |
|
474 |
*/ |
|
475 |
public void setChooserPanels( AbstractColorChooserPanel[] panels) { |
|
476 |
AbstractColorChooserPanel[] oldValue = chooserPanels; |
|
477 |
chooserPanels = panels; |
|
478 |
firePropertyChange(CHOOSER_PANELS_PROPERTY, oldValue, panels); |
|
479 |
} |
|
480 |
||
481 |
/** |
|
482 |
* Returns the specified color panels. |
|
483 |
* |
|
484 |
* @return an array of <code>AbstractColorChooserPanel</code> objects |
|
485 |
*/ |
|
486 |
public AbstractColorChooserPanel[] getChooserPanels() { |
|
487 |
return chooserPanels; |
|
488 |
} |
|
489 |
||
490 |
/** |
|
491 |
* Returns the data model that handles color selections. |
|
492 |
* |
|
493 |
* @return a <code>ColorSelectionModel</code> object |
|
494 |
*/ |
|
495 |
public ColorSelectionModel getSelectionModel() { |
|
496 |
return selectionModel; |
|
497 |
} |
|
498 |
||
499 |
||
500 |
/** |
|
501 |
* Sets the model containing the selected color. |
|
502 |
* |
|
503 |
* @param newModel the new <code>ColorSelectionModel</code> object |
|
504 |
* |
|
505 |
* @beaninfo |
|
506 |
* bound: true |
|
507 |
* hidden: true |
|
508 |
* description: The model which contains the currently selected color. |
|
509 |
*/ |
|
510 |
public void setSelectionModel(ColorSelectionModel newModel ) { |
|
511 |
ColorSelectionModel oldModel = selectionModel; |
|
512 |
selectionModel = newModel; |
|
513 |
firePropertyChange(JColorChooser.SELECTION_MODEL_PROPERTY, oldModel, newModel); |
|
514 |
} |
|
515 |
||
516 |
||
517 |
/** |
|
518 |
* See <code>readObject</code> and <code>writeObject</code> in |
|
519 |
* <code>JComponent</code> for more |
|
520 |
* information about serialization in Swing. |
|
521 |
*/ |
|
522 |
private void writeObject(ObjectOutputStream s) throws IOException { |
|
523 |
s.defaultWriteObject(); |
|
524 |
if (getUIClassID().equals(uiClassID)) { |
|
525 |
byte count = JComponent.getWriteObjCounter(this); |
|
526 |
JComponent.setWriteObjCounter(this, --count); |
|
527 |
if (count == 0 && ui != null) { |
|
528 |
ui.installUI(this); |
|
529 |
} |
|
530 |
} |
|
531 |
} |
|
532 |
||
533 |
||
534 |
/** |
|
535 |
* Returns a string representation of this <code>JColorChooser</code>. |
|
536 |
* This method |
|
537 |
* is intended to be used only for debugging purposes, and the |
|
538 |
* content and format of the returned string may vary between |
|
539 |
* implementations. The returned string may be empty but may not |
|
540 |
* be <code>null</code>. |
|
541 |
* |
|
542 |
* @return a string representation of this <code>JColorChooser</code> |
|
543 |
*/ |
|
544 |
protected String paramString() { |
|
545 |
StringBuffer chooserPanelsString = new StringBuffer(""); |
|
546 |
for (int i=0; i<chooserPanels.length; i++) { |
|
547 |
chooserPanelsString.append("[" + chooserPanels[i].toString() |
|
548 |
+ "]"); |
|
549 |
} |
|
550 |
String previewPanelString = (previewPanel != null ? |
|
551 |
previewPanel.toString() : ""); |
|
552 |
||
553 |
return super.paramString() + |
|
554 |
",chooserPanels=" + chooserPanelsString.toString() + |
|
555 |
",previewPanel=" + previewPanelString; |
|
556 |
} |
|
557 |
||
558 |
///////////////// |
|
559 |
// Accessibility support |
|
560 |
//////////////// |
|
561 |
||
562 |
protected AccessibleContext accessibleContext = null; |
|
563 |
||
564 |
/** |
|
565 |
* Gets the AccessibleContext associated with this JColorChooser. |
|
566 |
* For color choosers, the AccessibleContext takes the form of an |
|
567 |
* AccessibleJColorChooser. |
|
568 |
* A new AccessibleJColorChooser instance is created if necessary. |
|
569 |
* |
|
570 |
* @return an AccessibleJColorChooser that serves as the |
|
571 |
* AccessibleContext of this JColorChooser |
|
572 |
*/ |
|
573 |
public AccessibleContext getAccessibleContext() { |
|
574 |
if (accessibleContext == null) { |
|
575 |
accessibleContext = new AccessibleJColorChooser(); |
|
576 |
} |
|
577 |
return accessibleContext; |
|
578 |
} |
|
579 |
||
580 |
/** |
|
581 |
* This class implements accessibility support for the |
|
582 |
* <code>JColorChooser</code> class. It provides an implementation of the |
|
583 |
* Java Accessibility API appropriate to color chooser user-interface |
|
584 |
* elements. |
|
585 |
*/ |
|
586 |
protected class AccessibleJColorChooser extends AccessibleJComponent { |
|
587 |
||
588 |
/** |
|
589 |
* Get the role of this object. |
|
590 |
* |
|
591 |
* @return an instance of AccessibleRole describing the role of the |
|
592 |
* object |
|
593 |
* @see AccessibleRole |
|
594 |
*/ |
|
595 |
public AccessibleRole getAccessibleRole() { |
|
596 |
return AccessibleRole.COLOR_CHOOSER; |
|
597 |
} |
|
598 |
||
599 |
} // inner class AccessibleJColorChooser |
|
600 |
} |
|
601 |
||
602 |
||
603 |
/* |
|
604 |
* Class which builds a color chooser dialog consisting of |
|
605 |
* a JColorChooser with "Ok", "Cancel", and "Reset" buttons. |
|
606 |
* |
|
607 |
* Note: This needs to be fixed to deal with localization! |
|
608 |
*/ |
|
23697 | 609 |
@SuppressWarnings("serial") // Superclass is not serializable across versions |
2 | 610 |
class ColorChooserDialog extends JDialog { |
611 |
private Color initialColor; |
|
612 |
private JColorChooser chooserPane; |
|
613 |
private JButton cancelButton; |
|
614 |
||
615 |
public ColorChooserDialog(Dialog owner, String title, boolean modal, |
|
616 |
Component c, JColorChooser chooserPane, |
|
617 |
ActionListener okListener, ActionListener cancelListener) |
|
618 |
throws HeadlessException { |
|
619 |
super(owner, title, modal); |
|
620 |
initColorChooserDialog(c, chooserPane, okListener, cancelListener); |
|
621 |
} |
|
622 |
||
623 |
public ColorChooserDialog(Frame owner, String title, boolean modal, |
|
624 |
Component c, JColorChooser chooserPane, |
|
625 |
ActionListener okListener, ActionListener cancelListener) |
|
626 |
throws HeadlessException { |
|
627 |
super(owner, title, modal); |
|
628 |
initColorChooserDialog(c, chooserPane, okListener, cancelListener); |
|
629 |
} |
|
630 |
||
631 |
protected void initColorChooserDialog(Component c, JColorChooser chooserPane, |
|
632 |
ActionListener okListener, ActionListener cancelListener) { |
|
633 |
//setResizable(false); |
|
634 |
||
635 |
this.chooserPane = chooserPane; |
|
636 |
||
637 |
Locale locale = getLocale(); |
|
638 |
String okString = UIManager.getString("ColorChooser.okText", locale); |
|
639 |
String cancelString = UIManager.getString("ColorChooser.cancelText", locale); |
|
640 |
String resetString = UIManager.getString("ColorChooser.resetText", locale); |
|
641 |
||
642 |
Container contentPane = getContentPane(); |
|
643 |
contentPane.setLayout(new BorderLayout()); |
|
644 |
contentPane.add(chooserPane, BorderLayout.CENTER); |
|
645 |
||
646 |
/* |
|
647 |
* Create Lower button panel |
|
648 |
*/ |
|
649 |
JPanel buttonPane = new JPanel(); |
|
650 |
buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER)); |
|
651 |
JButton okButton = new JButton(okString); |
|
652 |
getRootPane().setDefaultButton(okButton); |
|
14658 | 653 |
okButton.getAccessibleContext().setAccessibleDescription(okString); |
2 | 654 |
okButton.setActionCommand("OK"); |
655 |
okButton.addActionListener(new ActionListener() { |
|
656 |
public void actionPerformed(ActionEvent e) { |
|
657 |
hide(); |
|
658 |
} |
|
659 |
}); |
|
660 |
if (okListener != null) { |
|
661 |
okButton.addActionListener(okListener); |
|
662 |
} |
|
663 |
buttonPane.add(okButton); |
|
664 |
||
665 |
cancelButton = new JButton(cancelString); |
|
14658 | 666 |
cancelButton.getAccessibleContext().setAccessibleDescription(cancelString); |
2 | 667 |
|
668 |
// The following few lines are used to register esc to close the dialog |
|
23697 | 669 |
@SuppressWarnings("serial") // anonymous class |
2 | 670 |
Action cancelKeyAction = new AbstractAction() { |
671 |
public void actionPerformed(ActionEvent e) { |
|
672 |
((AbstractButton)e.getSource()).fireActionPerformed(e); |
|
673 |
} |
|
674 |
}; |
|
675 |
KeyStroke cancelKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); |
|
676 |
InputMap inputMap = cancelButton.getInputMap(JComponent. |
|
677 |
WHEN_IN_FOCUSED_WINDOW); |
|
678 |
ActionMap actionMap = cancelButton.getActionMap(); |
|
679 |
if (inputMap != null && actionMap != null) { |
|
680 |
inputMap.put(cancelKeyStroke, "cancel"); |
|
681 |
actionMap.put("cancel", cancelKeyAction); |
|
682 |
} |
|
683 |
// end esc handling |
|
684 |
||
685 |
cancelButton.setActionCommand("cancel"); |
|
686 |
cancelButton.addActionListener(new ActionListener() { |
|
687 |
public void actionPerformed(ActionEvent e) { |
|
688 |
hide(); |
|
689 |
} |
|
690 |
}); |
|
691 |
if (cancelListener != null) { |
|
692 |
cancelButton.addActionListener(cancelListener); |
|
693 |
} |
|
694 |
buttonPane.add(cancelButton); |
|
695 |
||
696 |
JButton resetButton = new JButton(resetString); |
|
14658 | 697 |
resetButton.getAccessibleContext().setAccessibleDescription(resetString); |
2 | 698 |
resetButton.addActionListener(new ActionListener() { |
699 |
public void actionPerformed(ActionEvent e) { |
|
700 |
reset(); |
|
701 |
} |
|
702 |
}); |
|
703 |
int mnemonic = SwingUtilities2.getUIDefaultsInt("ColorChooser.resetMnemonic", locale, -1); |
|
704 |
if (mnemonic != -1) { |
|
705 |
resetButton.setMnemonic(mnemonic); |
|
706 |
} |
|
707 |
buttonPane.add(resetButton); |
|
708 |
contentPane.add(buttonPane, BorderLayout.SOUTH); |
|
709 |
||
710 |
if (JDialog.isDefaultLookAndFeelDecorated()) { |
|
711 |
boolean supportsWindowDecorations = |
|
712 |
UIManager.getLookAndFeel().getSupportsWindowDecorations(); |
|
713 |
if (supportsWindowDecorations) { |
|
714 |
getRootPane().setWindowDecorationStyle(JRootPane.COLOR_CHOOSER_DIALOG); |
|
715 |
} |
|
716 |
} |
|
717 |
applyComponentOrientation(((c == null) ? getRootPane() : c).getComponentOrientation()); |
|
718 |
||
719 |
pack(); |
|
720 |
setLocationRelativeTo(c); |
|
721 |
||
722 |
this.addWindowListener(new Closer()); |
|
723 |
} |
|
724 |
||
725 |
public void show() { |
|
726 |
initialColor = chooserPane.getColor(); |
|
727 |
super.show(); |
|
728 |
} |
|
729 |
||
730 |
public void reset() { |
|
731 |
chooserPane.setColor(initialColor); |
|
732 |
} |
|
733 |
||
23697 | 734 |
@SuppressWarnings("serial") // JDK-implementation class |
2 | 735 |
class Closer extends WindowAdapter implements Serializable{ |
736 |
public void windowClosing(WindowEvent e) { |
|
737 |
cancelButton.doClick(0); |
|
738 |
Window w = e.getWindow(); |
|
739 |
w.hide(); |
|
740 |
} |
|
741 |
} |
|
742 |
||
23697 | 743 |
@SuppressWarnings("serial") // JDK-implementation class |
2 | 744 |
static class DisposeOnClose extends ComponentAdapter implements Serializable{ |
745 |
public void componentHidden(ComponentEvent e) { |
|
746 |
Window w = (Window)e.getComponent(); |
|
747 |
w.dispose(); |
|
748 |
} |
|
749 |
} |
|
750 |
||
751 |
} |
|
752 |
||
23697 | 753 |
@SuppressWarnings("serial") // JDK-implementation class |
2 | 754 |
class ColorTracker implements ActionListener, Serializable { |
755 |
JColorChooser chooser; |
|
756 |
Color color; |
|
757 |
||
758 |
public ColorTracker(JColorChooser c) { |
|
759 |
chooser = c; |
|
760 |
} |
|
761 |
||
762 |
public void actionPerformed(ActionEvent e) { |
|
763 |
color = chooser.getColor(); |
|
764 |
} |
|
765 |
||
766 |
public Color getColor() { |
|
767 |
return color; |
|
768 |
} |
|
769 |
} |