gtkcolorchooserpanel.java

来自「JAVA 所有包」· Java 代码 · 共 1,284 行 · 第 1/3 页

JAVA
1,284
字号
/* * @(#)GTKColorChooserPanel.java	1.10 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.java.swing.plaf.gtk;import java.awt.*;import java.awt.event.*;import java.awt.image.*;import javax.swing.*;import javax.swing.colorchooser.*;import javax.swing.event.*;import javax.swing.plaf.*;/** * A color chooser panel mimicking that of GTK's: a color wheel showing * hue and a triangle that varies saturation and brightness. * * @version 1.10, 11/17/05 * @author Scott Violet */class GTKColorChooserPanel extends AbstractColorChooserPanel implements              ChangeListener {    private static final float PI_3 = (float)(Math.PI / 3);    private ColorTriangle triangle;    private JLabel lastLabel;    private JLabel label;    private JSpinner hueSpinner;    private JSpinner saturationSpinner;    private JSpinner valueSpinner;    private JSpinner redSpinner;    private JSpinner greenSpinner;    private JSpinner blueSpinner;    private JTextField colorNameTF;    private boolean settingColor;    // The colors are mirrored to avoid creep in adjusting an individual    // value.    private float hue;    private float saturation;    private float brightness;    /**     * Convenience method to transfer focus to the next child of component.     */    // PENDING: remove this when a variant of this is added to awt.    static void compositeRequestFocus(Component component, boolean direction) { 	if (component instanceof Container) { 	    Container container = (Container)component; 	    if (container.isFocusCycleRoot()) { 		FocusTraversalPolicy policy = container.                                              getFocusTraversalPolicy(); 		Component comp = policy.getDefaultComponent(container); 		if (comp!=null) { 		    comp.requestFocus(); 		    return; 		} 	    } 	    Container rootAncestor = container.getFocusCycleRootAncestor(); 	    if (rootAncestor!=null) { 		FocusTraversalPolicy policy = rootAncestor.                                                  getFocusTraversalPolicy(); 		Component comp;                if (direction) {                    comp = policy.getComponentAfter(rootAncestor, container);                }                else {                    comp = policy.getComponentBefore(rootAncestor, container);                }                if (comp != null) { 		    comp.requestFocus(); 		    return; 		} 	    } 	} 	component.requestFocus();    }    /**     * Returns a user presentable description of this GTKColorChooserPane.     */    public String getDisplayName() {        return (String)UIManager.get("GTKColorChooserPanel.nameText");    }    /**     * Returns the mnemonic to use with <code>getDisplayName</code>.     */    public int getMnemonic() {        String m = (String)UIManager.get("GTKColorChooserPanel.mnemonic");        if (m != null) {            try {                int value = Integer.parseInt(m);                return value;            } catch (NumberFormatException nfe) {}        }        return -1;    }    /**     * Character to underline that represents the mnemonic.     */    public int getDisplayedMnemonicIndex() {        String m = (String)UIManager.get(                           "GTKColorChooserPanel.displayedMnemonicIndex");        if (m != null) {            try {                int value = Integer.parseInt(m);                return value;            } catch (NumberFormatException nfe) {}        }        return -1;    }    public Icon getSmallDisplayIcon() {        return null;    }    public Icon getLargeDisplayIcon() {        return null;    }    public void uninstallChooserPanel(JColorChooser enclosingChooser) {        super.uninstallChooserPanel(enclosingChooser);        removeAll();    }    /**     * Builds and configures the widgets for the GTKColorChooserPanel.     */    protected void buildChooser() {        triangle = new ColorTriangle();        triangle.setName("GTKColorChooserPanel.triangle");        // PENDING: when we straighten out user setting opacity, this should        // be changed.        label = new OpaqueLabel();        label.setName("GTKColorChooserPanel.colorWell");        label.setOpaque(true);        label.setMinimumSize(new Dimension(67, 32));        label.setPreferredSize(new Dimension(67, 32));        label.setMaximumSize(new Dimension(67, 32));        // PENDING: when we straighten out user setting opacity, this should        // be changed.        lastLabel = new OpaqueLabel();        lastLabel.setName("GTKColorChooserPanel.lastColorWell");        lastLabel.setOpaque(true);        lastLabel.setMinimumSize(new Dimension(67, 32));        lastLabel.setPreferredSize(new Dimension(67, 32));        lastLabel.setMaximumSize(new Dimension(67, 32));        hueSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 360, 1));        configureSpinner(hueSpinner, "GTKColorChooserPanel.hueSpinner");        saturationSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1));        configureSpinner(saturationSpinner,                         "GTKColorChooserPanel.saturationSpinner");        valueSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1));        configureSpinner(valueSpinner, "GTKColorChooserPanel.valueSpinner");        redSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1));        configureSpinner(redSpinner, "GTKColorChooserPanel.redSpinner");        greenSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1));        configureSpinner(greenSpinner, "GTKColorChooserPanel.greenSpinner");        blueSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1));        configureSpinner(blueSpinner, "GTKColorChooserPanel.blueSpinner");        colorNameTF = new JTextField(8);        setLayout(new GridBagLayout());        add(this, "GTKColorChooserPanel.hue", hueSpinner, -1, -1);        add(this, "GTKColorChooserPanel.red", redSpinner, -1, -1);        add(this, "GTKColorChooserPanel.saturation", saturationSpinner, -1,-1);        add(this, "GTKColorChooserPanel.green", greenSpinner, -1, -1);        add(this, "GTKColorChooserPanel.value", valueSpinner, -1, -1);        add(this, "GTKColorChooserPanel.blue", blueSpinner, -1, -1);        add(new JSeparator(SwingConstants.HORIZONTAL), new                  GridBagConstraints(1, 3, 4, 1, 1, 0,                  GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,                  new Insets(14, 0, 0, 0), 0, 0));        add(this, "GTKColorChooserPanel.colorName", colorNameTF, 0, 4);        add(triangle, new GridBagConstraints(0, 0, 1, 5, 0, 0,                      GridBagConstraints.LINE_START, GridBagConstraints.NONE,                      new Insets(14, 20, 2, 9), 0, 0));        Box hBox = Box.createHorizontalBox();        hBox.add(lastLabel);        hBox.add(label);        add(hBox, new GridBagConstraints(0, 5, 1, 1, 0, 0,                      GridBagConstraints.CENTER, GridBagConstraints.NONE,                      new Insets(0, 0, 0, 0), 0, 0));        add(new JSeparator(SwingConstants.HORIZONTAL), new                  GridBagConstraints(0, 6, 5, 1, 1, 0,                  GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,                  new Insets(12, 0, 0, 0), 0, 0));    }    /**     * Configures the spinner.     */    private void configureSpinner(JSpinner spinner, String name) {        spinner.addChangeListener(this);        spinner.setName(name);        JComponent editor = spinner.getEditor();        if (editor instanceof JSpinner.DefaultEditor) {            JFormattedTextField ftf = ((JSpinner.DefaultEditor)editor).                                                 getTextField();            ftf.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);        }    }    /**     * Adds the widget creating a JLabel with the specified name.     */    private void add(Container parent, String key, JComponent widget,                     int x, int y) {        JLabel label = new JLabel(UIManager.getString(key + "Text",                                                      getLocale()));        String mnemonic = (String)UIManager.get(key + "Mnemonic", getLocale());        if (mnemonic != null) {            try {                label.setDisplayedMnemonic(Integer.parseInt(mnemonic));            } catch (NumberFormatException nfe) {            }            String mnemonicIndex = (String)UIManager.get(key + "MnemonicIndex",                                                    getLocale());            if (mnemonicIndex != null) {                try {                    label.setDisplayedMnemonicIndex(Integer.parseInt(                                                        mnemonicIndex));                } catch (NumberFormatException nfe) {                }            }        }        label.setLabelFor(widget);        if (x < 0) {            x = parent.getComponentCount() % 4;        }        if (y < 0) {            y = parent.getComponentCount() / 4;        }        GridBagConstraints con = new GridBagConstraints(x + 1, y, 1, 1, 0, 0,                   GridBagConstraints.FIRST_LINE_END, GridBagConstraints.NONE,                   new Insets(4, 0, 0, 4), 0, 0);        if (y == 0) {            con.insets.top = 14;        }        parent.add(label, con);        con.gridx++;        parent.add(widget, con);    }    /**     * Refreshes the display from the model.     */    public void updateChooser() {        if (!settingColor) {            lastLabel.setBackground(getColorFromModel());            setColor(getColorFromModel(), true, true, false);        }    }    /**     * Resets the red component of the selected color.     */    private void setRed(int red) {        setRGB(red << 16 | getColor().getGreen() << 8 | getColor().getBlue());    }    /**     * Resets the green component of the selected color.     */    private void setGreen(int green) {        setRGB(getColor().getRed() << 16 | green << 8 | getColor().getBlue());    }    /**     * Resets the blue component of the selected color.     */    private void setBlue(int blue) {        setRGB(getColor().getRed() << 16 | getColor().getGreen() << 8 | blue);    }    /**     * Sets the hue of the selected color and updates the display if     * necessary.     */    private void setHue(float hue, boolean update) {        setHSB(hue, saturation, brightness);        if (update) {            settingColor = true;            hueSpinner.setValue(new Integer((int)(hue * 360)));            settingColor = false;        }    }    /**     * Returns the current amount of hue.     */    private float getHue() {        return hue;    }    /**     * Resets the saturation.     */    private void setSaturation(float saturation) {        setHSB(hue, saturation, brightness);    }    /**     * Returns the saturation.     */    private float getSaturation() {        return saturation;    }    /**     * Sets the brightness.     */    private void setBrightness(float brightness) {        setHSB(hue, saturation, brightness);    }    /**     * Returns the brightness.     */    private float getBrightness() {        return brightness;    }    /**     * Sets the saturation and brightness and updates the display if     * necessary.     */    private void setSaturationAndBrightness(float s, float b, boolean update) {        setHSB(hue, s, b);        if (update) {            settingColor = true;            saturationSpinner.setValue(new Integer((int)(s * 255)));            valueSpinner.setValue(new Integer((int)(b * 255)));            settingColor = false;        }    }    /**     * Resets the rgb values.     */    private void setRGB(int rgb) {        Color color = new Color(rgb);        setColor(color, false, true, true);        settingColor = true;        hueSpinner.setValue(new Integer((int)(hue * 360)));        saturationSpinner.setValue(new Integer((int)(saturation * 255)));        valueSpinner.setValue(new Integer((int)(brightness * 255)));        settingColor = false;    }    /**     * Resets the hsb values.     */    private void setHSB(float h, float s, float b) {        Color color = Color.getHSBColor(h, s, b);        this.hue = h;        this.saturation = s;        this.brightness = b;        setColor(color, false, false, true);        settingColor = true;        redSpinner.setValue(new Integer(color.getRed()));        greenSpinner.setValue(new Integer(color.getGreen()));        blueSpinner.setValue(new Integer(color.getBlue()));        settingColor = false;    }        /**     * Rests the color.     *     * @param color new Color     * @param updateSpinners whether or not to update the spinners.     * @param updateHSB if true, the hsb fields are updated based on the     *                  new color     * @param updateModel if true, the model is set.     */    private void setColor(Color color, boolean updateSpinners,                          boolean updateHSB, boolean updateModel) {        if (color == null) {            color = Color.BLACK;        }        settingColor = true;        if (updateHSB) {            float[] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(),                                         color.getBlue(), null);            hue = hsb[0];            saturation = hsb[1];            brightness = hsb[2];        }        if (updateModel) {            getColorSelectionModel().setSelectedColor(color);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?