0N/A/*
3261N/A * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/Aimport javax.swing.colorchooser.*;
0N/Aimport javax.swing.plaf.ColorChooserUI;
0N/Aimport javax.accessibility.*;
0N/A
0N/Aimport sun.swing.SwingUtilities2;
0N/A
0N/A
0N/A/**
0N/A * <code>JColorChooser</code> provides a pane of controls designed to allow
0N/A * a user to manipulate and select a color.
0N/A * For information about using color choosers, see
0N/A * <a
0N/A href="http://java.sun.com/docs/books/tutorial/uiswing/components/colorchooser.html">How to Use Color Choosers</a>,
0N/A * a section in <em>The Java Tutorial</em>.
0N/A *
0N/A * <p>
0N/A *
0N/A * This class provides three levels of API:
0N/A * <ol>
0N/A * <li>A static convenience method which shows a modal color-chooser
0N/A * dialog and returns the color selected by the user.
0N/A * <li>A static convenience method for creating a color-chooser dialog
0N/A * where <code>ActionListeners</code> can be specified to be invoked when
0N/A * the user presses one of the dialog buttons.
0N/A * <li>The ability to create instances of <code>JColorChooser</code> panes
0N/A * directly (within any container). <code>PropertyChange</code> listeners
0N/A * can be added to detect when the current "color" property changes.
0N/A * </ol>
0N/A * <p>
0N/A * <strong>Warning:</strong> Swing is not thread safe. For more
0N/A * information see <a
0N/A * href="package-summary.html#threading">Swing's Threading
0N/A * Policy</a>.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A *
0N/A * @beaninfo
0N/A * attribute: isContainer false
0N/A * description: A component that supports selecting a Color.
0N/A *
0N/A *
0N/A * @author James Gosling
0N/A * @author Amy Fowler
0N/A * @author Steve Wilson
0N/A */
0N/Apublic class JColorChooser extends JComponent implements Accessible {
0N/A
0N/A /**
0N/A * @see #getUIClassID
0N/A * @see #readObject
0N/A */
0N/A private static final String uiClassID = "ColorChooserUI";
0N/A
0N/A private ColorSelectionModel selectionModel;
0N/A
2723N/A private JComponent previewPanel = ColorChooserComponentFactory.getPreviewPanel();
0N/A
0N/A private AbstractColorChooserPanel[] chooserPanels = new AbstractColorChooserPanel[0];
0N/A
0N/A private boolean dragEnabled;
0N/A
0N/A /**
0N/A * The selection model property name.
0N/A */
0N/A public static final String SELECTION_MODEL_PROPERTY = "selectionModel";
0N/A
0N/A /**
0N/A * The preview panel property name.
0N/A */
0N/A public static final String PREVIEW_PANEL_PROPERTY = "previewPanel";
0N/A
0N/A /**
0N/A * The chooserPanel array property name.
0N/A */
0N/A public static final String CHOOSER_PANELS_PROPERTY = "chooserPanels";
0N/A
0N/A
0N/A /**
0N/A * Shows a modal color-chooser dialog and blocks until the
0N/A * dialog is hidden. If the user presses the "OK" button, then
0N/A * this method hides/disposes the dialog and returns the selected color.
0N/A * If the user presses the "Cancel" button or closes the dialog without
0N/A * pressing "OK", then this method hides/disposes the dialog and returns
0N/A * <code>null</code>.
0N/A *
0N/A * @param component the parent <code>Component</code> for the dialog
0N/A * @param title the String containing the dialog's title
0N/A * @param initialColor the initial Color set when the color-chooser is shown
0N/A * @return the selected color or <code>null</code> if the user opted out
0N/A * @exception HeadlessException if GraphicsEnvironment.isHeadless()
0N/A * returns true.
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A */
0N/A public static Color showDialog(Component component,
0N/A String title, Color initialColor) throws HeadlessException {
0N/A
0N/A final JColorChooser pane = new JColorChooser(initialColor != null?
0N/A initialColor : Color.white);
0N/A
0N/A ColorTracker ok = new ColorTracker(pane);
0N/A JDialog dialog = createDialog(component, title, true, pane, ok, null);
0N/A
0N/A dialog.addComponentListener(new ColorChooserDialog.DisposeOnClose());
0N/A
0N/A dialog.show(); // blocks until user brings dialog down...
0N/A
0N/A return ok.getColor();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Creates and returns a new dialog containing the specified
0N/A * <code>ColorChooser</code> pane along with "OK", "Cancel", and "Reset"
0N/A * buttons. If the "OK" or "Cancel" buttons are pressed, the dialog is
0N/A * automatically hidden (but not disposed). If the "Reset"
0N/A * button is pressed, the color-chooser's color will be reset to the
0N/A * color which was set the last time <code>show</code> was invoked on the
0N/A * dialog and the dialog will remain showing.
0N/A *
0N/A * @param c the parent component for the dialog
0N/A * @param title the title for the dialog
0N/A * @param modal a boolean. When true, the remainder of the program
0N/A * is inactive until the dialog is closed.
0N/A * @param chooserPane the color-chooser to be placed inside the dialog
0N/A * @param okListener the ActionListener invoked when "OK" is pressed
0N/A * @param cancelListener the ActionListener invoked when "Cancel" is pressed
0N/A * @return a new dialog containing the color-chooser pane
0N/A * @exception HeadlessException if GraphicsEnvironment.isHeadless()
0N/A * returns true.
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A */
0N/A public static JDialog createDialog(Component c, String title, boolean modal,
0N/A JColorChooser chooserPane, ActionListener okListener,
0N/A ActionListener cancelListener) throws HeadlessException {
0N/A
0N/A Window window = JOptionPane.getWindowForComponent(c);
0N/A ColorChooserDialog dialog;
0N/A if (window instanceof Frame) {
0N/A dialog = new ColorChooserDialog((Frame)window, title, modal, c, chooserPane,
0N/A okListener, cancelListener);
0N/A } else {
0N/A dialog = new ColorChooserDialog((Dialog)window, title, modal, c, chooserPane,
0N/A okListener, cancelListener);
0N/A }
5553N/A dialog.getAccessibleContext().setAccessibleDescription(title);
0N/A return dialog;
0N/A }
0N/A
0N/A /**
0N/A * Creates a color chooser pane with an initial color of white.
0N/A */
0N/A public JColorChooser() {
0N/A this(Color.white);
0N/A }
0N/A
0N/A /**
0N/A * Creates a color chooser pane with the specified initial color.
0N/A *
0N/A * @param initialColor the initial color set in the chooser
0N/A */
0N/A public JColorChooser(Color initialColor) {
0N/A this( new DefaultColorSelectionModel(initialColor) );
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Creates a color chooser pane with the specified
0N/A * <code>ColorSelectionModel</code>.
0N/A *
0N/A * @param model the <code>ColorSelectionModel</code> to be used
0N/A */
0N/A public JColorChooser(ColorSelectionModel model) {
0N/A selectionModel = model;
0N/A updateUI();
0N/A dragEnabled = false;
0N/A }
0N/A
0N/A /**
0N/A * Returns the L&F object that renders this component.
0N/A *
0N/A * @return the <code>ColorChooserUI</code> object that renders
0N/A * this component
0N/A */
0N/A public ColorChooserUI getUI() {
0N/A return (ColorChooserUI)ui;
0N/A }
0N/A
0N/A /**
0N/A * Sets the L&F object that renders this component.
0N/A *
0N/A * @param ui the <code>ColorChooserUI</code> L&F object
0N/A * @see UIDefaults#getUI
0N/A *
0N/A * @beaninfo
0N/A * bound: true
0N/A * hidden: true
0N/A * description: The UI object that implements the color chooser's LookAndFeel.
0N/A */
0N/A public void setUI(ColorChooserUI ui) {
0N/A super.setUI(ui);
0N/A }
0N/A
0N/A /**
0N/A * Notification from the <code>UIManager</code> that the L&F has changed.
0N/A * Replaces the current UI object with the latest version from the
0N/A * <code>UIManager</code>.
0N/A *
0N/A * @see JComponent#updateUI
0N/A */
0N/A public void updateUI() {
0N/A setUI((ColorChooserUI)UIManager.getUI(this));
0N/A }
0N/A
0N/A /**
0N/A * Returns the name of the L&F class that renders this component.
0N/A *
0N/A * @return the string "ColorChooserUI"
0N/A * @see JComponent#getUIClassID
0N/A * @see UIDefaults#getUI
0N/A */
0N/A public String getUIClassID() {
0N/A return uiClassID;
0N/A }
0N/A
0N/A /**
0N/A * Gets the current color value from the color chooser.
0N/A * By default, this delegates to the model.
0N/A *
0N/A * @return the current color value of the color chooser
0N/A */
0N/A public Color getColor() {
0N/A return selectionModel.getSelectedColor();
0N/A }
0N/A
0N/A /**
0N/A * Sets the current color of the color chooser to the specified color.
0N/A * The <code>ColorSelectionModel</code> will fire a <code>ChangeEvent</code>
0N/A * @param color the color to be set in the color chooser
0N/A * @see JComponent#addPropertyChangeListener
0N/A *
0N/A * @beaninfo
0N/A * bound: false
0N/A * hidden: false
0N/A * description: The current color the chooser is to display.
0N/A */
0N/A public void setColor(Color color) {
0N/A selectionModel.setSelectedColor(color);
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Sets the current color of the color chooser to the
0N/A * specified RGB color. Note that the values of red, green,
0N/A * and blue should be between the numbers 0 and 255, inclusive.
0N/A *
0N/A * @param r an int specifying the amount of Red
0N/A * @param g an int specifying the amount of Green
0N/A * @param b an int specifying the amount of Blue
0N/A * @exception IllegalArgumentException if r,g,b values are out of range
0N/A * @see java.awt.Color
0N/A */
0N/A public void setColor(int r, int g, int b) {
0N/A setColor(new Color(r,g,b));
0N/A }
0N/A
0N/A /**
0N/A * Sets the current color of the color chooser to the
0N/A * specified color.
0N/A *
0N/A * @param c an integer value that sets the current color in the chooser
0N/A * where the low-order 8 bits specify the Blue value,
0N/A * the next 8 bits specify the Green value, and the 8 bits
0N/A * above that specify the Red value.
0N/A */
0N/A public void setColor(int c) {
0N/A setColor((c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF);
0N/A }
0N/A
0N/A /**
0N/A * Sets the <code>dragEnabled</code> property,
0N/A * which must be <code>true</code> to enable
0N/A * automatic drag handling (the first part of drag and drop)
0N/A * on this component.
0N/A * The <code>transferHandler</code> property needs to be set
0N/A * to a non-<code>null</code> value for the drag to do
0N/A * anything. The default value of the <code>dragEnabled</code>
0N/A * property
0N/A * is <code>false</code>.
0N/A *
0N/A * <p>
0N/A *
0N/A * When automatic drag handling is enabled,
0N/A * most look and feels begin a drag-and-drop operation
0N/A * when the user presses the mouse button over the preview panel.
0N/A * Some look and feels might not support automatic drag and drop;
0N/A * they will ignore this property. You can work around such
0N/A * look and feels by modifying the component
0N/A * to directly call the <code>exportAsDrag</code> method of a
0N/A * <code>TransferHandler</code>.
0N/A *
0N/A * @param b the value to set the <code>dragEnabled</code> property to
0N/A * @exception HeadlessException if
0N/A * <code>b</code> is <code>true</code> and
0N/A * <code>GraphicsEnvironment.isHeadless()</code>
0N/A * returns <code>true</code>
0N/A *
0N/A * @since 1.4
0N/A *
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A * @see #getDragEnabled
0N/A * @see #setTransferHandler
0N/A * @see TransferHandler
0N/A *
0N/A * @beaninfo
0N/A * description: Determines whether automatic drag handling is enabled.
0N/A * bound: false
0N/A */
0N/A public void setDragEnabled(boolean b) {
0N/A if (b && GraphicsEnvironment.isHeadless()) {
0N/A throw new HeadlessException();
0N/A }
0N/A dragEnabled = b;
0N/A }
0N/A
0N/A /**
0N/A * Gets the value of the <code>dragEnabled</code> property.
0N/A *
0N/A * @return the value of the <code>dragEnabled</code> property
0N/A * @see #setDragEnabled
0N/A * @since 1.4
0N/A */
0N/A public boolean getDragEnabled() {
0N/A return dragEnabled;
0N/A }
0N/A
0N/A /**
0N/A * Sets the current preview panel.
0N/A * This will fire a <code>PropertyChangeEvent</code> for the property
0N/A * named "previewPanel".
0N/A *
0N/A * @param preview the <code>JComponent</code> which displays the current color
0N/A * @see JComponent#addPropertyChangeListener
0N/A *
0N/A * @beaninfo
0N/A * bound: true
0N/A * hidden: true
0N/A * description: The UI component which displays the current color.
0N/A */
0N/A public void setPreviewPanel(JComponent preview) {
0N/A
0N/A if (previewPanel != preview) {
0N/A JComponent oldPreview = previewPanel;
0N/A previewPanel = preview;
0N/A firePropertyChange(JColorChooser.PREVIEW_PANEL_PROPERTY, oldPreview, preview);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the preview panel that shows a chosen color.
0N/A *
0N/A * @return a <code>JComponent</code> object -- the preview panel
0N/A */
0N/A public JComponent getPreviewPanel() {
0N/A return previewPanel;
0N/A }
0N/A
0N/A /**
0N/A * Adds a color chooser panel to the color chooser.
0N/A *
0N/A * @param panel the <code>AbstractColorChooserPanel</code> to be added
0N/A */
0N/A public void addChooserPanel( AbstractColorChooserPanel panel ) {
0N/A AbstractColorChooserPanel[] oldPanels = getChooserPanels();
0N/A AbstractColorChooserPanel[] newPanels = new AbstractColorChooserPanel[oldPanels.length+1];
0N/A System.arraycopy(oldPanels, 0, newPanels, 0, oldPanels.length);
0N/A newPanels[newPanels.length-1] = panel;
0N/A setChooserPanels(newPanels);
0N/A }
0N/A
0N/A /**
0N/A * Removes the Color Panel specified.
0N/A *
0N/A * @param panel a string that specifies the panel to be removed
0N/A * @return the color panel
0N/A * @exception IllegalArgumentException if panel is not in list of
0N/A * known chooser panels
0N/A */
0N/A public AbstractColorChooserPanel removeChooserPanel( AbstractColorChooserPanel panel ) {
0N/A
0N/A
0N/A int containedAt = -1;
0N/A
0N/A for (int i = 0; i < chooserPanels.length; i++) {
0N/A if (chooserPanels[i] == panel) {
0N/A containedAt = i;
0N/A break;
0N/A }
0N/A }
0N/A if (containedAt == -1) {
0N/A throw new IllegalArgumentException("chooser panel not in this chooser");
0N/A }
0N/A
0N/A AbstractColorChooserPanel[] newArray = new AbstractColorChooserPanel[chooserPanels.length-1];
0N/A
0N/A if (containedAt == chooserPanels.length-1) { // at end
0N/A System.arraycopy(chooserPanels, 0, newArray, 0, newArray.length);
0N/A }
0N/A else if (containedAt == 0) { // at start
0N/A System.arraycopy(chooserPanels, 1, newArray, 0, newArray.length);
0N/A }
0N/A else { // in middle
0N/A System.arraycopy(chooserPanels, 0, newArray, 0, containedAt);
0N/A System.arraycopy(chooserPanels, containedAt+1,
0N/A newArray, containedAt, (chooserPanels.length - containedAt - 1));
0N/A }
0N/A
0N/A setChooserPanels(newArray);
0N/A
0N/A return panel;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Specifies the Color Panels used to choose a color value.
0N/A *
0N/A * @param panels an array of <code>AbstractColorChooserPanel</code>
0N/A * objects
0N/A *
0N/A * @beaninfo
0N/A * bound: true
0N/A * hidden: true
0N/A * description: An array of different chooser types.
0N/A */
0N/A public void setChooserPanels( AbstractColorChooserPanel[] panels) {
0N/A AbstractColorChooserPanel[] oldValue = chooserPanels;
0N/A chooserPanels = panels;
0N/A firePropertyChange(CHOOSER_PANELS_PROPERTY, oldValue, panels);
0N/A }
0N/A
0N/A /**
0N/A * Returns the specified color panels.
0N/A *
0N/A * @return an array of <code>AbstractColorChooserPanel</code> objects
0N/A */
0N/A public AbstractColorChooserPanel[] getChooserPanels() {
0N/A return chooserPanels;
0N/A }
0N/A
0N/A /**
0N/A * Returns the data model that handles color selections.
0N/A *
0N/A * @return a <code>ColorSelectionModel</code> object
0N/A */
0N/A public ColorSelectionModel getSelectionModel() {
0N/A return selectionModel;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets the model containing the selected color.
0N/A *
0N/A * @param newModel the new <code>ColorSelectionModel</code> object
0N/A *
0N/A * @beaninfo
0N/A * bound: true
0N/A * hidden: true
0N/A * description: The model which contains the currently selected color.
0N/A */
0N/A public void setSelectionModel(ColorSelectionModel newModel ) {
0N/A ColorSelectionModel oldModel = selectionModel;
0N/A selectionModel = newModel;
0N/A firePropertyChange(JColorChooser.SELECTION_MODEL_PROPERTY, oldModel, newModel);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * See <code>readObject</code> and <code>writeObject</code> in
0N/A * <code>JComponent</code> for more
0N/A * information about serialization in Swing.
0N/A */
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
0N/A s.defaultWriteObject();
0N/A if (getUIClassID().equals(uiClassID)) {
0N/A byte count = JComponent.getWriteObjCounter(this);
0N/A JComponent.setWriteObjCounter(this, --count);
0N/A if (count == 0 && ui != null) {
0N/A ui.installUI(this);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a string representation of this <code>JColorChooser</code>.
0N/A * This method
0N/A * is intended to be used only for debugging purposes, and the
0N/A * content and format of the returned string may vary between
0N/A * implementations. The returned string may be empty but may not
0N/A * be <code>null</code>.
0N/A *
0N/A * @return a string representation of this <code>JColorChooser</code>
0N/A */
0N/A protected String paramString() {
0N/A StringBuffer chooserPanelsString = new StringBuffer("");
0N/A for (int i=0; i<chooserPanels.length; i++) {
0N/A chooserPanelsString.append("[" + chooserPanels[i].toString()
0N/A + "]");
0N/A }
0N/A String previewPanelString = (previewPanel != null ?
0N/A previewPanel.toString() : "");
0N/A
0N/A return super.paramString() +
0N/A ",chooserPanels=" + chooserPanelsString.toString() +
0N/A ",previewPanel=" + previewPanelString;
0N/A }
0N/A
0N/A/////////////////
0N/A// Accessibility support
0N/A////////////////
0N/A
0N/A protected AccessibleContext accessibleContext = null;
0N/A
0N/A /**
0N/A * Gets the AccessibleContext associated with this JColorChooser.
0N/A * For color choosers, the AccessibleContext takes the form of an
0N/A * AccessibleJColorChooser.
0N/A * A new AccessibleJColorChooser instance is created if necessary.
0N/A *
0N/A * @return an AccessibleJColorChooser that serves as the
0N/A * AccessibleContext of this JColorChooser
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (accessibleContext == null) {
0N/A accessibleContext = new AccessibleJColorChooser();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A /**
0N/A * This class implements accessibility support for the
0N/A * <code>JColorChooser</code> class. It provides an implementation of the
0N/A * Java Accessibility API appropriate to color chooser user-interface
0N/A * elements.
0N/A */
0N/A protected class AccessibleJColorChooser extends AccessibleJComponent {
0N/A
0N/A /**
0N/A * Get the role of this object.
0N/A *
0N/A * @return an instance of AccessibleRole describing the role of the
0N/A * object
0N/A * @see AccessibleRole
0N/A */
0N/A public AccessibleRole getAccessibleRole() {
0N/A return AccessibleRole.COLOR_CHOOSER;
0N/A }
0N/A
0N/A } // inner class AccessibleJColorChooser
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Class which builds a color chooser dialog consisting of
0N/A * a JColorChooser with "Ok", "Cancel", and "Reset" buttons.
0N/A *
0N/A * Note: This needs to be fixed to deal with localization!
0N/A */
0N/Aclass ColorChooserDialog extends JDialog {
0N/A private Color initialColor;
0N/A private JColorChooser chooserPane;
0N/A private JButton cancelButton;
0N/A
0N/A public ColorChooserDialog(Dialog owner, String title, boolean modal,
0N/A Component c, JColorChooser chooserPane,
0N/A ActionListener okListener, ActionListener cancelListener)
0N/A throws HeadlessException {
0N/A super(owner, title, modal);
0N/A initColorChooserDialog(c, chooserPane, okListener, cancelListener);
0N/A }
0N/A
0N/A public ColorChooserDialog(Frame owner, String title, boolean modal,
0N/A Component c, JColorChooser chooserPane,
0N/A ActionListener okListener, ActionListener cancelListener)
0N/A throws HeadlessException {
0N/A super(owner, title, modal);
0N/A initColorChooserDialog(c, chooserPane, okListener, cancelListener);
0N/A }
0N/A
0N/A protected void initColorChooserDialog(Component c, JColorChooser chooserPane,
0N/A ActionListener okListener, ActionListener cancelListener) {
0N/A //setResizable(false);
0N/A
0N/A this.chooserPane = chooserPane;
0N/A
0N/A Locale locale = getLocale();
0N/A String okString = UIManager.getString("ColorChooser.okText", locale);
0N/A String cancelString = UIManager.getString("ColorChooser.cancelText", locale);
0N/A String resetString = UIManager.getString("ColorChooser.resetText", locale);
0N/A
0N/A Container contentPane = getContentPane();
0N/A contentPane.setLayout(new BorderLayout());
0N/A contentPane.add(chooserPane, BorderLayout.CENTER);
0N/A
0N/A /*
0N/A * Create Lower button panel
0N/A */
0N/A JPanel buttonPane = new JPanel();
0N/A buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
0N/A JButton okButton = new JButton(okString);
0N/A getRootPane().setDefaultButton(okButton);
5553N/A okButton.getAccessibleContext().setAccessibleDescription(okString);
0N/A okButton.setActionCommand("OK");
0N/A okButton.addActionListener(new ActionListener() {
0N/A public void actionPerformed(ActionEvent e) {
0N/A hide();
0N/A }
0N/A });
0N/A if (okListener != null) {
0N/A okButton.addActionListener(okListener);
0N/A }
0N/A buttonPane.add(okButton);
0N/A
0N/A cancelButton = new JButton(cancelString);
5553N/A cancelButton.getAccessibleContext().setAccessibleDescription(cancelString);
0N/A
0N/A // The following few lines are used to register esc to close the dialog
0N/A Action cancelKeyAction = new AbstractAction() {
0N/A public void actionPerformed(ActionEvent e) {
0N/A ((AbstractButton)e.getSource()).fireActionPerformed(e);
0N/A }
0N/A };
0N/A KeyStroke cancelKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
0N/A InputMap inputMap = cancelButton.getInputMap(JComponent.
0N/A WHEN_IN_FOCUSED_WINDOW);
0N/A ActionMap actionMap = cancelButton.getActionMap();
0N/A if (inputMap != null && actionMap != null) {
0N/A inputMap.put(cancelKeyStroke, "cancel");
0N/A actionMap.put("cancel", cancelKeyAction);
0N/A }
0N/A // end esc handling
0N/A
0N/A cancelButton.setActionCommand("cancel");
0N/A cancelButton.addActionListener(new ActionListener() {
0N/A public void actionPerformed(ActionEvent e) {
0N/A hide();
0N/A }
0N/A });
0N/A if (cancelListener != null) {
0N/A cancelButton.addActionListener(cancelListener);
0N/A }
0N/A buttonPane.add(cancelButton);
0N/A
0N/A JButton resetButton = new JButton(resetString);
5553N/A resetButton.getAccessibleContext().setAccessibleDescription(resetString);
0N/A resetButton.addActionListener(new ActionListener() {
0N/A public void actionPerformed(ActionEvent e) {
0N/A reset();
0N/A }
0N/A });
0N/A int mnemonic = SwingUtilities2.getUIDefaultsInt("ColorChooser.resetMnemonic", locale, -1);
0N/A if (mnemonic != -1) {
0N/A resetButton.setMnemonic(mnemonic);
0N/A }
0N/A buttonPane.add(resetButton);
0N/A contentPane.add(buttonPane, BorderLayout.SOUTH);
0N/A
0N/A if (JDialog.isDefaultLookAndFeelDecorated()) {
0N/A boolean supportsWindowDecorations =
0N/A UIManager.getLookAndFeel().getSupportsWindowDecorations();
0N/A if (supportsWindowDecorations) {
0N/A getRootPane().setWindowDecorationStyle(JRootPane.COLOR_CHOOSER_DIALOG);
0N/A }
0N/A }
0N/A applyComponentOrientation(((c == null) ? getRootPane() : c).getComponentOrientation());
0N/A
0N/A pack();
0N/A setLocationRelativeTo(c);
0N/A
0N/A this.addWindowListener(new Closer());
0N/A }
0N/A
0N/A public void show() {
0N/A initialColor = chooserPane.getColor();
0N/A super.show();
0N/A }
0N/A
0N/A public void reset() {
0N/A chooserPane.setColor(initialColor);
0N/A }
0N/A
0N/A class Closer extends WindowAdapter implements Serializable{
0N/A public void windowClosing(WindowEvent e) {
0N/A cancelButton.doClick(0);
0N/A Window w = e.getWindow();
0N/A w.hide();
0N/A }
0N/A }
0N/A
0N/A static class DisposeOnClose extends ComponentAdapter implements Serializable{
0N/A public void componentHidden(ComponentEvent e) {
0N/A Window w = (Window)e.getComponent();
0N/A w.dispose();
0N/A }
0N/A }
0N/A
0N/A}
0N/A
0N/Aclass ColorTracker implements ActionListener, Serializable {
0N/A JColorChooser chooser;
0N/A Color color;
0N/A
0N/A public ColorTracker(JColorChooser c) {
0N/A chooser = c;
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A color = chooser.getColor();
0N/A }
0N/A
0N/A public Color getColor() {
0N/A return color;
0N/A }
0N/A}