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