KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > colors > ColorComboBox


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.options.colors;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Dialog JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Font JavaDoc;
27 import java.awt.FontMetrics JavaDoc;
28 import java.awt.Graphics JavaDoc;
29 import java.awt.SystemColor JavaDoc;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.ActionListener JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34 import javax.swing.ComboBoxEditor JavaDoc;
35 import javax.swing.DefaultComboBoxModel JavaDoc;
36 import javax.swing.DefaultListCellRenderer JavaDoc;
37 import javax.swing.JColorChooser JavaDoc;
38 import javax.swing.JComboBox JavaDoc;
39 import javax.swing.JComponent JavaDoc;
40 import javax.swing.JLabel JavaDoc;
41 import javax.swing.JList JavaDoc;
42 import javax.swing.JPanel JavaDoc;
43 import javax.swing.JTextField JavaDoc;
44 import javax.swing.ListCellRenderer JavaDoc;
45 import javax.swing.SwingUtilities JavaDoc;
46 import javax.swing.UIManager JavaDoc;
47 import org.openide.util.NbBundle;
48
49
50 /**
51  *
52  * @author Administrator
53  */

54 public class ColorComboBox {
55     
56     public static final String JavaDoc PROP_COLOR = "color"; //NOI18N
57

58     private static Object JavaDoc[] content = new Object JavaDoc[] {
59     new ColorValue (Color.BLACK),
60     new ColorValue (Color.BLUE),
61     new ColorValue (Color.CYAN),
62     new ColorValue (Color.DARK_GRAY),
63     new ColorValue (Color.GRAY),
64     new ColorValue (Color.GREEN),
65     new ColorValue (Color.LIGHT_GRAY),
66     new ColorValue (Color.MAGENTA),
67     new ColorValue (Color.ORANGE),
68     new ColorValue (Color.PINK),
69     new ColorValue (Color.RED),
70     new ColorValue (Color.WHITE),
71     new ColorValue (Color.YELLOW),
72     ColorValue.CUSTOM_COLOR,
73     new ColorValue (loc ("CTL_None_Color"), null) //NOI18N
74
};
75     
76     
77     /** Creates a new instance of ColorChooser */
78     static void init (final JComboBox JavaDoc combo) {
79         combo.setModel (new DefaultComboBoxModel JavaDoc (content));
80         combo.setRenderer (new ColorComboBoxRenderer (combo));
81         combo.setEditable (true);
82         combo.setEditor (new ColorComboBoxRenderer (combo));
83     combo.setSelectedItem (new ColorValue (null, null));
84         combo.addActionListener (new ComboBoxListener (combo));
85     }
86     
87     static void setInheritedColor (JComboBox JavaDoc combo, Color JavaDoc color) {
88     Object JavaDoc[] ncontent = new Object JavaDoc [content.length];
89     System.arraycopy (content, 0, ncontent, 0, content.length);
90         if (color != null)
91             ncontent [content.length - 1] = new ColorValue (
92                 loc ("CTL_Inherited_Color"), color //NOI18N
93
);
94         else
95             ncontent [content.length - 1] = new ColorValue (
96                 loc ("CTL_None_Color"), null //NOI18N
97
);
98     combo.setModel (new DefaultComboBoxModel JavaDoc (ncontent));
99     }
100     
101     static void setColor (JComboBox JavaDoc combo, Color JavaDoc color) {
102         if (color == null) {
103             combo.setSelectedIndex (content.length - 1);
104         } else {
105             combo.setSelectedItem (new ColorValue (color));
106         }
107     }
108     
109     static Color JavaDoc getColor (JComboBox JavaDoc combo) {
110     if (combo.getSelectedIndex () == (content.length - 1)) return null;
111         return ((ColorValue) combo.getSelectedItem ()).color;
112     }
113     
114     private static String JavaDoc loc (String JavaDoc key) {
115         return NbBundle.getMessage (ColorComboBox.class, key);
116     }
117     
118     // ..........................................................................
119
private static class ComboBoxListener implements ActionListener JavaDoc {
120         
121         private JComboBox JavaDoc combo;
122         private Object JavaDoc lastSelection;
123         
124         ComboBoxListener(JComboBox JavaDoc combo) {
125             this.combo = combo;
126             lastSelection = combo.getSelectedItem();
127         }
128         
129         public void actionPerformed(ActionEvent JavaDoc ev) {
130             if (combo.getSelectedItem() == ColorValue.CUSTOM_COLOR) {
131                 Color JavaDoc c = JColorChooser.showDialog(
132                         SwingUtilities.getAncestorOfClass
133                         (Dialog JavaDoc.class, combo),
134                         loc("SelectColor"),
135                         null
136                         );
137                 if (c != null) {
138                     setColor (combo, c);
139                 } else if (lastSelection != null) {
140                     combo.setSelectedItem(lastSelection);
141                 }
142             }
143             lastSelection = combo.getSelectedItem();
144         }
145         
146     } // ComboListener
147

148 }
149
Popular Tags