KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > gtk > GTKDefaultStyle


1 /*
2  * GTKDefaultStyle.java
3  *
4  * @(#)GTKDefaultStyle.java 1.1 05/08/05
5  *
6  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
7  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
8  */

9
10 package com.sun.java.swing.plaf.gtk;
11
12 import java.awt.*;
13 import javax.swing.*;
14 import javax.swing.plaf.*;
15 import javax.swing.plaf.synth.*;
16
17 /**
18  *
19  * @version 1.1, 08/05/05
20  * @author Kirill Kirichenko
21  */

22 class GTKDefaultStyle extends GTKStyle {
23     
24     private static final int DEFAULT_XTHICKNESS = 2;
25     private static final int DEFAULT_YTHICKNESS = 2;
26     
27     private static final Color[][] DEFAULT_COLORS;
28     /**
29      * State the color array at an particular index in DEFAULT_COLORS
30      * represents.
31      */

32     private static final int[] DEFAULT_COLOR_MAP;
33     
34     static {
35         DEFAULT_COLOR_MAP = new int[] {
36             SynthConstants.PRESSED, SynthConstants.SELECTED,
37             SynthConstants.ENABLED, SynthConstants.MOUSE_OVER,
38             SynthConstants.DISABLED
39         };
40
41         DEFAULT_COLORS = new Color[5][];
42
43         // 2.0 colors
44
//
45
if (!GTKLookAndFeel.is2_2()) {
46             DEFAULT_COLORS[0] = getColorsFrom(
47                     new ColorUIResource(195, 195, 195), BLACK_COLOR);
48             DEFAULT_COLORS[1] = getColorsFrom(
49                     new ColorUIResource(0, 0, 156), WHITE_COLOR);
50             DEFAULT_COLORS[2] = getColorsFrom(
51                     new ColorUIResource(214, 214, 214), BLACK_COLOR);
52             DEFAULT_COLORS[3] = getColorsFrom(
53                     new ColorUIResource(233, 233, 233), BLACK_COLOR);
54             DEFAULT_COLORS[4] = getColorsFrom(
55                     new ColorUIResource(214, 214, 214),
56                     new ColorUIResource(117, 117, 117));
57             DEFAULT_COLORS[0][GTKColorType.TEXT_BACKGROUND.getID()] = new
58                     ColorUIResource(188, 210, 238);
59             DEFAULT_COLORS[1][GTKColorType.TEXT_BACKGROUND.getID()] = new
60                     ColorUIResource(164, 223, 255);
61             DEFAULT_COLORS[1][GTKColorType.TEXT_FOREGROUND.getID()] =
62                     BLACK_COLOR;
63             DEFAULT_COLORS[2][GTKColorType.TEXT_FOREGROUND.getID()] =
64                     BLACK_COLOR;
65             DEFAULT_COLORS[4][GTKColorType.TEXT_FOREGROUND.getID()] =
66                 DEFAULT_COLORS[2][GTKColorType.TEXT_FOREGROUND.getID()];
67         }
68         else {
69             // 2.2 colors
70
DEFAULT_COLORS[0] = getColorsFrom(
71                     new ColorUIResource(186, 181, 171), BLACK_COLOR);
72             DEFAULT_COLORS[1] = getColorsFrom(
73                     new ColorUIResource(75, 105, 131), WHITE_COLOR);
74             DEFAULT_COLORS[2] = getColorsFrom(
75                     new ColorUIResource(220, 218, 213), BLACK_COLOR);
76             DEFAULT_COLORS[3] = getColorsFrom(
77                     new ColorUIResource(238, 235, 231), BLACK_COLOR);
78             DEFAULT_COLORS[4] = getColorsFrom(
79                     new ColorUIResource(220, 218, 213),
80                     new ColorUIResource(117, 117, 117));
81             DEFAULT_COLORS[0][GTKColorType.TEXT_BACKGROUND.getID()] = new
82                     ColorUIResource(128, 125, 116);
83             DEFAULT_COLORS[1][GTKColorType.TEXT_BACKGROUND.getID()] = new
84                     ColorUIResource(75, 105, 131);
85             DEFAULT_COLORS[2][GTKColorType.TEXT_BACKGROUND.getID()] =
86                     WHITE_COLOR;
87             DEFAULT_COLORS[3][GTKColorType.TEXT_BACKGROUND.getID()] =
88                     WHITE_COLOR;
89             DEFAULT_COLORS[4][GTKColorType.TEXT_BACKGROUND.getID()] = new
90                     ColorUIResource(238, 235, 231);
91             DEFAULT_COLORS[0][GTKColorType.TEXT_FOREGROUND.getID()] =
92                     WHITE_COLOR;
93             DEFAULT_COLORS[1][GTKColorType.TEXT_FOREGROUND.getID()] =
94                     WHITE_COLOR;
95             DEFAULT_COLORS[2][GTKColorType.TEXT_FOREGROUND.getID()] =
96                     BLACK_COLOR;
97             DEFAULT_COLORS[3][GTKColorType.TEXT_FOREGROUND.getID()] =
98                     BLACK_COLOR;
99             DEFAULT_COLORS[4][GTKColorType.TEXT_FOREGROUND.getID()] = new
100                     ColorUIResource(117, 117, 117);
101         }
102     }
103     
104     GTKDefaultStyle(Font font) {
105         super(font);
106     }
107     
108     Color getStyleSpecificColor(SynthContext context, int state, ColorType type) {
109         Region id = (context != null) ? context.getRegion() : null;
110         Color color = null;
111         
112         if (id == Region.TOOL_TIP) {
113             color = getToolTipColor(state, type);
114         } else if (id == Region.PROGRESS_BAR && GTKLookAndFeel.is2_2()) {
115             color = getProgressBarColor(state, type);
116         } else if ((id == Region.MENU || id == Region.MENU_ITEM ||
117                     id == Region.POPUP_MENU_SEPARATOR ||
118                     id == Region.CHECK_BOX_MENU_ITEM ||
119                     id == Region.RADIO_BUTTON_MENU_ITEM ||
120                     id == Region.MENU_ITEM_ACCELERATOR) &&
121                     GTKLookAndFeel.is2_2()) {
122             color = getMenuItemColor(state, type);
123         }
124         
125         if (color != null) {
126             return color;
127         }
128         
129         for (int counter = DEFAULT_COLOR_MAP.length - 1;
130                      counter >= 0; counter--) {
131             if ((DEFAULT_COLOR_MAP[counter] & state) != 0) {
132                 if (type.getID() < DEFAULT_COLORS[counter].length) {
133                     return DEFAULT_COLORS[counter][type.getID()];
134                 }
135             }
136         }
137         if (type.getID() < DEFAULT_COLORS[2].length) {
138             return DEFAULT_COLORS[2][type.getID()];
139         }
140
141         return null;
142     }
143
144     private Color getToolTipColor(int state, ColorType type) {
145         if (state == SynthConstants.ENABLED) {
146             if(type == GTKColorType.BACKGROUND) {
147                  return(GTKLookAndFeel.is2_2() ?
148                         new ColorUIResource(0xEEE1B3) :
149                         new ColorUIResource(0xFFFFC0));
150             }
151     
152             if(type == GTKColorType.FOREGROUND) {
153                 return new ColorUIResource(0x000000);
154             }
155         }
156         return null;
157     }
158
159     private Color getProgressBarColor(int state, ColorType type) {
160         if (state == SynthConstants.ENABLED) {
161             if(type == GTKColorType.BACKGROUND) {
162                  return new ColorUIResource(0xBAB5AB);
163             }
164         } else if (state == SynthConstants.MOUSE_OVER) {
165             if(type == GTKColorType.BACKGROUND) {
166                  return new ColorUIResource(0x4B6983);
167             }
168         }
169         return null;
170     }
171
172     private Color getMenuItemColor(int state, ColorType type) {
173         if (state == SynthConstants.SELECTED) {
174             if(type == GTKColorType.TEXT_FOREGROUND ||
175                type == GTKColorType.FOREGROUND) {
176                  return new ColorUIResource(0xFFFFFF);
177             }
178         } else if (state == SynthConstants.MOUSE_OVER) {
179             if(type == GTKColorType.BACKGROUND) {
180                  return new ColorUIResource(0x9db8d2);
181             }
182             if(type == GTKColorType.FOREGROUND) {
183                  return GTKStyle.WHITE_COLOR;
184             }
185             if(type == GTKColorType.TEXT_FOREGROUND) {
186                  return new ColorUIResource(0xFFFFFF);
187             }
188         }
189
190         return null;
191     }
192     
193     /**
194      * Returns the X thickness to use for this GTKStyle.
195      *
196      * @return x thickness.
197      */

198     int getXThickness() {
199         return DEFAULT_XTHICKNESS;
200     }
201
202     /**
203      * Returns the Y thickness to use for this GTKStyle.
204      *
205      * @return x thickness.
206      */

207     int getYThickness() {
208         return DEFAULT_YTHICKNESS;
209     }
210     
211     /**
212      * Returns the value for a class specific property. A class specific value
213      * is a value that will be picked up based on class hierarchy.
214      *
215      * @param context SynthContext indentifying requestor
216      * @param key Key identifying class specific value
217      * @return Value, or null if one has not been defined.
218      */

219     Object JavaDoc getClassSpecificValue(Region region, String JavaDoc key) {
220         return null;
221     }
222
223     /**
224      * Creates an array of colors populated based on the passed in
225      * the background color. Specifically this sets the
226      * BACKGROUND, LIGHT, DARK, MID, BLACK, WHITE and FOCUS colors
227      * from that of color, which is assumed to be the background.
228      */

229     private static Color[] getColorsFrom(Color bg, Color fg) {
230         Color lightColor = calculateLightColor(bg);
231         Color darkColor = calculateDarkColor(bg);
232         Color midColor = calculateMidColor(lightColor, darkColor);
233         Color[] colors = new Color[GTKColorType.MAX_COUNT];
234         colors[GTKColorType.BACKGROUND.getID()] = bg;
235         colors[GTKColorType.LIGHT.getID()] = lightColor;
236         colors[GTKColorType.DARK.getID()] = darkColor;
237         colors[GTKColorType.MID.getID()] = midColor;
238         colors[GTKColorType.BLACK.getID()] = BLACK_COLOR;
239         colors[GTKColorType.WHITE.getID()] = WHITE_COLOR;
240         colors[GTKColorType.FOCUS.getID()] = BLACK_COLOR;
241         colors[GTKColorType.FOREGROUND.getID()] = fg;
242         colors[GTKColorType.TEXT_FOREGROUND.getID()] = fg;
243         colors[GTKColorType.TEXT_BACKGROUND.getID()] = WHITE_COLOR;
244         return colors;
245     }
246
247     /**
248      * Calculates the LIGHT color from the background color.
249      */

250     private static Color calculateLightColor(Color bg) {
251         return GTKColorType.adjustColor(bg, 1.0f, 1.3f, 1.3f);
252     }
253
254     /**
255      * Calculates the DARK color from the background color.
256      */

257     private static Color calculateDarkColor(Color bg) {
258         return GTKColorType.adjustColor(bg, 1.0f, .7f, .7f);
259     }
260
261     /**
262      * Calculates the MID color from the light and dark colors.
263      */

264     private static Color calculateMidColor(Color lightColor, Color darkColor) {
265         int light = lightColor.getRGB();
266         int dark = darkColor.getRGB();
267         int rLight = (light & 0xFF0000) >> 16;
268         int rDark = (dark & 0xFF0000) >> 16;
269         int gLight = (light & 0x00FF00) >> 8;
270         int gDark = (dark & 0x00FF00) >> 8;
271         int bLight = (light & 0xFF);
272         int bDark = (dark & 0xFF);
273         return new ColorUIResource((((rLight + rDark) / 2) << 16) |
274                                    (((gLight + gDark) / 2) << 8) |
275                                    ((bLight + bDark) / 2));
276     }
277 }
278
Popular Tags