KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > lookandfeel > CustomOceanTheme


1 package snow.lookandfeel;
2
3 import snow.utils.storage.AppProperties;
4
5 import java.io.*;
6 import javax.swing.*;
7 import javax.swing.event.*;
8 import javax.swing.plaf.metal.*;
9 import javax.swing.plaf.*;
10 import java.awt.*;
11 import java.awt.event.*;
12 import java.util.*;
13
14 public class CustomOceanTheme extends OceanTheme
15 {
16   final private String JavaDoc customName;
17
18   // the properties, maybe customized by the user
19
final private AppProperties props;
20
21   // each subclass may defins its own defaults (efcn => dark, cloud => bright)
22
final private AppProperties themeDefaults = new AppProperties();
23
24
25   public final static String JavaDoc COLOR_BLACK = "Black";
26   public final static String JavaDoc COLOR_White = "White";
27
28   public final static String JavaDoc COLOR_Green = "Green";
29   public final static String JavaDoc COLOR_Red = "Red";
30
31   public final static String JavaDoc COLOR_Primary1 = "Primary 1";
32   public final static String JavaDoc COLOR_Primary2 = "Primary 2";
33   public final static String JavaDoc COLOR_Primary3 = "Primary 3";
34
35   public final static String JavaDoc COLOR_Secondary1 = "Secondary 1";
36   public final static String JavaDoc COLOR_Secondary2 = "Secondary 2";
37   public final static String JavaDoc COLOR_Secondary3 = "Secondary 3";
38
39   public final static String JavaDoc COLOR_ControlText = "ControlTextColor";
40   public final static String JavaDoc COLOR_DesktopColor = "DesktopColor";
41   public final static String JavaDoc COLOR_InactiveControlText = "InactiveControlTextColor";
42   public final static String JavaDoc COLOR_MenuDisabledForeground = "MenuDisabledForegroundColor";
43
44   public final static String JavaDoc COLOR_TextHighlight = "TextHighlightColor";
45   public final static String JavaDoc COLOR_UserText = "UserTextColor";
46
47   public static final String JavaDoc FONT_SubText = "SubTextFont";
48   public static final String JavaDoc FONT_System = "SystemFont";
49   public static final String JavaDoc FONT_UserText = "UserTextFont";
50   public static final String JavaDoc FONT_WindowTitle = "WindowTitleFont";
51   public static final String JavaDoc FONT_ControlText = "ControlTextFont";
52   public static final String JavaDoc FONT_MenuText = "MenuTextFont";
53
54
55   private ImageIcon backgroundImage;
56
57   public CustomOceanTheme(String JavaDoc customName, AppProperties props, ImageIcon backgroundImage)
58   {
59      super();
60      this.customName = customName;
61      this.props = props;
62      this.backgroundImage = backgroundImage;
63
64      this.setDefaultColor(this.COLOR_Green, new ColorUIResource(180,250,180));
65      this.setDefaultColor(this.COLOR_Red, new ColorUIResource(250,180,180));
66   } // Constructor
67

68   /** overwrite this if you change something to the theme
69   */

70   public void writeDefaults()
71   {
72   }
73
74   public String JavaDoc getName() { return customName; }
75
76  /**
77   * Returns the background image for this theme if it exists,
78   * and null otherwise.
79   */

80   public ImageIcon getBackgroundImage()
81   {
82     return this.backgroundImage;
83   }
84
85   //
86
// COLORS
87
//
88

89   public ColorUIResource getBlack() { return getColor(COLOR_BLACK); }
90   public ColorUIResource getWhite() { return getColor(COLOR_White); }
91   public ColorUIResource getGreen() { return getColor(COLOR_Green); }
92   public ColorUIResource getRed() { return getColor(COLOR_Red); }
93   protected ColorUIResource getPrimary1() { return getColor(COLOR_Primary1); }
94   protected ColorUIResource getPrimary2() { return getColor(COLOR_Primary2); }
95   protected ColorUIResource getPrimary3() { return getColor(COLOR_Primary3); }
96   protected ColorUIResource getSecondary1() { return getColor(COLOR_Secondary1); }
97   protected ColorUIResource getSecondary2() { return getColor(COLOR_Secondary2); }
98   protected ColorUIResource getSecondary3() { return getColor(COLOR_Secondary3); }
99
100   /** table text
101   */

102   public ColorUIResource getControlTextColor() { return getColor(this.COLOR_ControlText); }
103   public ColorUIResource getDesktopColor() { return getColor(this.COLOR_DesktopColor); }
104   public ColorUIResource getInactiveControlTextColor() { return getColor(this.COLOR_InactiveControlText); }
105   public ColorUIResource getMenuDisabledForeground() { return getColor(this.COLOR_MenuDisabledForeground); }
106   public ColorUIResource getTextHighlightColor() { return getColor(this.COLOR_TextHighlight); }
107   public ColorUIResource getUserTextColor() { return getColor(this.COLOR_UserText); }
108
109
110   /** retrieves a color for this theme
111   */

112   public ColorUIResource getColor(String JavaDoc key)
113   {
114     // first look in the user props
115
Color found = props.getColor(customName+key, null);
116     if(found!=null) return new ColorUIResource(found);
117
118     // if not found, look in the defaults
119
found = themeDefaults.getColor(customName+key, null);
120     if(found!=null) return new ColorUIResource(found);
121
122     // Not found => look in the default ocean
123
if(key.equals(COLOR_BLACK)) return super.getBlack();
124     if(key.equals(COLOR_White)) return super.getWhite();
125     if(key.equals(COLOR_Primary1)) return super.getPrimary1();
126     if(key.equals(COLOR_Primary2)) return super.getPrimary2();
127     if(key.equals(COLOR_Primary3)) return super.getPrimary3();
128     if(key.equals(COLOR_Secondary1)) return super.getSecondary1();
129     if(key.equals(COLOR_Secondary2)) return super.getSecondary2();
130     if(key.equals(COLOR_Secondary3)) return super.getSecondary3();
131
132     if(key.equals(COLOR_ControlText)) return super.getControlTextColor();
133     if(key.equals(COLOR_DesktopColor)) return super.getDesktopColor();
134     if(key.equals(COLOR_InactiveControlText)) return super.getInactiveControlTextColor();
135     if(key.equals(COLOR_MenuDisabledForeground)) return super.getMenuDisabledForeground();
136     if(key.equals(this.COLOR_TextHighlight)) return super.getTextHighlightColor();
137     if(key.equals(COLOR_UserText)) return super.getUserTextColor();
138
139     return new ColorUIResource(Color.cyan);
140   }
141
142   /** if null, is removed
143   */

144   public void setDefaultColor(String JavaDoc key, Color c)
145   {
146     if(c==null)
147     {
148        themeDefaults.remove(customName+key);
149     }
150     else
151     {
152        themeDefaults.setColor(customName+key, c);
153     }
154   }
155
156
157 @Override JavaDoc
158   public void addCustomEntriesToTable(UIDefaults table)
159   {
160     super.addCustomEntriesToTable(table);
161     UIManager.put( "ScrollBar.width", 12); // instead 17
162
UIManager.put( "SplitPane.dividerSize", 8); // instead 10
163
UIManager.put( "TabbedPane.contentBorderInsets", new InsetsUIResource(4,0,0,0));
164   }
165
166
167
168   //
169
// Fonts
170
//
171

172
173   public FontUIResource getSubTextFont() { return getFont(FONT_SubText); }
174   public FontUIResource getSystemTextFont() { return getFont(FONT_System); }
175   public FontUIResource getUserTextFont() { return getFont(FONT_UserText); }
176   public FontUIResource getWindowTitleFont() { return getFont(FONT_WindowTitle); }
177   public FontUIResource getControlTextFont() { return getFont(FONT_ControlText); }
178   public FontUIResource getMenuTextFont() { return getFont(FONT_MenuText); }
179
180   public FontUIResource getFont(String JavaDoc key)
181   {
182     Font found = props.getFont(customName+key, null);
183     if(found!=null) return new FontUIResource(found);
184
185     found = this.themeDefaults.getFont(customName+key, null);
186     if(found!=null) return new FontUIResource(found);
187
188     // Maps defaults with parent
189
if(key.equals(FONT_SubText)) return super.getSubTextFont();
190     if(key.equals(FONT_System)) return super.getSystemTextFont();
191     if(key.equals(FONT_UserText)) return super.getUserTextFont();
192     if(key.equals(FONT_WindowTitle)) return super.getWindowTitleFont();
193     if(key.equals(FONT_ControlText)) return super.getControlTextFont();
194     if(key.equals(FONT_MenuText)) return super.getMenuTextFont();
195
196     return null;
197   }
198
199   public void setDefaultFont(String JavaDoc key, Font f)
200   {
201     if(f==null)
202     {
203        themeDefaults.remove(customName+key);
204     }
205     else
206     {
207        themeDefaults.setFont(customName+key, f);
208     }
209   }
210
211   //
212
// Edition
213
//
214

215   public Vector<String JavaDoc> getKeysForEdition()
216   {
217     Vector<String JavaDoc> keys = new Vector<String JavaDoc>();
218     keys.add( COLOR_BLACK );
219     keys.add( COLOR_White );
220
221     keys.add( COLOR_Green );
222     keys.add( COLOR_Red );
223
224     keys.add( COLOR_Primary1 );
225     keys.add( COLOR_Primary2 );
226     keys.add( COLOR_Primary3 );
227     keys.add( COLOR_Secondary1 );
228     keys.add( COLOR_Secondary2 );
229     keys.add( COLOR_Secondary3 );
230
231     keys.add( COLOR_ControlText );
232     keys.add( COLOR_UserText );
233     keys.add( COLOR_TextHighlight );
234     keys.add( COLOR_DesktopColor );
235     keys.add( COLOR_InactiveControlText );
236     keys.add( COLOR_MenuDisabledForeground );
237
238     keys.add( FONT_SubText );
239     keys.add( FONT_System );
240     keys.add( FONT_UserText );
241     keys.add( FONT_WindowTitle );
242     keys.add( FONT_ControlText );
243     keys.add( FONT_MenuText );
244     return keys;
245   }
246
247   public Object JavaDoc getResource(String JavaDoc key)
248   {
249     FontUIResource f = this.getFont(key);
250     if(f!=null) return f;
251     ColorUIResource c = this.getColor(key);
252     return c;
253   }
254
255   public void setResource(String JavaDoc key, Object JavaDoc res)
256   {
257     if(res instanceof Font)
258     {
259       this.props.setFont(customName+key, (Font) res);
260     }
261     else if(res instanceof Color)
262     {
263       this.props.setColor(customName+key, (Color) res);
264     }
265
266   }
267
268   public void clearUserSettings()
269   {
270     for(String JavaDoc s: getKeysForEdition())
271     {
272       props.setColor(customName+s, null);
273       props.setFont(customName+s, null);
274     }
275     writeDefaults();
276   }
277
278   public static void main(String JavaDoc[] aaa)
279   {
280      ThemesManager.main(aaa);
281   }
282
283
284 } // CustomOceanTheme
Popular Tags