OSDN Git Service

* javax/naming/CompoundName.java (CompoundName): Don't check for
[pf3gnuchains/gcc-fork.git] / libjava / javax / swing / JColorChooser.java
1 /* JColorChooser.java --
2    Copyright (C) 2002, 2004 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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)
9 any later version.
10
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.
15
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
19 02111-1307 USA.
20
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
24 combination.
25
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. */
37
38 package javax.swing;
39
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;
59
60
61 /**
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.
69  */
70 public class JColorChooser extends JComponent implements Accessible
71 {
72   /** DOCUMENT ME! */
73   private static final long serialVersionUID = 9168066781620640889L;
74
75   /**
76    * AccessibleJColorChooser
77    */
78   protected class AccessibleJColorChooser
79     extends JComponent.AccessibleJComponent
80   {
81     /** DOCUMENT ME! */
82     private static final long serialVersionUID = -2038297864782299082L;
83
84     /**
85      * Constructor AccessibleJColorChooser
86      */
87     protected AccessibleJColorChooser()
88     {
89     }
90
91     /**
92      * getAccessibleRole
93      *
94      * @return AccessibleRole
95      */
96     public AccessibleRole getAccessibleRole()
97     {
98       return AccessibleRole.COLOR_CHOOSER;
99     } // getAccessibleRole()
100   } // AccessibleJColorChooser
101
102   /** The model used with the JColorChooser. */
103   private ColorSelectionModel selectionModel;
104
105   /** The preview panel associated with the JColorChooser. */
106   private JComponent previewPanel;
107
108   /**
109    * The set of AbstractColorChooserPanels associated with the JColorChooser.
110    */
111   private AbstractColorChooserPanel[] chooserPanels;
112
113   /** A Drag and Drop property. */
114   private boolean dragEnabled;
115
116   /**
117    * The property fired by the JColorChooser when the selectionModel property
118    * changes.
119    */
120   public static final String SELECTION_MODEL_PROPERTY = "selectionModel";
121
122   /**
123    * The property fired by the JColorChooser when the previewPanel property
124    * changes.
125    */
126   public static final String PREVIEW_PANEL_PROPERTY = "previewPanel";
127
128   /**
129    * The property fired by the JColorChooser when the chooserPanels property
130    * changes.
131    */
132   public static final String CHOOSER_PANELS_PROPERTY = "chooserPanels";
133
134   /** accessibleContext */
135   protected AccessibleContext accessibleContext;
136
137   /**
138    * This method creates a new JColorChooser with the default initial color.
139    */
140   public JColorChooser()
141   {
142     this(new DefaultColorSelectionModel());
143   } // JColorChooser()
144
145   /**
146    * This method creates a new JColorChooser with the given initial color.
147    *
148    * @param initial The initial color.
149    */
150   public JColorChooser(Color initial)
151   {
152     this(new DefaultColorSelectionModel(initial));
153   } // JColorChooser()
154
155   /**
156    * This method creates a new JColorChooser with the given model. The model
157    * will dictate what the initial color for the JColorChooser is.
158    *
159    * @param model The Model to use with the JColorChooser.
160    */
161   public JColorChooser(ColorSelectionModel model)
162   {
163     if (model == null)
164       model = new DefaultColorSelectionModel();
165     selectionModel = model;
166     updateUI();
167   } // JColorChooser()
168
169   /**
170    * This method sets the current color for the JColorChooser.
171    *
172    * @param color The new color for the JColorChooser.
173    */
174   public void setColor(Color color)
175   {
176     if (color != null)
177       selectionModel.setSelectedColor(color);
178   } // setColor()
179
180   /**
181    * This method sets the current color for the JColorChooser using RGB
182    * values.
183    *
184    * @param r The red value.
185    * @param g The green value.
186    * @param b The blue value.
187    */
188   public void setColor(int r, int g, int b)
189   {
190     selectionModel.setSelectedColor(new Color(r, g, b));
191   } // setColor()
192
193   /**
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.
197    *
198    * @param color The new current color of the JColorChooser.
199    */
200   public void setColor(int color)
201   {
202     setColor(new Color(color, false));
203   } // setColor()
204
205   /**
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.
210    *
211    * @param component The Component that parents the JDialog.
212    * @param title The title displayed in the JDialog.
213    * @param initial The initial color.
214    *
215    * @return The selected color.
216    */
217   public static Color showDialog(Component component, String title,
218                                  Color initial)
219   {
220     JColorChooser choose = new JColorChooser(initial);
221
222     JDialog dialog = createDialog(component, title, true, choose, null, null);
223
224     dialog.getContentPane().add(choose);
225     dialog.pack();
226     dialog.show();
227
228     return choose.getColor();
229   } // showDialog()
230
231   /**
232    * This is a helper method to make the given JDialog block until it is
233    * hidden.
234    *
235    * @param dialog The JDialog to block.
236    */
237   private static void makeModal(JDialog dialog)
238   {
239     try
240       {
241         synchronized (dialog)
242           {
243             while (dialog.isVisible())
244               dialog.wait();
245           }
246       }
247     catch (InterruptedException e)
248       {
249       }
250   }
251
252   /**
253    * This is a helper method to find the first Frame or Dialog ancestor of the
254    * given Component.
255    *
256    * @param c The Component to find ancestors for.
257    *
258    * @return A Frame or Dialog ancestor. Null if none are found.
259    */
260   private static Component findParent(Component c)
261   {
262     Component parent = SwingUtilities.getAncestorOfClass(Frame.class, c);
263     if (parent != null)
264       return parent;
265     parent = SwingUtilities.getAncestorOfClass(Dialog.class, c);
266     return parent;
267   }
268
269   /**
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
277    * hidden.
278    *
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
285    *        button.
286    *
287    * @return A JDialog with the JColorChooser inside of it.
288    *
289    * @throws AWTError If the component is not a suitable parent.
290    */
291   public static JDialog createDialog(Component component, String title,
292                                      boolean modal, JColorChooser chooserPane,
293                                      ActionListener okListener,
294                                      ActionListener cancelListener)
295   {
296     Component parent = findParent(component);
297     if (parent == null)
298       throw new AWTError("No suitable parent found for Component.");
299     JDialog dialog;
300     if (parent instanceof Frame)
301       dialog = new ModalDialog((Frame) parent, title);
302     else
303       dialog = new ModalDialog((Dialog) parent, title);
304     dialog.setModal(modal);
305
306     dialog.getContentPane().setLayout(new BorderLayout());
307
308     JPanel panel = new JPanel();
309     panel.setLayout(new FlowLayout());
310
311     ActionListener al = new DefaultOKCancelListener(dialog);
312
313     JButton ok = new JButton("OK");
314     ok.addActionListener(okListener);
315     ok.addActionListener(al);
316
317     JButton cancel = new JButton("Cancel");
318     cancel.addActionListener(cancelListener);
319     cancel.addActionListener(al);
320
321     JButton reset = new JButton("Reset");
322     reset.addActionListener(new DefaultResetListener(chooserPane));
323
324     dialog.getContentPane().add(chooserPane, BorderLayout.NORTH);
325
326     panel.add(ok);
327     panel.add(cancel);
328     panel.add(reset);
329
330     dialog.getContentPane().add(panel, BorderLayout.SOUTH);
331
332     return dialog;
333   } // createDialog()
334
335   /**
336    * This method returns the UI Component used for this JColorChooser.
337    *
338    * @return The UI Component for this JColorChooser.
339    */
340   public ColorChooserUI getUI()
341   {
342     return (ColorChooserUI) ui;
343   } // getUI()
344
345   /**
346    * This method sets the UI Component used for this JColorChooser.
347    *
348    * @param ui The UI Component to use with this JColorChooser.
349    */
350   public void setUI(ColorChooserUI ui)
351   {
352     super.setUI(ui);
353   } // setUI()
354
355   /**
356    * This method resets the UI Component property to the Look and Feel
357    * default.
358    */
359   public void updateUI()
360   {
361     setUI((ColorChooserUI) UIManager.getUI(this));
362     revalidate();
363   } // updateUI()
364
365   /**
366    * This method returns a String identifier for the UI Class to be used with
367    * the JColorChooser.
368    *
369    * @return The String identifier for the UI Class.
370    */
371   public String getUIClassID()
372   {
373     return "ColorChooserUI";
374   } // getUIClassID()
375
376   /**
377    * This method returns the current color for the JColorChooser.
378    *
379    * @return The current color for the JColorChooser.
380    */
381   public Color getColor()
382   {
383     return selectionModel.getSelectedColor(); // TODO
384   } // getColor()
385
386   /**
387    * This method changes the previewPanel property for the JTabbedPane. The
388    * previewPanel is responsible for indicating the current color of the
389    * JColorChooser.
390    *
391    * @param component The Component that will act as the previewPanel.
392    */
393   public void setPreviewPanel(JComponent component)
394   {
395     if (component != previewPanel)
396       {
397         JComponent old = previewPanel;
398         previewPanel = component;
399         firePropertyChange(PREVIEW_PANEL_PROPERTY, old, previewPanel);
400       }
401   } // setPreviewPanel()
402
403   /**
404    * This method returns the current previewPanel used with this
405    * JColorChooser.
406    *
407    * @return The current previewPanel.
408    */
409   public JComponent getPreviewPanel()
410   {
411     return previewPanel; // TODO
412   } // getPreviewPanel()
413
414   /**
415    * This method adds the given AbstractColorChooserPanel to the list of the
416    * JColorChooser's chooserPanels.
417    *
418    * @param panel The AbstractColorChooserPanel to add.
419    */
420   public void addChooserPanel(AbstractColorChooserPanel panel)
421   {
422     if (panel == null)
423       return;
424     AbstractColorChooserPanel[] old = chooserPanels;
425     AbstractColorChooserPanel[] newPanels = new AbstractColorChooserPanel[(old == null)
426                                                                           ? 1
427                                                                           : old.length
428                                                                           + 1];
429     if (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()
436
437   /**
438    * This method removes the given AbstractColorChooserPanel from the
439    * JColorChooser's list of chooserPanels.
440    *
441    * @param panel The AbstractColorChooserPanel to remove.
442    *
443    * @return The AbstractColorChooserPanel that was removed.
444    */
445   public AbstractColorChooserPanel removeChooserPanel(AbstractColorChooserPanel panel)
446   {
447     int index = -1;
448     for (int i = 0; i < chooserPanels.length; i++)
449       if (panel == chooserPanels[i])
450         {
451           index = i;
452           break;
453         }
454
455     if (index == -1)
456       return null;
457
458     AbstractColorChooserPanel[] old = chooserPanels;
459     if (chooserPanels.length == 1)
460       chooserPanels = null;
461     else
462       {
463         AbstractColorChooserPanel[] newPanels = new AbstractColorChooserPanel[chooserPanels.length
464                                                 - 1];
465         System.arraycopy(chooserPanels, 0, newPanels, 0, index);
466         System.arraycopy(chooserPanels, index, newPanels, index - 1,
467                          chooserPanels.length - index);
468         chooserPanels = newPanels;
469       }
470     panel.uninstallChooserPanel(this);
471     firePropertyChange(CHOOSER_PANELS_PROPERTY, old, chooserPanels);
472     return panel;
473   }
474
475   /**
476    * This method sets the chooserPanels property for this JColorChooser.
477    *
478    * @param panels The new set of AbstractColorChooserPanels to use.
479    */
480   public void setChooserPanels(AbstractColorChooserPanel[] panels)
481   {
482     if (panels != chooserPanels)
483       {
484         if (chooserPanels != null)
485           for (int i = 0; i < chooserPanels.length; i++)
486             if (chooserPanels[i] != null)
487               chooserPanels[i].uninstallChooserPanel(this);
488
489         AbstractColorChooserPanel[] old = chooserPanels;
490         chooserPanels = panels;
491
492         if (panels != null)
493           for (int i = 0; i < panels.length; i++)
494             if (panels[i] != null)
495               panels[i].installChooserPanel(this);
496
497         firePropertyChange(CHOOSER_PANELS_PROPERTY, old, chooserPanels);
498       }
499   } // setChooserPanels()
500
501   /**
502    * This method returns the AbstractColorChooserPanels used with this
503    * JColorChooser.
504    *
505    * @return The AbstractColorChooserPanels used with this JColorChooser.
506    */
507   public AbstractColorChooserPanel[] getChooserPanels()
508   {
509     return chooserPanels;
510   } // getChooserPanels()
511
512   /**
513    * This method returns the ColorSelectionModel used with this JColorChooser.
514    *
515    * @return The ColorSelectionModel.
516    */
517   public ColorSelectionModel getSelectionModel()
518   {
519     return selectionModel;
520   } // getSelectionModel()
521
522   /**
523    * This method sets the ColorSelectionModel to be used with this
524    * JColorChooser.
525    *
526    * @param model The ColorSelectionModel to be used with this JColorChooser.
527    *
528    * @throws AWTError If the given model is null.
529    */
530   public void setSelectionModel(ColorSelectionModel model)
531   {
532     if (model == null)
533       throw new AWTError("ColorSelectionModel is not allowed to be null.");
534     selectionModel = model;
535   } // setSelectionModel()
536
537   /**
538    * DOCUMENT ME!
539    *
540    * @return DOCUMENT ME!
541    */
542   public boolean getDragEnabled()
543   {
544     return dragEnabled;
545   }
546
547   /**
548    * DOCUMENT ME!
549    *
550    * @param b DOCUMENT ME!
551    */
552   public void setDragEnabled(boolean b)
553   {
554     dragEnabled = b;
555   }
556
557   /**
558    * This method returns a String describing the JColorChooser.
559    *
560    * @return A String describing the JColorChooser.
561    */
562   protected String paramString()
563   {
564     return "JColorChooser";
565   } // paramString()
566
567   /**
568    * getAccessibleContext
569    *
570    * @return AccessibleContext
571    */
572   public AccessibleContext getAccessibleContext()
573   {
574     if (accessibleContext == null)
575       accessibleContext = new AccessibleJColorChooser();
576
577     return accessibleContext;
578   }
579
580   /**
581    * A helper class that hides a JDialog when the action is performed.
582    */
583   static class DefaultOKCancelListener implements ActionListener
584   {
585     /** The JDialog to hide. */
586     private JDialog dialog;
587
588     /**
589      * Creates a new DefaultOKCancelListener with the given JDialog to hide.
590      *
591      * @param dialog The JDialog to hide.
592      */
593     public DefaultOKCancelListener(JDialog dialog)
594     {
595       super();
596       this.dialog = dialog;
597     }
598
599     /**
600      * This method hides the JDialog when called.
601      *
602      * @param e The ActionEvent.
603      */
604     public void actionPerformed(ActionEvent e)
605     {
606       dialog.hide();
607     }
608   }
609
610   /**
611    * This method resets the JColorChooser color to the initial color when the
612    * action is performed.
613    */
614   static class DefaultResetListener implements ActionListener
615   {
616     /** The JColorChooser to reset. */
617     private JColorChooser chooser;
618
619     /** The initial color. */
620     private Color init;
621
622     /**
623      * Creates a new DefaultResetListener with the given JColorChooser.
624      *
625      * @param chooser The JColorChooser to reset.
626      */
627     public DefaultResetListener(JColorChooser chooser)
628     {
629       super();
630       this.chooser = chooser;
631       init = chooser.getColor();
632     }
633
634     /**
635      * This method resets the JColorChooser to its initial color.
636      *
637      * @param e The ActionEvent.
638      */
639     public void actionPerformed(ActionEvent e)
640     {
641       chooser.setColor(init);
642     }
643   }
644
645   /**
646    * This is a custom JDialog that will notify when it is hidden and the modal
647    * property is set.
648    */
649   static class ModalDialog extends JDialog
650   {
651     /** The modal property. */
652     private boolean modal;
653
654     /**
655      * Creates a new ModalDialog object with the given parent and title.
656      *
657      * @param parent The parent of the JDialog.
658      * @param title The title of the JDialog.
659      */
660     public ModalDialog(Frame parent, String title)
661     {
662       super(parent, title);
663     }
664
665     /**
666      * Creates a new ModalDialog object with the given parent and title.
667      *
668      * @param parent The parent of the JDialog.
669      * @param title The title of the JDialog.
670      */
671     public ModalDialog(Dialog parent, String title)
672     {
673       super(parent, title);
674     }
675
676     /**
677      * This method sets the modal property.
678      *
679      * @param modal The modal property.
680      */
681     public void setModal(boolean modal)
682     {
683       this.modal = modal;
684     }
685
686     /**
687      * This method shows the ModalDialog.
688      */
689     public void show()
690     {
691       super.show();
692       if (modal)
693         makeModal(this);
694     }
695
696     /**
697      * This method hides the ModalDialog.
698      */
699     public synchronized void hide()
700     {
701       super.hide();
702       notifyAll();
703     }
704   }
705 }