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