blob: a5b88957c187e17118d654950c70f6adedb20c67 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998-1999 Sun Microsystems, Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of Sun Microsystems nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 */
34
35
36import javax.swing.plaf.*;
37import javax.swing.plaf.metal.*;
38import javax.swing.*;
39import javax.swing.border.*;
40import java.awt.*;
41import java.io.*;
42import java.util.*;
43
44/**
45 * This class allows you to load a theme from a file.
46 * It uses the standard Java Properties file format.
47 * To create a theme you provide a text file which contains
48 * tags corresponding to colors of the theme along with a value
49 * for that color. For example:
50 *
51 * name=My Ugly Theme
52 * primary1=255,0,0
53 * primary2=0,255,0
54 * primary3=0,0,255
55 *
56 * This class only loads colors from the properties file,
57 * but it could easily be extended to load fonts - or even icons.
58 *
59 * @author Steve Wilson
60 */
61public class PropertiesMetalTheme extends DefaultMetalTheme {
62
63 private String name = "Custom Theme";
64
65 private ColorUIResource primary1;
66 private ColorUIResource primary2;
67 private ColorUIResource primary3;
68
69 private ColorUIResource secondary1;
70 private ColorUIResource secondary2;
71 private ColorUIResource secondary3;
72
73 private ColorUIResource black;
74 private ColorUIResource white;
75
76
77 /**
78 * pass an inputstream pointing to a properties file.
79 * Colors will be initialized to be the same as the DefaultMetalTheme,
80 * and then any colors provided in the properties file will override that.
81 */
82 public PropertiesMetalTheme( InputStream stream ) {
83 initColors();
84 loadProperties(stream);
85 }
86
87 /**
88 * Initialize all colors to be the same as the DefaultMetalTheme.
89 */
90 private void initColors() {
91 primary1 = super.getPrimary1();
92 primary2 = super.getPrimary2();
93 primary3 = super.getPrimary3();
94
95 secondary1 = super.getSecondary1();
96 secondary2 = super.getSecondary2();
97 secondary3 = super.getSecondary3();
98
99 black = super.getBlack();
100 white = super.getWhite();
101 }
102
103 /**
104 * Load the theme name and colors from the properties file
105 * Items not defined in the properties file are ignored
106 */
107 private void loadProperties(InputStream stream) {
108 Properties prop = new Properties();
109 try {
110 prop.load(stream);
111 } catch (IOException e) {
112 System.out.println(e);
113 }
114
115 Object tempName = prop.get("name");
116 if (tempName != null) {
117 name = tempName.toString();
118 }
119
120 Object colorString = null;
121
122 colorString = prop.get("primary1");
123 if (colorString != null){
124 primary1 = parseColor(colorString.toString());
125 }
126
127 colorString = prop.get("primary2");
128 if (colorString != null) {
129 primary2 = parseColor(colorString.toString());
130 }
131
132 colorString = prop.get("primary3");
133 if (colorString != null) {
134 primary3 = parseColor(colorString.toString());
135 }
136
137 colorString = prop.get("secondary1");
138 if (colorString != null) {
139 secondary1 = parseColor(colorString.toString());
140 }
141
142 colorString = prop.get("secondary2");
143 if (colorString != null) {
144 secondary2 = parseColor(colorString.toString());
145 }
146
147 colorString = prop.get("secondary3");
148 if (colorString != null) {
149 secondary3 = parseColor(colorString.toString());
150 }
151
152 colorString = prop.get("black");
153 if (colorString != null) {
154 black = parseColor(colorString.toString());
155 }
156
157 colorString = prop.get("white");
158 if (colorString != null) {
159 white = parseColor(colorString.toString());
160 }
161
162 }
163
164 public String getName() { return name; }
165
166 protected ColorUIResource getPrimary1() { return primary1; }
167 protected ColorUIResource getPrimary2() { return primary2; }
168 protected ColorUIResource getPrimary3() { return primary3; }
169
170 protected ColorUIResource getSecondary1() { return secondary1; }
171 protected ColorUIResource getSecondary2() { return secondary2; }
172 protected ColorUIResource getSecondary3() { return secondary3; }
173
174 protected ColorUIResource getBlack() { return black; }
175 protected ColorUIResource getWhite() { return white; }
176
177 /**
178 * parse a comma delimited list of 3 strings into a Color
179 */
180 private ColorUIResource parseColor(String s) {
181 int red = 0;
182 int green = 0;
183 int blue = 0;
184 try {
185 StringTokenizer st = new StringTokenizer(s, ",");
186
187 red = Integer.parseInt(st.nextToken());
188 green = Integer.parseInt(st.nextToken());
189 blue = Integer.parseInt(st.nextToken());
190
191 } catch (Exception e) {
192 System.out.println(e);
193 System.out.println("Couldn't parse color :" + s);
194 }
195
196 return new ColorUIResource(red, green, blue);
197 }
198}