1 package org.glassbox;
2
3 import java.awt.*;
4 import javax.swing.*;
5 import javax.swing.border.*;
6
7 /***
8 The theme encapsulates the visual apprearance of an application.
9 The theme is build from a properties file defining all resources needed for the application.
10 If the System property <code>org.glassbox.Theme</code> is set to a valid file name this file
11 is used as configuration. Otherwise a default configuration is used.
12 @author Tilman Linden
13 */
14 public class Theme {
15 private static final Font FONT = new Font("arial", Font.PLAIN, 10);
16 private static final Color COLOR = Color.white;
17 private static final Paint PAINT = Color.black;
18 private static final Cursor CURSOR = Cursor.getDefaultCursor();
19 private static final Insets INSETS = new Insets(3, 3, 3, 3);
20 private static final Stroke STROKE = new BasicStroke();
21 private static final Icon ICON = null;
22 private static final Border BORDER = BorderFactory.createLineBorder(Color.black, 1);
23 private static final Dimension SIZE = new Dimension(0, 0);
24 private static final String TEXT = "";
25
26 private static ThemeProperties _properties;
27
28 private static ThemeProperties DEFAULT = new ThemeProperties() {
29 public Font getFont(String key) {
30 return FONT;
31 }
32
33 public Color getColor(String key) {
34 return COLOR;
35 }
36
37 public Cursor getCursor(String key) {
38 return CURSOR;
39 }
40
41 public Paint getPaint(String key) {
42 return PAINT;
43 }
44
45 public Insets getInsets(String key) {
46 return INSETS;
47 }
48
49 public Stroke getStroke(String key) {
50 return STROKE;
51 }
52
53 public Icon getIcon(String key) {
54 return ICON;
55 }
56
57 public Border getBorder(String key) {
58 return BORDER;
59 }
60
61 public Dimension getSize(String key) {
62 return SIZE;
63 }
64
65 public String getText(String key) {
66 return TEXT;
67 }
68 };
69
70 static {
71 _properties = DEFAULT;
72 String theme = Glassbox.getProperty("org.glassbox.Theme");
73 if (theme!=null) {
74 try {
75 ThemeBuilder builder = new SimpleThemeBuilder(Theme.class.getResourceAsStream(theme));
76 _properties = builder.build();
77 } catch (Exception x) {
78 x.printStackTrace();
79
80 }
81 }
82 }
83
84 public static Font getFont(String id) {
85 Font font = _properties.getFont(id);
86 return font==null?FONT:font;
87 }
88
89 public static Color getColor(String id) {
90 Color color = _properties.getColor(id);
91 return color==null?COLOR:color;
92 }
93
94 public static Cursor getCursor(String id) {
95 Cursor cursor = _properties.getCursor(id);
96 return cursor==null?CURSOR:cursor;
97 }
98
99 public static Paint getPaint(String id) {
100 Paint paint = _properties.getPaint(id);
101 return paint==null?PAINT:paint;
102 }
103
104 public static Insets getInsets(String id) {
105 Insets insets = _properties.getInsets(id);
106 return insets==null?INSETS:insets;
107 }
108
109 public static Stroke getStroke(String id) {
110 Stroke stroke = _properties.getStroke(id);
111 return stroke==null?STROKE:stroke;
112 }
113
114 public static Icon getIcon(String id) {
115 Icon icon = _properties.getIcon(id);
116 return icon==null?ICON:icon;
117 }
118
119 public static Border getBorder(String id) {
120 Border border = _properties.getBorder(id);
121 return border==null?BORDER:border;
122 }
123
124 public static Dimension getSize(String id) {
125 Dimension dim = _properties.getSize(id);
126 return dim==null?SIZE:dim;
127 }
128
129 public static String getText(String id) {
130 String text = _properties.getText(id);
131 return text==null?TEXT:text;
132 }
133 }