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