Statistics
| Revision:

root / branches / v2_0_0_prep / frameworks / _fwAndami / src / org / gvsig / andami / iconthemes / AbstractIconTheme.java @ 38078

History | View | Annotate | Download (8.79 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 org.gvsig.andami.iconthemes;
42

    
43

    
44
import java.net.URL;
45
import java.util.ArrayList;
46
import java.util.Collections;
47
import java.util.HashMap;
48
import java.util.HashSet;
49
import java.util.Iterator;
50
import java.util.Set;
51
import java.util.List;
52

    
53
import javax.swing.ImageIcon;
54

    
55
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
57

    
58

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

    
82
        HashMap<String, Object> iconList = new HashMap<String, Object>();
83
        private String name=null;
84
        private String description=null;
85
        private String version="1.0";
86
        private Object resource=null;
87
        IIconTheme defaultTheme = null;
88

    
89

    
90
        /**
91
         * Abstract method that allows load an a icon. This method will be reimplemented by
92
         * inherit classes.
93
         * @param iconName
94
         * @param resource
95
         * @return
96
         */
97
        protected abstract ImageIcon loadIcon(String iconName,Object resource) ;
98

    
99
        /* (non-Javadoc)
100
         * @see com.iver.andami.iconthemes.IIconTheme#load()
101
         */
102
        public abstract void load() ;
103

    
104
//        /**
105
//         * Returns the logger
106
//         * @return
107
//         */
108
//    protected Logger log() {
109
//      return LoggerFactory.getLogger(this.getClass());
110
//    }
111

    
112
    /* (non-Javadoc)
113
         * @see com.iver.andami.iconthemes.IIconTheme#setDefault(com.iver.andami.iconthemes.AbstractIconTheme)
114
         */
115
    public void setDefault(IIconTheme def){
116
            if( def == this ) {
117
                    defaultTheme = null;
118
            } else {
119
                    defaultTheme = def;
120
            }
121
    }
122

    
123
    /* (non-Javadoc)
124
         * @see com.iver.andami.iconthemes.IIconTheme#getDefault()
125
         */
126
    public IIconTheme getDefault(){
127
            return defaultTheme;
128
    }
129

    
130
        /* (non-Javadoc)
131
         * @see com.iver.andami.iconthemes.IIconTheme#exists(java.lang.String)
132
         */
133
        public boolean exists(String iconName) {
134
                if (iconList.containsKey(iconName)) {
135
                        return true;
136
                }
137
                if (defaultTheme !=null && defaultTheme.exists(iconName)){
138
                        return true;
139
                }
140
                return false;
141
        }
142

    
143
        public Iterator<String> iterator() {
144
                Set<String> names = new HashSet<String>();
145
                
146
                if( defaultTheme !=null ) {
147
                        Iterator<String> it = defaultTheme.iterator();
148
                        while( it.hasNext() ) {
149
                                names.add(it.next());
150
                        }
151
                }
152
                Iterator<String> it = iconList.keySet().iterator();
153
                while( it.hasNext() ) {
154
                        names.add(it.next());
155
                }
156
                List<String> names2 = new ArrayList<String>(names);
157
                Collections.sort(names2);
158
                return names2.iterator();
159
        }
160
        
161
        
162
        
163
        /* (non-Javadoc)
164
         * @see com.iver.andami.iconthemes.IIconTheme#get(java.lang.String)
165
         */
166
        public ImageIcon get(String iconName) {
167
                return get(iconName,null);
168
        }
169

    
170
        /* (non-Javadoc)
171
         * @see com.iver.andami.iconthemes.IIconTheme#get(java.lang.String, java.lang.ClassLoader)
172
         */
173
        public ImageIcon get(String iconName, ClassLoader loader) {
174

    
175
                if( loader != null && iconName.contains(".") ) {
176
                        logger.debug("Loading icon from resource:''{1}''.", iconName);
177
                        return toImageIcon(loader.getResource(iconName),iconName);
178
                }
179

    
180
                Object object = iconList.get(iconName);
181
                if (object!=null) {
182
                        return toImageIcon(object,iconName);
183
                }
184
                if( defaultTheme!=null && defaultTheme.exists(iconName)) {
185
                        return  defaultTheme.get(iconName, null);
186
                }
187
                return getNoIcon();
188
        }
189

    
190
        public ImageIcon getNoIcon() {
191
                Object object = iconList.get("no-icon");
192
                if (object!=null){
193
                        return toImageIcon(object,"no-icon");
194
                }
195
                if (defaultTheme==null ){
196
                        return null;
197
                }
198
                return defaultTheme.getNoIcon();
199
        }
200

    
201

    
202
        protected ImageIcon toImageIcon(Object object,String iconName){
203
                if (object == null){
204
                        return null;
205
                }
206
                if( object instanceof URL ) {
207
                        return new ImageIcon((URL)object);
208
                }
209
                ImageIcon icon = loadIcon(iconName,object);
210
                return icon;
211
        }
212

    
213
        /* (non-Javadoc)
214
         * @see com.iver.andami.iconthemes.IIconTheme#registerDefault(java.lang.String, javax.swing.ImageIcon)
215
         */
216
        public void registerDefault(String iconName, ImageIcon image) {
217
                if (defaultTheme!=null)        defaultTheme.register(iconName, image);
218
                else register(iconName, image);
219
        }
220

    
221
        /* (non-Javadoc)
222
         * @see com.iver.andami.iconthemes.IIconTheme#registerDefault(java.lang.String, java.lang.Object)
223
         */
224
        public void registerDefault(String iconName, Object resource) {
225
                if (defaultTheme!=null) {
226
                        defaultTheme.register(iconName, resource);
227
                } else {
228
                        register(iconName, resource);
229
                }
230
        }
231

    
232
        /* (non-Javadoc)
233
         * @see com.iver.andami.iconthemes.IIconTheme#register(java.lang.String, javax.swing.ImageIcon)
234
         */
235
        public void register(String iconName, ImageIcon image) {
236
                iconList.put(iconName, image);
237
        }
238

    
239
        /* (non-Javadoc)
240
         * @see com.iver.andami.iconthemes.IIconTheme#register(java.lang.String, java.lang.Object)
241
         */
242
        public void register(String iconName, Object resource) {
243
                iconList.put(iconName, resource);
244
        }
245

    
246
        /* (non-Javadoc)
247
         * @see com.iver.andami.iconthemes.IIconTheme#getName()
248
         */
249
        public String getName() {
250
                return name;
251
        }
252

    
253
        /* (non-Javadoc)
254
         * @see com.iver.andami.iconthemes.IIconTheme#setName(java.lang.String)
255
         */
256
        public void setName(String themeName) {
257
                name = themeName;
258
        }
259

    
260
        /* (non-Javadoc)
261
         * @see com.iver.andami.iconthemes.IIconTheme#getDescription()
262
         */
263
        public String getDescription() {
264
                return description;
265
        }
266

    
267
        /* (non-Javadoc)
268
         * @see com.iver.andami.iconthemes.IIconTheme#setDescription(java.lang.String)
269
         */
270
        public void setDescription(String description) {
271
                this.description = description;
272
        }
273

    
274
        /* (non-Javadoc)
275
         * @see com.iver.andami.iconthemes.IIconTheme#getVersion()
276
         */
277
        public String getVersion() {
278
                return version;
279
        }
280

    
281
        /* (non-Javadoc)
282
         * @see com.iver.andami.iconthemes.IIconTheme#setVersion(java.lang.String)
283
         */
284
        public void setVersion(String version) {
285
                this.version = version;
286
        }
287

    
288
        /* (non-Javadoc)
289
         * @see com.iver.andami.iconthemes.IIconTheme#getResource()
290
         */
291
        public Object getResource() {
292
                return resource;
293
        }
294
        /* (non-Javadoc)
295
         * @see com.iver.andami.iconthemes.IIconTheme#setResource(java.lang.Object)
296
         */
297
        public void setResource(Object resource) {
298
                this.resource = resource;
299
        }
300

    
301
        /**
302
         * Returns the name of the icon theme
303
         */
304
        public String toString() {
305
                return getName();
306
        }
307

    
308
        /* (non-Javadoc)
309
         * @see com.iver.andami.iconthemes.IIconTheme#getURL(java.lang.String)
310
         *
311
         */
312
        public URL getURL(String iconName) {
313
                Object object = defaultTheme.get(iconName);
314
                if (object !=null && object instanceof URL)
315
                        return (URL) object;
316
                return null;
317
        }
318

    
319
        public String getResourceID(String iconName) {
320
                Object icon = null;
321
                icon = iconList.get(iconName);
322
                if( icon != null ) {
323
                        return icon.toString();
324
                }
325
                icon = ((AbstractIconTheme)defaultTheme).iconList.get(iconName);
326
                if( icon != null ) {
327
                        return icon.toString();
328
                }
329
                return "";
330
        }
331

    
332
}