Revision 11002

View differences:

trunk/frameworks/_fwAndami/src/com/iver/andami/iconthemes/IconThemeManager.java
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.iconthemes;
43

  
44
import java.io.File;
45
import java.io.FileInputStream;
46
import java.io.IOException;
47
import java.io.InputStream;
48
import java.util.ArrayList;
49
import java.util.Enumeration;
50
import java.util.zip.ZipEntry;
51
import java.util.zip.ZipException;
52
import java.util.zip.ZipFile;
53

  
54
import org.kxml2.io.KXmlParser;
55
import org.xmlpull.v1.XmlPullParserException;
56

  
57
/**
58
 * <p>This class deals with icon themes, it understands the on-disk theme
59
 * format, it is able to list the availabe themes, to get the default
60
 * theme and to change it.</p>
61
 * 
62
 * <p>A XML theme description file should look similar to:
63
 * <pre>
64
 * &lt;?xml version="1.0" encoding="utf-8"?&gt;
65
 * &lt;theme&gt;
66
 *	&lt;name&gt;Default gvSIG icon theme&lt;/name&gt;
67
 *	&lt;description&gt;Clear fancy super great icon theme for gvSIG. Author: Salvador Dal?, &lt;salvador.dali@art.org&gt; &lt;/description&gt;
68
 *	&lt;version&gt;1.2&lt;/version&gt;
69
 * &lt;/theme&gt;
70
 * </pre>
71
 *   
72
 * 
73
 * @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
74
 *
75
 */
76
public class IconThemeManager {
77
	protected File themesDir = null;
78
	protected final String themeDefinitionFile = "theme.xml";
79

  
80
	/**
81
	 * 
82
	 * @param themesDir
83
	 */
84
	public IconThemeManager(File themesDir) {
85
		this.themesDir = themesDir;		
86
	}
87
	
88
	/**
89
	 * 
90
	 * @return
91
	 */
92
	public IconThemeInfo[] list() {
93
		File[] files = themesDir.listFiles();
94
		if (files==null) return null;
95
		
96
		ArrayList themeList = new ArrayList();
97
		IconThemeInfo info;
98
		for (int i=0; i<files.length; i++) {
99
			if (files[i].isDirectory()) {
100
				info = readInfoFromDir(files[i]);
101
				if (info!=null)
102
					themeList.add(info);
103
			}
104
			else if (files[i].isFile()) { // ensure it's a regular file
105
				info = readInfoFromZip(files[i]);
106
				if (info!=null)
107
					themeList.add(info);
108
			}
109
		}
110
		
111
		return (IconThemeInfo[]) themeList.toArray(new IconThemeInfo[0]);
112
	}
113
	
114
	private IconThemeInfo readInfoFromDir(File dir){
115
		File themeDefinition = new File(dir + File.separator + themeDefinitionFile);
116
		IconThemeInfo themeInfo;
117
		try {
118
			// try reading the XML file
119
			if (themeDefinition.exists() && themeDefinition.isFile()) {
120
				themeInfo = readXML(new FileInputStream(themeDefinition));
121
				themeInfo.setResource(dir);
122
				return themeInfo;
123
			}
124
		} catch (IOException e) {}
125
		catch (XmlPullParserException e) {}
126
		// the XML parsing failed, just show the dir name
127
		themeInfo = new IconThemeInfo();
128
		themeInfo.setName(dir.getName());
129
		themeInfo.setDescription(dir.getName());
130
		themeInfo.setResource(dir);
131
		return themeInfo;
132
	}
133
	
134
	private IconThemeInfo readXML(InputStream xmlStream) throws XmlPullParserException, IOException {
135
		KXmlParser parser = new KXmlParser();
136
		// we use null encoding, in this way kxml2 tries to detect the encoding		
137
		parser.setInput(xmlStream, null);
138
		IconThemeInfo themeInfo = new IconThemeInfo();
139
		for (parser.next(); parser.getEventType()!=KXmlParser.END_DOCUMENT; parser.next()) {
140
			// este bucle externo recorre las etiquetas de primer y segundo nivel
141
			if (parser.getEventType()==KXmlParser.START_TAG) {
142
				if (parser.getName().equals("name")) {
143
					themeInfo.setName(parser.nextText());
144
				}
145
				else if (parser.getName().equals("description")) {
146
					themeInfo.setDescription(parser.nextText());
147
				}
148
				else if (parser.getName().equals("version")) {
149
					themeInfo.setVersion(parser.nextText());
150
				}
151
			}
152
		}	
153
		return themeInfo;
154
	}
155
	
156
	private IconThemeInfo readInfoFromZip(File zipFile) {
157
		try {
158
			ZipFile file = new ZipFile(zipFile);
159
			IconThemeInfo themeInfo;
160
			Enumeration entries = file.entries();
161
			ZipEntry xmlEntry=null, dirEntry=null;
162
			// search for theme.xml and the directory names
163
			while (entries.hasMoreElements() && (xmlEntry==null||dirEntry==null)) {
164
				ZipEntry entry = (ZipEntry) entries.nextElement();
165
				if (entry.isDirectory()) {
166
					dirEntry = entry;
167
				}
168
				if (entry.getName().equals(themeDefinitionFile)) {
169
					xmlEntry = entry;
170
				}
171
			}
172

  
173
			try {
174
				// try with the XML file
175
				if (xmlEntry!=null) {
176
					themeInfo = readXML(file.getInputStream(xmlEntry));
177
					themeInfo.setResource(zipFile);
178
				}
179
			} catch (XmlPullParserException e) {}
180
			
181
			themeInfo = new IconThemeInfo();
182
			themeInfo.setResource(zipFile);
183
			// now try with the directory
184
			if (dirEntry!=null) {
185
				
186
				themeInfo.setName(dirEntry.getName());
187
				themeInfo.setDescription(dirEntry.getName());
188
				return themeInfo;
189
			}
190
			else { // otherwise just use the zipName
191
				themeInfo.setName(zipFile.getName());
192
				themeInfo.setDescription(zipFile.getName());
193
				return themeInfo;
194
			}
195
			
196
		} catch (ZipException e) {
197
			// TODO Auto-generated catch block
198
			e.printStackTrace();
199
		} catch (IOException e) {
200
			// TODO Auto-generated catch block
201
			e.printStackTrace();
202
		}
203
		return null;
204
	}
205
	
206
	
207
}
0 208

  
trunk/frameworks/_fwAndami/src/com/iver/andami/iconthemes/IconThemeInfo.java
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.iconthemes;
43

  
44
import java.io.File;
45

  
46
/**
47
 * Contains information about a theme. It includes the name,  
48
 * 
49
 * @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
50
 *
51
 */
