Statistics
| Revision:

root / tags / J2ME_compat_v1_2_Build_1209 / frameworks / _fwAndami / src / com / iver / andami / iconthemes / AbstractIconTheme.java @ 19520

History | View | Annotate | Download (7.85 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

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

    
80

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

    
90
        /* (non-Javadoc)
91
         * @see com.iver.andami.iconthemes.IIconTheme#load()
92
         */
93
        public abstract void load() ;
94

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

    
103
    /* (non-Javadoc)
104
         * @see com.iver.andami.iconthemes.IIconTheme#setDefault(com.iver.andami.iconthemes.AbstractIconTheme)
105
         */
106
    public void setDefault(IIconTheme def){
107
            if( def == this ) {
108
                    defaultTheme = null;
109
            } else {
110
                    defaultTheme = def;
111
            }
112
    }
113

    
114
    /* (non-Javadoc)
115
         * @see com.iver.andami.iconthemes.IIconTheme#getDefault()
116
         */
117
    public IIconTheme getDefault(){
118
            return defaultTheme;
119
    }
120

    
121
        /* (non-Javadoc)
122
         * @see com.iver.andami.iconthemes.IIconTheme#exists(java.lang.String)
123
         */
124
        public boolean exists(String iconName) {
125
                if (iconList.containsKey(iconName)) {
126
                        return true;
127
                }
128
                if (defaultTheme !=null && defaultTheme.exists(iconName)){
129
                        return true;
130
                }
131
                return false;
132
        }
133

    
134
        /* (non-Javadoc)
135
         * @see com.iver.andami.iconthemes.IIconTheme#get(java.lang.String)
136
         */
137
        public ImageIcon get(String iconName) {
138
                return get(iconName,null);
139
        }
140

    
141
        /* (non-Javadoc)
142
         * @see com.iver.andami.iconthemes.IIconTheme#get(java.lang.String, java.lang.ClassLoader)
143
         */
144
        public ImageIcon get(String iconName, ClassLoader loader) {
145

    
146
                if( loader != null && iconName.contains(".") ) {
147
                        Logger.getLogger(this.getClass()).warn("Loading icon from resource: '"+ iconName+"'");
148
                        return toImageIcon(loader.getResource(iconName),iconName);
149
                }
150

    
151
                if (defaultTheme==null ){
152
                        Object object = iconList.get(iconName);
153
                        if (object!=null) {
154
                                return toImageIcon(object,iconName);
155
                        }
156
                        return getNoIcon();
157
                }
158
                Object object = iconList.get(iconName);
159
                if (object!=null) {
160
                        return toImageIcon(object,iconName);
161
                }
162
                if( defaultTheme.exists(iconName)) {
163
                        return  defaultTheme.get(iconName, null);
164
                }
165
                return getNoIcon();
166
        }
167

    
168
        public ImageIcon getNoIcon() {
169
                Object object = iconList.get("no-icon");
170
                if (object!=null){
171
                        return toImageIcon(object,"no-icon");
172
                }
173
                if (defaultTheme==null ){
174
                        return null;
175
                }
176
                return defaultTheme.getNoIcon();
177
        }
178

    
179

    
180
        protected ImageIcon toImageIcon(Object object,String iconName){
181
                if (object == null){
182
                        return null;
183
                }
184
                if( object instanceof URL ) {
185
                        return new ImageIcon((URL)object);
186
                }
187
                ImageIcon icon = loadIcon(iconName,object);
188
                return icon;
189
        }
190

    
191
        /* (non-Javadoc)
192
         * @see com.iver.andami.iconthemes.IIconTheme#registerDefault(java.lang.String, javax.swing.ImageIcon)
193
         */
194
        public void registerDefault(String iconName, ImageIcon image) {
195
                if (defaultTheme!=null)        defaultTheme.register(iconName, image);
196
                else register(iconName, image);
197
        }
198

    
199
        /* (non-Javadoc)
200
         * @see com.iver.andami.iconthemes.IIconTheme#registerDefault(java.lang.String, java.lang.Object)
201
         */
202
        public void registerDefault(String iconName, Object resource) {
203
                if (defaultTheme!=null)defaultTheme.register(iconName, resource);
204
                else register(iconName, resource);
205
        }
206

    
207
        /* (non-Javadoc)
208
         * @see com.iver.andami.iconthemes.IIconTheme#register(java.lang.String, javax.swing.ImageIcon)
209
         */
210
        public void register(String iconName, ImageIcon image) {
211
                iconList.put(iconName, image);
212
        }
213

    
214
        /* (non-Javadoc)
215
         * @see com.iver.andami.iconthemes.IIconTheme#register(java.lang.String, java.lang.Object)
216
         */
217
        public void register(String iconName, Object resource) {
218
                iconList.put(iconName, resource);
219
        }
220

    
221
        /* (non-Javadoc)
222
         * @see com.iver.andami.iconthemes.IIconTheme#getName()
223
         */
224
        public String getName() {
225
                return name;
226
        }
227

    
228
        /* (non-Javadoc)
229
         * @see com.iver.andami.iconthemes.IIconTheme#setName(java.lang.String)
230
         */
231
        public void setName(String themeName) {
232
                name = themeName;
233
        }
234

    
235
        /* (non-Javadoc)
236
         * @see com.iver.andami.iconthemes.IIconTheme#getDescription()
237
         */
238
        public String getDescription() {
239
                return description;
240
        }
241

    
242
        /* (non-Javadoc)
243
         * @see com.iver.andami.iconthemes.IIconTheme#setDescription(java.lang.String)
244
         */
245
        public void setDescription(String description) {
246
                this.description = description;
247
        }
248

    
249
        /* (non-Javadoc)
250
         * @see com.iver.andami.iconthemes.IIconTheme#getVersion()
251
         */
252
        public String getVersion() {
253
                return version;
254
        }
255

    
256
        /* (non-Javadoc)
257
         * @see com.iver.andami.iconthemes.IIconTheme#setVersion(java.lang.String)
258
         */
259
        public void setVersion(String version) {
260
                this.version = version;
261
        }
262

    
263
        /* (non-Javadoc)
264
         * @see com.iver.andami.iconthemes.IIconTheme#getResource()
265
         */
266
        public Object getResource() {
267
                return resource;
268
        }
269
        /* (non-Javadoc)
270
         * @see com.iver.andami.iconthemes.IIconTheme#setResource(java.lang.Object)
271
         */
272
        public void setResource(Object resource) {
273
                this.resource = resource;
274
        }
275

    
276
        /**
277
         * Returns the name of the icon theme
278
         */
279
        public String toString() {
280
                return getName();
281
        }
282

    
283
        /* (non-Javadoc)
284
         * @see com.iver.andami.iconthemes.IIconTheme#getURL(java.lang.String)
285
         *
286
         */
287
        public URL getURL(String iconName) {
288
                Object object = defaultTheme.get(iconName);
289
                if (object !=null && object instanceof URL)
290
                        return (URL) object;
291
                return null;
292
        }
293

    
294

    
295
}