001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dennis Ushakov
019: * @version $Revision$
020: */package javax.swing.colorchooser;
021:
022: import java.awt.Color;
023: import java.awt.Dimension;
024: import java.awt.FlowLayout;
025: import java.awt.GridLayout;
026:
027: import javax.swing.Icon;
028: import javax.swing.JLabel;
029: import javax.swing.JPanel;
030: import javax.swing.JSlider;
031: import javax.swing.JSpinner;
032: import javax.swing.SpinnerNumberModel;
033: import javax.swing.UIManager;
034: import javax.swing.event.ChangeEvent;
035: import javax.swing.event.ChangeListener;
036:
037: import org.apache.harmony.x.swing.Utilities;
038:
039: class RGBPanel extends AbstractColorChooserPanel {
040:
041: private JSlider[] sliders;
042: private JSpinner[] spinners;
043:
044: public String getDisplayName() {
045: return UIManager.getString("ColorChooser.rgbNameText");
046: }
047:
048: public Icon getSmallDisplayIcon() {
049: return null;
050: }
051:
052: public Icon getLargeDisplayIcon() {
053: return null;
054: }
055:
056: public void updateChooser() {
057: Color color = getColorSelectionModel().getSelectedColor();
058: if (color == null) {
059: return;
060: }
061: float[] rgb = color.getRGBColorComponents(null);
062: for (int i = 0; i < 3; i++) {
063: int colorComponent = Math.round(rgb[i] * 255);
064: sliders[i].setValue(colorComponent);
065: spinners[i].setValue(new Integer(colorComponent));
066: }
067: }
068:
069: protected void buildChooser() {
070: mnemonic = UIManager.getInt("ColorChooser.rgbMnemonic");
071: displayedMnemonicIndex = Integer.parseInt(UIManager
072: .getString("ColorChooser.rgbDisplayedMnemonicIndex"));
073:
074: String[] namesRGB = {
075: UIManager.getString("ColorChooser.rgbRedText"),
076: UIManager.getString("ColorChooser.rgbGreenText"),
077: UIManager.getString("ColorChooser.rgbBlueText") };
078: int[] mnemonics = {
079: UIManager.getInt("ColorChooser.rgbRedMnemonic"),
080: UIManager.getInt("ColorChooser.rgbGreenMnemonic"),
081: UIManager.getInt("ColorChooser.rgbBlueMnemonic") };
082:
083: JPanel panel = new JPanel(new GridLayout(3, 1));
084: Color color = getColorSelectionModel().getSelectedColor();
085: float[] rgb = color == null ? Color.BLACK
086: .getRGBColorComponents(null) : color
087: .getRGBColorComponents(null);
088:
089: JPanel[] panels = { new JPanel(), new JPanel(), new JPanel() };
090:
091: sliders = new JSlider[3];
092: spinners = new JSpinner[3];
093:
094: for (int i = 0; i < 3; i++) {
095: int colorComponent = Math.round(rgb[i] * 255);
096: JSlider slider = new JSlider(0, 255, colorComponent);
097: sliders[i] = slider;
098: slider.setPaintTicks(true);
099: slider.setPaintLabels(true);
100: slider.setMajorTickSpacing(255 / 3);
101: slider.setMinorTickSpacing(255 / 15);
102:
103: slider.addChangeListener(new ChangeListener() {
104: public void stateChanged(ChangeEvent e) {
105: Color color = getColorSelectionModel()
106: .getSelectedColor();
107: float[] rgb = color.getRGBColorComponents(null);
108:
109: for (int i = 0; i < 3; i++) {
110: if (e.getSource() == sliders[i]) {
111: rgb[i] = sliders[i].getValue() / 255f;
112: }
113: }
114: getColorSelectionModel().setSelectedColor(
115: new Color(rgb[0], rgb[1], rgb[2]));
116: }
117: });
118:
119: JSpinner spinner = new JSpinner(new SpinnerNumberModel(
120: colorComponent, 0, 255, 1));
121: spinners[i] = spinner;
122: Dimension stringSize = Utilities.getStringSize("999",
123: spinner.getEditor().getFontMetrics(
124: spinner.getEditor().getFont()));
125: Utilities.addInsets(stringSize, spinner.getEditor()
126: .getInsets());
127: spinner.getEditor().setPreferredSize(
128: new Dimension(stringSize.width, stringSize.height));
129: spinner.addChangeListener(new ChangeListener() {
130: public void stateChanged(ChangeEvent e) {
131: Color color = getColorSelectionModel()
132: .getSelectedColor();
133: float[] rgb = color.getRGBColorComponents(null);
134:
135: for (int i = 0; i < 3; i++) {
136: if (e.getSource() == spinners[i]) {
137: rgb[i] = ((Number) spinners[i].getValue())
138: .floatValue() / 255;
139: }
140: }
141: getColorSelectionModel().setSelectedColor(
142: new Color(rgb[0], rgb[1], rgb[2]));
143: }
144: });
145: JPanel spinnerPanel = new JPanel();
146: spinnerPanel.add(spinner);
147:
148: JLabel label = new JLabel(namesRGB[i]);
149: label.setLabelFor(slider);
150: label.setDisplayedMnemonic(mnemonics[i]);
151:
152: panels[i].add(label);
153: panels[i].add(slider);
154: panels[i].add(spinnerPanel);
155: ((FlowLayout) panels[i].getLayout())
156: .setAlignment(FlowLayout.RIGHT);
157: panel.add(panels[i]);
158: }
159:
160: this.add(panel);
161: }
162: }
|