KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > customizer > SplashUISupport


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.apisupport.project.ui.customizer;
21
22 import java.awt.AlphaComposite JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Component JavaDoc;
25 import java.awt.Dialog JavaDoc;
26 import java.awt.Dimension JavaDoc;
27 import java.awt.Graphics JavaDoc;
28 import java.awt.Graphics2D JavaDoc;
29 import java.awt.Rectangle JavaDoc;
30 import java.awt.SystemColor JavaDoc;
31 import java.awt.event.ActionEvent JavaDoc;
32 import java.awt.event.ActionListener JavaDoc;
33 import java.text.NumberFormat JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Locale JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.StringTokenizer JavaDoc;
38 import javax.swing.ComboBoxEditor JavaDoc;
39 import javax.swing.DefaultComboBoxModel JavaDoc;
40 import javax.swing.JColorChooser JavaDoc;
41 import javax.swing.JComboBox JavaDoc;
42 import javax.swing.JComponent JavaDoc;
43 import javax.swing.JFormattedTextField JavaDoc;
44 import javax.swing.JFormattedTextField.AbstractFormatter;
45 import javax.swing.JLabel JavaDoc;
46 import javax.swing.JList JavaDoc;
47 import javax.swing.ListCellRenderer JavaDoc;
48 import javax.swing.SwingUtilities JavaDoc;
49 import javax.swing.text.DefaultFormatter JavaDoc;
50 import org.openide.util.NbBundle;
51
52 /**
53  *
54  * @author Radek Matous
55  */

