jdk/src/java.desktop/share/classes/javax/swing/JColorChooser.java
author aghaisas
Mon, 10 Jul 2017 14:55:29 +0530
changeset 47151 362dcbee0613
parent 43322 046c9ccca33d
permissions -rw-r--r--
6919529: NPE from MultiUIDefaults.getUIError Reviewed-by: aghaisas, psadhukhan, serb Contributed-by: shashidhara.veerabhadraiah@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30464
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28231
diff changeset
     2
 * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
    29
import java.beans.JavaBean;
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
    30
import java.beans.BeanProperty;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.colorchooser.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.plaf.ColorChooserUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <code>JColorChooser</code> provides a pane of controls designed to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * a user to manipulate and select a color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * For information about using color choosers, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <a
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 20169
diff changeset
    45
 href="http://docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html">How to Use Color Choosers</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * a section in <em>The Java Tutorial</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This class provides three levels of API:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <li>A static convenience method which shows a modal color-chooser
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * dialog and returns the color selected by the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <li>A static convenience method for creating a color-chooser dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * where <code>ActionListeners</code> can be specified to be invoked when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * the user presses one of the dialog buttons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <li>The ability to create instances of <code>JColorChooser</code> panes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * directly (within any container). <code>PropertyChange</code> listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * can be added to detect when the current "color" property changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <strong>Warning:</strong> Swing is not thread safe. For more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * information see <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * href="package-summary.html#threading">Swing's Threading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Policy</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
    72
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @author James Gosling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @author Amy Fowler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * @author Steve Wilson
25201
4adc75e0c4e5 8046485: Add missing @since tag under javax.swing.*
henryjen
parents: 24969
diff changeset
    79
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
    81
@JavaBean(defaultProperty = "UI", description = "A component that supports selecting a Color.")
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
    82
