1/*
2 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. 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 Oracle 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 * This source code is provided to illustrate the usage of a given feature
34 * or technique and has been deliberately simplified. Additional steps
35 * required for a production-quality application, such as security checks,
36 * input validation and proper error handling, might not be present in
37 * this sample code.
38 */
39
40
41
42import java.io.IOException;
43import java.io.InputStream;
44import java.util.Properties;
45import java.util.StringTokenizer;
46
47import javax.swing.plaf.ColorUIResource;
48import javax.swing.plaf.metal.DefaultMetalTheme;
49
50
51/**
52 * This class allows you to load a theme from a file.
53 * It uses the standard Java Properties file format.
54 * To create a theme you provide a text file which contains
55 * tags corresponding to colors of the theme along with a value
56 * for that color.  For example:
57 *
58 * name=My Ugly Theme
59 * primary1=255,0,0
60 * primary2=0,255,0
61 * primary3=0,0,255
62 *
63 * This class only loads colors from the properties file,
64 * but it could easily be extended to load fonts -  or even icons.
65 *
66 * @author Steve Wilson
67 * @author Alexander Kouznetsov
68 */
69public class PropertiesMetalTheme extends DefaultMetalTheme {
70
71    private String name = "Custom Theme";
72    private ColorUIResource primary1;
73    private ColorUIResource primary2;
74    private ColorUIResource primary3;
75    private ColorUIResource secondary1;
76    private ColorUIResource secondary2;
77    private ColorUIResource secondary3;
78    private ColorUIResource black;
79    private ColorUIResource white;
80
81    /**
82     * pass an inputstream pointing to a properties file.
83     * Colors will be initialized to be the same as the DefaultMetalTheme,
84     * and then any colors provided in the properties file will override that.
85     */
86    public PropertiesMetalTheme(InputStream stream) {
87        initColors();
88        loadProperties(stream);
89    }
90
91    /**
92     * Initialize all colors to be the same as the DefaultMetalTheme.
93     */
94    private void initColors() {
95        primary1 = super.getPrimary1();
96        primary2 = super.getPrimary2();
97        primary3 = super.getPrimary3();
98
99        secondary1 = super.getSecondary1();
100        secondary2 = super.getSecondary2();
101        secondary3 = super.getSecondary3();
102
103        black = super.getBlack();
104        white = super.getWhite();
105    }
106
107    /**
108     * Load the theme name and colors from the properties file
109     * Items not defined in the properties file are ignored
110     */
111    private void loadProperties(InputStream stream) {
112        Properties prop = new Properties();
113        try {
114            prop.load(stream);
115        } catch (IOException e) {
116            System.out.println(e);
117        }
118
119        Object tempName = prop.get("name");
120        if (tempName != null) {
121            name = tempName.toString();
122        }
123
124        Object colorString = null;
125
126        colorString = prop.get("primary1");
127        if (colorString != null) {
128            primary1 = parseColor(colorString.toString());
129        }
130
131        colorString = prop.get("primary2");
132        if (colorString != null) {
133            primary2 = parseColor(colorString.toString());
134        }
135
136        colorString = prop.get("primary3");
137        if (colorString != null) {
138            primary3 = parseColor(colorString.toString());
139        }
140
141        colorString = prop.get("secondary1");
142        if (colorString != null) {
143            secondary1 = parseColor(colorString.toString());
144        }
145
146        colorString = prop.get("secondary2");
147        if (colorString != null) {
148            secondary2 = parseColor(colorString.toString());
149        }
150
151        colorString = prop.get("secondary3");
152        if (colorString != null) {
153            secondary3 = parseColor(colorString.toString());
154        }
155
156        colorString = prop.get("black");
157        if (colorString != null) {
158            black = parseColor(colorString.toString());
159        }
160
161        colorString = prop.get("white");
162        if (colorString != null) {
163            white = parseColor(colorString.toString());
164        }
165
166    }
167
168    @Override
169    public String getName() {
170        return name;
171    }
172
173    @Override
174    protected ColorUIResource getPrimary1() {
175        return primary1;
176    }
177
178    @Override
179    protected ColorUIResource getPrimary2() {
180        return primary2;
181    }
182
183    @Override
184    protected ColorUIResource getPrimary3() {
185        return primary3;
186    }
187
188    @Override
189    protected ColorUIResource getSecondary1() {
190        return secondary1;
191    }
192
193    @Override
194    protected ColorUIResource getSecondary2() {
195        return secondary2;
196    }
197
198    @Override
199    protected ColorUIResource getSecondary3() {
200        return secondary3;
201    }
202
203    @Override
204    protected ColorUIResource getBlack() {
205        return black;
206    }
207
208    @Override
209    protected ColorUIResource getWhite() {
210        return white;
211    }
212
213    /**
214     * parse a comma delimited list of 3 strings into a Color
215     */
216    private ColorUIResource parseColor(String s) {
217        int red = 0;
218        int green = 0;
219        int blue = 0;
220        try {
221            StringTokenizer st = new StringTokenizer(s, ",");
222
223            red = Integer.parseInt(st.nextToken());
224            green = Integer.parseInt(st.nextToken());
225            blue = Integer.parseInt(st.nextToken());
226
227        } catch (Exception e) {
228            System.out.println(e);
229            System.out.println("Couldn't parse color :" + s);
230        }
231
232        return new ColorUIResource(red, green, blue);
233    }
234}
235