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