1 /* JColorChooser.java --
2 Copyright (C) 2002, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
40 import java.awt.AWTError;
41 import java.awt.BorderLayout;
42 import java.awt.Color;
43 import java.awt.Component;
44 import java.awt.Dialog;
45 import java.awt.FlowLayout;
46 import java.awt.Frame;
47 import java.awt.event.ActionEvent;
48 import java.awt.event.ActionListener;
49 import java.io.IOException;
50 import java.io.ObjectOutputStream;
51 import javax.accessibility.Accessible;
52 import javax.accessibility.AccessibleContext;
53 import javax.accessibility.AccessibleRole;
54 import javax.swing.colorchooser.AbstractColorChooserPanel;
55 import javax.swing.colorchooser.ColorChooserComponentFactory;
56 import javax.swing.colorchooser.ColorSelectionModel;
57 import javax.swing.colorchooser.DefaultColorSelectionModel;
58 import javax.swing.plaf.ColorChooserUI;
62 * The JColorChooser is a Swing widget that offers users different ways to
63 * select a color. By default, three different panels are presented to the
64 * user that are capable of changing the selected color. There are three ways
65 * to utilize JColorChooser. The first is to build a JColorChooser and add it
66 * to the content pane. The second is to use the createDialog method to
67 * create a JDialog that holds a JColorChooser. The third is to show a
68 * JColorChooser in a JDialog directly using the showDialog method.
70 public class JColorChooser extends JComponent implements Accessible
73 private static final long serialVersionUID = 9168066781620640889L;
76 * AccessibleJColorChooser
78 protected class AccessibleJColorChooser
79 extends JComponent.AccessibleJComponent
82 private static final long serialVersionUID = -2038297864782299082L;
85 * Constructor AccessibleJColorChooser
87 protected AccessibleJColorChooser()
94 * @return AccessibleRole
96 public AccessibleRole getAccessibleRole()
98 return AccessibleRole.COLOR_CHOOSER;
99 } // getAccessibleRole()
100 } // AccessibleJColorChooser
102 /** The model used with the JColorChooser. */
103 private ColorSelectionModel selectionModel;
105 /** The preview panel associated with the JColorChooser. */
106 private JComponent previewPanel;
109 * The set of AbstractColorChooserPanels associated with the JColorChooser.
111 private AbstractColorChooserPanel[] chooserPanels;
113 /** A Drag and Drop property. */
114 private boolean dragEnabled;
117 * The property fired by the JColorChooser when the selectionModel property
120 public static final String SELECTION_MODEL_PROPERTY = "selectionModel";
123 * The property fired by the JColorChooser when the previewPanel property
126 public static final String PREVIEW_PANEL_PROPERTY = "previewPanel";
129 * The property fired by the JColorChooser when the chooserPanels property
132 public static final String CHOOSER_PANELS_PROPERTY = "chooserPanels";
134 /** accessibleContext */
135 protected AccessibleContext accessibleContext;
138 * This method creates a new JColorChooser with the default initial color.
140 public JColorChooser()
142 this(new DefaultColorSelectionModel());
146 * This method creates a new JColorChooser with the given initial color.
148 * @param initial The initial color.
150 public JColorChooser(Color initial)
152 this(new DefaultColorSelectionModel(initial));
156 * This method creates a new JColorChooser with the given model. The model
157 * will dictate what the initial color for the JColorChooser is.
159 * @param model The Model to use with the JColorChooser.
161 public JColorChooser(ColorSelectionModel model)
164 model = new DefaultColorSelectionModel();
165 selectionModel = model;
170 * This method sets the current color for the JColorChooser.
172 * @param color The new color for the JColorChooser.
174 public void setColor(Color color)
177 selectionModel.setSelectedColor(color);
181 * This method sets the current color for the JColorChooser using RGB
184 * @param r The red value.
185 * @param g The green value.
186 * @param b The blue value.
188 public void setColor(int r, int g, int b)
190 selectionModel.setSelectedColor(new Color(r, g, b));
194 * This method sets the current color for the JColorChooser using the
195 * integer value. Bits 0-7 represent the blue value. Bits 8-15 represent
196 * the green value. Bits 16-23 represent the red value.
198 * @param color The new current color of the JColorChooser.
200 public void setColor(int color)
202 setColor(new Color(color, false));
206 * This method shows a JColorChooser inside a JDialog. The JDialog will
207 * block until it is hidden. The JDialog comes with three buttons: OK,
208 * Cancel, and Reset. Pressing OK or Cancel hide the JDialog. Pressing
209 * Reset will reset the JColorChooser to its initial value.
211 * @param component The Component that parents the JDialog.
212 * @param title The title displayed in the JDialog.
213 * @param initial The initial color.
215 * @return The selected color.
217 public static Color showDialog(Component component, String title,
220 JColorChooser choose = new JColorChooser(initial);
222 JDialog dialog = createDialog(component, title, true, choose, null, null);
224 dialog.getContentPane().add(choose);
228 return choose.getColor();
232 * This is a helper method to make the given JDialog block until it is
235 * @param dialog The JDialog to block.
237 private static void makeModal(JDialog dialog)
241 synchronized (dialog)
243 while (dialog.isVisible())
247 catch (InterruptedException e)
253 * This is a helper method to find the first Frame or Dialog ancestor of the
256 * @param c The Component to find ancestors for.
258 * @return A Frame or Dialog ancestor. Null if none are found.
260 private static Component findParent(Component c)
262 Component parent = SwingUtilities.getAncestorOfClass(Frame.class, c);
265 parent = SwingUtilities.getAncestorOfClass(Dialog.class, c);
270 * This method will take the given JColorChooser and place it in a JDialog
271 * with the given modal property. Three buttons are displayed in the
272 * JDialog: OK, Cancel and Reset. If OK or Cancel are pressed, the JDialog
273 * is hidden. If Reset is pressed, then the JColorChooser will take on its
274 * default color value. The given okListener will be registered to the OK
275 * button and the cancelListener will be registered to the Cancel button.
276 * If the modal property is set, then the JDialog will block until it is
279 * @param component The Component that will parent the JDialog.
280 * @param title The title displayed in the JDialog.
281 * @param modal The modal property.
282 * @param chooserPane The JColorChooser to place in the JDialog.
283 * @param okListener The ActionListener to register to the OK button.
284 * @param cancelListener The ActionListener to register to the Cancel
287 * @return A JDialog with the JColorChooser inside of it.
289 * @throws AWTError If the component is not a suitable parent.
291 public static JDialog createDialog(Component component, String title,
292 boolean modal, JColorChooser chooserPane,
293 ActionListener okListener,
294 ActionListener cancelListener)
296 Component parent = findParent(component);
298 throw new AWTError("No suitable parent found for Component.");
300 if (parent instanceof Frame)
301 dialog = new ModalDialog((Frame) parent, title);
303 dialog = new ModalDialog((Dialog) parent, title);
304 dialog.setModal(modal);
306 dialog.getContentPane().setLayout(new BorderLayout());
308 JPanel panel = new JPanel();
309 panel.setLayout(new FlowLayout());
311 ActionListener al = new DefaultOKCancelListener(dialog);
313 JButton ok = new JButton("OK");
314 ok.addActionListener(okListener);
315 ok.addActionListener(al);
317 JButton cancel = new JButton("Cancel");
318 cancel.addActionListener(cancelListener);
319 cancel.addActionListener(al);
321 JButton reset = new JButton("Reset");
322 reset.addActionListener(new DefaultResetListener(chooserPane));
324 dialog.getContentPane().add(chooserPane, BorderLayout.NORTH);
330 dialog.getContentPane().add(panel, BorderLayout.SOUTH);
336 * This method returns the UI Component used for this JColorChooser.
338 * @return The UI Component for this JColorChooser.
340 public ColorChooserUI getUI()
342 return (ColorChooserUI) ui;
346 * This method sets the UI Component used for this JColorChooser.
348 * @param ui The UI Component to use with this JColorChooser.
350 public void setUI(ColorChooserUI ui)
356 * This method resets the UI Component property to the Look and Feel
359 public void updateUI()
361 setUI((ColorChooserUI) UIManager.getUI(this));
366 * This method returns a String identifier for the UI Class to be used with
369 * @return The String identifier for the UI Class.
371 public String getUIClassID()
373 return "ColorChooserUI";
377 * This method returns the current color for the JColorChooser.
379 * @return The current color for the JColorChooser.
381 public Color getColor()
383 return selectionModel.getSelectedColor(); // TODO
387 * This method changes the previewPanel property for the JTabbedPane. The
388 * previewPanel is responsible for indicating the current color of the
391 * @param component The Component that will act as the previewPanel.
393 public void setPreviewPanel(JComponent component)
395 if (component != previewPanel)
397 JComponent old = previewPanel;
398 previewPanel = component;
399 firePropertyChange(PREVIEW_PANEL_PROPERTY, old, previewPanel);
401 } // setPreviewPanel()
404 * This method returns the current previewPanel used with this
407 * @return The current previewPanel.
409 public JComponent getPreviewPanel()
411 return previewPanel; // TODO
412 } // getPreviewPanel()
415 * This method adds the given AbstractColorChooserPanel to the list of the
416 * JColorChooser's chooserPanels.
418 * @param panel The AbstractColorChooserPanel to add.
420 public void addChooserPanel(AbstractColorChooserPanel panel)
424 AbstractColorChooserPanel[] old = chooserPanels;
425 AbstractColorChooserPanel[] newPanels = new AbstractColorChooserPanel[(old == null)
430 System.arraycopy(old, 0, newPanels, 0, old.length);
431 newPanels[newPanels.length - 1] = panel;
432 chooserPanels = newPanels;
433 panel.installChooserPanel(this);
434 firePropertyChange(CHOOSER_PANELS_PROPERTY, old, newPanels);
435 } // addChooserPanel()
438 * This method removes the given AbstractColorChooserPanel from the
439 * JColorChooser's list of chooserPanels.
441 * @param panel The AbstractColorChooserPanel to remove.
443 * @return The AbstractColorChooserPanel that was removed.
445 public AbstractColorChooserPanel removeChooserPanel(AbstractColorChooserPanel panel)
448 for (int i = 0; i < chooserPanels.length; i++)
449 if (panel == chooserPanels[i])
458 AbstractColorChooserPanel[] old = chooserPanels;
459 if (chooserPanels.length == 1)
460 chooserPanels = null;
463 AbstractColorChooserPanel[] newPanels = new AbstractColorChooserPanel[chooserPanels.length
465 System.arraycopy(chooserPanels, 0, newPanels, 0, index);
466 System.arraycopy(chooserPanels, index, newPanels, index - 1,
467 chooserPanels.length - index);
468 chooserPanels = newPanels;
470 panel.uninstallChooserPanel(this);
471 firePropertyChange(CHOOSER_PANELS_PROPERTY, old, chooserPanels);
476 * This method sets the chooserPanels property for this JColorChooser.
478 * @param panels The new set of AbstractColorChooserPanels to use.
480 public void setChooserPanels(AbstractColorChooserPanel[] panels)
482 if (panels != chooserPanels)
484 if (chooserPanels != null)
485 for (int i = 0; i < chooserPanels.length; i++)
486 if (chooserPanels[i] != null)
487 chooserPanels[i].uninstallChooserPanel(this);
489 AbstractColorChooserPanel[] old = chooserPanels;
490 chooserPanels = panels;
493 for (int i = 0; i < panels.length; i++)
494 if (panels[i] != null)
495 panels[i].installChooserPanel(this);
497 firePropertyChange(CHOOSER_PANELS_PROPERTY, old, chooserPanels);
499 } // setChooserPanels()
502 * This method returns the AbstractColorChooserPanels used with this
505 * @return The AbstractColorChooserPanels used with this JColorChooser.
507 public AbstractColorChooserPanel[] getChooserPanels()
509 return chooserPanels;
510 } // getChooserPanels()
513 * This method returns the ColorSelectionModel used with this JColorChooser.
515 * @return The ColorSelectionModel.
517 public ColorSelectionModel getSelectionModel()
519 return selectionModel;
520 } // getSelectionModel()
523 * This method sets the ColorSelectionModel to be used with this
526 * @param model The ColorSelectionModel to be used with this JColorChooser.
528 * @throws AWTError If the given model is null.
530 public void setSelectionModel(ColorSelectionModel model)
533 throw new AWTError("ColorSelectionModel is not allowed to be null.");
534 selectionModel = model;
535 } // setSelectionModel()
540 * @return DOCUMENT ME!
542 public boolean getDragEnabled()
550 * @param b DOCUMENT ME!
552 public void setDragEnabled(boolean b)
558 * This method returns a String describing the JColorChooser.
560 * @return A String describing the JColorChooser.
562 protected String paramString()
564 return "JColorChooser";
568 * getAccessibleContext
570 * @return AccessibleContext
572 public AccessibleContext getAccessibleContext()
574 if (accessibleContext == null)
575 accessibleContext = new AccessibleJColorChooser();
577 return accessibleContext;
581 * A helper class that hides a JDialog when the action is performed.
583 static class DefaultOKCancelListener implements ActionListener
585 /** The JDialog to hide. */
586 private JDialog dialog;
589 * Creates a new DefaultOKCancelListener with the given JDialog to hide.
591 * @param dialog The JDialog to hide.
593 public DefaultOKCancelListener(JDialog dialog)
596 this.dialog = dialog;
600 * This method hides the JDialog when called.
602 * @param e The ActionEvent.
604 public void actionPerformed(ActionEvent e)
611 * This method resets the JColorChooser color to the initial color when the
612 * action is performed.
614 static class DefaultResetListener implements ActionListener
616 /** The JColorChooser to reset. */
617 private JColorChooser chooser;
619 /** The initial color. */
623 * Creates a new DefaultResetListener with the given JColorChooser.
625 * @param chooser The JColorChooser to reset.
627 public DefaultResetListener(JColorChooser chooser)
630 this.chooser = chooser;
631 init = chooser.getColor();
635 * This method resets the JColorChooser to its initial color.
637 * @param e The ActionEvent.
639 public void actionPerformed(ActionEvent e)
641 chooser.setColor(init);
646 * This is a custom JDialog that will notify when it is hidden and the modal
649 static class ModalDialog extends JDialog
651 /** The modal property. */
652 private boolean modal;
655 * Creates a new ModalDialog object with the given parent and title.
657 * @param parent The parent of the JDialog.
658 * @param title The title of the JDialog.
660 public ModalDialog(Frame parent, String title)
662 super(parent, title);
666 * Creates a new ModalDialog object with the given parent and title.
668 * @param parent The parent of the JDialog.
669 * @param title The title of the JDialog.
671 public ModalDialog(Dialog parent, String title)
673 super(parent, title);
677 * This method sets the modal property.
679 * @param modal The modal property.
681 public void setModal(boolean modal)
687 * This method shows the ModalDialog.
697 * This method hides the ModalDialog.
699 public synchronized void hide()