View Javadoc

1   /***
2    * XMLMetalTheme.java
3    *
4    * This file is part of the creme library.
5    * The creme library intends to ease the development effort of large
6    * applications by providing easy to use support classes.
7    *
8    * Copyright (C) 2002 Denis Bregeon
9    *
10   *
11   * This library is free software; you can redistribute it and/or
12   * modify it under the terms of the GNU Lesser General Public
13   * License as published by the Free Software Foundation; either
14   * version 2.1 of the License, or (at your option) any later version.
15   *
16   * This library is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19   * Lesser General Public License for more details.
20   *
21   * You should have received a copy of the GNU Lesser General Public
22   * License along with this library; if not, write to the Free Software
23   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24   *
25   * contact information: dbregeon@sourceforge.net
26   */
27  package org.jcreme.swing.plaf.metal;
28  
29  import javax.swing.plaf.ColorUIResource;
30  import javax.swing.plaf.FontUIResource;
31  import javax.swing.plaf.metal.DefaultMetalTheme;
32  import javax.xml.transform.Source;
33  import javax.xml.transform.Transformer;
34  import javax.xml.transform.TransformerException;
35  import javax.xml.transform.TransformerFactory;
36  import javax.xml.transform.stream.StreamResult;
37  import javax.xml.transform.stream.StreamSource;
38  
39  /***
40   * This class enables to provide a MetalTheme to an application from an XML
41   * file. The XML file must conform the metaltheme schema.
42   * 
43   * @author $Author: dbregeon $
44   * @version $Revision: 1.1 $
45   */
46  public class XMLMetalTheme extends DefaultMetalTheme {
47      /***
48       * The name of the stylesheet to use. The stylesheet will be retrieved from
49       * the classpath.
50       */
51      public static final String TRANSFORMATION_FILE_NAME = "org/jcreme/swing/plaf/metal/metaltheme.xsl";
52  
53      /***
54       * The name associated to the XMLMetalTheme object in the stylesheet.
55       */
56      public static final String PARAMETER_NAME = "xmlmetaltheme";
57  
58      /***
59       * For efficiency purpose, the transformer is instantiated only once.
60       */
61      protected static Transformer transformer = null;
62  
63      /***
64       * This block initializes the transformer instance.
65       */
66      static {
67          Source streamSource = new StreamSource(ClassLoader
68                  .getSystemResourceAsStream(TRANSFORMATION_FILE_NAME));
69          try {
70              transformer = TransformerFactory.newInstance().newTransformer(
71                      streamSource);
72          } catch (TransformerException e) {
73              e.printStackTrace();
74          }
75      }
76  
77      /***
78       * The name of the theme.
79       */
80      protected String name = null;
81  
82      /***
83       * The first primary color of the theme.
84       */
85      protected ColorUIResource primary1 = null;
86  
87      /***
88       * The second primary color of the theme.
89       */
90      protected ColorUIResource primary2 = null;
91  
92      /***
93       * The third primary color of the theme.
94       */
95      protected ColorUIResource primary3 = null;
96  
97      /***
98       * The first secondary color of the theme.
99       */
100     protected ColorUIResource secondary1 = null;
101 
102     /***
103      * The second secondary color of the theme.
104      */
105     protected ColorUIResource secondary2 = null;
106 
107     /***
108      * The third secondary color of the theme.
109      */
110     protected ColorUIResource secondary3 = null;
111 
112     /***
113      * The control text font of the theme.
114      */
115     protected FontUIResource controlTextFont = null;
116 
117     /***
118      * The menu text font of the theme.
119      */
120     protected FontUIResource menuTextFont = null;
121 
122     /***
123      * The sub text font of the theme.
124      */
125     protected FontUIResource subTextFont = null;
126 
127     /***
128      * The system text font of the theme.
129      */
130     protected FontUIResource systemTextFont = null;
131 
132     /***
133      * The user text font of the theme.
134      */
135     protected FontUIResource userTextFont = null;
136 
137     /***
138      * The window title font of the theme.
139      */
140     protected FontUIResource windowTitleFont = null;
141 
142     /***
143      * Creates a new instance of XMLMetalTheme The object is created by
144      * transforming the provided file through the metaltheme stylesheet.
145      * 
146      * @param sourceFileName
147      *            the name of the xml file containing the values for the theme.
148      *            The file must conform the metaltheme schema. The file will be
149      *            retrieved as a system property.
150      */
151     public XMLMetalTheme(String sourceFileName) {
152         Source file = new StreamSource(ClassLoader
153                 .getSystemResourceAsStream(sourceFileName));
154         synchronized (transformer) {
155             transformer.setParameter(PARAMETER_NAME, this);
156             try {
157                 transformer.transform(file, new StreamResult(new java.io.File(
158                         "toto")));
159             } catch (TransformerException e) {
160                 e.printStackTrace();
161             }
162         }
163     }
164 
165     /***
166      * Gives access to the theme's name.
167      * 
168      * @return the name given to the theme.
169      */
170     public String getName() {
171         return this.name;
172     }
173 
174     /***
175      * Gives access to the first primary color of the theme.
176      * 
177      * @return the first primary color of the theme.
178      */
179     public ColorUIResource getPrimary1() {
180         ColorUIResource result = this.primary1;
181         if (result == null) {
182             result = super.getPrimary1();
183         }
184         return result;
185     }
186 
187     /***
188      * Gives access to the second primary color of the theme.
189      * 
190      * @return the second primary color of the theme.
191      */
192     public ColorUIResource getPrimary2() {
193         ColorUIResource result = this.primary2;
194         if (result == null) {
195             result = super.getPrimary2();
196         }
197         return result;
198     }
199 
200     /***
201      * Gives access to the third primary color of the theme.
202      * 
203      * @return the third primary color of the theme.
204      */
205     public ColorUIResource getPrimary3() {
206         ColorUIResource result = this.primary3;
207         if (result == null) {
208             result = super.getPrimary3();
209         }
210         return result;
211     }
212 
213     /***
214      * Gives access to the first secondary color of the theme.
215      * 
216      * @return the first secondary color of the theme.
217      */
218     public ColorUIResource getSecondary1() {
219         ColorUIResource result = this.secondary1;
220         if (result == null) {
221             result = super.getSecondary1();
222         }
223         return result;
224     }
225 
226     /***
227      * Gives access to the second secondary color of the theme.
228      * 
229      * @return the second secondary color of the theme.
230      */
231     public ColorUIResource getSecondary2() {
232         ColorUIResource result = this.secondary2;
233         if (result == null) {
234             result = super.getSecondary2();
235         }
236         return result;
237     }
238 
239     /***
240      * Gives access to the third secondary color of the theme.
241      * 
242      * @return the third secondary color of the theme.
243      */
244     public ColorUIResource getSecondary3() {
245         ColorUIResource result = this.secondary3;
246         if (result == null) {
247             result = super.getSecondary3();
248         }
249         return result;
250     }
251 
252     /***
253      * Gives access to the control text font of the theme.
254      * 
255      * @return the control text font of the theme.
256      */
257     public FontUIResource getControlTextFont() {
258         FontUIResource result = this.controlTextFont;
259         if (result == null) {
260             result = super.getControlTextFont();
261         }
262         return result;
263     }
264 
265     /***
266      * Gives access to the menu text font of the theme.
267      * 
268      * @return the menu text font of the theme.
269      */
270     public FontUIResource getMenuTextFont() {
271         FontUIResource result = this.menuTextFont;
272         if (result == null) {
273             result = super.getMenuTextFont();
274         }
275         return result;
276     }
277 
278     /***
279      * Gives access to the sub text font of the theme.
280      * 
281      * @return the sub text font of the theme.
282      */
283     public FontUIResource getSubTextFont() {
284         FontUIResource result = this.subTextFont;
285         if (result == null) {
286             result = super.getSubTextFont();
287         }
288         return result;
289     }
290 
291     /***
292      * Gives access to the system text font of the theme.
293      * 
294      * @return the system text font of the theme.
295      */
296     public FontUIResource getSystemTextFont() {
297         FontUIResource result = this.systemTextFont;
298         if (result == null) {
299             result = super.getSystemTextFont();
300         }
301         return result;
302     }
303 
304     /***
305      * Gives access to the user text font of the theme.
306      * 
307      * @return the user text font of the theme.
308      */
309     public FontUIResource getUserTextFont() {
310         FontUIResource result = this.userTextFont;
311         if (result == null) {
312             result = super.getUserTextFont();
313         }
314         return result;
315     }
316 
317     /***
318      * Gives access to the window title font of the theme.
319      * 
320      * @return the window title font of the theme.
321      */
322     public FontUIResource getWindowTitleFont() {
323         FontUIResource result = this.windowTitleFont;
324         if (result == null) {
325             result = super.getWindowTitleFont();
326         }
327         return result;
328     }
329 
330     /***
331      * This method enables to give a name to the theme.
332      * 
333      * @param n
334      *            the name.
335      */
336     public void setName(String n) {
337         this.name = n;
338     }
339 
340     /***
341      * This method enables to set the first primary color.
342      * 
343      * @param red
344      * @param green
345      * @param blue
346      */
347     public void setPrimary1(int red, int green, int blue) {
348         this.primary1 = new ColorUIResource(red, green, blue);
349     }
350 
351     /***
352      * This method enables to set the second primary color.
353      * 
354      * @param red
355      * @param green
356      * @param blue
357      */
358     public void setPrimary2(int red, int green, int blue) {
359         this.primary2 = new ColorUIResource(red, green, blue);
360     }
361 
362     /***
363      * This method enables to set the third primary color.
364      * 
365      * @param red
366      * @param green
367      * @param blue
368      */
369     public void setPrimary3(int red, int green, int blue) {
370         this.primary3 = new ColorUIResource(red, green, blue);
371     }
372 
373     /***
374      * This method enables to set the first secondary color.
375      * 
376      * @param red
377      * @param green
378      * @param blue
379      */
380     public void setSecondary1(int red, int green, int blue) {
381         this.secondary1 = new ColorUIResource(red, green, blue);
382     }
383 
384     /***
385      * This method enables to set the second secondary color.
386      * 
387      * @param red
388      * @param green
389      * @param blue
390      */
391     public void setSecondary2(int red, int green, int blue) {
392         this.secondary2 = new ColorUIResource(red, green, blue);
393     }
394 
395     /***
396      * This method enables to set the third secondary color.
397      * 
398      * @param red
399      * @param green
400      * @param blue
401      */
402     public void setSecondary3(int red, int green, int blue) {
403         this.secondary3 = new ColorUIResource(red, green, blue);
404     }
405 
406     /***
407      * This method enables to set the control text font.
408      * 
409      * @param font
410      * @param style
411      * @param size
412      */
413     public void setControlTextFont(String font, int style, int size) {
414         this.controlTextFont = new FontUIResource(font, style, size);
415     }
416 
417     /***
418      * This method enables to set the menu text font.
419      * 
420      * @param font
421      * @param style
422      * @param size
423      */
424     public void setMenuTextFont(String font, int style, int size) {
425         this.menuTextFont = new FontUIResource(font, style, size);
426     }
427 
428     /***
429      * This method enables to set the sub text font.
430      * 
431      * @param font
432      * @param style
433      * @param size
434      */
435     public void setSubTextFont(String font, int style, int size) {
436         this.subTextFont = new FontUIResource(font, style, size);
437     }
438 
439     /***
440      * This method enables to set the system text font.
441      * 
442      * @param font
443      * @param style
444      * @param size
445      */
446     public void setSystemTextFont(String font, int style, int size) {
447         this.systemTextFont = new FontUIResource(font, style, size);
448     }
449 
450     /***
451      * This method enables to set the user text font.
452      * 
453      * @param font
454      * @param style
455      * @param size
456      */
457     public void setUserTextFont(String font, int style, int size) {
458         this.userTextFont = new FontUIResource(font, style, size);
459     }
460 
461     /***
462      * This method enables to set the window title font.
463      * 
464      * @param font
465      * @param style
466      * @param size
467      */
468     public void setWindowTitleFont(String font, int style, int size) {
469         this.windowTitleFont = new FontUIResource(font, style, size);
470     }
471 }