FileDocCategorySizeDatePackage
DefaultHSBChooserPanel.javaAPI DocJava SE 5 API25530Fri Aug 26 14:58:00 BST 2005javax.swing.colorchooser

DefaultHSBChooserPanel

public class DefaultHSBChooserPanel extends AbstractColorChooserPanel implements ChangeListener, HierarchyListener
Implements the default HSB Color chooser
version
1.25 12/19/03
author
Tom Santos
author
Steve Wilson
author
Mark Davidson
author
Shannon Hickey

Fields Summary
private transient HSBImage
palette
private transient HSBImage
sliderPalette
private transient Image
paletteImage
private transient Image
sliderPaletteImage
private JSlider
slider
private JSpinner
hField
private JSpinner
sField
private JSpinner
bField
private JTextField
redField
private JTextField
greenField
private JTextField
blueField
private boolean
isAdjusting
private Point
paletteSelection
private JLabel
paletteLabel
private JLabel
sliderPaletteLabel
private JRadioButton
hRadio
private JRadioButton
sRadio
private JRadioButton
bRadio
private static final int
PALETTE_DIMENSION
private static final int
MAX_HUE_VALUE
private static final int
MAX_SATURATION_VALUE
private static final int
MAX_BRIGHTNESS_VALUE
private int
currentMode
private static final int
HUE_MODE
private static final int
SATURATION_MODE
private static final int
BRIGHTNESS_MODE
Constructors Summary
public DefaultHSBChooserPanel()


      
    
Methods Summary
private voidaddPaletteListeners()

        paletteLabel.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e ) {
                float[] hsb = new float[3];
                palette.getHSBForLocation( e.getX(), e.getY(), hsb );
                updateHSB( hsb[0], hsb[1], hsb[2] );
            }
        });

        paletteLabel.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged( MouseEvent e ){
                int labelWidth = paletteLabel.getWidth();

                int labelHeight = paletteLabel.getHeight();
                int x = e.getX();
                int y = e.getY();

                if ( x >= labelWidth ) {
                    x = labelWidth - 1;
                }

                if ( y >= labelHeight ) {
                    y = labelHeight - 1;
                }

                if ( x < 0 ) {
                    x = 0;
                }

                if ( y < 0 ) {
                    y = 0;
                }
                
                float[] hsb = new float[3];
                palette.getHSBForLocation( x, y, hsb );
                updateHSB( hsb[0], hsb[1], hsb[2] );
            }
        });
    
protected voidbuildChooser()
Builds a new chooser panel.

        setLayout(new BorderLayout());
        JComponent spp = buildSliderPalettePanel();
        add(spp, BorderLayout.BEFORE_LINE_BEGINS);

        JPanel controlHolder = new JPanel(new SmartGridLayout(1,3));
        JComponent hsbControls = buildHSBControls();
        controlHolder.add(hsbControls);

        controlHolder.add(new JLabel(" ")); // spacer

        JComponent rgbControls = buildRGBControls();
        controlHolder.add(rgbControls);

        controlHolder.setBorder(new EmptyBorder( 10, 5, 10, 5));
        add( controlHolder, BorderLayout.CENTER);
    
private javax.swing.JComponentbuildHSBControls()
Creates the panel with the editable HSB fields and the radio buttons.


        String hueString = UIManager.getString("ColorChooser.hsbHueText");
        String saturationString = UIManager.getString("ColorChooser.hsbSaturationText");
        String brightnessString = UIManager.getString("ColorChooser.hsbBrightnessText");

        RadioButtonHandler handler = new RadioButtonHandler();

        hRadio = new JRadioButton(hueString);
        hRadio.addActionListener(handler);
        hRadio.setSelected(true);

        sRadio = new JRadioButton(saturationString);
        sRadio.addActionListener(handler);

        bRadio = new JRadioButton(brightnessString);
        bRadio.addActionListener(handler);

        ButtonGroup group = new ButtonGroup();
        group.add(hRadio);
        group.add(sRadio);
        group.add(bRadio);

        float[] hsb = getHSBColorFromModel();
        
        hField = new JSpinner(new SpinnerNumberModel((int)(hsb[0] * 359), 0, 359, 1));
        sField = new JSpinner(new SpinnerNumberModel((int)(hsb[1] * 100), 0, 100, 1));
        bField = new JSpinner(new SpinnerNumberModel((int)(hsb[2] * 100), 0, 100, 1));

        hField.addChangeListener(this);
        sField.addChangeListener(this);
        bField.addChangeListener(this);

        JPanel panel = new JPanel( new SmartGridLayout(2, 3) );
        
        panel.add(hRadio);
        panel.add(hField);
        panel.add(sRadio);
        panel.add(sField);
        panel.add(bRadio);
        panel.add(bField);

        return panel;
    
