0N/A/*
2362N/A * Copyright (c) 1998, 2008, 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.colorchooser;
0N/A
0N/Aimport java.awt.*;
832N/Aimport java.beans.PropertyChangeEvent;
832N/Aimport java.beans.PropertyChangeListener;
0N/Aimport javax.swing.*;
0N/A
0N/A/**
0N/A * This is the abstract superclass for color choosers. If you want to add
0N/A * a new color chooser panel into a <code>JColorChooser</code>, subclass
0N/A * this class.
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 * @author Tom Santos
0N/A * @author Steve Wilson
0N/A */
0N/Apublic abstract class AbstractColorChooserPanel extends JPanel {
0N/A
832N/A private final PropertyChangeListener enabledListener = new PropertyChangeListener() {
832N/A public void propertyChange(PropertyChangeEvent event) {
832N/A Object value = event.getNewValue();
832N/A if (value instanceof Boolean) {
832N/A setEnabled((Boolean) value);
832N/A }
832N/A }
832N/A };
832N/A
0N/A /**
0N/A *
0N/A */
0N/A private JColorChooser chooser;
0N/A
0N/A /**
0N/A * Invoked automatically when the model's state changes.
0N/A * It is also called by <code>installChooserPanel</code> to allow
0N/A * you to set up the initial state of your chooser.
0N/A * Override this method to update your <code>ChooserPanel</code>.
0N/A */
0N/A public abstract void updateChooser();
0N/A
0N/A /**
0N/A * Builds a new chooser panel.
0N/A */
0N/A protected abstract void buildChooser();
0N/A
0N/A /**
0N/A * Returns a string containing the display name of the panel.
0N/A * @return the name of the display panel
0N/A */
0N/A public abstract String getDisplayName();
0N/A
0N/A /**
0N/A * Provides a hint to the look and feel as to the
0N/A * <code>KeyEvent.VK</code> constant that can be used as a mnemonic to
0N/A * access the panel. A return value <= 0 indicates there is no mnemonic.
0N/A * <p>
0N/A * The return value here is a hint, it is ultimately up to the look
0N/A * and feel to honor the return value in some meaningful way.
0N/A * <p>
0N/A * This implementation returns 0, indicating the
0N/A * <code>AbstractColorChooserPanel</code> does not support a mnemonic,
0N/A * subclasses wishing a mnemonic will need to override this.
0N/A *
0N/A * @return KeyEvent.VK constant identifying the mnemonic; <= 0 for no
0N/A * mnemonic
0N/A * @see #getDisplayedMnemonicIndex
0N/A * @since 1.4
0N/A */
0N/A public int getMnemonic() {
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Provides a hint to the look and feel as to the index of the character in
0N/A * <code>getDisplayName</code> that should be visually identified as the
0N/A * mnemonic. The look and feel should only use this if
0N/A * <code>getMnemonic</code> returns a value > 0.
0N/A * <p>
0N/A * The return value here is a hint, it is ultimately up to the look
0N/A * and feel to honor the return value in some meaningful way. For example,
0N/A * a look and feel may wish to render each
0N/A * <code>AbstractColorChooserPanel</code> in a <code>JTabbedPane</code>,
0N/A * and further use this return value to underline a character in
0N/A * the <code>getDisplayName</code>.
0N/A * <p>
0N/A * This implementation returns -1, indicating the
0N/A * <code>AbstractColorChooserPanel</code> does not support a mnemonic,
0N/A * subclasses wishing a mnemonic will need to override this.
0N/A *
0N/A * @return Character index to render mnemonic for; -1 to provide no
0N/A * visual identifier for this panel.
0N/A * @see #getMnemonic
0N/A * @since 1.4
0N/A */
0N/A public int getDisplayedMnemonicIndex() {
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Returns the small display icon for the panel.
0N/A * @return the small display icon
0N/A */
0N/A public abstract Icon getSmallDisplayIcon();
0N/A
0N/A /**
0N/A * Returns the large display icon for the panel.
0N/A * @return the large display icon
0N/A */
0N/A public abstract Icon getLargeDisplayIcon();
0N/A
0N/A /**
0N/A * Invoked when the panel is added to the chooser.
0N/A * If you override this, be sure to call <code>super</code>.
0N/A * @param enclosingChooser the panel to be added
0N/A * @exception RuntimeException if the chooser panel has already been
0N/A * installed
0N/A */
0N/A public void installChooserPanel(JColorChooser enclosingChooser) {
0N/A if (chooser != null) {
0N/A throw new RuntimeException ("This chooser panel is already installed");
0N/A }
0N/A chooser = enclosingChooser;
832N/A chooser.addPropertyChangeListener("enabled", enabledListener);
832N/A setEnabled(chooser.isEnabled());
0N/A buildChooser();
0N/A updateChooser();
0N/A }
0N/A
0N/A /**
0N/A * Invoked when the panel is removed from the chooser.
0N/A * If override this, be sure to call <code>super</code>.
0N/A */
0N/A public void uninstallChooserPanel(JColorChooser enclosingChooser) {
832N/A chooser.removePropertyChangeListener("enabled", enabledListener);
0N/A chooser = null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the model that the chooser panel is editing.
0N/A * @return the <code>ColorSelectionModel</code> model this panel
0N/A * is editing
0N/A */
0N/A public ColorSelectionModel getColorSelectionModel() {
612N/A return (this.chooser != null)
612N/A ? this.chooser.getSelectionModel()
612N/A : null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the color that is currently selected.
0N/A * @return the <code>Color</code> that is selected
0N/A */
0N/A protected Color getColorFromModel() {
612N/A ColorSelectionModel model = getColorSelectionModel();
612N/A return (model != null)
612N/A ? model.getSelectedColor()
612N/A : null;
612N/A }
612N/A
612N/A void setSelectedColor(Color color) {
612N/A ColorSelectionModel model = getColorSelectionModel();
612N/A if (model != null) {
612N/A model.setSelectedColor(color);
612N/A }
0N/A }
0N/A
0N/A /**
0N/A * Draws the panel.
0N/A * @param g the <code>Graphics</code> object
0N/A */
0N/A public void paint(Graphics g) {
0N/A super.paint(g);
0N/A }
0N/A
0N/A /**
0N/A * Returns an integer from the defaults table. If <code>key</code> does
0N/A * not map to a valid <code>Integer</code>, <code>default</code> is
0N/A * returned.
0N/A *
0N/A * @param key an <code>Object</code> specifying the int
0N/A * @param defaultValue Returned value if <code>key</code> is not available,
0N/A * or is not an Integer
0N/A * @return the int
0N/A */
0N/A int getInt(Object key, int defaultValue) {
0N/A Object value = UIManager.get(key, getLocale());
0N/A
0N/A if (value instanceof Integer) {
0N/A return ((Integer)value).intValue();
0N/A }
0N/A if (value instanceof String) {
0N/A try {
0N/A return Integer.parseInt((String)value);
0N/A } catch (NumberFormatException nfe) {}
0N/A }
0N/A return defaultValue;
0N/A }
0N/A}