Source code: com/port80/theme/ThemeManager.java
1 //
2 // Copyright(c) 2002, Chris Leung
3 //
4
5 package com.port80.theme;
6
7 import java.awt.Color;
8 import java.awt.Font;
9 import java.awt.Image;
10 import java.awt.Toolkit;
11 import java.awt.image.ImageProducer;
12 import java.io.InputStream;
13 import java.net.URL;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19
20 import javax.swing.ImageIcon;
21
22 import org.xml.sax.helpers.DefaultHandler;
23
24 import com.port80.util.Conf;
25 import com.port80.util.Debug;
26 import com.port80.util.msg;
27
28 /** Theme manager manage UI resources loading and caching.
29 *
30 * This class parse an config. file in XML format that defines
31 * resources for the theme.
32 *
33 * @see Conf.dtd that defines the XML format.
34 *
35 */
36
37 public class ThemeManager extends DefaultHandler {
38
39 // Static fields ///////////////////////////////////////////////////////
40 //
41
42 private static final String PACKAGENAME ="com.port80.theme";
43 private static final String NAME ="ThemeManager";
44 private static final String CLASSNAME =PACKAGENAME+"."+NAME;
45 private static final String RESOURCEDIR ="com/port80/theme/themes";
46
47 private static final String tCf = "cf";
48 private static final String tMap = "bag";
49 private static final String tVector = "seq";
50 private static final String tKey = "key";
51 private static final String tValue = "v";
52
53 static {
54 Debug.add(CLASSNAME);
55 }
56
57
58
59 // Instance fields /////////////////////////////////////////////////////
60 //
61
62 protected static final boolean NEVER =false;
63 protected boolean DEBUG =false;
64 protected boolean VERBOSE =false;
65 {
66 DEBUG=Debug.isEnabled(PACKAGENAME)||Debug.isEnabled(CLASSNAME);
67 VERBOSE=Debug.isVerbose();
68 }
69 // Config. data
70 String themeName="default";
71 Conf cf=new Conf();
72 Map colorTable=new HashMap();
73 Map fontTable=new HashMap();
74 Map iconTable=new HashMap();
75 Map decorTable=new HashMap();
76
77 // Constructors ////////////////////////////////////////////////////////
78 //
79
80 public ThemeManager() {
81 this(null);
82 }
83
84 public ThemeManager(String themename) {
85 super();
86 if(themename!=null) themeName=themename;
87 //
88 try {
89 String fname=RESOURCEDIR+"/"+themeName+".xml";
90 InputStream is=ClassLoader.getSystemResourceAsStream(fname);
91 if(is!=null) {
92 cf.read(is);
93 if(false) msg.println(NAME+"(): theme="+themeName+", cf=\n"+cf.toString());
94 loadColors();
95 loadFonts();
96 loadIcons();
97 } else {
98 msg.err("ThemeManager(): Can't load theme config. file: "+fname);
99 }
100 } catch(Exception e) {
101 msg.warn("theme definition not found: "+PACKAGENAME+"."+themeName+".xml");
102 }
103 }
104
105 private void loadColors() {
106 Map colors=(Map)cf.get("colors");
107 if(colors==null) {
108 msg.err("ThemeManager.loadColors(): colors==null");
109 return;
110 }
111 String colorname,colorvalue;
112 int c;
113 for(Iterator it=colors.keySet().iterator(); it.hasNext();) {
114 colorname=(String)it.next();
115 colorvalue=(String)colors.get(colorname);
116 try {
117 c=Integer.parseInt(colorvalue,16);
118 } catch(Exception e) {
119 msg.err("ThemeManager.loadColors(): Error parsing color: name="+colorname+", value="+colorvalue);
120 c=0;
121 }
122 colorTable.put(colorname,new Color(c));
123 }
124 }
125
126 private void loadFonts() {
127 Map fonts=(Map)cf.get("fonts");
128 if(fonts==null) {
129 msg.err("ThemeManager.loadFonts(): fonts==null");
130 return;
131 }
132 List attrs;
133 String name,face,s;
134 int weight,size;
135 for(Iterator it=fonts.keySet().iterator(); it.hasNext();) {
136 name=(String)it.next();
137 attrs=(List)fonts.get(name);
138 //
139 face=(String)attrs.get(0);
140 //
141 s=(String)attrs.get(1);
142 weight=Font.PLAIN;
143 if(s.equals("bold")) weight=Font.BOLD;
144 //
145 try {
146 size=Integer.parseInt((String)attrs.get(2),12);
147 } catch(Exception e) {
148 msg.err("ThemeManager.loadFonts(): Error parsing font size: "+attrs.get(2));
149 size=12;
150 }
151 fontTable.put(name,new Font(face,weight,size));
152 }
153 }
154
155 private void loadIcons() {
156 String icondir=cf.getString("iconDir");
157 if(icondir==null) icondir="/icons";
158 Map icons=(Map)cf.get("icons");
159 if(icons==null) {
160 msg.err("ThemeManager.loadIcons(): icons==null");
161 return;
162 }
163 // MediaTracker tracker=new MediaTracker((Component)App.getApp());
164 String iconname=null;
165 URL iconurl=null;
166 Image image;
167 for(Iterator it=icons.keySet().iterator(); it.hasNext();) {
168 iconname=(String)it.next();
169 // msg.debug("# icon: name="+iconname+", url="+iconurl);
170 try {
171 iconurl=ClassLoader.getSystemResource(icondir+"/"+icons.get(iconname));
172 image=Toolkit.getDefaultToolkit().createImage((ImageProducer)iconurl.getContent());
173 } catch(Exception e){
174 msg.warn("ConfTreeRenderer.loadIcons(): Error loading icon"
175 +": name="+iconname
176 +": path="+icondir+"/"+icons.get(iconname)
177 +": url="+iconurl
178 +": error="+e.getMessage()
179 );
180 image=null;
181 }
182 if(image!=null) {
183 // tracker.addImage(image, 0);
184 iconTable.put(iconname,new ImageIcon(image));
185 }
186 }
187 /*
188 try {
189 tracker.waitForAll();
190 } catch(InterruptedException ex){
191 msg.printStack("ConfTreeRenderer.loadIcons(): "
192 +"media tracker interrupted!\n"
193 +" "+ex.getMessage()
194 );
195 }
196 */
197 }
198
199 ////////////////////////////////////////
200
201 public Object get(String name) {
202 return cf.get(name);
203 }
204 public int getint(String name,int def) {
205 return cf.getInt(name,def);
206 }
207 public String getString(String name) {
208 return cf.getString(name);
209 }
210
211 /*
212 public Object set(String name,Object value) {
213 return cf.set(name,value);
214 }
215 */
216
217 ////////////////////////////////////////
218
219 public Font getFont(String name){
220 return(Font)fontTable.get(name);
221 }
222 public Font setFont(String name,Font font){
223 return (Font)fontTable.put(name,font);
224 }
225 public Set getFontNames() {
226 return fontTable.keySet();
227 }
228
229 ////////////////////////////////////////
230
231 public ImageIcon getIcon(String name) {
232 return(ImageIcon)iconTable.get(name);
233 }
234 public ImageIcon setIcon(String name,ImageIcon icon) {
235 return (ImageIcon)iconTable.put(name,icon);
236 }
237 public Set getIconNames() {
238 return iconTable.keySet();
239 }
240
241 ////////////////////////////////////////
242
243 public Color getColor(String name) {
244 return(Color)colorTable.get(name);
245 }
246 public Color setColor(String name,Color color){
247 return (Color)colorTable.put(name,color);
248 }
249 public Set getColorNames() {
250 return colorTable.keySet();
251 }
252
253
254 // Main ////////////////////////////////////////////////////////////////
255 //
256
257 public static void main(String[] args) throws Exception {
258 ThemeManager app=new ThemeManager("kde");
259 app.test();
260 System.exit(0);
261 }
262
263 public void test() {
264
265 System.out.println("### getColorNames()="+getColorNames());
266 System.out.println("### getFontNames()="+getFontNames());
267 System.out.println("### getIconNames()="+getIconNames());
268 }
269
270 ////////////////////////////////////////////////////////////////////////
271 }
272
273