Statistics
| Revision:

root / trunk / frameworks / _fwAndami / src / com / iver / andami / iconthemes2 / AbstractIconTheme.java @ 15633

History | View | Annotate | Download (9.34 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib��ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

    
42
package com.iver.andami.iconthemes2;
43

    
44

    
45
import java.net.URL;
46
import java.util.HashMap;
47

    
48
import javax.swing.ImageIcon;
49

    
50
import org.apache.log4j.Logger;
51

    
52
import com.iver.andami.PluginServices;
53

    
54

    
55
/**
56
 * <p>This class represents an icon theme, which is basically a mapping of
57
 * symbolic icon names, and real icons (or icon paths). This is useful to
58
 * change an application's icons in an easy way. An icon theme
59
 * is usually read from disk on start up, but can also be created or
60
 * modified on a later time.</p>
61
 *
62
 * <p>Developers are encouraged to always use the
63
 * <code>get(iconName, fallbackImage)</code> methods to get icons,
64
 * as they ensure that the icons are not overwritten in the theme, but it
65
 * also ensures than an image is got in case the icon was still not
66
 * registered. Note that in this case, the iconName gets registered
67
 * (it is associated with the provided fallbackImage).
68
 * </p>
69
 *
70
 * <p>Developers are encouraged to NOT override icons which are
71
 * present in the theme, as this defeats the purpose of IconThemes.</p>
72
 *
73
 * @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
74
 */
75
public abstract class AbstractIconTheme {
76
        private HashMap iconList = new HashMap();
77
        private String name=null;
78
        private String description=null;
79
        private String version="1.0";
80
        private Object resource=null;
81
        private AbstractIconTheme defaultTheme = null;
82

    
83

    
84
        /**
85
         * Abstract method that allows load an a icon. This method will be reimplemented by
86
         * inherit classes.
87
         * @param iconName
88
         * @param resource
89
         * @return
90
         */
91
        protected abstract ImageIcon loadIcon(String iconName,Object resource) ;
92

    
93
        /**
94
         * Load all icons from the IconTheme
95
         */
96
        public abstract void load() ;
97

    
98
        /**
99
         * Returns the logger
100
         * @return
101
         */
102
    protected Logger log() {
103
      return Logger.getLogger(this.getClass());
104
    }
105

    
106
    public void setDefault(AbstractIconTheme def){
107
            defaultTheme = def;
108
    }
109

    
110
    public AbstractIconTheme getDefault(){
111
            return defaultTheme;
112
    }
113

    
114
        /**
115
         * Returns <code>true</code> if the icon theme contains a mapping for the
116
         * specified iconName.
117
         *
118
         * @param iconName The key to check if it has been registered in this
119
         * IconTheme
120
         *
121
         * @return <code>true</code> if this IconTheme contains
122
         * <code>iconName</code>, <code>false</code> otherwise.
123
         */
124
        public boolean exists(String iconName) {
125
                if (iconList.containsKey(iconName))return true;
126
                if (defaultTheme.exists(iconName))return true;
127
                return false;
128
        }
129

    
130
        /**
131
         * Gets the ImageIcon associated with the provided key, if the key
132
         * is present in the theme, or returns <code>null</code> otherwise.
133
         *
134
         * @param iconName
135
         *                         The key whose associated icon is to be retrieved
136
         *
137
         * @return
138
         *                         The icon associated with the provided key, or
139
         * <code>null</code> otherwise.
140
         */
141
        public ImageIcon get(String iconName) {
142
                Object object = iconList.get(iconName);
143
                if (object!=null) {
144
                        if( object instanceof URL ) {
145
                                return new ImageIcon((URL)object);
146
                        }
147
                        ImageIcon icon = loadIcon(iconName,object);
148
                        if( icon != null ) {
149
                                return icon;
150
                        }
151
                }
152
                if (defaultTheme!=null){
153
                        object =  defaultTheme.get(iconName);
154
                        if (object!=null){
155
                                if( object instanceof ImageIcon )
156
                                        return (ImageIcon) object;
157
                                return loadIcon(iconName,object);
158
                                }
159
                }
160
                if(get("no-icon")!=null)return get("no-icon");
161
                else return null;
162
        }
163

    
164
        /**
165
         * <p>Register in this theme the provided iconName and the associated
166
         * image. Developers
167
         * must not override icons already registered, as this defeats the
168
         * purpose of the IconTheme. Therefore, use the <code>exists</code>
169
         * method before using <code>register</code>, to ensure the icon
170
         * is not previously registered.</p>
171
         *
172
         * @param iconName The name of the icon to register. It is the name
173
         * that will be used later to retrieve the icon.
174
         *
175
         * @param image The image that is going to be associated with the
176
         * provided icon name.
177
         */
178
        public void registerDefault(String iconName, ImageIcon image) {
179
                if (defaultTheme!=null)        defaultTheme.register(iconName, image);
180
                else register(iconName, image);
181
        }
182

    
183
        /**
184
         * <p>Register in this theme the provided iconName and the associated
185
         * resource. Developers must not override icons already registered,
186
         * as this defeats the purpose of the IconTheme. Therefore, use the
187
         * <code>exists</code> method before using <code>register</code>, to
188
         * ensure the icon is not previously registered.</p>
189
         *
190
         * @param iconName The name of the icon to register. It is the name
191
         *         that will be used later to retrieve the icon.
192
         * @param resource The resource that is going to be asssocioated with the providad
193
         *         icon name
194
         */
195
        public void registerDefault(String iconName, Object resource) {
196
                if (defaultTheme!=null)defaultTheme.register(iconName, resource);
197
                else register(iconName, resource);
198
        }
199

    
200
        /**
201
         * <p>Register in this theme the provided iconName and the associated
202
         * image. Developers
203
         * must not override icons already registered, as this defeats the
204
         * purpose of the IconTheme. Therefore, use the <code>exists</code>
205
         * method before using <code>register</code>, to ensure the icon
206
         * is not previously registered.</p>
207
         *
208
         * @param iconName The name of the icon to register. It is the name
209
         * that will be used later to retrieve the icon.
210
         *
211
         * @param image The image that is going to be associated with the
212
         * provided icon name.
213
         */
214
        public void register(String iconName, ImageIcon image) {
215
                iconList.put(iconName, image);
216
        }
217

    
218
        /**
219
         * <p>Register in this theme the provided iconName and the associated
220
         * resource. Developers must not override icons already registered,
221
         * as this defeats the purpose of the IconTheme. Therefore, use the
222
         * <code>exists</code> method before using <code>register</code>, to
223
         * ensure the icon is not previously registered.</p>
224
         *
225
         * @param iconName The name of the icon to register. It is the name
226
         *         that will be used later to retrieve the icon.
227
         * @param resource The resource that is going to be asssocioated with the providad
228
         *         icon name
229
         */
230
        public void register(String iconName, Object resource) {
231
                iconList.put(iconName, resource);
232
        }
233

    
234
        /**
235
         * Gets the theme name.
236
         * @return theme name
237
         */
238
        public String getName() {
239
                return name;
240
        }
241

    
242
        /**
243
         * Sets the theme name.
244
         *
245
         * @param themeName
246
         */
247
        public void setName(String themeName) {
248
                name = themeName;
249
        }
250

    
251
        /**
252
         * Gets the theme description.
253
         *
254
         * @return The description of this theme.
255
         */
256
        public String getDescription() {
257
                return description;
258
        }
259

    
260
        /**
261
         * Sets the theme description. It should be a short description
262
         * (around 20-30 words), including the highlights of the theme,
263
         * the author and maybe its email address or a link the the theme's
264
         * homepage.
265
         *
266
         * @param description
267
         */
268
        public void setDescription(String description) {
269
                this.description = description;
270
        }
271

    
272
        /**
273
         * Returns the theme version. It defaults to "1.0".
274
         *
275
         * @return The version of this theme.
276
         */
277
        public String getVersion() {
278
                return version;
279
        }
280

    
281
        /**
282
         * Set the theme version.
283
         *
284
         * @param version
285
         */
286
        public void setVersion(String version) {
287
                this.version = version;
288
        }
289

    
290
        /**
291
         * Gets the Object which contains physically contains this theme on disk.
292
         * It may be a ZipFile or JarFile, or a directory.
293
         *
294
         * @return
295
         */
296
        public Object getResource() {
297
                return resource;
298
        }
299
        /**
300
         * Sets the file which contains physically contains this theme on disk.
301
         * It may be a ZipFile or JarFile, or a directory.
302
         *
303
         * @return
304
         */
305
        public void setResource(Object resource) {
306
                this.resource = resource;
307
        }
308

    
309
        /**
310
         * Returns the name of the icon theme
311
         */
312
        public String toString() {
313
                return getName();
314
        }
315

    
316
        /**
317
         * Return the URL which is currently associated with the
318
         * provided icon name, if this icon was registered as an
319
         * URL, or <code>null</code> if it is not present in the theme
320
         * or it was registered as an IconImage.
321
         *
322
         * @param iconName
323
         * @return The URL which is currently associated with the
324
         * provided icon name, if this icon was registered as an
325
         * URL, or <code>null</code> if it is not present in the theme
326
         * or it was registered as an IconImage.
327
         */
328
        public URL getURL(String iconName) {
329
                Object object = defaultTheme.get(iconName);
330
                if (object !=null && object instanceof URL)
331
                        return (URL) object;
332
                return null;
333
        }
334

    
335

    
336
}