Statistics
| Revision:

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

History | View | Annotate | Download (7.93 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.HashMap;
46

    
47
import javax.swing.ImageIcon;
48

    
49
import org.slf4j.Logger;
50
import org.slf4j.LoggerFactory;
51

    
52

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

    
81

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

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

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

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

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

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

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

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

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

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

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

    
180

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
295

    
296
}