jdk/src/share/classes/javax/swing/colorchooser/AbstractColorChooserPanel.java
author malenkov
Wed, 07 May 2008 21:54:03 +0400
changeset 465 507ce3a1af17
parent 2 90ce3da70b43
child 1288 27aaa860d855
permissions -rw-r--r--
6348456: BasicColorChooserUI ignores JColorChooser selection model changes Summary: Some methods are moved from AbstractColorChooserPanel to BasicColorChooserUI Reviewed-by: peterz, alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
465
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
     2
 * Copyright 1998-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.colorchooser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This is the abstract superclass for color choosers.  If you want to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * a new color chooser panel into a <code>JColorChooser</code>, subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Tom Santos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Steve Wilson
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public abstract class AbstractColorChooserPanel extends JPanel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private JColorChooser chooser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
      * Invoked automatically when the model's state changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
      * It is also called by <code>installChooserPanel</code> to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
      * you to set up the initial state of your chooser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
      * Override this method to update your <code>ChooserPanel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public abstract void updateChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Builds a new chooser panel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected abstract void buildChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Returns a string containing the display name of the panel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @return the name of the display panel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public abstract String getDisplayName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Provides a hint to the look and feel as to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * <code>KeyEvent.VK</code> constant that can be used as a mnemonic to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * access the panel. A return value <= 0 indicates there is no mnemonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * The return value here is a hint, it is ultimately up to the look
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * and feel to honor the return value in some meaningful way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * This implementation returns 0, indicating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * <code>AbstractColorChooserPanel</code> does not support a mnemonic,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * subclasses wishing a mnemonic will need to override this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @return KeyEvent.VK constant identifying the mnemonic; <= 0 for no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *         mnemonic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @see #getDisplayedMnemonicIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public int getMnemonic() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Provides a hint to the look and feel as to the index of the character in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <code>getDisplayName</code> that should be visually identified as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * mnemonic. The look and feel should only use this if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <code>getMnemonic</code> returns a value > 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * The return value here is a hint, it is ultimately up to the look
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * and feel to honor the return value in some meaningful way. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * a look and feel may wish to render each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <code>AbstractColorChooserPanel</code> in a <code>JTabbedPane</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * and further use this return value to underline a character in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * the <code>getDisplayName</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * This implementation returns -1, indicating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <code>AbstractColorChooserPanel</code> does not support a mnemonic,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * subclasses wishing a mnemonic will need to override this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @return Character index to render mnemonic for; -1 to provide no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *                   visual identifier for this panel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @see #getMnemonic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public int getDisplayedMnemonicIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Returns the small display icon for the panel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @return the small display icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public abstract Icon getSmallDisplayIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Returns the large display icon for the panel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @return the large display icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public abstract Icon getLargeDisplayIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Invoked when the panel is added to the chooser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * If you override this, be sure to call <code>super</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param enclosingChooser  the panel to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @exception RuntimeException  if the chooser panel has already been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *                          installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public void installChooserPanel(JColorChooser enclosingChooser) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (chooser != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new RuntimeException ("This chooser panel is already installed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        chooser = enclosingChooser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        buildChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        updateChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Invoked when the panel is removed from the chooser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * If override this, be sure to call <code>super</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
  public void uninstallChooserPanel(JColorChooser enclosingChooser) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        chooser = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
      * Returns the model that the chooser panel is editing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
      * @return the <code>ColorSelectionModel</code> model this panel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
      *         is editing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public ColorSelectionModel getColorSelectionModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return chooser.getSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Returns the color that is currently selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @return the <code>Color</code> that is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    protected Color getColorFromModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return getColorSelectionModel().getSelectedColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Draws the panel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param g  the <code>Graphics</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        super.paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Returns an integer from the defaults table. If <code>key</code> does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * not map to a valid <code>Integer</code>, <code>default</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param key  an <code>Object</code> specifying the int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param defaultValue Returned value if <code>key</code> is not available,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *                     or is not an Integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return the int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    int getInt(Object key, int defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        Object value = UIManager.get(key, getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (value instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            return ((Integer)value).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (value instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                return Integer.parseInt((String)value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            } catch (NumberFormatException nfe) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
}