root / trunk / frameworks / _fwAndami / src / com / iver / andami / iconthemes / AbstractIconTheme.java @ 15918
History | View | Annotate | Download (9.78 KB)
1 |
/*
|
---|---|
2 |
* Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
|
3 |
*
|
4 |
* This program is free software; you can redistribute it and/or
|
5 |
* modify it under the terms of the GNU General Public License
|
6 |
* as published by the Free Software Foundation; either version 2
|
7 |
* of the License, or (at your option) any later version.
|
8 |
*
|
9 |
* This program is distributed in the hope that it will be useful,
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
* GNU General Public License for more details.
|
13 |
*
|
14 |
* You should have received a copy of the GNU General Public License
|
15 |
* along with this program; if not, write to the Free Software
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
17 |
*
|
18 |
* For more information, contact:
|
19 |
*
|
20 |
* Generalitat Valenciana
|
21 |
* Conselleria d'Infraestructures i Transport
|
22 |
* Av. Blasco Ib��ez, 50
|
23 |
* 46010 VALENCIA
|
24 |
* SPAIN
|
25 |
*
|
26 |
* +34 963862235
|
27 |
* gvsig@gva.es
|
28 |
* www.gvsig.gva.es
|
29 |
*
|
30 |
* or
|
31 |
*
|
32 |
* IVER T.I. S.A
|
33 |
* Salamanca 50
|
34 |
* 46005 Valencia
|
35 |
* Spain
|
36 |
*
|
37 |
* +34 963163400
|
38 |
* dac@iver.es
|
39 |
*/
|
40 |
|
41 |
package com.iver.andami.iconthemes; |
42 |
|
43 |
|
44 |
import java.net.URL; |
45 |
import java.util.HashMap; |
46 |
|
47 |
import javax.swing.ImageIcon; |
48 |
|
49 |
import org.apache.log4j.Logger; |
50 |
|
51 |
import com.iver.andami.PluginServices; |
52 |
|
53 |
|
54 |
/**
|
55 |
* <p>This class represents an icon theme, which is basically a mapping of
|
56 |
* symbolic icon names, and real icons (or icon paths). This is useful to
|
57 |
* change an application's icons in an easy way. An icon theme
|
58 |
* is usually read from disk on start up, but can also be created or
|
59 |
* modified on a later time.</p>
|
60 |
*
|
61 |
* <p>Developers are encouraged to always use the
|
62 |
* <code>get(iconName, fallbackImage)</code> methods to get icons,
|
63 |
* as they ensure that the icons are not overwritten in the theme, but it
|
64 |
* also ensures than an image is got in case the icon was still not
|
65 |
* registered. Note that in this case, the iconName gets registered
|
66 |
* (it is associated with the provided fallbackImage).
|
67 |
* </p>
|
68 |
*
|
69 |
* <p>Developers are encouraged to NOT override icons which are
|
70 |
* present in the theme, as this defeats the purpose of IconThemes.</p>
|
71 |
*
|
72 |
* @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
|
73 |
*/
|
74 |
public abstract class AbstractIconTheme { |
75 |
private HashMap iconList = new HashMap(); |
76 |
private String name=null; |
77 |
private String description=null; |
78 |
private String version="1.0"; |
79 |
private Object resource=null; |
80 |
private AbstractIconTheme defaultTheme = null; |
81 |
|
82 |
|
83 |
/**
|
84 |
* Abstract method that allows load an a icon. This method will be reimplemented by
|
85 |
* inherit classes.
|
86 |
* @param iconName
|
87 |
* @param resource
|
88 |
* @return
|
89 |
*/
|
90 |
protected abstract ImageIcon loadIcon(String iconName,Object resource) ; |
91 |
|
92 |
/**
|
93 |
* Load all icons from the IconTheme
|
94 |
*/
|
95 |
public abstract void load() ; |
96 |
|
97 |
/**
|
98 |
* Returns the logger
|
99 |
* @return
|
100 |
*/
|
101 |
protected Logger log() { |
102 |
return Logger.getLogger(this.getClass()); |
103 |
} |
104 |
|
105 |
public void setDefault(AbstractIconTheme def){ |
106 |
defaultTheme = def; |
107 |
} |
108 |
|
109 |
public AbstractIconTheme getDefault(){
|
110 |
return defaultTheme;
|
111 |
} |
112 |
|
113 |
/**
|
114 |
* Returns <code>true</code> if the icon theme contains a mapping for the
|
115 |
* specified iconName.
|
116 |
*
|
117 |
* @param iconName The key to check if it has been registered in this
|
118 |
* IconTheme
|
119 |
*
|
120 |
* @return <code>true</code> if this IconTheme contains
|
121 |
* <code>iconName</code>, <code>false</code> otherwise.
|
122 |
*/
|
123 |
public boolean exists(String iconName) { |
124 |
if (iconList.containsKey(iconName))return true; |
125 |
if (defaultTheme.exists(iconName))return true; |
126 |
return false; |
127 |
} |
128 |
|
129 |
/**
|
130 |
* Gets the ImageIcon associated with the provided key, if the key
|
131 |
* is present in the theme, or returns <code>null</code> otherwise.
|
132 |
*
|
133 |
* @param iconName
|
134 |
* The key whose associated icon is to be retrieved
|
135 |
*
|
136 |
* @return
|
137 |
* The icon associated with the provided key, or
|
138 |
* <code>null</code> otherwise.
|
139 |
*/
|
140 |
public ImageIcon get(String iconName) { |
141 |
Object object = iconList.get(iconName);
|
142 |
if (object!=null) { |
143 |
return toImageIcon(object,iconName);
|
144 |
} |
145 |
if (defaultTheme!=null && defaultTheme != this){ |
146 |
object = defaultTheme.get(iconName); |
147 |
if (object!=null){ |
148 |
return toImageIcon(object,iconName);
|
149 |
} |
150 |
} |
151 |
object = iconList.get("no-icon");
|
152 |
if (object == null && (defaultTheme!=null && defaultTheme != this)){ |
153 |
object = defaultTheme.get(iconName); |
154 |
} |
155 |
return toImageIcon(object,iconName);
|
156 |
} |
157 |
|
158 |
protected ImageIcon toImageIcon(Object object,String iconName){ |
159 |
if (object == null){ |
160 |
return null; |
161 |
} |
162 |
if( object instanceof URL ) { |
163 |
return new ImageIcon((URL)object); |
164 |
} |
165 |
ImageIcon icon = loadIcon(iconName,object);
|
166 |
return icon;
|
167 |
} |
168 |
|
169 |
/**
|
170 |
* <p>Register in this theme the provided iconName and the associated
|
171 |
* image. Developers
|
172 |
* must not override icons already registered, as this defeats the
|
173 |
* purpose of the IconTheme. Therefore, use the <code>exists</code>
|
174 |
* method before using <code>register</code>, to ensure the icon
|
175 |
* is not previously registered.</p>
|
176 |
*
|
177 |
* @param iconName The name of the icon to register. It is the name
|
178 |
* that will be used later to retrieve the icon.
|
179 |
*
|
180 |
* @param image The image that is going to be associated with the
|
181 |
* provided icon name.
|
182 |
*/
|
183 |
public void registerDefault(String iconName, ImageIcon image) { |
184 |
if (defaultTheme!=null) defaultTheme.register(iconName, image); |
185 |
else register(iconName, image);
|
186 |
} |
187 |
|
188 |
/**
|
189 |
* <p>Register in this theme the provided iconName and the associated
|
190 |
* resource. Developers must not override icons already registered,
|
191 |
* as this defeats the purpose of the IconTheme. Therefore, use the
|
192 |
* <code>exists</code> method before using <code>register</code>, to
|
193 |
* ensure the icon is not previously registered.</p>
|
194 |
*
|
195 |
* @param iconName The name of the icon to register. It is the name
|
196 |
* that will be used later to retrieve the icon.
|
197 |
* @param resource The resource that is going to be asssocioated with the providad
|
198 |
* icon name
|
199 |
*/
|
200 |
public void registerDefault(String iconName, Object resource) { |
201 |
if (defaultTheme!=null)defaultTheme.register(iconName, resource); |
202 |
else register(iconName, resource);
|
203 |
} |
204 |
|
205 |
/**
|
206 |
* <p>Register in this theme the provided iconName and the associated
|
207 |
* image. Developers
|
208 |
* must not override icons already registered, as this defeats the
|
209 |
* purpose of the IconTheme. Therefore, use the <code>exists</code>
|
210 |
* method before using <code>register</code>, to ensure the icon
|
211 |
* is not previously registered.</p>
|
212 |
*
|
213 |
* @param iconName The name of the icon to register. It is the name
|
214 |
* that will be used later to retrieve the icon.
|
215 |
*
|
216 |
* @param image The image that is going to be associated with the
|
217 |
* provided icon name.
|
218 |
*/
|
219 |
public void register(String iconName, ImageIcon image) { |
220 |
iconList.put(iconName, image); |
221 |
} |
222 |
|
223 |
/**
|
224 |
* <p>Register in this theme the provided iconName and the associated
|
225 |
* resource. Developers must not override icons already registered,
|
226 |
* as this defeats the purpose of the IconTheme. Therefore, use the
|
227 |
* <code>exists</code> method before using <code>register</code>, to
|
228 |
* ensure the icon is not previously registered.</p>
|
229 |
*
|
230 |
* @param iconName The name of the icon to register. It is the name
|
231 |
* that will be used later to retrieve the icon.
|
232 |
* @param resource The resource that is going to be asssocioated with the providad
|
233 |
* icon name
|
234 |
*/
|
235 |
public void register(String iconName, Object resource) { |
236 |
iconList.put(iconName, resource); |
237 |
} |
238 |
|
239 |
/**
|
240 |
* Gets the theme name.
|
241 |
* @return theme name
|
242 |
*/
|
243 |
public String getName() { |
244 |
return name;
|
245 |
} |
246 |
|
247 |
/**
|
248 |
* Sets the theme name.
|
249 |
*
|
250 |
* @param themeName
|
251 |
*/
|
252 |
public void setName(String themeName) { |
253 |
name = themeName; |
254 |
} |
255 |
|
256 |
/**
|
257 |
* Gets the theme description.
|
258 |
*
|
259 |
* @return The description of this theme.
|
260 |
*/
|
261 |
public String getDescription() { |
262 |
return description;
|
263 |
} |
264 |
|
265 |
/**
|
266 |
* Sets the theme description. It should be a short description
|
267 |
* (around 20-30 words), including the highlights of the theme,
|
268 |
* the author and maybe its email address or a link the the theme's
|
269 |
* homepage.
|
270 |
*
|
271 |
* @param description
|
272 |
*/
|
273 |
public void setDescription(String description) { |
274 |
this.description = description;
|
275 |
} |
276 |
|
277 |
/**
|
278 |
* Returns the theme version. It defaults to "1.0".
|
279 |
*
|
280 |
* @return The version of this theme.
|
281 |
*/
|
282 |
public String getVersion() { |
283 |
return version;
|
284 |
} |
285 |
|
286 |
/**
|
287 |
* Set the theme version.
|
288 |
*
|
289 |
* @param version
|
290 |
*/
|
291 |
public void setVersion(String version) { |
292 |
this.version = version;
|
293 |
} |
294 |
|
295 |
/**
|
296 |
* Gets the Object which contains physically contains this theme on disk.
|
297 |
* It may be a ZipFile or JarFile, or a directory.
|
298 |
*
|
299 |
* @return
|
300 |
*/
|
301 |
public Object getResource() { |
302 |
return resource;
|
303 |
} |
304 |
/**
|
305 |
* Sets the file which contains physically contains this theme on disk.
|
306 |
* It may be a ZipFile or JarFile, or a directory.
|
307 |
*
|
308 |
* @return
|
309 |
*/
|
310 |
public void setResource(Object resource) { |
311 |
this.resource = resource;
|
312 |
} |
313 |
|
314 |
/**
|
315 |
* Returns the name of the icon theme
|
316 |
*/
|
317 |
public String toString() { |
318 |
return getName();
|
319 |
} |
320 |
|
321 |
/**
|
322 |
* Return the URL which is currently associated with the
|
323 |
* provided icon name, if this icon was registered as an
|
324 |
* URL, or <code>null</code> if it is not present in the theme
|
325 |
* or it was registered as an IconImage.
|
326 |
*
|
327 |
* @param iconName
|
328 |
* @return The URL which is currently associated with the
|
329 |
* provided icon name, if this icon was registered as an
|
330 |
* URL, or <code>null</code> if it is not present in the theme
|
331 |
* or it was registered as an IconImage.
|
332 |
*/
|
333 |
public URL getURL(String iconName) { |
334 |
Object object = defaultTheme.get(iconName);
|
335 |
if (object !=null && object instanceof URL) |
336 |
return (URL) object; |
337 |
return null; |
338 |
} |
339 |
|
340 |
|
341 |
} |