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: package javax.swing;
019:
020: import java.awt.BorderLayout;
021: import java.awt.Color;
022: import java.awt.Component;
023: import java.awt.Dialog;
024: import java.awt.Frame;
025: import java.awt.GraphicsEnvironment;
026: import java.awt.HeadlessException;
027: import java.awt.Window;
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030: import javax.accessibility.Accessible;
031: import javax.accessibility.AccessibleContext;
032: import javax.accessibility.AccessibleRole;
033: import javax.swing.colorchooser.AbstractColorChooserPanel;
034: import javax.swing.colorchooser.ColorChooserComponentFactory;
035: import javax.swing.colorchooser.ColorSelectionModel;
036: import javax.swing.colorchooser.DefaultColorSelectionModel;
037: import javax.swing.plaf.ColorChooserUI;
038:
039: import org.apache.harmony.x.swing.internal.nls.Messages;
040:
041: /**
042: * <p>
043: * <i>JColorChooser</i>
044: * </p>
045: * <h3>Implementation Notes:</h3>
046: * <ul>
047: * <li>The <code>serialVersionUID</code> fields are explicitly declared as a performance
048: * optimization, not as a guarantee of serialization compatibility.</li>
049: * </ul>
050: */
051: public class JColorChooser extends JComponent implements Accessible {
052: private static final long serialVersionUID = -3698198979867714534L;
053:
054: protected class AccessibleJColorChooser extends
055: AccessibleJComponent {
056: private static final long serialVersionUID = -4916849065058077868L;
057:
058: @Override
059: public AccessibleRole getAccessibleRole() {
060: return AccessibleRole.COLOR_CHOOSER;
061: }
062: }
063:
064: public static final String SELECTION_MODEL_PROPERTY = "selectionModel";
065:
066: public static final String PREVIEW_PANEL_PROPERTY = "previewPanel";
067:
068: public static final String CHOOSER_PANELS_PROPERTY = "chooserPanels";
069:
070: private ColorSelectionModel colorSelectionModel;
071:
072: private boolean dragEnabled;
073:
074: private JComponent previewPanel;
075:
076: private AbstractColorChooserPanel[] chooserPanels = new AbstractColorChooserPanel[] {};
077:
078: private static Color resultColor;
079:
080: public JColorChooser() {
081: this (new DefaultColorSelectionModel(Color.WHITE));
082: }
083:
084: public JColorChooser(Color initialColor) {
085: this (new DefaultColorSelectionModel(initialColor));
086: }
087:
088: public JColorChooser(ColorSelectionModel model) {
089: colorSelectionModel = model;
090: setLayout(new BorderLayout());
091: updateUI();
092: }
093:
094: @SuppressWarnings("deprecation")
095: public static Color showDialog(Component component, String title,
096: Color initialColor) throws HeadlessException {
097: final JColorChooser colorChooser = new JColorChooser(
098: initialColor);
099: ActionListener okListener = new ActionListener() {
100: public void actionPerformed(ActionEvent e) {
101: resultColor = colorChooser.getColor();
102: }
103: };
104: ActionListener cancelListener = new ActionListener() {
105: public void actionPerformed(ActionEvent e) {
106: resultColor = null;
107: }
108: };
109: createDialog(component, title, true, colorChooser, okListener,
110: cancelListener).show();
111: return resultColor;
112: }
113:
114: public static JDialog createDialog(Component c, String title,
115: boolean modal, final JColorChooser chooserPane,
116: ActionListener okListener, ActionListener cancelListener)
117: throws HeadlessException {
118: Window ancestingWindow = c instanceof Window ? (Window) c
119: : SwingUtilities.getWindowAncestor(c);
120: final JDialog result;
121: if (ancestingWindow instanceof Frame) {
122: result = new JDialog((Frame) ancestingWindow);
123: } else if (ancestingWindow instanceof Dialog) {
124: result = new JDialog((Dialog) ancestingWindow);
125: } else {
126: result = new JDialog();
127: }
128: result.setModal(modal);
129: result.setLocationRelativeTo(c);
130: result.setTitle(title);
131: result.getContentPane().add(chooserPane);
132: if (JDialog.isDefaultLookAndFeelDecorated()) {
133: result.getRootPane().setWindowDecorationStyle(
134: JRootPane.COLOR_CHOOSER_DIALOG);
135: }
136: JPanel buttonsPanel = new JPanel();
137: ActionListener disposeListener = new ActionListener() {
138: public void actionPerformed(ActionEvent e) {
139: result.dispose();
140: }
141: };
142: String okText = UIManager.getString("ColorChooser.okText");
143: final JButton okButton = new JButton(okText);
144: okButton.addActionListener(okListener);
145: String cancelText = UIManager
146: .getString("ColorChooser.cancelText");
147: final JButton cancelButton = new JButton(cancelText);
148: cancelButton.addActionListener(cancelListener);
149: String resetText = UIManager
150: .getString("ColorChooser.resetText");
151: JButton resetButton = new JButton(resetText);
152: int resetMnemonic = UIManager
153: .getInt("ColorChooser.resetMnemonic");
154: resetButton.setMnemonic(resetMnemonic);
155: resetButton.addActionListener(new ActionListener() {
156: public void actionPerformed(ActionEvent e) {
157: chooserPane.setColor(Color.WHITE);
158: }
159: });
160: okButton.addActionListener(disposeListener);
161: cancelButton.addActionListener(disposeListener);
162: buttonsPanel.setLayout(new BoxLayout(buttonsPanel,
163: BoxLayout.X_AXIS));
164: buttonsPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5,
165: 5));
166: buttonsPanel.add(Box.createHorizontalGlue());
167: buttonsPanel.add(okButton);
168: buttonsPanel.add(Box.createHorizontalStrut(6));
169: buttonsPanel.add(cancelButton);
170: buttonsPanel.add(Box.createHorizontalStrut(6));
171: buttonsPanel.add(resetButton);
172: result.add(buttonsPanel, BorderLayout.SOUTH);
173: result.getRootPane().setDefaultButton(okButton);
174: result.pack();
175: InputMap map = LookAndFeel.makeComponentInputMap(chooserPane,
176: new Object[] { "ESCAPE", "cancelAction" });
177: SwingUtilities.replaceUIInputMap(chooserPane,
178: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, map);
179: chooserPane.getActionMap().put("cancelAction",
180: new AbstractAction() {
181: private static final long serialVersionUID = 1L;
182:
183: public void actionPerformed(ActionEvent e) {
184: cancelButton.doClick(0);
185: }
186: });
187: return result;
188: }
189:
190: public ColorChooserUI getUI() {
191: return (ColorChooserUI) ui;
192: }
193:
194: public void setUI(ColorChooserUI ui) {
195: super .setUI(ui);
196: }
197:
198: @Override
199: public void updateUI() {
200: setUI((ColorChooserUI) UIManager.getUI(this ));
201: }
202:
203: @Override
204: public String getUIClassID() {
205: return "ColorChooserUI";
206: }
207:
208: public Color getColor() {
209: return colorSelectionModel.getSelectedColor();
210: }
211:
212: public void setColor(Color color) {
213: colorSelectionModel.setSelectedColor(color);
214: }
215:
216: public void setColor(int r, int g, int b) {
217: setColor(new Color(r, g, b));
218: }
219:
220: public void setColor(int c) {
221: setColor(new Color(c));
222: }
223:
224: public void setDragEnabled(boolean b) {
225: if (b && GraphicsEnvironment.isHeadless()) {
226: throw new HeadlessException();
227: }
228: dragEnabled = b;
229: }
230:
231: public boolean getDragEnabled() {
232: return dragEnabled;
233: }
234:
235: public void setPreviewPanel(JComponent preview) {
236: JComponent oldValue = previewPanel;
237: if (preview == null) {
238: previewPanel = ColorChooserComponentFactory
239: .getPreviewPanel();
240: } else {
241: previewPanel = preview;
242: }
243: firePropertyChange(PREVIEW_PANEL_PROPERTY, oldValue,
244: previewPanel);
245: }
246:
247: public JComponent getPreviewPanel() {
248: return previewPanel;
249: }
250:
251: public void addChooserPanel(AbstractColorChooserPanel panel) {
252: AbstractColorChooserPanel[] newChooserPanels = new AbstractColorChooserPanel[chooserPanels.length + 1];
253: System.arraycopy(chooserPanels, 0, newChooserPanels, 0,
254: chooserPanels.length);
255: newChooserPanels[chooserPanels.length] = panel;
256: setChooserPanels(newChooserPanels);
257: }
258:
259: public AbstractColorChooserPanel removeChooserPanel(
260: AbstractColorChooserPanel panel) {
261: AbstractColorChooserPanel panelToRemove = null;
262: int index = 0;
263: for (int i = 0; i < chooserPanels.length; i++) {
264: if (panel.equals(chooserPanels[i])) {
265: panelToRemove = chooserPanels[i];
266: index = i;
267: break;
268: }
269: }
270: if (panelToRemove == null) {
271: throw new IllegalArgumentException(Messages
272: .getString("swing.0A")); //$NON-NLS-1$
273: }
274: AbstractColorChooserPanel[] newChooserPanels = new AbstractColorChooserPanel[chooserPanels.length - 1];
275: System.arraycopy(chooserPanels, 0, newChooserPanels, 0, index);
276: System.arraycopy(chooserPanels, index + 1, newChooserPanels,
277: index, newChooserPanels.length - index);
278: setChooserPanels(newChooserPanels);
279: return panelToRemove;
280: }
281:
282: public void setChooserPanels(AbstractColorChooserPanel[] panels) {
283: AbstractColorChooserPanel[] oldValue = chooserPanels;
284: chooserPanels = panels;
285: firePropertyChange(CHOOSER_PANELS_PROPERTY, oldValue,
286: chooserPanels);
287: }
288:
289: public AbstractColorChooserPanel[] getChooserPanels() {
290: return chooserPanels;
291: }
292:
293: public ColorSelectionModel getSelectionModel() {
294: return colorSelectionModel;
295: }
296:
297: public void setSelectionModel(ColorSelectionModel newModel) {
298: ColorSelectionModel oldModel = colorSelectionModel;
299: colorSelectionModel = newModel;
300: firePropertyChange(SELECTION_MODEL_PROPERTY, oldModel, newModel);
301: }
302:
303: @Override
304: public AccessibleContext getAccessibleContext() {
305: if (accessibleContext == null) {
306: accessibleContext = new AccessibleJColorChooser();
307: }
308: return accessibleContext;
309: }
310: }
|