56 class SplashUISupport {
57     private SplashUISupport() {}
58     
59     static Rectangle JavaDoc stringToBounds(final String JavaDoc bounds) throws NumberFormatException JavaDoc {
60         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(bounds, " ,"); // NOI18N
61
int x, y, width, height;
62         x = y = width = height =0;
63         for (int i = 0; i < 4; i++) {
64             if (!st.hasMoreElements()) {
65                 throw new NumberFormatException JavaDoc();
66             }
67             switch (i) {
68                 case 0:
69                     x = Integer.parseInt(st.nextToken());
70                     break;
71                 case 1:
72                     y = Integer.parseInt(st.nextToken());
73                     break;
74                 case 2:
75                     width = Integer.parseInt(st.nextToken());
76                     break;
77                 case 3:
78                     height = Integer.parseInt(st.nextToken());
79                     break;
80                     
81             }
82         }
83         return new Rectangle JavaDoc(x,y,width,height);
84     }
85     
86     static String JavaDoc boundsToString(final Rectangle JavaDoc bounds) throws NumberFormatException JavaDoc {
87         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
88         sb.append(String.valueOf(bounds.x)).append(",");//NOI18N
89
sb.append(String.valueOf(bounds.y)).append(",");//NOI18N
90
sb.append(String.valueOf(bounds.width)).append(",");//NOI18N
91
sb.append(String.valueOf(bounds.height));//NOI18N
92
return sb.toString();
93     }
94     
95     static Color JavaDoc stringToColor(final String JavaDoc color) throws NumberFormatException JavaDoc {
96         return new Color JavaDoc(Integer.decode(color).intValue());
97     }
98     
99     static String JavaDoc colorToString(final Color JavaDoc color) throws NumberFormatException JavaDoc {
100         return "0x" + Integer.toString((~0xff000000 & color.getRGB()), 16).toUpperCase(Locale.ENGLISH); // NOI18N
101
}
102     
103     static int stringToInteger(final String JavaDoc integer) throws NumberFormatException JavaDoc {
104         return Integer.decode(integer).intValue();
105     }
106     
107     static String JavaDoc integerToString(final int integer) throws NumberFormatException JavaDoc {
108         return Integer.toString(integer, 10);
109     }
110     
111     static JFormattedTextField JavaDoc getIntegerField() {
112         JFormattedTextField JavaDoc retval = new JFormattedTextField JavaDoc(NumberFormat.getIntegerInstance());
113         retval = new JFormattedTextField JavaDoc(new FontFormatter(retval.getFormatter()));
114         return retval;
115     }
116         
117     static JFormattedTextField JavaDoc getBoundsField() {
118         JFormattedTextField JavaDoc retval = new JFormattedTextField JavaDoc(new BoundsFormatter());
119         return retval;
120     }
121     
122     static SplashUISupport.ColorComboBox getColorComboBox() {
123         SplashUISupport.ColorComboBox retval = new SplashUISupport.ColorComboBox();
124         return retval;
125     }
126     
127     private static class FontFormatter extends DefaultFormatter JavaDoc {
128         private AbstractFormatter deleg;
129         FontFormatter(AbstractFormatter deleg) {
130             setOverwriteMode(false);
131             this.deleg = deleg;
132         }
133         public Object JavaDoc stringToValue(String JavaDoc string) throws java.text.ParseException JavaDoc {
134             Object JavaDoc retval = deleg.stringToValue(string);
135             int i = ((Number JavaDoc)retval).intValue();
136             if (i < 0) {
137                 throw new java.text.ParseException JavaDoc(string,0);
138             }
139             return retval;
140         }
141         
142         public String JavaDoc valueToString(Object JavaDoc value) throws java.text.ParseException JavaDoc {
143             return deleg.valueToString(value);
144         }
145     }
146     
147     private static class BoundsFormatter extends DefaultFormatter JavaDoc {
148         BoundsFormatter() {
149             setOverwriteMode(false);
150         }
151         public Object JavaDoc stringToValue(String JavaDoc string) throws java.text.ParseException JavaDoc {
152             if (string == null) {
153                 return super.stringToValue(string);
154             } else {
155                 try {
156                     return SplashUISupport.stringToBounds(string);
157                 } catch (NumberFormatException JavaDoc ex) {
158                     throw new java.text.ParseException JavaDoc(string,0);
159                 }
160             }
161         }
162         
163         public String JavaDoc valueToString(Object JavaDoc value) throws java.text.ParseException JavaDoc {
164             if (value == null) {
165                 return super.valueToString(value);
166             } else {
167                 try {
168                     return SplashUISupport.boundsToString((Rectangle JavaDoc)value);
169                 } catch (NumberFormatException JavaDoc ex) {
170                     throw new java.text.ParseException JavaDoc(value.toString(),0);
171                 }
172             }
173         }
174     }
175     
176     /**
177      * copy & pasted from editor/options org.netbeans.modules.options.colors.ColorComboBox
178      */

179     static class ColorComboBox extends JComboBox JavaDoc {
180         
181         public static final String JavaDoc PROP_COLOR = "color"; // NOI18N
182
public static final Value CUSTOM_COLOR =
183                 new Value(loc("Custom"), null); //NOI18N
184

185         private static Map JavaDoc colorMap = new HashMap JavaDoc();
186         static {
187             colorMap.put(Color.BLACK, loc("Black")); //NOI18N
188
colorMap.put(Color.BLUE, loc("Blue")); //NOI18N
189
colorMap.put(Color.CYAN, loc("Cyan")); //NOI18N
190
colorMap.put(Color.DARK_GRAY, loc("Dark_Gray")); //NOI18N
191
colorMap.put(Color.GRAY, loc("Gray")); //NOI18N
192
colorMap.put(Color.GREEN, loc("Green")); //NOI18N
193
colorMap.put(Color.LIGHT_GRAY, loc("Light_Gray")); //NOI18N
194
colorMap.put(Color.MAGENTA, loc("Magenta")); //NOI18N
195
colorMap.put(Color.ORANGE, loc("Orange")); //NOI18N
196
colorMap.put(Color.PINK, loc("Pink")); //NOI18N
197
colorMap.put(Color.RED, loc("Red")); //NOI18N
198
colorMap.put(Color.WHITE, loc("White")); //NOI18N
199
colorMap.put(Color.YELLOW, loc("Yellow")); //NOI18N
200
}
201         
202         private static Object JavaDoc[] content = new Object JavaDoc[] {
203             new Value(Color.BLACK),
204                     new Value(Color.BLUE),
205                     new Value(Color.CYAN),
206                     new Value(Color.DARK_GRAY),
207                     new Value(Color.GRAY),
208                     new Value(Color.GREEN),
209                     new Value(Color.LIGHT_GRAY),
210                     new Value(Color.MAGENTA),
211                     new Value(Color.ORANGE),
212                     new Value(Color.PINK),
213                     new Value(Color.RED),
214                     new Value(Color.WHITE),
215                     new Value(Color.YELLOW),
216                     CUSTOM_COLOR,
217                     new Value(NbBundle.getMessage(SplashUISupport.class, "SplashUISupport_color_none"), null),
218         };
219         
220         
221         /** Creates a new instance of ColorChooser */
222         public ColorComboBox() {
223             super(content);
224             setRenderer(new Renderer JavaDoc());
225             setEditable(true);
226             setEditor(new Renderer JavaDoc());
227             setSelectedItem(new Value(null, null));
228             addActionListener(new ActionListener JavaDoc() {
229                 public void actionPerformed(ActionEvent JavaDoc ev) {
230                     if (getSelectedItem() == CUSTOM_COLOR) {
231                         Color JavaDoc c = JColorChooser.showDialog(
232                                 SwingUtilities.getAncestorOfClass
233                                 (Dialog JavaDoc.class, ColorComboBox.this),
234                                 loc("SelectColor"), // NOI18N
235
null
236                                 );
237                         setColor(c);
238                     }
239                     ColorComboBox.this.firePropertyChange(PROP_COLOR, null, null);
240                 }
241             });
242         }
243         
244         public void setDefaultColor(Color JavaDoc color) {
245             Object JavaDoc[] ncontent = new Object JavaDoc [content.length];
246             System.arraycopy(content, 0, ncontent, 0, content.length);
247             ncontent [content.length - 1] = new Value(
248                     NbBundle.getMessage(SplashUISupport.class, "SplashUISupport_color_default"), color // NOI18N
249
);
250             setModel(new DefaultComboBoxModel JavaDoc(ncontent));
251         }
252         
253         public void setColor(Color JavaDoc color) {
254             if (color == null)
255                 setSelectedIndex(content.length - 1);
256             else
257                 setSelectedItem(new Value(color));
258         }
259         
260         public Color JavaDoc getColor() {
261             if (getSelectedIndex() == (content.length - 1)) return null;
262             return ((Value) getSelectedItem()).color;
263         }
264         
265         private static String JavaDoc loc(String JavaDoc key) {
266             return NbBundle.getMessage(ColorComboBox.class, key);
267         }
268         
269         
270         // innerclasses ............................................................
271

272         public static class Value {
273             String JavaDoc text;
274             Color JavaDoc color;
275             
276             Value(Color JavaDoc color) {
277                 this.color = color;
278                 text = (String JavaDoc) colorMap.get(color);
279                 if (text != null) return;
280                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
281                 sb.append('[').append(color.getRed()).
282                         append(',').append(color.getGreen()).
283                         append(',').append(color.getBlue()).
284                         append(']');
285                 text = sb.toString();
286             }
287             
288             Value(String JavaDoc text, Color JavaDoc color) {
289                 this.text = text;
290                 this.color = color;
291             }
292         }
293         
294         private static class Editor extends JLabel JavaDoc implements ComboBoxEditor JavaDoc {
295             
296             private Object JavaDoc value;
297             
298             Editor() {
299                 //setOpaque (false);
300
}
301             
302             public Component JavaDoc getEditorComponent() {
303                 return this;
304             }
305             
306             public void setItem(Object JavaDoc anObject) {
307                 value = anObject;
308                 if (value instanceof String JavaDoc) {
309                     setText(NbBundle.getMessage(SplashUISupport.class, "SplashUISupport_color_default"));
310                     super.setForeground(SystemColor.textText);
311                     super.setBackground(SystemColor.text);
312                 } else {
313                     setText("");
314                     super.setBackground((Color JavaDoc) value);
315                 }
316             }
317             
318             public Object JavaDoc getItem() {
319                 return value;
320             }
321             
322             public void setBackground(Color JavaDoc c) {}
323             public void setForeground(Color JavaDoc c) {}
324             
325             public void selectAll() {}
326             public void addActionListener(ActionListener JavaDoc l) {}
327             public void removeActionListener(ActionListener JavaDoc l) {}
328         }
329         
330         private class Renderer extends JComponent JavaDoc implements
331                 ListCellRenderer JavaDoc, ComboBoxEditor JavaDoc {
332             
333             private int SIZE = 9;
334             private Value value;
335             
336             Renderer() {
337                 setPreferredSize(new Dimension JavaDoc(
338                         50, getFontMetrics(ColorComboBox.this.getFont()).getHeight() + 2
339                         ));
340                 setOpaque(true);
341             }
342             
343             public void paint(Graphics JavaDoc g) {
344                 Graphics2D JavaDoc g2d = (Graphics2D JavaDoc)g;
345                 if (!isEnabled()) {
346                     g2d.setComposite(AlphaComposite.getInstance(
347                             AlphaComposite.SRC_OVER, 0.3f));
348                 }
349                 Color JavaDoc oldColor = g.getColor();
350                 Dimension JavaDoc size = getSize();
351                 g.setColor(getBackground());
352                 g.fillRect(0, 0, size.width, size.height);
353                 int i = (size.height - SIZE) / 2;
354                 if (value.color != null) {
355                     g.setColor(Color.black);
356                     g.drawRect(i, i, SIZE, SIZE);
357                     g.setColor(value.color);
358                     g.fillRect(i + 1, i + 1, SIZE - 1, SIZE - 1);
359                 }
360                 if (value.text != null) {
361                     g.setColor(Color.black);
362                     if (value.color != null)
363                         g.drawString(value.text, i + SIZE + 5, i + SIZE);
364                     else
365                         g.drawString(value.text, 5, i + SIZE);
366                 }
367                 g.setColor(oldColor);
368             }
369             
370             public void setEnabled(boolean enabled) {
371                 setBackground(enabled ?
372                     SystemColor.text : SystemColor.control
373                         );
374                 super.setEnabled(enabled);
375             }
376             
377             public Component JavaDoc getListCellRendererComponent(
378                     JList JavaDoc list,
379                     Object JavaDoc value,
380                     int index,
381                     boolean isSelected,
382                     boolean cellHasFocus
383                     ) {
384                 this.value = (Value) value;
385                 setEnabled(list.isEnabled());
386                 return this;
387             }
388             
389             public Component JavaDoc getEditorComponent() {
390                 setEnabled(ColorComboBox.this.isEnabled());
391                 return this;
392             }
393             
394             public void setItem(Object JavaDoc anObject) {
395                 this.value = (Value) anObject;
396             }
397             
398             public Object JavaDoc getItem() {
399                 return value;
400             }
401             public void selectAll() {}
402             public void addActionListener(ActionListener JavaDoc l) {}
403             public void removeActionListener(ActionListener JavaDoc l) {} }
404     }
405     
406 }
407
Popular Tags