GTKColorChooserPanelpublic class GTKColorChooserPanel extends AbstractColorChooserPanel implements ChangeListenerA color chooser panel mimicking that of GTK's: a color wheel showing
hue and a triangle that varies saturation and brightness. |
Fields Summary |
---|
private static final float | 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 | private float | hue | private float | saturation | private float | brightness | private static final int | FLAGS_CHANGED_ANGLEFlag indicating the angle, or hue, has changed and the triangle
needs to be recreated. | private static final int | FLAGS_DRAGGINGIndicates the wheel is being dragged. | private static final int | FLAGS_DRAGGING_TRIANGLEIndicates the triangle is being dragged. | private static final int | FLAGS_SETTING_COLORIndicates a color is being set and we should ignore setColor | private static final int | FLAGS_FOCUSED_WHEELIndicates the wheel has focus. | private static final int | FLAGS_FOCUSED_TRIANGLEIndicates the triangle has focus. |
Methods Summary |
---|
private void | add(java.awt.Container parent, java.lang.String key, javax.swing.JComponent widget, int x, int y)Adds the widget creating a JLabel with the specified name.
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);
| protected void | buildChooser()Builds and configures the widgets for the GTKColorChooserPanel.
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));
| static void | compositeRequestFocus(java.awt.Component component, boolean direction)Convenience method to transfer focus to the next child of component.
// PENDING: remove this when a variant of this is added to awt.
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();
| private void | configureSpinner(javax.swing.JSpinner spinner, java.lang.String name)Configures the spinner.
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);
}
| private float | getBrightness()Returns the brightness.
return brightness;
| public java.awt.Color | getColor()
return label.getBackground();
| public java.lang.String | getDisplayName()Returns a user presentable description of this GTKColorChooserPane.
return (String)UIManager.get("GTKColorChooserPanel.nameText");
| public int | getDisplayedMnemonicIndex()Character to underline that represents the mnemonic.
String m = (String)UIManager.get(
"GTKColorChooserPanel.displayedMnemonicIndex");
if (m != null) {
try {
int value = Integer.parseInt(m);
return value;
} catch (NumberFormatException nfe) {}
}
return -1;
| private float | getHue()Returns the current amount of hue.
return hue;
| public javax.swing.Icon | getLargeDisplayIcon()
return null;
| public int | getMnemonic()Returns the mnemonic to use with getDisplayName .
String m = (String)UIManager.get("GTKColorChooserPanel.mnemonic");
if (m != null) {
try {
int value = Integer.parseInt(m);
return value;
} catch (NumberFormatException nfe) {}
}
return -1;
| private float | getSaturation()Returns the saturation.
return saturation;
| public javax.swing.Icon | getSmallDisplayIcon()
return null;
| private void | setBlue(int blue)Resets the blue component of the selected color.
setRGB(getColor().getRed() << 16 | getColor().getGreen() << 8 | blue);
| private void | setBrightness(float brightness)Sets the brightness.
setHSB(hue, saturation, brightness);
| private void | setColor(java.awt.Color color, boolean updateSpinners, boolean updateHSB, boolean updateModel)Rests the color.
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);
}
triangle.setColor(hue, saturation, brightness);
label.setBackground(color);
// Force Integer to pad the string with 0's by adding 0x1000000 and
// then removing the first character.
String hexString = Integer.toHexString(
(color.getRGB() & 0xFFFFFF) | 0x1000000);
colorNameTF.setText("#" + hexString.substring(1));
if (updateSpinners) {
redSpinner.setValue(new Integer(color.getRed()));
greenSpinner.setValue(new Integer(color.getGreen()));
blueSpinner.setValue(new Integer(color.getBlue()));
hueSpinner.setValue(new Integer((int)(hue * 360)));
saturationSpinner.setValue(new Integer((int)(saturation * 255)));
valueSpinner.setValue(new Integer((int)(brightness * 255)));
}
settingColor = false;
| private void | setGreen(int green)Resets the green component of the selected color.
setRGB(getColor().getRed() << 16 | green << 8 | getColor().getBlue());
| private void | setHSB(float h, float s, float b)Resets the hsb values.
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;
| private void | setHue(float hue, boolean update)Sets the hue of the selected color and updates the display if
necessary.
setHSB(hue, saturation, brightness);
if (update) {
settingColor = true;
hueSpinner.setValue(new Integer((int)(hue * 360)));
settingColor = false;
}
| private void | setRGB(int rgb)Resets the rgb values.
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;
| private void | setRed(int red)Resets the red component of the selected color.
setRGB(red << 16 | getColor().getGreen() << 8 | getColor().getBlue());
| private void | setSaturation(float saturation)Resets the saturation.
setHSB(hue, saturation, brightness);
| private void | setSaturationAndBrightness(float s, float b, boolean update)Sets the saturation and brightness and updates the display if
necessary.
setHSB(hue, s, b);
if (update) {
settingColor = true;
saturationSpinner.setValue(new Integer((int)(s * 255)));
valueSpinner.setValue(new Integer((int)(b * 255)));
settingColor = false;
}
| public void | stateChanged(javax.swing.event.ChangeEvent e)ChangeListener method, updates the necessary display widgets.
if (settingColor) {
return;
}
Color color = getColor();
if (e.getSource() == hueSpinner) {
setHue(((Number)hueSpinner.getValue()).floatValue() / 360, false);
}
else if (e.getSource() == saturationSpinner) {
setSaturation(((Number)saturationSpinner.getValue()).
floatValue() / 255);
}
else if (e.getSource() == valueSpinner) {
setBrightness(((Number)valueSpinner.getValue()).
floatValue() / 255);
}
else if (e.getSource() == redSpinner) {
setRed(((Number)redSpinner.getValue()).intValue());
}
else if (e.getSource() == greenSpinner) {
setGreen(((Number)greenSpinner.getValue()).intValue());
}
else if (e.getSource() == blueSpinner) {
setBlue(((Number)blueSpinner.getValue()).intValue());
}
| public void | uninstallChooserPanel(javax.swing.JColorChooser enclosingChooser)
super.uninstallChooserPanel(enclosingChooser);
removeAll();
| public void | updateChooser()Refreshes the display from the model.
if (!settingColor) {
lastLabel.setBackground(getColorFromModel());
setColor(getColorFromModel(), true, true, false);
}
|
|