@SwingContainer(false)
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    83
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
public class JColorChooser extends JComponent implements Accessible {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @see #getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @see #readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static final String uiClassID = "ColorChooserUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private ColorSelectionModel selectionModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
6512
ce0a2790e09f 6977726: JColorChooser.getPreviewPanel() returnes null starting from jdk7 b105.
malenkov
parents: 5506
diff changeset
    94
    private JComponent previewPanel = ColorChooserComponentFactory.getPreviewPanel();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private AbstractColorChooserPanel[] chooserPanels = new AbstractColorChooserPanel[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private boolean dragEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * The selection model property name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public static final String      SELECTION_MODEL_PROPERTY = "selectionModel";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * The preview panel property name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public static final String      PREVIEW_PANEL_PROPERTY = "previewPanel";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * The chooserPanel array property name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public static final String      CHOOSER_PANELS_PROPERTY = "chooserPanels";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Shows a modal color-chooser dialog and blocks until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * dialog is hidden.  If the user presses the "OK" button, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * this method hides/disposes the dialog and returns the selected color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * If the user presses the "Cancel" button or closes the dialog without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * pressing "OK", then this method hides/disposes the dialog and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param component    the parent <code>Component</code> for the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param title        the String containing the dialog's title
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param initialColor the initial Color set when the color-chooser is shown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @return the selected color or <code>null</code> if the user opted out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
32488
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   132
    public static Color showDialog(Component component,
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   133
            String title, Color initialColor) throws HeadlessException {
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   134
        return showDialog(component, title, initialColor, true);
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   135
    }
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   136
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   137
    /**
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   138
     * Shows a modal color-chooser dialog and blocks until the
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   139
     * dialog is hidden.  If the user presses the "OK" button, then
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   140
     * this method hides/disposes the dialog and returns the selected color.
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   141
     * If the user presses the "Cancel" button or closes the dialog without
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   142
     * pressing "OK", then this method hides/disposes the dialog and returns
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   143
     * <code>null</code>.
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   144
     *
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   145
     * @param component    the parent <code>Component</code> for the dialog
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   146
     * @param title        the String containing the dialog's title
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   147
     * @param initialColor the initial Color set when the color-chooser is shown
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   148
     * @param colorTransparencySelectionEnabled true if the transparency of
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   149
     *            a color can be selected
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   150
     * @return the selected color or <code>null</code> if the user opted out
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   151
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   152
     * returns true.
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   153
     * @see java.awt.GraphicsEnvironment#isHeadless
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   154
     */
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 28228
diff changeset
   155
    @SuppressWarnings("deprecation")
32488
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   156
    public static Color showDialog(Component component, String title,
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   157
            Color initialColor, boolean colorTransparencySelectionEnabled)
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   158
            throws HeadlessException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        final JColorChooser pane = new JColorChooser(initialColor != null?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                               initialColor : Color.white);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
32488
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   163
        for (AbstractColorChooserPanel ccPanel : pane.getChooserPanels()) {
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   164
            ccPanel.setColorTransparencySelectionEnabled(
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   165
                    colorTransparencySelectionEnabled);
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   166
        }
02db7857c66d 8051548: JColorChooser should have a way to disable transparency controls
alexsch
parents: 30464
diff changeset
   167
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        ColorTracker ok = new ColorTracker(pane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        JDialog dialog = createDialog(component, title, true, pane, ok, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        dialog.addComponentListener(new ColorChooserDialog.DisposeOnClose());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        dialog.show(); // blocks until user brings dialog down...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return ok.getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Creates and returns a new dialog containing the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <code>ColorChooser</code> pane along with "OK", "Cancel", and "Reset"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * buttons. If the "OK" or "Cancel" buttons are pressed, the dialog is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * automatically hidden (but not disposed).  If the "Reset"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * button is pressed, the color-chooser's color will be reset to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * color which was set the last time <code>show</code> was invoked on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * dialog and the dialog will remain showing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param c              the parent component for the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param title          the title for the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param modal          a boolean. When true, the remainder of the program
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *                       is inactive until the dialog is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param chooserPane    the color-chooser to be placed inside the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param okListener     the ActionListener invoked when "OK" is pressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param cancelListener the ActionListener invoked when "Cancel" is pressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @return a new dialog containing the color-chooser pane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public static JDialog createDialog(Component c, String title, boolean modal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        JColorChooser chooserPane, ActionListener okListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        ActionListener cancelListener) throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        Window window = JOptionPane.getWindowForComponent(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        ColorChooserDialog dialog;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (window instanceof Frame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            dialog = new ColorChooserDialog((Frame)window, title, modal, c, chooserPane,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                            okListener, cancelListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            dialog = new ColorChooserDialog((Dialog)window, title, modal, c, chooserPane,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                            okListener, cancelListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
14658
67661687f722 4631925: JColor Chooser is not fully accessible
jviswana
parents: 7668
diff changeset
   212
        dialog.getAccessibleContext().setAccessibleDescription(title);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return dialog;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Creates a color chooser pane with an initial color of white.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public JColorChooser() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        this(Color.white);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Creates a color chooser pane with the specified initial color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param initialColor the initial color set in the chooser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public JColorChooser(Color initialColor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        this( new DefaultColorSelectionModel(initialColor) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Creates a color chooser pane with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <code>ColorSelectionModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param model the <code>ColorSelectionModel</code> to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public JColorChooser(ColorSelectionModel model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        selectionModel = model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        updateUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        dragEnabled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 14658
diff changeset
   246
     * Returns the L&amp;F object that renders this component.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @return the <code>ColorChooserUI</code> object that renders
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *          this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public ColorChooserUI getUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return (ColorChooserUI)ui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 14658
diff changeset
   256
     * Sets the L&amp;F object that renders this component.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 14658
diff changeset
   258
     * @param ui  the <code>ColorChooserUI</code> L&amp;F object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
   261
    @BeanProperty(hidden = true, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
   262
            = "The UI object that implements the color chooser's LookAndFeel.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public void setUI(ColorChooserUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        super.setUI(ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 14658
diff changeset
   268
     * Notification from the <code>UIManager</code> that the L&amp;F has changed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Replaces the current UI object with the latest version from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <code>UIManager</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @see JComponent#updateUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        setUI((ColorChooserUI)UIManager.getUI(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 14658
diff changeset
   279
     * Returns the name of the L&amp;F class that renders this component.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @return the string "ColorChooserUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
   285
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Gets the current color value from the color chooser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * By default, this delegates to the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @return the current color value of the color chooser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public Color getColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return selectionModel.getSelectedColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * Sets the current color of the color chooser to the specified color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * The <code>ColorSelectionModel</code> will fire a <code>ChangeEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param color the color to be set in the color chooser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @see JComponent#addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
   306
    @BeanProperty(bound = false, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
   307
            = "The current color the chooser is to display.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public void setColor(Color color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        selectionModel.setSelectedColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Sets the current color of the color chooser to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * specified RGB color.  Note that the values of red, green,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * and blue should be between the numbers 0 and 255, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @param r   an int specifying the amount of Red
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @param g   an int specifying the amount of Green
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param b   an int specifying the amount of Blue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @exception IllegalArgumentException if r,g,b values are out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @see java.awt.Color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public void setColor(int r, int g, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        setColor(new Color(r,g,b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Sets the current color of the color chooser to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * specified color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param c an integer value that sets the current color in the chooser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *          where the low-order 8 bits specify the Blue value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *          the next 8 bits specify the Green value, and the 8 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *          above that specify the Red value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public void setColor(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        setColor((c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Sets the <code>dragEnabled</code> property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * which must be <code>true</code> to enable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * automatic drag handling (the first part of drag and drop)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * on this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * The <code>transferHandler</code> property needs to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * to a non-<code>null</code> value for the drag to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * anything.  The default value of the <code>dragEnabled</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * is <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * When automatic drag handling is enabled,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * most look and feels begin a drag-and-drop operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * when the user presses the mouse button over the preview panel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Some look and feels might not support automatic drag and drop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * they will ignore this property.  You can work around such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * look and feels by modifying the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * to directly call the <code>exportAsDrag</code> method of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * <code>TransferHandler</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param b the value to set the <code>dragEnabled</code> property to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @exception HeadlessException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *            <code>b</code> is <code>true</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *            <code>GraphicsEnvironment.isHeadless()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *            returns <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @see #getDragEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @see #setTransferHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @see TransferHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
   376
    @BeanProperty(bound = false, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
   377
            = "Determines whether automatic drag handling is enabled.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public void setDragEnabled(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (b && GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            throw new HeadlessException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        dragEnabled = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Gets the value of the <code>dragEnabled</code> property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @return  the value of the <code>dragEnabled</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @see #setDragEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public boolean getDragEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return dragEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Sets the current preview panel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * This will fire a <code>PropertyChangeEvent</code> for the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * named "previewPanel".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @param preview the <code>JComponent</code> which displays the current color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @see JComponent#addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
   404
    @BeanProperty(hidden = true, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
   405
            = "The UI component which displays the current color.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    public void setPreviewPanel(JComponent preview) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (previewPanel != preview) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            JComponent oldPreview = previewPanel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            previewPanel = preview;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            firePropertyChange(JColorChooser.PREVIEW_PANEL_PROPERTY, oldPreview, preview);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Returns the preview panel that shows a chosen color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @return a <code>JComponent</code> object -- the preview panel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public JComponent getPreviewPanel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return previewPanel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Adds a color chooser panel to the color chooser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @param panel the <code>AbstractColorChooserPanel</code> to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public void addChooserPanel( AbstractColorChooserPanel panel ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        AbstractColorChooserPanel[] oldPanels = getChooserPanels();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        AbstractColorChooserPanel[] newPanels = new AbstractColorChooserPanel[oldPanels.length+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        System.arraycopy(oldPanels, 0, newPanels, 0, oldPanels.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        newPanels[newPanels.length-1] = panel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        setChooserPanels(newPanels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Removes the Color Panel specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param panel   a string that specifies the panel to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @return the color panel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @exception IllegalArgumentException if panel is not in list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *                  known chooser panels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    public AbstractColorChooserPanel removeChooserPanel( AbstractColorChooserPanel panel ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        int containedAt = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        for (int i = 0; i < chooserPanels.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            if (chooserPanels[i] == panel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                containedAt = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (containedAt == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            throw new IllegalArgumentException("chooser panel not in this chooser");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        AbstractColorChooserPanel[] newArray = new AbstractColorChooserPanel[chooserPanels.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (containedAt == chooserPanels.length-1) {  // at end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            System.arraycopy(chooserPanels, 0, newArray, 0, newArray.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        else if (containedAt == 0) {  // at start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            System.arraycopy(chooserPanels, 1, newArray, 0, newArray.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        else {  // in middle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            System.arraycopy(chooserPanels, 0, newArray, 0, containedAt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            System.arraycopy(chooserPanels, containedAt+1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                             newArray, containedAt, (chooserPanels.length - containedAt - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        setChooserPanels(newArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        return panel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * Specifies the Color Panels used to choose a color value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @param panels  an array of <code>AbstractColorChooserPanel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *          objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
   486
    @BeanProperty(hidden = true, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
   487
            = "An array of different chooser types.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    public void setChooserPanels( AbstractColorChooserPanel[] panels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        AbstractColorChooserPanel[] oldValue = chooserPanels;
43322
046c9ccca33d 8003495: [findbugs] javax.swing.* - Storing a reference to an externally mutable object into the internal representation
alexsch
parents: 33253
diff changeset
   490
        chooserPanels = Arrays.copyOf(panels, panels.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        firePropertyChange(CHOOSER_PANELS_PROPERTY, oldValue, panels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * Returns the specified color panels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @return an array of <code>AbstractColorChooserPanel</code> objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    public AbstractColorChooserPanel[] getChooserPanels() {
43322
046c9ccca33d 8003495: [findbugs] javax.swing.* - Storing a reference to an externally mutable object into the internal representation
alexsch
parents: 33253
diff changeset
   500
        return Arrays.copyOf(chooserPanels, chooserPanels.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Returns the data model that handles color selections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @return a <code>ColorSelectionModel</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public ColorSelectionModel getSelectionModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        return selectionModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * Sets the model containing the selected color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @param newModel   the new <code>ColorSelectionModel</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
   518
    @BeanProperty(hidden = true, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
   519
            = "The model which contains the currently selected color.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public void setSelectionModel(ColorSelectionModel newModel ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        ColorSelectionModel oldModel = selectionModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        selectionModel = newModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        firePropertyChange(JColorChooser.SELECTION_MODEL_PROPERTY, oldModel, newModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * See <code>readObject</code> and <code>writeObject</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * <code>JComponent</code> for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * information about serialization in Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * Returns a string representation of this <code>JColorChooser</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * implementations. The returned string may be empty but may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @return  a string representation of this <code>JColorChooser</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    protected String paramString() {
28228
be83f404724d 8067207: Replace concat String to append in StringBuilder parameters (client)
weijun
parents: 25859
diff changeset
   555
        StringBuilder chooserPanelsString = new StringBuilder();
be83f404724d 8067207: Replace concat String to append in StringBuilder parameters (client)
weijun
parents: 25859
diff changeset
   556
        for (AbstractColorChooserPanel panel : chooserPanels) {
be83f404724d 8067207: Replace concat String to append in StringBuilder parameters (client)
weijun
parents: 25859
diff changeset
   557
            chooserPanelsString.append('[').append(panel)
be83f404724d 8067207: Replace concat String to append in StringBuilder parameters (client)
weijun
parents: 25859
diff changeset
   558
                               .append(']');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
28228
be83f404724d 8067207: Replace concat String to append in StringBuilder parameters (client)
weijun
parents: 25859
diff changeset
   560
        String previewPanelString = (previewPanel != null ? previewPanel
be83f404724d 8067207: Replace concat String to append in StringBuilder parameters (client)
weijun
parents: 25859
diff changeset
   561
                .toString() : "");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
28228
be83f404724d 8067207: Replace concat String to append in StringBuilder parameters (client)
weijun
parents: 25859
diff changeset
   563
        return super.paramString() + ",chooserPanels="
be83f404724d 8067207: Replace concat String to append in StringBuilder parameters (client)
weijun
parents: 25859
diff changeset
   564
                + chooserPanelsString.toString() + ",previewPanel="
be83f404724d 8067207: Replace concat String to append in StringBuilder parameters (client)
weijun
parents: 25859
diff changeset
   565
                + previewPanelString;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
30464
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28231
diff changeset
   572
    /**
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28231
diff changeset
   573
     * The accessible context.
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28231
diff changeset
   574
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    protected AccessibleContext accessibleContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Gets the AccessibleContext associated with this JColorChooser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * For color choosers, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * AccessibleJColorChooser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * A new AccessibleJColorChooser instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @return an AccessibleJColorChooser that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *         AccessibleContext of this JColorChooser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32488
diff changeset
   586
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            accessibleContext = new AccessibleJColorChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * <code>JColorChooser</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * Java Accessibility API appropriate to color chooser user-interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    protected class AccessibleJColorChooser extends AccessibleJComponent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         * object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            return AccessibleRole.COLOR_CHOOSER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    } // inner class AccessibleJColorChooser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
 * Class which builds a color chooser dialog consisting of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
 * a JColorChooser with "Ok", "Cancel", and "Reset" buttons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
 * Note: This needs to be fixed to deal with localization!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
 */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22574
diff changeset
   623
@SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
class ColorChooserDialog extends JDialog {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    private Color initialColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    private JColorChooser chooserPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    private JButton cancelButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    public ColorChooserDialog(Dialog owner, String title, boolean modal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        Component c, JColorChooser chooserPane,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        ActionListener okListener, ActionListener cancelListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        super(owner, title, modal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        initColorChooserDialog(c, chooserPane, okListener, cancelListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    public ColorChooserDialog(Frame owner, String title, boolean modal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        Component c, JColorChooser chooserPane,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        ActionListener okListener, ActionListener cancelListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        super(owner, title, modal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        initColorChooserDialog(c, chooserPane, okListener, cancelListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    protected void initColorChooserDialog(Component c, JColorChooser chooserPane,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        ActionListener okListener, ActionListener cancelListener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        //setResizable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        this.chooserPane = chooserPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        Locale locale = getLocale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        String okString = UIManager.getString("ColorChooser.okText", locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        String cancelString = UIManager.getString("ColorChooser.cancelText", locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        String resetString = UIManager.getString("ColorChooser.resetText", locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        Container contentPane = getContentPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        contentPane.setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        contentPane.add(chooserPane, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
         * Create Lower button panel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        JPanel buttonPane = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        JButton okButton = new JButton(okString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        getRootPane().setDefaultButton(okButton);
14658
67661687f722 4631925: JColor Chooser is not fully accessible
jviswana
parents: 7668
diff changeset
   667
        okButton.getAccessibleContext().setAccessibleDescription(okString);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        okButton.setActionCommand("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        okButton.addActionListener(new ActionListener() {
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 28228
diff changeset
   670
            @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        if (okListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            okButton.addActionListener(okListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        buttonPane.add(okButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        cancelButton = new JButton(cancelString);
14658
67661687f722 4631925: JColor Chooser is not fully accessible
jviswana
parents: 7668
diff changeset
   681
        cancelButton.getAccessibleContext().setAccessibleDescription(cancelString);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        // The following few lines are used to register esc to close the dialog
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22574
diff changeset
   684
        @SuppressWarnings("serial") // anonymous class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        Action cancelKeyAction = new AbstractAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                ((AbstractButton)e.getSource()).fireActionPerformed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        KeyStroke cancelKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        InputMap inputMap = cancelButton.getInputMap(JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                                                     WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        ActionMap actionMap = cancelButton.getActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        if (inputMap != null && actionMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            inputMap.put(cancelKeyStroke, "cancel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            actionMap.put("cancel", cancelKeyAction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        // end esc handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        cancelButton.setActionCommand("cancel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        cancelButton.addActionListener(new ActionListener() {
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 28228
diff changeset
   702
            @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        if (cancelListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            cancelButton.addActionListener(cancelListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        buttonPane.add(cancelButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        JButton resetButton = new JButton(resetString);
14658
67661687f722 4631925: JColor Chooser is not fully accessible
jviswana
parents: 7668
diff changeset
   713
        resetButton.getAccessibleContext().setAccessibleDescription(resetString);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        resetButton.addActionListener(new ActionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
           public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
               reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        int mnemonic = SwingUtilities2.getUIDefaultsInt("ColorChooser.resetMnemonic", locale, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        if (mnemonic != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            resetButton.setMnemonic(mnemonic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        buttonPane.add(resetButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        contentPane.add(buttonPane, BorderLayout.SOUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        if (JDialog.isDefaultLookAndFeelDecorated()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            boolean supportsWindowDecorations =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            UIManager.getLookAndFeel().getSupportsWindowDecorations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            if (supportsWindowDecorations) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                getRootPane().setWindowDecorationStyle(JRootPane.COLOR_CHOOSER_DIALOG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        applyComponentOrientation(((c == null) ? getRootPane() : c).getComponentOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        setLocationRelativeTo(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        this.addWindowListener(new Closer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 28228
diff changeset
   741
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    public void show() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        initialColor = chooserPane.getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        super.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        chooserPane.setColor(initialColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22574
diff changeset
   751
    @SuppressWarnings("serial") // JDK-implementation class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    class Closer extends WindowAdapter implements Serializable{
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 28228
diff changeset
   753
        @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        public void windowClosing(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            cancelButton.doClick(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            Window w = e.getWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            w.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22574
diff changeset
   761
    @SuppressWarnings("serial") // JDK-implementation class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    static class DisposeOnClose extends ComponentAdapter implements Serializable{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        public void componentHidden(ComponentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            Window w = (Window)e.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            w.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22574
diff changeset
   771
@SuppressWarnings("serial") // JDK-implementation class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
class ColorTracker implements ActionListener, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    JColorChooser chooser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    Color color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    public ColorTracker(JColorChooser c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        chooser = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        color = chooser.getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    public Color getColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        return color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
}