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