private javax.swing.JComponentbuildRGBControls()
Creates the panel with the uneditable RGB field

        JPanel panel = new JPanel(new SmartGridLayout(2,3));

        Color color = getColorFromModel();
        redField = new JTextField( String.valueOf(color.getRed()), 3 );
        redField.setEditable(false);
        redField.setHorizontalAlignment( JTextField.RIGHT );

        greenField = new JTextField(String.valueOf(color.getGreen()), 3 );
        greenField.setEditable(false);
        greenField.setHorizontalAlignment( JTextField.RIGHT );

        blueField = new JTextField( String.valueOf(color.getBlue()), 3 );
        blueField.setEditable(false);
        blueField.setHorizontalAlignment( JTextField.RIGHT );

        String redString = UIManager.getString("ColorChooser.hsbRedText");
        String greenString = UIManager.getString("ColorChooser.hsbGreenText");
        String blueString = UIManager.getString("ColorChooser.hsbBlueText");

        panel.add( new JLabel(redString) );
        panel.add( redField );
        panel.add( new JLabel(greenString) );
        panel.add( greenField );
        panel.add( new JLabel(blueString) );
        panel.add( blueField );
        
        return panel;                  
    
protected javax.swing.JComponentbuildSliderPalettePanel()


        // This slider has to have a minimum of 0.  A lot of math in this file is simplified due to this. 
        slider = new JSlider(JSlider.VERTICAL, 0, MAX_HUE_VALUE, 0);
        slider.setInverted(true);
        slider.setPaintTrack(false);
        slider.setPreferredSize(new Dimension(slider.getPreferredSize().width, PALETTE_DIMENSION + 15));
        slider.addChangeListener(this);
	// We're not painting ticks, but need to ask UI classes to
	// paint arrow shape anyway, if possible.
	slider.putClientProperty("Slider.paintThumbArrowShape", Boolean.TRUE);
        paletteLabel = createPaletteLabel();
        addPaletteListeners();
        sliderPaletteLabel = new JLabel();
        
        JPanel panel = new JPanel();
        panel.add( paletteLabel );
        panel.add( slider );
        panel.add( sliderPaletteLabel );
        
        initializePalettesIfNecessary();
        
        return panel;
    
private voidcleanupPalettesIfNecessary()

        if (palette == null) {
            return;
        }
        
        palette.aborted = true;
        sliderPalette.aborted = true;

        palette.nextFrame();
        sliderPalette.nextFrame();

        palette = null;
        sliderPalette = null;
        
        paletteImage = null;
        sliderPaletteImage = null;
        
        paletteLabel.setIcon(null);
        sliderPaletteLabel.setIcon(null);
    
protected javax.swing.JLabelcreatePaletteLabel()

        return new JLabel() {
            protected void paintComponent( Graphics g ) {
                super.paintComponent( g );
                g.setColor( Color.white );
                g.drawOval( paletteSelection.x - 4, paletteSelection.y - 4, 8, 8 );
            }
        };
    
public java.lang.StringgetDisplayName()

        return UIManager.getString("ColorChooser.hsbNameText");
    
public intgetDisplayedMnemonicIndex()
Provides a hint to the look and feel as to the index of the character in getDisplayName that should be visually identified as the mnemonic. The look and feel should only use this if getMnemonic returns a value > 0.

The return value here is a hint, it is ultimately up to the look and feel to honor the return value in some meaningful way. For example, a look and feel may wish to render each AbstractColorChooserPanel in a JTabbedPane, and further use this return value to underline a character in the getDisplayName.

This implementation looks up the value from the default ColorChooser.rgbDisplayedMnemonicIndex, or if it isn't available (or not an Integer) returns -1. The lookup for the default is done through the UIManager: UIManager.get("ColorChooser.hsbDisplayedMnemonicIndex");.

return
Character index to render mnemonic for; -1 to provide no visual identifier for this panel.
see
#getMnemonic
since
1.4

        return getInt("ColorChooser.hsbDisplayedMnemonicIndex", -1);
    
private float[]getHSBColorFromModel()
Returns an float array containing the HSB values of the selected color from the ColorSelectionModel

        Color color = getColorFromModel();
        float[] hsb = new float[3];
        Color.RGBtoHSB( color.getRed(), color.getGreen(), color.getBlue(), hsb );
        
        return hsb;
    
public javax.swing.IcongetLargeDisplayIcon()

        return null;
    
