Source code: com/port80/eclipse/jdt/ThemeManager.java
1 package com.port80.eclipse.jdt;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.eclipse.core.internal.plugins.PluginDescriptor;
7 import org.eclipse.swt.events.DisposeEvent;
8 import org.eclipse.swt.graphics.Color;
9 import org.eclipse.swt.graphics.Cursor;
10 import org.eclipse.swt.graphics.Font;
11 import org.eclipse.swt.graphics.Image;
12
13 import com.port80.eclipse.util.ColorFactory;
14 import com.port80.eclipse.util.CursorFactory;
15 import com.port80.eclipse.util.FontFactory;
16 import com.port80.eclipse.util.ImageFactory;
17
18 public class ThemeManager {
19
20 ////////////////////////////////////////////////////////////////////////
21
22 private static final String NAME = "ThemeManager";
23 private static final boolean DEBUG = true;
24
25 private static final String[] DEFAULT_COLORS = new String[] { //
26 "Default", "0xe0e0e0", // Default background.
27 "Background", "0xe0e0e0", // Default background.
28 "Text", "0x000000", // Default text color.
29 "HeadBG", "DarkSlateGrey", //
30 "LightBG", "0xf0f0f0", // Default background.
31 "DarkBG", "SlateGrey", //
32 };
33
34 private static final String[] DEFAULT_FONTS = new String[] { //
35 "Default", "verdana--8", // Default normal font.
36 "Large", "verdana--10", //
37 "LargeBold", "verdana-bold-10", //
38 "Bold", "verdana-bold-8", //
39 "Title", "verdana-bold-14", //
40 //
41 "Fixed", "lucidatypewriter--12", //Fixed default.
42 "FixedBold", "lucidatypewriter-bold-12", //Fixed default.
43 };
44
45 private static final String[] DEFAULT_IMAGES = new String[] { //
46 "DownArrow", "icons/full/ctool16/next_nav.gif", //
47 "UpArrow", "icons/full/ctool16/prev_nav.gif", //
48 };
49
50 private static final String[] DEFAULT_CURSORS = new String[] { //
51 "Default", "IBeam", //
52 "Wait", "Wait", //
53 };
54
55 private static final String[] STANDARD_COLORS = new String[] { //
56 "black", "white", "gray", //
57 "red", "green", "blue", //
58 "yellow", "orange", "cyan", //
59 };
60
61 ////////////////////////////////////////////////////////////////////////
62
63 private String fTheme;
64 private ColorFactory fColorFactory;
65 private FontFactory fFontFactory;
66 private CursorFactory fCursorFactory;
67 private ImageFactory fImageFactory;
68 //
69 // Theme color name->system color name mapping.
70 private Map fColorTable = new HashMap();
71 // Theme font name-> system font name mapping.
72 private Map fFontTable = new HashMap();
73 private Map fCursorTable = new HashMap();
74 // Themem image name->system image name (filepath) mapping.
75 private Map fImageTable = new HashMap();
76 private String fPluginURL;
77
78 ////////////////////////////////////////////////////////////////////////
79
80 public ThemeManager() {
81 fColorFactory = new ColorFactory();
82 fFontFactory = new FontFactory();
83 fCursorFactory = new CursorFactory();
84 fImageFactory = new ImageFactory();
85 init();
86 }
87
88 ////////////////////////////////////////////////////////////////////////
89
90 //FIXME: For now, all hardcoded.
91 private void init() {
92 PluginDescriptor desc = (PluginDescriptor) JdtPlugin.getDefault().getDescriptor();
93 fPluginURL = desc.getInstallURLInternal().getFile();
94 for (int i = 0; i < DEFAULT_COLORS.length; i += 2) {
95 fColorTable.put(DEFAULT_COLORS[i].toLowerCase(), DEFAULT_COLORS[i + 1].toLowerCase());
96 }
97 for (int i = 0; i < STANDARD_COLORS.length; ++i) {
98 fColorTable.put(STANDARD_COLORS[i].toLowerCase(), STANDARD_COLORS[i].toLowerCase());
99 }
100 for (int i = 0; i < DEFAULT_FONTS.length; i += 2) {
101 fFontTable.put(DEFAULT_FONTS[i].toLowerCase(), DEFAULT_FONTS[i + 1].toLowerCase());
102 }
103 for (int i = 0; i < DEFAULT_IMAGES.length; i += 2) {
104 fImageTable.put(DEFAULT_IMAGES[i].toLowerCase(), DEFAULT_IMAGES[i + 1]);
105 }
106 for (int i = 0; i < DEFAULT_CURSORS.length; i += 2) {
107 fCursorTable.put(DEFAULT_CURSORS[i].toLowerCase(), DEFAULT_CURSORS[i + 1]);
108 }
109 }
110
111 ////////////////////////////////////////////////////////////////////////
112
113 public String getColorName(String name) {
114 return (String) fColorTable.get(name.toLowerCase());
115 }
116
117 public String getFontName(String name) {
118 return (String) fFontTable.get(name.toLowerCase());
119 }
120
121 public String getImageName(String name) {
122 return (String) fImageTable.get(name.toLowerCase());
123 }
124
125 public String getCursorName(String name) {
126 return (String) fCursorTable.get(name.toLowerCase());
127 }
128
129 ////////////////////////////////////////////////////////////////////////
130
131 public FontFactory getFontFactory() {
132 return fFontFactory;
133 }
134
135 ////////////////////////////////////////////////////////////////////////
136
137 public Color getColor(String name) {
138 String colorname;
139 if (name.startsWith("0x"))
140 colorname = name;
141 else
142 colorname = (String) fColorTable.get(name.toLowerCase());
143 Color ret = null;
144 if (colorname != null)
145 ret = fColorFactory.create(colorname);
146 if (ret != null)
147 return ret;
148 JdtPlugin.log(NAME + ".getColor(): color==null: name=" + name);
149 return null;
150 }
151
152 public Font getFont(String name) {
153 String fontname = (String) fFontTable.get(name.toLowerCase());
154 Font ret = null;
155 if (fontname != null)
156 ret = fFontFactory.create(fontname);
157 if (ret != null)
158 return ret;
159 JdtPlugin.log(NAME + ".getFont(): font==null: name=" + name);
160 return null;
161 }
162
163 public Cursor getCursor(String name) {
164 String cursorname = (String) fCursorTable.get(name.toLowerCase());
165 Cursor ret = null;
166 if (cursorname != null)
167 ret = fCursorFactory.create(cursorname);
168 if (ret != null)
169 return ret;
170 JdtPlugin.log(NAME + ".getCursor(): cursor==null: name=" + name);
171 return null;
172 }
173
174 public Image getImage(String name) {
175 // fPluginURL = JdtPlugin.getDefault().getDescriptor().getInstallURL().getFile();
176 String filepath = fPluginURL + (String) fImageTable.get(name.toLowerCase());
177 if (DEBUG)
178 System.err.println(NAME + ".getImage(): filepath=" + filepath);
179 Image ret = null;
180 if (filepath != null)
181 ret = fImageFactory.create(filepath);
182 if (ret != null)
183 return ret;
184 JdtPlugin.log(NAME + ".getImage(): image==null: name=" + name);
185 return null;
186 }
187
188 ////////////////////////////////////////////////////////////////////////
189
190 /**
191 * Application can hook to this listener to the main window instead of the individual factories.
192 * DisposeListener should be invoked only when application exits.
193 * All factory DisposeListener can be called more than once without harm.
194 */
195 public void widgetDisposed(DisposeEvent e) {
196 dispose();
197 }
198
199 public void dispose() {
200 fColorFactory.dispose();
201 fFontFactory.dispose();
202 fImageFactory.dispose();
203 fCursorFactory.dispose();
204 }
205
206 ////////////////////////////////////////////////////////////////////////
207
208 }