root / trunk / frameworks / _fwAndami / src / com / iver / andami / iconthemes / IconThemeManager.java @ 11026
History | View | Annotate | Download (8.1 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.iconthemes; |
43 |
|
44 |
import java.io.File; |
45 |
import java.io.FileInputStream; |
46 |
import java.io.FileNotFoundException; |
47 |
import java.io.FilenameFilter; |
48 |
import java.io.IOException; |
49 |
import java.io.InputStream; |
50 |
import java.net.MalformedURLException; |
51 |
import java.util.ArrayList; |
52 |
import java.util.Enumeration; |
53 |
import java.util.zip.ZipEntry; |
54 |
import java.util.zip.ZipException; |
55 |
import java.util.zip.ZipFile; |
56 |
|
57 |
import org.kxml2.io.KXmlParser; |
58 |
import org.xmlpull.v1.XmlPullParserException; |
59 |
|
60 |
import sun.security.action.GetIntegerAction; |
61 |
|
62 |
/**
|
63 |
* <p>This class deals with icon themes, it understands the on-disk theme
|
64 |
* format, it is able to list the availabe themes, to get the default
|
65 |
* theme and to change it.</p>
|
66 |
*
|
67 |
* <p>A XML theme description file should look similar to:
|
68 |
* <pre>
|
69 |
* <?xml version="1.0" encoding="utf-8"?>
|
70 |
* <theme>
|
71 |
* <name>Default gvSIG icon theme</name>
|
72 |
* <description>Clear fancy super great icon theme for gvSIG. Author: Salvador Dal?, &lt;salvador.dali@art.org&gt;</description>
|
73 |
* <version>1.2</version>
|
74 |
* </theme>
|
75 |
* </pre>
|
76 |
*
|
77 |
*
|
78 |
* @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
|
79 |
*
|
80 |
*/
|
81 |
public class IconThemeManager { |
82 |
private File themesDir = null; |
83 |
private final String themeDefinitionFile = "theme.xml"; |
84 |
|
85 |
/**
|
86 |
*
|
87 |
* @param themesDir
|
88 |
*/
|
89 |
public IconThemeManager(File themesDir) throws FileNotFoundException { |
90 |
if (!themesDir.exists())
|
91 |
throw new FileNotFoundException(); |
92 |
this.themesDir = themesDir;
|
93 |
} |
94 |
|
95 |
|
96 |
public IconTheme get(String themeName) { |
97 |
IconThemeInfo[] themeList = list();
|
98 |
for (int i=0; i<themeList.length; i++) { |
99 |
if (themeList[i].getName().equals(themeName)) {
|
100 |
return get(themeList[i]);
|
101 |
} |
102 |
} |
103 |
return null; |
104 |
} |
105 |
|
106 |
public IconTheme get(IconThemeInfo themeInfo) {
|
107 |
IconTheme theme = new IconTheme(themeInfo);
|
108 |
if (themeInfo.getResource().isDirectory()) {
|
109 |
File basedir = themeInfo.getResource();
|
110 |
|
111 |
//TODO: crear el filtro de im?genes
|
112 |
File[] imageList = basedir.listFiles(new FilenameFilter() { |
113 |
public boolean accept(File dir, String fileName) { |
114 |
String extension = ""; |
115 |
int pointPos = fileName.lastIndexOf("."); |
116 |
if (pointPos>0 && pointPos < (fileName.length()-1) ) |
117 |
extension = fileName.substring(pointPos+1).toLowerCase();
|
118 |
if ( extension.equals("jpg") || extension.equals("jpeg") |
119 |
|| extension.equals("png")
|
120 |
|| extension.equals("gif")
|
121 |
|
122 |
) |
123 |
return true; |
124 |
else
|
125 |
return false; |
126 |
} |
127 |
}); |
128 |
|
129 |
int pointPos;
|
130 |
String name;
|
131 |
for (int i=imageList.length-1; i>=0; i--) { |
132 |
pointPos = imageList[i].getName().lastIndexOf(".");
|
133 |
name = imageList[i].getName().substring(0, pointPos);
|
134 |
try {
|
135 |
theme.register(name, imageList[i].toURL()); |
136 |
System.out.println("registerning "+name+": "+imageList[i].toURL()); |
137 |
} catch (MalformedURLException e) {} |
138 |
} |
139 |
} |
140 |
else {
|
141 |
|
142 |
} |
143 |
return theme;
|
144 |
} |
145 |
|
146 |
/**
|
147 |
*
|
148 |
* @return
|
149 |
*/
|
150 |
public IconThemeInfo[] list() { |
151 |
File[] files = themesDir.listFiles(); |
152 |
if (files==null) return null; |
153 |
|
154 |
ArrayList themeList = new ArrayList(); |
155 |
IconThemeInfo info; |
156 |
for (int i=0; i<files.length; i++) { |
157 |
if (files[i].isDirectory()) {
|
158 |
info = readInfoFromDir(files[i]); |
159 |
if (info!=null) |
160 |
themeList.add(info); |
161 |
} |
162 |
else if (files[i].isFile()) { // ensure it's a regular file |
163 |
info = readInfoFromZip(files[i]); |
164 |
if (info!=null) |
165 |
themeList.add(info); |
166 |
} |
167 |
} |
168 |
|
169 |
return (IconThemeInfo[]) themeList.toArray(new IconThemeInfo[0]); |
170 |
} |
171 |
|
172 |
private IconThemeInfo readInfoFromDir(File dir){ |
173 |
File themeDefinition = new File(dir + File.separator + themeDefinitionFile); |
174 |
IconThemeInfo themeInfo; |
175 |
try {
|
176 |
// try reading the XML file
|
177 |
if (themeDefinition.exists() && themeDefinition.isFile()) {
|
178 |
themeInfo = readXML(new FileInputStream(themeDefinition)); |
179 |
themeInfo.setResource(dir); |
180 |
return themeInfo;
|
181 |
} |
182 |
} catch (IOException e) {} |
183 |
catch (XmlPullParserException e) {
|
184 |
e.printStackTrace(); |
185 |
} |
186 |
// the XML parsing failed, just show the dir name
|
187 |
themeInfo = new IconThemeInfo();
|
188 |
themeInfo.setName(dir.getName()); |
189 |
themeInfo.setDescription(dir.getName()); |
190 |
themeInfo.setResource(dir); |
191 |
return themeInfo;
|
192 |
} |
193 |
|
194 |
private IconThemeInfo readXML(InputStream xmlStream) throws XmlPullParserException, IOException { |
195 |
KXmlParser parser = new KXmlParser();
|
196 |
// we use null encoding, in this way kxml2 tries to detect the encoding
|
197 |
parser.setInput(xmlStream, null);
|
198 |
IconThemeInfo themeInfo = new IconThemeInfo();
|
199 |
for (parser.next(); parser.getEventType()!=KXmlParser.END_DOCUMENT; parser.next()) {
|
200 |
// este bucle externo recorre las etiquetas de primer y segundo nivel
|
201 |
if (parser.getEventType()==KXmlParser.START_TAG) {
|
202 |
if (parser.getName().equals("name")) { |
203 |
themeInfo.setName(parser.nextText()); |
204 |
} |
205 |
else if (parser.getName().equals("description")) { |
206 |
themeInfo.setDescription(parser.nextText()); |
207 |
} |
208 |
else if (parser.getName().equals("version")) { |
209 |
themeInfo.setVersion(parser.nextText()); |
210 |
} |
211 |
} |
212 |
} |
213 |
return themeInfo;
|
214 |
} |
215 |
|
216 |
private IconThemeInfo readInfoFromZip(File zipFile) { |
217 |
try {
|
218 |
ZipFile file = new ZipFile(zipFile); |
219 |
IconThemeInfo themeInfo; |
220 |
Enumeration entries = file.entries();
|
221 |
ZipEntry xmlEntry=null, dirEntry=null; |
222 |
// search for theme.xml and the directory names
|
223 |
while (entries.hasMoreElements() && (xmlEntry==null||dirEntry==null)) { |
224 |
ZipEntry entry = (ZipEntry) entries.nextElement(); |
225 |
if (entry.isDirectory()) {
|
226 |
dirEntry = entry; |
227 |
} |
228 |
if (basename(entry.getName()).equals(themeDefinitionFile)) {
|
229 |
xmlEntry = entry; |
230 |
} |
231 |
} |
232 |
|
233 |
try {
|
234 |
// try with the XML file
|
235 |
if (xmlEntry!=null) { |
236 |
themeInfo = readXML(file.getInputStream(xmlEntry)); |
237 |
themeInfo.setResource(zipFile); |
238 |
return themeInfo;
|
239 |
} |
240 |
} catch (XmlPullParserException e) {
|
241 |
e.printStackTrace(); |
242 |
System.out.println(file.getName());
|
243 |
} |
244 |
|
245 |
themeInfo = new IconThemeInfo();
|
246 |
themeInfo.setResource(zipFile); |
247 |
// now try with the directory
|
248 |
if (dirEntry!=null) { |
249 |
|
250 |
themeInfo.setName(dirEntry.getName()); |
251 |
themeInfo.setDescription(dirEntry.getName()); |
252 |
return themeInfo;
|
253 |
} |
254 |
else { // otherwise just use the zipName |
255 |
themeInfo.setName(zipFile.getName()); |
256 |
themeInfo.setDescription(zipFile.getName()); |
257 |
return themeInfo;
|
258 |
} |
259 |
|
260 |
} catch (ZipException e) { |
261 |
// TODO Auto-generated catch block
|
262 |
e.printStackTrace(); |
263 |
} catch (IOException e) { |
264 |
// TODO Auto-generated catch block
|
265 |
e.printStackTrace(); |
266 |
} |
267 |
return null; |
268 |
} |
269 |
|
270 |
private String basename(String fullname) { |
271 |
String[] parts = fullname.split(File.separator+"|/"); |
272 |
return parts[parts.length-1]; |
273 |
} |
274 |
|
275 |
public File getThemesPath() { |
276 |
return themesDir;
|
277 |
} |
278 |
|
279 |
public void setThemesPath(File themesDir) { |
280 |
this.themesDir = themesDir;
|
281 |
} |
282 |
|
283 |
public IconTheme getDefault() {
|
284 |
return new IconTheme(new IconThemeInfo()); |
285 |
// TODO
|
286 |
} |
287 |
|
288 |
public void setDefault(IconThemeInfo iconTheme) { |
289 |
// TODO
|
290 |
} |
291 |
|
292 |
public void setDefault(IconTheme iconTheme) { |
293 |
setDefault(iconTheme.getInfo()); |
294 |
} |
295 |
} |