< prev index next >

src/java.desktop/share/classes/javax/swing/JColorChooser.java

Print this page




  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      */


 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         return showDialog(component, title, initialColor, true);
 139     }
 140 
 141     /**
 142      * Shows a modal color-chooser dialog and blocks until the
 143      * dialog is hidden.  If the user presses the "OK" button, then
 144      * this method hides/disposes the dialog and returns the selected color.
 145      * If the user presses the "Cancel" button or closes the dialog without
 146      * pressing "OK", then this method hides/disposes the dialog and returns
 147      * <code>null</code>.
 148      *
 149      * @param component    the parent <code>Component</code> for the dialog
 150      * @param title        the String containing the dialog's title
 151      * @param initialColor the initial Color set when the color-chooser is shown
 152      * @param colorTransparencySelectionEnabled true if the transparency of
 153      *            a color can be selected
 154      * @return the selected color or <code>null</code> if the user opted out
 155      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 156      * returns true.
 157      * @see java.awt.GraphicsEnvironment#isHeadless
 158      */
 159     @SuppressWarnings("deprecation")
 160     public static Color showDialog(Component component, String title,
 161             Color initialColor, boolean colorTransparencySelectionEnabled)
 162             throws HeadlessException {
 163 
 164         final JColorChooser pane = new JColorChooser(initialColor != null?
 165                                                initialColor : Color.white);
 166 
 167         for (AbstractColorChooserPanel ccPanel : pane.getChooserPanels()) {
 168             ccPanel.setColorTransparencySelectionEnabled(
 169                     colorTransparencySelectionEnabled);
 170         }
 171 
 172         ColorTracker ok = new ColorTracker(pane);
 173         JDialog dialog = createDialog(component, title, true, pane, ok, null);
 174 
 175         dialog.addComponentListener(new ColorChooserDialog.DisposeOnClose());
 176 
 177         dialog.show(); // blocks until user brings dialog down...
 178 
 179         return ok.getColor();
 180     }
 181 
 182     /**
 183      * Creates and returns a new dialog containing the specified
 184      * <code>ColorChooser</code> pane along with "OK", "Cancel", and "Reset"
 185      * buttons. If the "OK" or "Cancel" buttons are pressed, the dialog is
 186      * automatically hidden (but not disposed).  If the "Reset"
 187      * button is pressed, the color-chooser's color will be reset to the
 188      * color which was set the last time <code>show</code> was invoked on the
 189      * dialog and the dialog will remain showing.
 190      *
 191      * @param c              the parent component for the dialog
 192      * @param title          the title for the dialog
 193      * @param modal          a boolean. When true, the remainder of the program
 194      *                       is inactive until the dialog is closed.
 195      * @param chooserPane    the color-chooser to be placed inside the dialog
 196      * @param okListener     the ActionListener invoked when "OK" is pressed
 197      * @param cancelListener the ActionListener invoked when "Cancel" is pressed
 198      * @return a new dialog containing the color-chooser pane
 199      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 200      * returns true.
 201      * @see java.awt.GraphicsEnvironment#isHeadless
 202      */
 203     public static JDialog createDialog(Component c, String title, boolean modal,
 204         JColorChooser chooserPane, ActionListener okListener,
 205         ActionListener cancelListener) throws HeadlessException {
 206 
 207         Window window = JOptionPane.getWindowForComponent(c);
 208         ColorChooserDialog dialog;


 219 
 220     /**
 221      * Creates a color chooser pane with an initial color of white.
 222      */
 223     public JColorChooser() {
 224         this(Color.white);
 225     }
 226 
 227     /**
 228      * Creates a color chooser pane with the specified initial color.
 229      *
 230      * @param initialColor the initial color set in the chooser
 231      */
 232     public JColorChooser(Color initialColor) {
 233         this( new DefaultColorSelectionModel(initialColor) );
 234 
 235     }
 236 
 237     /**
 238      * Creates a color chooser pane with the specified
 239      * <code>ColorSelectionModel</code>.
 240      *
 241      * @param model the <code>ColorSelectionModel</code> to be used
 242      */
 243     public JColorChooser(ColorSelectionModel model) {
 244         selectionModel = model;
 245         updateUI();
 246         dragEnabled = false;
 247     }
 248 
 249     /**
 250      * Returns the L&amp;F object that renders this component.
 251      *
 252      * @return the <code>ColorChooserUI</code> object that renders
 253      *          this component
 254      */
 255     public ColorChooserUI getUI() {
 256         return (ColorChooserUI)ui;
 257     }
 258 
 259     /**
 260      * Sets the L&amp;F object that renders this component.
 261      *
 262      * @param ui  the <code>ColorChooserUI</code> L&amp;F object
 263      * @see UIDefaults#getUI
 264      *
 265      * @beaninfo
 266      *       bound: true
 267      *      hidden: true
 268      * description: The UI object that implements the color chooser's LookAndFeel.
 269      */
 270     public void setUI(ColorChooserUI ui) {
 271         super.setUI(ui);
 272     }
 273 
 274     /**
 275      * Notification from the <code>UIManager</code> that the L&amp;F has changed.
 276      * Replaces the current UI object with the latest version from the
 277      * <code>UIManager</code>.
 278      *
 279      * @see JComponent#updateUI
 280      */
 281     public void updateUI() {
 282         setUI((ColorChooserUI)UIManager.getUI(this));
 283     }
 284 
 285     /**
 286      * Returns the name of the L&amp;F class that renders this component.
 287      *
 288      * @return the string "ColorChooserUI"
 289      * @see JComponent#getUIClassID
 290      * @see UIDefaults#getUI
 291      */
 292     public String getUIClassID() {
 293         return uiClassID;
 294     }
 295 
 296     /**
 297      * Gets the current color value from the color chooser.
 298      * By default, this delegates to the model.
 299      *
 300      * @return the current color value of the color chooser
 301      */
 302     public Color getColor() {
 303         return selectionModel.getSelectedColor();
 304     }
 305 
 306     /**
 307      * Sets the current color of the color chooser to the specified color.
 308      * The <code>ColorSelectionModel</code> will fire a <code>ChangeEvent</code>
 309      * @param color the color to be set in the color chooser
 310      * @see JComponent#addPropertyChangeListener
 311      *
 312      * @beaninfo
 313      *       bound: false
 314      *      hidden: false
 315      * description: The current color the chooser is to display.
 316      */
 317     public void setColor(Color color) {
 318         selectionModel.setSelectedColor(color);
 319 
 320     }
 321 
 322     /**
 323      * Sets the current color of the color chooser to the
 324      * specified RGB color.  Note that the values of red, green,
 325      * and blue should be between the numbers 0 and 255, inclusive.
 326      *
 327      * @param r   an int specifying the amount of Red
 328      * @param g   an int specifying the amount of Green


 331      * @see java.awt.Color
 332      */
 333     public void setColor(int r, int g, int b) {
 334         setColor(new Color(r,g,b));
 335     }
 336 
 337     /**
 338      * Sets the current color of the color chooser to the
 339      * specified color.
 340      *
 341      * @param c an integer value that sets the current color in the chooser
 342      *          where the low-order 8 bits specify the Blue value,
 343      *          the next 8 bits specify the Green value, and the 8 bits
 344      *          above that specify the Red value.
 345      */
 346     public void setColor(int c) {
 347         setColor((c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF);
 348     }
 349 
 350     /**
 351      * Sets the <code>dragEnabled</code> property,
 352      * which must be <code>true</code> to enable
 353      * automatic drag handling (the first part of drag and drop)
 354      * on this component.
 355      * The <code>transferHandler</code> property needs to be set
 356      * to a non-<code>null</code> value for the drag to do
 357      * anything.  The default value of the <code>dragEnabled</code>
 358      * property
 359      * is <code>false</code>.
 360      *
 361      * <p>
 362      *
 363      * When automatic drag handling is enabled,
 364      * most look and feels begin a drag-and-drop operation
 365      * when the user presses the mouse button over the preview panel.
 366      * Some look and feels might not support automatic drag and drop;
 367      * they will ignore this property.  You can work around such
 368      * look and feels by modifying the component
 369      * to directly call the <code>exportAsDrag</code> method of a
 370      * <code>TransferHandler</code>.
 371      *
 372      * @param b the value to set the <code>dragEnabled</code> property to
 373      * @exception HeadlessException if
 374      *            <code>b</code> is <code>true</code> and
 375      *            <code>GraphicsEnvironment.isHeadless()</code>
 376      *            returns <code>true</code>
 377      *
 378      * @since 1.4
 379      *
 380      * @see java.awt.GraphicsEnvironment#isHeadless
 381      * @see #getDragEnabled
 382      * @see #setTransferHandler
 383      * @see TransferHandler
 384      *
 385      * @beaninfo
 386      *  description: Determines whether automatic drag handling is enabled.
 387      *        bound: false
 388      */
 389     public void setDragEnabled(boolean b) {
 390         if (b && GraphicsEnvironment.isHeadless()) {
 391             throw new HeadlessException();
 392         }
 393         dragEnabled = b;
 394     }
 395 
 396     /**
 397      * Gets the value of the <code>dragEnabled</code> property.
 398      *
 399      * @return  the value of the <code>dragEnabled</code> property
 400      * @see #setDragEnabled
 401      * @since 1.4
 402      */
 403     public boolean getDragEnabled() {
 404         return dragEnabled;
 405     }
 406 
 407     /**
 408      * Sets the current preview panel.
 409      * This will fire a <code>PropertyChangeEvent</code> for the property
 410      * named "previewPanel".
 411      *
 412      * @param preview the <code>JComponent</code> which displays the current color
 413      * @see JComponent#addPropertyChangeListener
 414      *
 415      * @beaninfo
 416      *       bound: true
 417      *      hidden: true
 418      * description: The UI component which displays the current color.
 419      */
 420     public void setPreviewPanel(JComponent preview) {
 421 
 422         if (previewPanel != preview) {
 423             JComponent oldPreview = previewPanel;
 424             previewPanel = preview;
 425             firePropertyChange(JColorChooser.PREVIEW_PANEL_PROPERTY, oldPreview, preview);
 426         }
 427     }
 428 
 429     /**
 430      * Returns the preview panel that shows a chosen color.
 431      *
 432      * @return a <code>JComponent</code> object -- the preview panel
 433      */
 434     public JComponent getPreviewPanel() {
 435         return previewPanel;
 436     }
 437 
 438     /**
 439      * Adds a color chooser panel to the color chooser.
 440      *
 441      * @param panel the <code>AbstractColorChooserPanel</code> to be added
 442      */
 443     public void addChooserPanel( AbstractColorChooserPanel panel ) {
 444         AbstractColorChooserPanel[] oldPanels = getChooserPanels();
 445         AbstractColorChooserPanel[] newPanels = new AbstractColorChooserPanel[oldPanels.length+1];
 446         System.arraycopy(oldPanels, 0, newPanels, 0, oldPanels.length);
 447         newPanels[newPanels.length-1] = panel;
 448         setChooserPanels(newPanels);
 449     }
 450 
 451     /**
 452      * Removes the Color Panel specified.
 453      *
 454      * @param panel   a string that specifies the panel to be removed
 455      * @return the color panel
 456      * @exception IllegalArgumentException if panel is not in list of
 457      *                  known chooser panels
 458      */
 459     public AbstractColorChooserPanel removeChooserPanel( AbstractColorChooserPanel panel ) {
 460 
 461 


 477             System.arraycopy(chooserPanels, 0, newArray, 0, newArray.length);
 478         }
 479         else if (containedAt == 0) {  // at start
 480             System.arraycopy(chooserPanels, 1, newArray, 0, newArray.length);
 481         }
 482         else {  // in middle
 483             System.arraycopy(chooserPanels, 0, newArray, 0, containedAt);
 484             System.arraycopy(chooserPanels, containedAt+1,
 485                              newArray, containedAt, (chooserPanels.length - containedAt - 1));
 486         }
 487 
 488         setChooserPanels(newArray);
 489 
 490         return panel;
 491     }
 492 
 493 
 494     /**
 495      * Specifies the Color Panels used to choose a color value.
 496      *
 497      * @param panels  an array of <code>AbstractColorChooserPanel</code>
 498      *          objects
 499      *
 500      * @beaninfo
 501      *       bound: true
 502      *      hidden: true
 503      * description: An array of different chooser types.
 504      */
 505     public void setChooserPanels( AbstractColorChooserPanel[] panels) {
 506         AbstractColorChooserPanel[] oldValue = chooserPanels;
 507         chooserPanels = panels;
 508         firePropertyChange(CHOOSER_PANELS_PROPERTY, oldValue, panels);
 509     }
 510 
 511     /**
 512      * Returns the specified color panels.
 513      *
 514      * @return an array of <code>AbstractColorChooserPanel</code> objects
 515      */
 516     public AbstractColorChooserPanel[] getChooserPanels() {
 517         return chooserPanels;
 518     }
 519 
 520     /**
 521      * Returns the data model that handles color selections.
 522      *
 523      * @return a <code>ColorSelectionModel</code> object
 524      */
 525     public ColorSelectionModel getSelectionModel() {
 526         return selectionModel;
 527     }
 528 
 529 
 530     /**
 531      * Sets the model containing the selected color.
 532      *
 533      * @param newModel   the new <code>ColorSelectionModel</code> object
 534      *
 535      * @beaninfo
 536      *       bound: true
 537      *      hidden: true
 538      * description: The model which contains the currently selected color.
 539      */
 540     public void setSelectionModel(ColorSelectionModel newModel ) {
 541         ColorSelectionModel oldModel = selectionModel;
 542         selectionModel = newModel;
 543         firePropertyChange(JColorChooser.SELECTION_MODEL_PROPERTY, oldModel, newModel);
 544     }
 545 
 546 
 547     /**
 548      * See <code>readObject</code> and <code>writeObject</code> in
 549      * <code>JComponent</code> for more
 550      * information about serialization in Swing.
 551      */
 552     private void writeObject(ObjectOutputStream s) throws IOException {
 553         s.defaultWriteObject();
 554         if (getUIClassID().equals(uiClassID)) {
 555             byte count = JComponent.getWriteObjCounter(this);
 556             JComponent.setWriteObjCounter(this, --count);
 557             if (count == 0 && ui != null) {
 558                 ui.installUI(this);
 559             }
 560         }
 561     }
 562 
 563 
 564     /**
 565      * Returns a string representation of this <code>JColorChooser</code>.
 566      * This method
 567      * is intended to be used only for debugging purposes, and the
 568      * content and format of the returned string may vary between
 569      * implementations. The returned string may be empty but may not
 570      * be <code>null</code>.
 571      *
 572      * @return  a string representation of this <code>JColorChooser</code>
 573      */
 574     protected String paramString() {
 575         StringBuilder chooserPanelsString = new StringBuilder();
 576         for (AbstractColorChooserPanel panel : chooserPanels) {
 577             chooserPanelsString.append('[').append(panel)
 578                                .append(']');
 579         }
 580         String previewPanelString = (previewPanel != null ? previewPanel
 581                 .toString() : "");
 582 
 583         return super.paramString() + ",chooserPanels="
 584                 + chooserPanelsString.toString() + ",previewPanel="
 585                 + previewPanelString;
 586     }
 587 
 588 /////////////////
 589 // Accessibility support
 590 ////////////////
 591 
 592     /**


 595     protected AccessibleContext accessibleContext = null;
 596 
 597     /**
 598      * Gets the AccessibleContext associated with this JColorChooser.
 599      * For color choosers, the AccessibleContext takes the form of an
 600      * AccessibleJColorChooser.
 601      * A new AccessibleJColorChooser instance is created if necessary.
 602      *
 603      * @return an AccessibleJColorChooser that serves as the
 604      *         AccessibleContext of this JColorChooser
 605      */
 606     public AccessibleContext getAccessibleContext() {
 607         if (accessibleContext == null) {
 608             accessibleContext = new AccessibleJColorChooser();
 609         }
 610         return accessibleContext;
 611     }
 612 
 613     /**
 614      * This class implements accessibility support for the
 615      * <code>JColorChooser</code> class.  It provides an implementation of the
 616      * Java Accessibility API appropriate to color chooser user-interface
 617      * elements.
 618      */
 619     protected class AccessibleJColorChooser extends AccessibleJComponent {
 620 
 621         /**
 622          * Get the role of this object.
 623          *
 624          * @return an instance of AccessibleRole describing the role of the
 625          * object
 626          * @see AccessibleRole
 627          */
 628         public AccessibleRole getAccessibleRole() {
 629             return AccessibleRole.COLOR_CHOOSER;
 630         }
 631 
 632     } // inner class AccessibleJColorChooser
 633 }
 634 
 635 




  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing;
  27 
  28 import java.awt.*;
  29 import java.awt.event.*;
  30 import java.io.*;
  31 import java.util.*;
  32 
  33 import javax.swing.colorchooser.*;
  34 import javax.swing.plaf.ColorChooserUI;
  35 import javax.accessibility.*;
  36 
  37 import sun.swing.SwingUtilities2;
  38 
  39 
  40 /**
  41  * {@code JColorChooser} provides a pane of controls designed to allow
  42  * a user to manipulate and select a color.
  43  * For information about using color choosers, see
  44  * <a
  45  href="http://docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html">How to Use Color Choosers</a>,
  46  * a section in <em>The Java Tutorial</em>.
  47  *
  48  * <p>
  49  *
  50  * This class provides three levels of API:
  51  * <ol>
  52  * <li>A static convenience method which shows a modal color-chooser
  53  * dialog and returns the color selected by the user.
  54  * <li>A static convenience method for creating a color-chooser dialog
  55  * where {@code ActionListeners} can be specified to be invoked when
  56  * the user presses one of the dialog buttons.
  57  * <li>The ability to create instances of {@code JColorChooser} panes
  58  * directly (within any container). {@code PropertyChange} listeners
  59  * can be added to detect when the current "color" property changes.
  60  * </ol>
  61  * <p>
  62  * <strong>Warning:</strong> Swing is not thread safe. For more
  63  * information see <a
  64  * href="package-summary.html#threading">Swing's Threading
  65  * Policy</a>.
  66  * <p>
  67  * <strong>Warning:</strong>
  68  * Serialized objects of this class will not be compatible with
  69  * future Swing releases. The current serialization support is
  70  * appropriate for short term storage or RMI between applications running
  71  * the same version of Swing.  As of 1.4, support for long term storage
  72  * of all JavaBeans&trade;
  73  * has been added to the {@code java.beans} package.
  74  * Please see {@link java.beans.XMLEncoder}.
  75  *
  76  *
  77  * @beaninfo
  78  *      attribute: isContainer false
  79  *    description: A component that supports selecting a Color.
  80  *
  81  *
  82  * @author James Gosling
  83  * @author Amy Fowler
  84  * @author Steve Wilson
  85  * @since 1.2
  86  */
  87 @SuppressWarnings("serial") // Same-version serialization only
  88 public class JColorChooser extends JComponent implements Accessible {
  89 
  90     /**
  91      * @see #getUIClassID
  92      * @see #readObject
  93      */


 106      */
 107     public static final String      SELECTION_MODEL_PROPERTY = "selectionModel";
 108 
 109     /**
 110      * The preview panel property name.
 111      */
 112     public static final String      PREVIEW_PANEL_PROPERTY = "previewPanel";
 113 
 114     /**
 115      * The chooserPanel array property name.
 116      */
 117     public static final String      CHOOSER_PANELS_PROPERTY = "chooserPanels";
 118 
 119 
 120     /**
 121      * Shows a modal color-chooser dialog and blocks until the
 122      * dialog is hidden.  If the user presses the "OK" button, then
 123      * this method hides/disposes the dialog and returns the selected color.
 124      * If the user presses the "Cancel" button or closes the dialog without
 125      * pressing "OK", then this method hides/disposes the dialog and returns
 126      * {@code null}.
 127      *
 128      * @param component    the parent {@code Component} for the dialog
 129      * @param title        the String containing the dialog's title
 130      * @param initialColor the initial Color set when the color-chooser is shown
 131      * @return the selected color or {@code null} if the user opted out
 132      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 133      * returns true.
 134      * @see java.awt.GraphicsEnvironment#isHeadless
 135      */
 136     public static Color showDialog(Component component,
 137             String title, Color initialColor) throws HeadlessException {
 138         return showDialog(component, title, initialColor, true);
 139     }
 140 
 141     /**
 142      * Shows a modal color-chooser dialog and blocks until the
 143      * dialog is hidden.  If the user presses the "OK" button, then
 144      * this method hides/disposes the dialog and returns the selected color.
 145      * If the user presses the "Cancel" button or closes the dialog without
 146      * pressing "OK", then this method hides/disposes the dialog and returns
 147      * {@code null}.
 148      *
 149      * @param component    the parent {@code Component} for the dialog
 150      * @param title        the String containing the dialog's title
 151      * @param initialColor the initial Color set when the color-chooser is shown
 152      * @param colorTransparencySelectionEnabled true if the transparency of
 153      *            a color can be selected
 154      * @return the selected color or {@code null} if the user opted out
 155      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 156      * returns true.
 157      * @see java.awt.GraphicsEnvironment#isHeadless
 158      */
 159     @SuppressWarnings("deprecation")
 160     public static Color showDialog(Component component, String title,
 161             Color initialColor, boolean colorTransparencySelectionEnabled)
 162             throws HeadlessException {
 163 
 164         final JColorChooser pane = new JColorChooser(initialColor != null?
 165                                                initialColor : Color.white);
 166 
 167         for (AbstractColorChooserPanel ccPanel : pane.getChooserPanels()) {
 168             ccPanel.setColorTransparencySelectionEnabled(
 169                     colorTransparencySelectionEnabled);
 170         }
 171 
 172         ColorTracker ok = new ColorTracker(pane);
 173         JDialog dialog = createDialog(component, title, true, pane, ok, null);
 174 
 175         dialog.addComponentListener(new ColorChooserDialog.DisposeOnClose());
 176 
 177         dialog.show(); // blocks until user brings dialog down...
 178 
 179         return ok.getColor();
 180     }
 181 
 182     /**
 183      * Creates and returns a new dialog containing the specified
 184      * {@code ColorChooser} pane along with "OK", "Cancel", and "Reset"
 185      * buttons. If the "OK" or "Cancel" buttons are pressed, the dialog is
 186      * automatically hidden (but not disposed).  If the "Reset"
 187      * button is pressed, the color-chooser's color will be reset to the
 188      * color which was set the last time {@code show} was invoked on the
 189      * dialog and the dialog will remain showing.
 190      *
 191      * @param c              the parent component for the dialog
 192      * @param title          the title for the dialog
 193      * @param modal          a boolean. When true, the remainder of the program
 194      *                       is inactive until the dialog is closed.
 195      * @param chooserPane    the color-chooser to be placed inside the dialog
 196      * @param okListener     the ActionListener invoked when "OK" is pressed
 197      * @param cancelListener the ActionListener invoked when "Cancel" is pressed
 198      * @return a new dialog containing the color-chooser pane
 199      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 200      * returns true.
 201      * @see java.awt.GraphicsEnvironment#isHeadless
 202      */
 203     public static JDialog createDialog(Component c, String title, boolean modal,
 204         JColorChooser chooserPane, ActionListener okListener,
 205         ActionListener cancelListener) throws HeadlessException {
 206 
 207         Window window = JOptionPane.getWindowForComponent(c);
 208         ColorChooserDialog dialog;


 219 
 220     /**
 221      * Creates a color chooser pane with an initial color of white.
 222      */
 223     public JColorChooser() {
 224         this(Color.white);
 225     }
 226 
 227     /**
 228      * Creates a color chooser pane with the specified initial color.
 229      *
 230      * @param initialColor the initial color set in the chooser
 231      */
 232     public JColorChooser(Color initialColor) {
 233         this( new DefaultColorSelectionModel(initialColor) );
 234 
 235     }
 236 
 237     /**
 238      * Creates a color chooser pane with the specified
 239      * {@code ColorSelectionModel}.
 240      *
 241      * @param model the {@code ColorSelectionModel} to be used
 242      */
 243     public JColorChooser(ColorSelectionModel model) {
 244         selectionModel = model;
 245         updateUI();
 246         dragEnabled = false;
 247     }
 248 
 249     /**
 250      * Returns the L&amp;F object that renders this component.
 251      *
 252      * @return the {@code ColorChooserUI} object that renders
 253      *          this component
 254      */
 255     public ColorChooserUI getUI() {
 256         return (ColorChooserUI)ui;
 257     }
 258 
 259     /**
 260      * Sets the L&amp;F object that renders this component.
 261      *
 262      * @param ui  the {@code ColorChooserUI} L&amp;F object
 263      * @see UIDefaults#getUI
 264      *
 265      * @beaninfo
 266      *       bound: true
 267      *      hidden: true
 268      * description: The UI object that implements the color chooser's LookAndFeel.
 269      */
 270     public void setUI(ColorChooserUI ui) {
 271         super.setUI(ui);
 272     }
 273 
 274     /**
 275      * Notification from the {@code UIManager} that the L&amp;F has changed.
 276      * Replaces the current UI object with the latest version from the
 277      * {@code UIManager}.
 278      *
 279      * @see JComponent#updateUI
 280      */
 281     public void updateUI() {
 282         setUI((ColorChooserUI)UIManager.getUI(this));
 283     }
 284 
 285     /**
 286      * Returns the name of the L&amp;F class that renders this component.
 287      *
 288      * @return the string "ColorChooserUI"
 289      * @see JComponent#getUIClassID
 290      * @see UIDefaults#getUI
 291      */
 292     public String getUIClassID() {
 293         return uiClassID;
 294     }
 295 
 296     /**
 297      * Gets the current color value from the color chooser.
 298      * By default, this delegates to the model.
 299      *
 300      * @return the current color value of the color chooser
 301      */
 302     public Color getColor() {
 303         return selectionModel.getSelectedColor();
 304     }
 305 
 306     /**
 307      * Sets the current color of the color chooser to the specified color.
 308      * The {@code ColorSelectionModel} will fire a {@code ChangeEvent}
 309      * @param color the color to be set in the color chooser
 310      * @see JComponent#addPropertyChangeListener
 311      *
 312      * @beaninfo
 313      *       bound: false
 314      *      hidden: false
 315      * description: The current color the chooser is to display.
 316      */
 317     public void setColor(Color color) {
 318         selectionModel.setSelectedColor(color);
 319 
 320     }
 321 
 322     /**
 323      * Sets the current color of the color chooser to the
 324      * specified RGB color.  Note that the values of red, green,
 325      * and blue should be between the numbers 0 and 255, inclusive.
 326      *
 327      * @param r   an int specifying the amount of Red
 328      * @param g   an int specifying the amount of Green


 331      * @see java.awt.Color
 332      */
 333     public void setColor(int r, int g, int b) {
 334         setColor(new Color(r,g,b));
 335     }
 336 
 337     /**
 338      * Sets the current color of the color chooser to the
 339      * specified color.
 340      *
 341      * @param c an integer value that sets the current color in the chooser
 342      *          where the low-order 8 bits specify the Blue value,
 343      *          the next 8 bits specify the Green value, and the 8 bits
 344      *          above that specify the Red value.
 345      */
 346     public void setColor(int c) {
 347         setColor((c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF);
 348     }
 349 
 350     /**
 351      * Sets the {@code dragEnabled} property,
 352      * which must be {@code true} to enable
 353      * automatic drag handling (the first part of drag and drop)
 354      * on this component.
 355      * The {@code transferHandler} property needs to be set
 356      * to a non-{@code null} value for the drag to do
 357      * anything.  The default value of the {@code dragEnabled}
 358      * property
 359      * is {@code false}.
 360      *
 361      * <p>
 362      *
 363      * When automatic drag handling is enabled,
 364      * most look and feels begin a drag-and-drop operation
 365      * when the user presses the mouse button over the preview panel.
 366      * Some look and feels might not support automatic drag and drop;
 367      * they will ignore this property.  You can work around such
 368      * look and feels by modifying the component
 369      * to directly call the {@code exportAsDrag} method of a
 370      * {@code TransferHandler}.
 371      *
 372      * @param b the value to set the {@code dragEnabled} property to
 373      * @exception HeadlessException if
 374      *            {@code b} is {@code true} and
 375      *            {@code GraphicsEnvironment.isHeadless()}
 376      *            returns {@code true}
 377      *
 378      * @since 1.4
 379      *
 380      * @see java.awt.GraphicsEnvironment#isHeadless
 381      * @see #getDragEnabled
 382      * @see #setTransferHandler
 383      * @see TransferHandler
 384      *
 385      * @beaninfo
 386      *  description: Determines whether automatic drag handling is enabled.
 387      *        bound: false
 388      */
 389     public void setDragEnabled(boolean b) {
 390         if (b && GraphicsEnvironment.isHeadless()) {
 391             throw new HeadlessException();
 392         }
 393         dragEnabled = b;
 394     }
 395 
 396     /**
 397      * Gets the value of the {@code dragEnabled} property.
 398      *
 399      * @return  the value of the {@code dragEnabled} property
 400      * @see #setDragEnabled
 401      * @since 1.4
 402      */
 403     public boolean getDragEnabled() {
 404         return dragEnabled;
 405     }
 406 
 407     /**
 408      * Sets the current preview panel.
 409      * This will fire a {@code PropertyChangeEvent} for the property
 410      * named "previewPanel".
 411      *
 412      * @param preview the {@code JComponent} which displays the current color
 413      * @see JComponent#addPropertyChangeListener
 414      *
 415      * @beaninfo
 416      *       bound: true
 417      *      hidden: true
 418      * description: The UI component which displays the current color.
 419      */
 420     public void setPreviewPanel(JComponent preview) {
 421 
 422         if (previewPanel != preview) {
 423             JComponent oldPreview = previewPanel;
 424             previewPanel = preview;
 425             firePropertyChange(JColorChooser.PREVIEW_PANEL_PROPERTY, oldPreview, preview);
 426         }
 427     }
 428 
 429     /**
 430      * Returns the preview panel that shows a chosen color.
 431      *
 432      * @return a {@code JComponent} object -- the preview panel
 433      */
 434     public JComponent getPreviewPanel() {
 435         return previewPanel;
 436     }
 437 
 438     /**
 439      * Adds a color chooser panel to the color chooser.
 440      *
 441      * @param panel the {@code AbstractColorChooserPanel} to be added
 442      */
 443     public void addChooserPanel( AbstractColorChooserPanel panel ) {
 444         AbstractColorChooserPanel[] oldPanels = getChooserPanels();
 445         AbstractColorChooserPanel[] newPanels = new AbstractColorChooserPanel[oldPanels.length+1];
 446         System.arraycopy(oldPanels, 0, newPanels, 0, oldPanels.length);
 447         newPanels[newPanels.length-1] = panel;
 448         setChooserPanels(newPanels);
 449     }
 450 
 451     /**
 452      * Removes the Color Panel specified.
 453      *
 454      * @param panel   a string that specifies the panel to be removed
 455      * @return the color panel
 456      * @exception IllegalArgumentException if panel is not in list of
 457      *                  known chooser panels
 458      */
 459     public AbstractColorChooserPanel removeChooserPanel( AbstractColorChooserPanel panel ) {
 460 
 461 


 477             System.arraycopy(chooserPanels, 0, newArray, 0, newArray.length);
 478         }
 479         else if (containedAt == 0) {  // at start
 480             System.arraycopy(chooserPanels, 1, newArray, 0, newArray.length);
 481         }
 482         else {  // in middle
 483             System.arraycopy(chooserPanels, 0, newArray, 0, containedAt);
 484             System.arraycopy(chooserPanels, containedAt+1,
 485                              newArray, containedAt, (chooserPanels.length - containedAt - 1));
 486         }
 487 
 488         setChooserPanels(newArray);
 489 
 490         return panel;
 491     }
 492 
 493 
 494     /**
 495      * Specifies the Color Panels used to choose a color value.
 496      *
 497      * @param panels  an array of {@code AbstractColorChooserPanel}
 498      *          objects
 499      *
 500      * @beaninfo
 501      *       bound: true
 502      *      hidden: true
 503      * description: An array of different chooser types.
 504      */
 505     public void setChooserPanels( AbstractColorChooserPanel[] panels) {
 506         AbstractColorChooserPanel[] oldValue = chooserPanels;
 507         chooserPanels = panels;
 508         firePropertyChange(CHOOSER_PANELS_PROPERTY, oldValue, panels);
 509     }
 510 
 511     /**
 512      * Returns the specified color panels.
 513      *
 514      * @return an array of {@code AbstractColorChooserPanel} objects
 515      */
 516     public AbstractColorChooserPanel[] getChooserPanels() {
 517         return chooserPanels;
 518     }
 519 
 520     /**
 521      * Returns the data model that handles color selections.
 522      *
 523      * @return a {@code ColorSelectionModel} object
 524      */
 525     public ColorSelectionModel getSelectionModel() {
 526         return selectionModel;
 527     }
 528 
 529 
 530     /**
 531      * Sets the model containing the selected color.
 532      *
 533      * @param newModel   the new {@code ColorSelectionModel} object
 534      *
 535      * @beaninfo
 536      *       bound: true
 537      *      hidden: true
 538      * description: The model which contains the currently selected color.
 539      */
 540     public void setSelectionModel(ColorSelectionModel newModel ) {
 541         ColorSelectionModel oldModel = selectionModel;
 542         selectionModel = newModel;
 543         firePropertyChange(JColorChooser.SELECTION_MODEL_PROPERTY, oldModel, newModel);
 544     }
 545 
 546 
 547     /**
 548      * See {@code readObject} and {@code writeObject} in
 549      * {@code JComponent} for more
 550      * information about serialization in Swing.
 551      */
 552     private void writeObject(ObjectOutputStream s) throws IOException {
 553         s.defaultWriteObject();
 554         if (getUIClassID().equals(uiClassID)) {
 555             byte count = JComponent.getWriteObjCounter(this);
 556             JComponent.setWriteObjCounter(this, --count);
 557             if (count == 0 && ui != null) {
 558                 ui.installUI(this);
 559             }
 560         }
 561     }
 562 
 563 
 564     /**
 565      * Returns a string representation of this {@code JColorChooser}.
 566      * This method
 567      * is intended to be used only for debugging purposes, and the
 568      * content and format of the returned string may vary between
 569      * implementations. The returned string may be empty but may not
 570      * be {@code null}.
 571      *
 572      * @return  a string representation of this {@code JColorChooser}
 573      */
 574     protected String paramString() {
 575         StringBuilder chooserPanelsString = new StringBuilder();
 576         for (AbstractColorChooserPanel panel : chooserPanels) {
 577             chooserPanelsString.append('[').append(panel)
 578                                .append(']');
 579         }
 580         String previewPanelString = (previewPanel != null ? previewPanel
 581                 .toString() : "");
 582 
 583         return super.paramString() + ",chooserPanels="
 584                 + chooserPanelsString.toString() + ",previewPanel="
 585                 + previewPanelString;
 586     }
 587 
 588 /////////////////
 589 // Accessibility support
 590 ////////////////
 591 
 592     /**


 595     protected AccessibleContext accessibleContext = null;
 596 
 597     /**
 598      * Gets the AccessibleContext associated with this JColorChooser.
 599      * For color choosers, the AccessibleContext takes the form of an
 600      * AccessibleJColorChooser.
 601      * A new AccessibleJColorChooser instance is created if necessary.
 602      *
 603      * @return an AccessibleJColorChooser that serves as the
 604      *         AccessibleContext of this JColorChooser
 605      */
 606     public AccessibleContext getAccessibleContext() {
 607         if (accessibleContext == null) {
 608             accessibleContext = new AccessibleJColorChooser();
 609         }
 610         return accessibleContext;
 611     }
 612 
 613     /**
 614      * This class implements accessibility support for the
 615      * {@code JColorChooser} class.  It provides an implementation of the
 616      * Java Accessibility API appropriate to color chooser user-interface
 617      * elements.
 618      */
 619     protected class AccessibleJColorChooser extends AccessibleJComponent {
 620 
 621         /**
 622          * Get the role of this object.
 623          *
 624          * @return an instance of AccessibleRole describing the role of the
 625          * object
 626          * @see AccessibleRole
 627          */
 628         public AccessibleRole getAccessibleRole() {
 629             return AccessibleRole.COLOR_CHOOSER;
 630         }
 631 
 632     } // inner class AccessibleJColorChooser
 633 }
 634 
 635 


< prev index next >