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

Save This Page
Home » openjdk-7 » javax » swing » [javadoc | source]