KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PropertiesMetalTheme


1 /*
2  * @(#)PropertiesMetalTheme.java 1.12 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)PropertiesMetalTheme.java 1.12 05/11/17
39  */

40
41
42 import javax.swing.plaf.*;
43 import javax.swing.plaf.metal.*;
44 import javax.swing.*;
45 import javax.swing.border.*;
46 import java.awt.*;
47 import java.io.*;
48 import java.util.*;
49
50 /**
51  * This class allows you to load a theme from a file.
52  * It uses the standard Java Properties file format.
53  * To create a theme you provide a text file which contains
54  * tags corresponding to colors of the theme along with a value
55  * for that color. For example:
56  *
57  * name=My Ugly Theme
58  * primary1=255,0,0
59  * primary2=0,255,0
60  * primary3=0,0,255
61  *
62  * This class only loads colors from the properties file,
63  * but it could easily be extended to load fonts - or even icons.
64  *
65  * @version 1.12 11/17/05
66  * @author Steve Wilson
67  */

68 public class PropertiesMetalTheme extends DefaultMetalTheme {
69
70     private String JavaDoc name = "Custom Theme";
71
72     private ColorUIResource primary1;
73     private ColorUIResource primary2;
74     private ColorUIResource primary3;
75
76     private ColorUIResource secondary1;
77     private ColorUIResource secondary2;
78     private ColorUIResource secondary3;
79
80     private ColorUIResource black;
81     private ColorUIResource white;
82
83
84     /**
85       * pass an inputstream pointing to a properties file.
86       * Colors will be initialized to be the same as the DefaultMetalTheme,
87       * and then any colors provided in the properties file will override that.
88       */

89     public PropertiesMetalTheme( InputStream stream ) {
90         initColors();
91         loadProperties(stream);
92     }
93
94     /**
95       * Initialize all colors to be the same as the DefaultMetalTheme.
96       */

97     private void initColors() {
98         primary1 = super.getPrimary1();
99         primary2 = super.getPrimary2();
100         primary3 = super.getPrimary3();
101
102         secondary1 = super.getSecondary1();
103         secondary2 = super.getSecondary2();
104         secondary3 = super.getSecondary3();
105
106     black = super.getBlack();
107     white = super.getWhite();
108     }
109
110     /**
111       * Load the theme name and colors from the properties file
112       * Items not defined in the properties file are ignored
113       */

114     private void loadProperties(InputStream stream) {
115     Properties prop = new Properties();
116     try {
117         prop.load(stream);
118     } catch (IOException e) {
119         System.out.println(e);
120     }
121
122     Object JavaDoc tempName = prop.get("name");
123     if (tempName != null) {
124         name = tempName.toString();
125     }
126
127     Object JavaDoc colorString = null;
128
129     colorString = prop.get("primary1");
130     if (colorString != null){
131         primary1 = parseColor(colorString.toString());
132     }
133
134     colorString = prop.get("primary2");
135     if (colorString != null) {
136         primary2 = parseColor(colorString.toString());
137     }
138
139     colorString = prop.get("primary3");
140     if (colorString != null) {
141         primary3 = parseColor(colorString.toString());
142     }
143
144     colorString = prop.get("secondary1");
145     if (colorString != null) {
146         secondary1 = parseColor(colorString.toString());
147     }
148
149     colorString = prop.get("secondary2");
150     if (colorString != null) {
151         secondary2 = parseColor(colorString.toString());
152     }
153
154     colorString = prop.get("secondary3");
155     if (colorString != null) {
156         secondary3 = parseColor(colorString.toString());
157     }
158
159     colorString = prop.get("black");
160     if (colorString != null) {
161         black = parseColor(colorString.toString());
162     }
163
164     colorString = prop.get("white");
165     if (colorString != null) {
166         white = parseColor(colorString.toString());
167     }
168
169     }
170
171     public String JavaDoc getName() { return name; }
172
173     protected ColorUIResource getPrimary1() { return primary1; }
174     protected ColorUIResource getPrimary2() { return primary2; }
175     protected ColorUIResource getPrimary3() { return primary3; }
176
177     protected ColorUIResource getSecondary1() { return secondary1; }
178     protected ColorUIResource getSecondary2() { return secondary2; }
179     protected ColorUIResource getSecondary3() { return secondary3; }
180
181     protected ColorUIResource getBlack() { return black; }
182     protected ColorUIResource getWhite() { return white; }
183
184     /**
185       * parse a comma delimited list of 3 strings into a Color
186       */

187     private ColorUIResource parseColor(String JavaDoc s) {
188         int red = 0;
189     int green = 0;
190     int blue = 0;
191     try {
192         StringTokenizer st = new StringTokenizer(s, ",");
193
194         red = Integer.parseInt(st.nextToken());
195         green = Integer.parseInt(st.nextToken());
196         blue = Integer.parseInt(st.nextToken());
197
198     } catch (Exception JavaDoc e) {
199         System.out.println(e);
200         System.out.println("Couldn't parse color :" + s);
201     }
202
203     return new ColorUIResource(red, green, blue);
204     }
205 }
206
Popular Tags