View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common 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 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.util;
21  
22  import java.awt.Color;
23  import java.awt.Component;
24  import java.awt.Container;
25  
26  import javax.swing.UIManager;
27  import javax.swing.plaf.metal.MetalLookAndFeel;
28  
29  
30  /**
31   * Helper class that provides simple access to most often
32   * used Color UI resources
33   *
34   * @author <a href="mailto:jernej.kamenik@cosylab.com">Jernej Kamenik</a>
35   * @version $id$
36   *
37   * @see javax.swing.plaf.metal.MetalLookAndFeel
38   */
39  public final class ColorHelper {
40      public final static String[] COLOR_NAMES = new String[] {
41              "Alarm", "AlarmOutline", "Control", "ControlDarkShadow",
42              "ControlHighlight", "ControlShadow", "ControlText", "CosyControl",
43              "CosyControlDarkShadow", "CosyControlHighlight", "CosyControlShadow",
44              "CosyControlText", "CosyErrorText", "CosyInputBackground",
45              "CosyOverlay", "Error", "EmergencyOutline", "Emergency", "Focus",
46              "Hyperlink", "Text", "TextHighlight", "TimeOut", "Warning",
47              "WarningOutline", "WindowBackground"
48          };
49      public final static String[] JAVA_COLOR_NAMES = new String[] {
50              "BLACK", "BLUE", "CYAN", "DARK_GRAY", "GRAY", "GREEN", "LIGHT_GRAY",
51              "MAGENTA", "ORANGE", "PINK", "RED", "WHITE", "YELLOW"
52          };
53      private final static Color COSY_CONTROL = new Color(206, 206, 227);
54      private final static Color COSY_CONTROL_SHADOW = new Color(126, 126, 177);
55      private final static Color COSY_CONTROL_HIGHLIGHT = new Color(246, 246, 255);
56      private final static Color COSY_CONTROL_DARK_SHADOW = new Color(86, 86, 157);
57      private final static Color HYPERLINK = new Color(0, 0, 255);
58      private final static Color ALARM = new Color(255, 128, 128);
59      private final static Color ALARM_OUTLINE = new Color(255, 0, 0);
60      private final static Color EMERGENCY = new Color(255, 64, 64);
61      private final static Color EMERGENCY_OUTLINE = new Color(255, 0, 0);
62      private final static Color WARNING = new Color(255, 255, 0);
63      private final static Color WARNING_OUTLINE = new Color(232, 216, 0);
64      private final static Color TIMEOUT = new Color(242, 0, 218);
65      private final static Color TIMEOUT_OUTLINE = new Color(242, 121, 230);
66      private final static Color ALARM_INVALID = new Color(242, 0, 200);
67      private final static Color ALARM_MAJOR = new Color(255, 0, 0);
68      private final static Color ALARM_MINOR = new Color(255, 235, 0);
69      private final static Color ALARM_INVALID_BACKGROUND = new Color(242, 124, 222);
70      private final static Color ALARM_MAJOR_BACKGROUND = new Color(255, 96, 96);
71      private final static Color ALARM_MINOR_BACKGROUND = new Color(255, 248, 40);
72      
73  
74      /**
75       * Returns the default control Color, which should be
76      * used to render the background of controls used in
77      * applications
78      *
79       * @return Control color
80       */
81      public static Color getControl() {
82          Color ret = UIManager.getColor("control");
83  
84          if (ret != null) {
85              return ret;
86          }
87  
88          return MetalLookAndFeel.getControl();
89      }
90  
91      /**
92       * Returns the default control shadow Color
93       *
94       * @return Color
95       */
96      public static Color getControlShadow() {
97          Color ret = UIManager.getColor("controlShadow");
98  
99          if (ret != null) {
100             return ret;
101         }
102 
103         return MetalLookAndFeel.getControlDarkShadow();
104     }
105 
106     /**
107      * Returns the default control dark shadow Color
108      *
109      * @return Color
110      */
111     public static Color getControlDarkShadow() {
112         Color ret = UIManager.getColor("controlDkShadow");
113 
114         if (ret != null) {
115             return ret;
116         }
117 
118         return MetalLookAndFeel.getControlDarkShadow();
119     }
120 
121     /**
122      * Returns the default control highlight Color
123      *
124      * @return Color
125      */
126     public static Color getControlHighlight() {
127         Color ret = UIManager.getColor("controlHighlight");
128 
129         if (ret != null) {
130             return ret;
131         }
132 
133         return MetalLookAndFeel.getControlHighlight();
134     }
135 
136     /**
137      * Returns the default control highlight Color
138      *
139      * @return Color
140      */
141     public static Color getControlLightHighlight() {
142         Color ret = UIManager.getColor("controlLtHighlight");
143 
144         if (ret != null) {
145             return ret;
146         }
147 
148         return MetalLookAndFeel.getControlHighlight();
149     }
150 
151     /**
152      * Returns the default control text Color
153      *
154      * @return Color
155      */
156     public static final Color getControlText() {
157         Color ret = UIManager.getColor("controlText");
158 
159         if (ret != null) {
160             return ret;
161         }
162 
163         return MetalLookAndFeel.getControlTextColor();
164     }
165 
166     /**
167      * Returns the default window background Color
168      *
169      * @return Color
170      */
171     public static Color getWindowBackground() {
172         Color ret = UIManager.getColor("window");
173 
174         if (ret != null) {
175             return ret;
176         }
177 
178         return MetalLookAndFeel.getWindowBackground();
179     }
180 
181     /**
182      * Returns the default text Color
183      *
184      * @return Color
185      */
186     public static Color getText() {
187         Color ret = UIManager.getColor("windowText");
188 
189         if (ret != null) {
190             return ret;
191         }
192 
193         return MetalLookAndFeel.getUserTextColor();
194     }
195 
196     /**
197      * Returns the default text highlight Color
198      *
199      * @return Color
200      */
201     public static Color getTextHighlight() {
202         Color ret = UIManager.getColor("textHighlight");
203 
204         if (ret != null) {
205             return ret;
206         }
207 
208         return MetalLookAndFeel.getTextHighlightColor();
209     }
210 
211 	/**
212 	 * @return
213 	 */
214 	public static Color getTextHighlightText() {
215 		Color ret = UIManager.getColor("textHighlightText");
216 
217 		if (ret != null) {
218 			return ret;
219 		}
220 
221 		return MetalLookAndFeel.getUserTextColor();
222 	}    
223     
224     /**
225      * Returns the default focus Color
226      *
227      * @return Color
228      * @see javax.swing.plaf.metal.MetalLookAndFeel#getFocusColor()
229      */
230     public static Color getFocus() {
231         //return UIManager.getColor("controlShadow");		
232         return MetalLookAndFeel.getFocusColor();
233     }
234 
235     /**
236      * Returns the default CosyBeans control Color
237      *
238      * @return Color
239      */
240     public static Color getCosyControl() {
241         return COSY_CONTROL;
242     }
243 
244     /**
245      * Returns the default CosyBeans control highlight Color
246      *
247      * @return Color
248      */
249     public static Color getCosyControlHighlight() {
250         return COSY_CONTROL_HIGHLIGHT;
251     }
252 
253     /**
254      * Returns the default CosyBeans control shadow Color
255      *
256      * @return Color
257      */
258     public static Color getCosyControlShadow() {
259         return COSY_CONTROL_SHADOW;
260     }
261 
262     /**
263      * Returns the default CosyBeans control dark shadow Color
264      *
265      * @return Color
266      */
267     public static Color getCosyControlDarkShadow() {
268         return COSY_CONTROL_DARK_SHADOW;
269     }
270 
271     /**
272      * Returns the default CosyBenas control text Color
273      *
274      * @return Color
275      */
276     public static Color getCosyControlText() {
277         return Color.BLACK;
278     }
279 
280     /**
281      * Returns the default CosyBeans input background Color
282      *
283      * @return Color
284      */
285     public static Color getCosyInputBackground() {
286         return MetalLookAndFeel.getWindowBackground();
287     }
288 
289     /**
290      * Returns the default CosyBeans error text Color
291      *
292      * @return Color
293      */
294     public static Color getCosyErrorText() {
295         return Color.RED;
296     }
297 
298     /**
299      * Returns the default CosyBeans overlay Color
300      *
301      * @return Color
302      */
303     public static Color getCosyOverlay() {
304         return Color.GRAY;
305     }
306 
307     /**
308      * Returns the default CosyBeans timeout Color.
309      *
310      * @return Color
311      */
312     public static Color getTimeOut() {
313         return TIMEOUT;
314     }
315 
316     public static Color getTimeOutOutline() {
317         return TIMEOUT_OUTLINE;
318     }
319 
320     /**
321      * Returns the default CosyBeans warning Color.
322      *
323      * @return Color
324      */
325     public static Color getWarning() {
326         return WARNING;
327     }
328 
329     /**
330      * Returns the default CosyBeans warning outline Color.
331      *
332      * @return Color
333      */
334     public static Color getWarningOutline() {
335         return WARNING_OUTLINE;
336     }
337 
338     /**
339      * Returns the default Color used to render errors.
340      *
341      * @return Color
342      */
343     public static Color getError() {
344         return Color.RED;
345     }
346 
347     /**
348      * Returns the default CosyBeans alarm Color.
349      *
350      * @return Color that should be used to render alarm states
351      */
352     public static Color getAlarm() {
353         return ALARM;
354     }
355 
356     /**
357      * Returns the default CosyBeans alarm Color.
358      *
359      * @return Color that should be used to render alarm states
360      */
361     public static Color getAlarmInvalid() {
362         return ALARM_INVALID;
363     }
364 
365     /**
366      * Returns the default CosyBeans alarm Color.
367      *
368      * @return Color that should be used to render alarm states
369      */
370     public static Color getAlarmMajor() {
371         return ALARM_MAJOR;
372     }
373     /**
374      * Returns the default CosyBeans alarm Color.
375      *
376      * @return Color that should be used to render alarm states
377      */
378     public static Color getAlarmMinor() {
379         return ALARM_MINOR;
380     }
381 
382     /**
383      * Returns the default CosyBeans alarm Color.
384      *
385      * @return Color that should be used to render alarm states
386      */
387     public static Color getAlarmMinorBackground() {
388         return ALARM_MINOR_BACKGROUND;
389     }
390     /**
391      * Returns the default CosyBeans alarm Color.
392      *
393      * @return Color that should be used to render alarm states
394      */
395     public static Color getAlarmInvalidBackground() {
396         return ALARM_INVALID_BACKGROUND;
397     }
398 
399     /**
400      * Returns the default CosyBeans alarm Color.
401      *
402      * @return Color that should be used to render alarm states
403      */
404     public static Color getAlarmMajorBackground() {
405         return ALARM_MAJOR_BACKGROUND;
406     }
407 
408     /**
409      * Returns the default CosyBeans alarm outline Color.
410      *
411      * @return Color that should be used to render outline of alarm symbols
412      */
413     public static Color getAlarmOutline() {
414         return ALARM_OUTLINE;
415     }
416 
417     /**
418      * Returns the default CosyBeans emergency Color.
419      *
420      * @return Color
421      */
422     public static Color getEmergency() {
423         return EMERGENCY;
424     }
425 
426     /**
427      * Returns the default CosyBeans emergency outline Color.
428      *
429      * @return Color
430      */
431     public static Color getEmergencyOutline() {
432         return EMERGENCY_OUTLINE;
433     }
434 
435     public static Color getHyperlink() {
436         return HYPERLINK;
437     }
438     
439     /**
440      * Recursively sets the background color to all components. If one of the component is 
441      * a Container, background is set for all components inside that container.
442      * 
443      * @param components components to set background on
444      * @param background background color
445      */
446     public static void setBackgroundForComponents(Component[] components, Color background) {
447     	for (Component c : components) {
448     		c.setBackground(background);
449     		if (c instanceof Container) {
450     			setBackgroundForComponents(((Container)c).getComponents(), background);
451     		}
452     	}
453     }
454     
455     /**
456      * Recursively sets the foreground color to all components. If one of the component is 
457      * a Container, foreground is set for all components inside that container.
458      * 
459      * @param components components to set foreground on
460      * @param foreground foreground color
461      */
462     public static void setForegroundForComponents(Component[] components, Color foreground) {
463     	for (Component c : components) {
464     		c.setForeground(foreground);
465     		if (c instanceof Container) {
466     			setForegroundForComponents(((Container)c).getComponents(), foreground);
467     		}
468     	}
469     }
470 
471 }