52
public class IconThemeInfo {
53
	protected String name;
54
	protected String description;
55
	protected String version="1.0";
56
	protected File resource;
57
	
58
	/**
59
	 * Gets the theme name.
60
	 * 
61
	 * @return The name of this theme.
62
	 */
63
	public String getName() {
64
		return name;
65
	}
66
	
67
	/**
68
	 * Sets the theme name.
69
	 * 
70
	 * @param themeName
71
	 */
72
	public void setName(String themeName) {
73
		name = themeName;
74
	}
75
	
76
	/**
77
	 * Gets the theme description.
78
	 * 
79
	 * @return The description of this theme.
80
	 */
81
	public String getDescription() {
82
		return description;
83
	}
84
	
85
	/**
86
	 * Sets the theme description. It should be a short description
87
	 * (around 20-30 words), including the highlights of the theme,
88
	 * the author and maybe its email address or a link the the theme's
89
	 * homepage. 
90
	 * 
91
	 * @param description
92
	 */
93
	public void setDescription(String description) {
94
		this.description = description;
95
	}
96
	
97
	/**
98
	 * Returns the theme version. It defaults to "1.0".
99
	 * 
100
	 * @return The version of this theme.
101
	 */
102
	public String getVersion() {
103
		return version;
104
	}
105

  
106
	/**
107
	 * Set the theme version. 
108
	 * 
109
	 * @param version
110
	 */
111
	public void setVersion(String version) {
112
		this.version = version;
113
	}
114
	
115
	/**
116
	 * Gets the file which contains physically contains this theme on disk.
117
	 * It may be a zipfile or jarfile, or a directory.
118
	 * 
119
	 * @return
120
	 */
121
	public File getResource() {
122
		return resource;
123
	}
124
	
125
	/**
126
	 * Sets the file which contains physically contains this theme on disk.
127
	 * It may be a zipfile or jarfile, or a directory.
128
	 * 
129
	 * @return
130
	 */
131
	public void setResource(File resource) {
132
		this.resource = resource;
133
	}
134
}
0 135

  
trunk/frameworks/_fwAndami/src/com/iver/andami/iconthemes/IconTheme.java
73 73
 * @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
74 74
 */
75 75
public class IconTheme {
76
	HashMap iconList = new HashMap();
77
	Logger logger = PluginServices.getLogger();
76
	protected HashMap iconList = new HashMap();
77
	protected Logger logger = PluginServices.getLogger();
78
	protected IconThemeInfo info;
78 79
	
79 80
	/**
81
	 * Creates a new empty IconTheme. The next step is to register icons in order
82
	 * the theme to be useful.
83
	 * 
84
	 * @param themeInfo An IconThemeInfo containing the name, the description, the
85
	 * version and the source resource of the theme.
86
	 */
87
	public IconTheme(IconThemeInfo themeInfo) {
88
		this.info = themeInfo;
89
	}
90

  
91
	
92
	/**
80 93
	 * Returns <code>true</code> if the icon theme contains a mapping for the
81 94
	 * specified iconName.
82 95
	 * 

Also available in: Unified diff