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