public intgetMnemonic()
Provides a hint to the look and feel as to the KeyEvent.VK constant that can be used as a mnemonic to access the panel. A return value <= 0 indicates there is no mnemonic.

The return value here is a hint, it is ultimately up to the look and feel to honor the return value in some meaningful way.

This implementation looks up the value from the default ColorChooser.hsbMnemonic, or if it isn't available (or not an Integer) returns -1. The lookup for the default is done through the UIManager: UIManager.get("ColorChooser.rgbMnemonic");.

return
KeyEvent.VK constant identifying the mnemonic; <= 0 for no mnemonic
see
#getDisplayedMnemonicIndex
since
1.4

        return getInt("ColorChooser.hsbMnemonic", -1);
    
public javax.swing.IcongetSmallDisplayIcon()

        return null;
    
public voidhierarchyChanged(java.awt.event.HierarchyEvent he)

        if ((he.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0) {
            if (isDisplayable()) {
                initializePalettesIfNecessary();
            } else {
                cleanupPalettesIfNecessary();
            }
        }
    
private voidinitializePalettesIfNecessary()

        if (palette != null) {
            return;
        }
        
        float[] hsb = getHSBColorFromModel();
        
        switch(currentMode){
            case HUE_MODE:
                palette = new HSBImage(HSBImage.HSQUARE, PALETTE_DIMENSION, PALETTE_DIMENSION, hsb[0], 1.0f, 1.0f);
                sliderPalette = new HSBImage(HSBImage.HSLIDER, 16, PALETTE_DIMENSION, 0f, 1.0f, 1.0f);
                break;
            case SATURATION_MODE:
                palette = new HSBImage(HSBImage.SSQUARE, PALETTE_DIMENSION, PALETTE_DIMENSION, 1.0f, hsb[1], 1.0f);
                sliderPalette = new HSBImage(HSBImage.SSLIDER, 16, PALETTE_DIMENSION, 1.0f, 0f, 1.0f);
                break;
            case BRIGHTNESS_MODE:
                palette = new HSBImage(HSBImage.BSQUARE, PALETTE_DIMENSION, PALETTE_DIMENSION, 1.0f, 1.0f, hsb[2]);
                sliderPalette = new HSBImage(HSBImage.BSLIDER, 16, PALETTE_DIMENSION, 1.0f, 1.0f, 0f);
                break;
        }
        paletteImage = Toolkit.getDefaultToolkit().createImage(palette);
        sliderPaletteImage = Toolkit.getDefaultToolkit().createImage(sliderPalette);
        
        paletteLabel.setIcon(new ImageIcon(paletteImage));
        sliderPaletteLabel.setIcon(new ImageIcon(sliderPaletteImage));
    
public voidinstallChooserPanel(javax.swing.JColorChooser enclosingChooser)

	super.installChooserPanel(enclosingChooser);
	addHierarchyListener(this);
    
private voidsetMode(int mode)

        if (currentMode == mode) {
            return;
        }
        
        isAdjusting = true;  // Ensure no events propagate from changing slider value.
        currentMode = mode;
        
        float[] hsb = getHSBColorFromModel();
        
        switch (currentMode) {
            case HUE_MODE:
                slider.setInverted(true);
                slider.setMaximum(MAX_HUE_VALUE);
                palette.setValues(HSBImage.HSQUARE, hsb[0], 1.0f, 1.0f);
                sliderPalette.setValues(HSBImage.HSLIDER, 0f, 1.0f, 1.0f);
                break;
            case SATURATION_MODE:
                slider.setInverted(false);
                slider.setMaximum(MAX_SATURATION_VALUE);
                palette.setValues(HSBImage.SSQUARE, hsb[0], hsb[1], 1.0f);
                sliderPalette.setValues(HSBImage.SSLIDER, hsb[0], 1.0f, 1.0f);
                break;
            case BRIGHTNESS_MODE:
                slider.setInverted(false);
                slider.setMaximum(MAX_BRIGHTNESS_VALUE);
                palette.setValues(HSBImage.BSQUARE, hsb[0], 1.0f, hsb[2]);
                sliderPalette.setValues(HSBImage.BSLIDER, hsb[0], 1.0f, 1.0f);
                break;
        }

        isAdjusting = false;
        
        palette.nextFrame();
        sliderPalette.nextFrame();

        updateChooser();
    
public voidstateChanged(javax.swing.event.ChangeEvent e)

        if (e.getSource() == slider) {
            boolean modelIsAdjusting = slider.getModel().getValueIsAdjusting();

            if (!modelIsAdjusting && !isAdjusting) {
                int sliderValue = slider.getValue();
                int sliderRange = slider.getMaximum();
                float value = (float)sliderValue / (float)sliderRange;

                float[] hsb = getHSBColorFromModel();

                switch ( currentMode ){
                    case HUE_MODE:
                        updateHSB(value, hsb[1], hsb[2]);
                        break;
                    case SATURATION_MODE:
                        updateHSB(hsb[0], value, hsb[2]);
                        break;
                    case BRIGHTNESS_MODE:
                        updateHSB(hsb[0], hsb[1], value);
                        break;
                }
            }
        } else if (e.getSource() instanceof JSpinner) {
            float hue = ((Integer)hField.getValue()).floatValue() / 359f;
            float saturation = ((Integer)sField.getValue()).floatValue() / 100f;
            float brightness = ((Integer)bField.getValue()).floatValue() / 100f;

            updateHSB(hue, saturation, brightness);
        }
    
public voiduninstallChooserPanel(javax.swing.JColorChooser enclosingChooser)
Invoked when the panel is removed from the chooser.

    	super.uninstallChooserPanel(enclosingChooser);
    	cleanupPalettesIfNecessary();
    	removeAll();
    	removeHierarchyListener(this);
    
public voidupdateChooser()
Invoked automatically when the model's state changes. It is also called by installChooserPanel to allow you to set up the initial state of your chooser. Override this method to update your ChooserPanel.

        if ( !isAdjusting ) {
            float[] hsb = getHSBColorFromModel();
            updateHSB( hsb[0], hsb[1], hsb[2] ); 
        }
    
private voidupdateHSB(float h, float s, float b)
Main internal method of updating the ui controls and the color model.

        if ( !isAdjusting ) {
            isAdjusting = true;

            updatePalette( h, s, b );
            updateSlider( h, s, b );
            updateHSBTextFields( h, s, b );

            Color color = Color.getHSBColor(h, s, b);
            updateRGBTextFields( color );

            getColorSelectionModel().setSelectedColor( color );

            isAdjusting = false;
        }
    
private voidupdateHSBTextFields(float hue, float saturation, float brightness)

        int h =  Math.round(hue * 359); 
        int s =  Math.round(saturation * 100); 
        int b =  Math.round(brightness * 100); 

        if (((Integer)hField.getValue()).intValue() != h) {
            hField.setValue(new Integer(h));
        }
        if (((Integer)sField.getValue()).intValue() != s) {
            sField.setValue(new Integer(s));
        }
        if (((Integer)bField.getValue()).intValue() != b) {
            bField.setValue(new Integer(b));
        }
    
private voidupdatePalette(float h, float s, float b)

        int x = 0;
        int y = 0;

        switch ( currentMode ) {
        case HUE_MODE:
            if ( h != palette.getHue() ) {
                palette.setHue( h );
                palette.nextFrame();
            }
            x = PALETTE_DIMENSION - (int)(s * PALETTE_DIMENSION);
            y = PALETTE_DIMENSION - (int)(b * PALETTE_DIMENSION);
            break;
        case SATURATION_MODE:
            if ( s != palette.getSaturation() ) {
                palette.setSaturation( s );
                palette.nextFrame();
            }
            x = (int)(h * PALETTE_DIMENSION);
            y = PALETTE_DIMENSION - (int)(b * PALETTE_DIMENSION);
            break;
        case BRIGHTNESS_MODE:
            if ( b != palette.getBrightness() ) {
                palette.setBrightness( b );
                palette.nextFrame();
            }
            x = (int)(h * PALETTE_DIMENSION);
            y = PALETTE_DIMENSION - (int)(s * PALETTE_DIMENSION);
            break;
        }
        
        paletteSelection.setLocation( x, y );
        paletteLabel.repaint();
    
private voidupdateRGBTextFields(java.awt.Color color)
Updates the values of the RGB fields to reflect the new color change

        redField.setText(String.valueOf(color.getRed()));
        greenField.setText(String.valueOf(color.getGreen()));
        blueField.setText(String.valueOf(color.getBlue()));
    
private voidupdateSlider(float h, float s, float b)

        // Update the slider palette if necessary.
        // When the slider is the hue slider or the hue hasn't changed,
        // the hue of the palette will not need to be updated.
        if (currentMode != HUE_MODE && h != sliderPalette.getHue() ) {
            sliderPalette.setHue( h );
            sliderPalette.nextFrame();
        }
        
        float value = 0f;

        switch ( currentMode ) {
        case HUE_MODE:
            value = h;
            break;
        case SATURATION_MODE:
            value = s;
            break;
        case BRIGHTNESS_MODE:
            value = b;
            break;
        }

        slider.setValue( Math.round(value * (slider.getMaximum())) );