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