Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / ui / theme / Theme.java @ 41312

History | View | Annotate | Download (13.6 KB)

1 40559 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 41311 jjdelcerro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 40559 jjdelcerro
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 41311 jjdelcerro
 * MA 02110-1301, USA.
20 40559 jjdelcerro
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24 40435 jjdelcerro
package org.gvsig.andami.ui.theme;
25
26
import java.awt.Color;
27
import java.awt.Point;
28
import java.io.File;
29
import java.io.FileInputStream;
30
import java.io.FileNotFoundException;
31
import java.io.IOException;
32
import java.util.ArrayList;
33 41311 jjdelcerro
import java.util.Map;
34 40435 jjdelcerro
35
import javax.swing.ImageIcon;
36 41311 jjdelcerro
import org.apache.commons.io.IOUtils;
37
import org.apache.commons.lang.text.StrSubstitutor;
38 40435 jjdelcerro
39
import org.gvsig.andami.Launcher;
40
import org.gvsig.andami.PluginServices;
41
import org.gvsig.andami.messages.NotificationManager;
42 41311 jjdelcerro
import org.gvsig.tools.util.XMLUtils;
43 40435 jjdelcerro
import org.kxml2.io.KXmlParser;
44 41311 jjdelcerro
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
46 40435 jjdelcerro
import org.xmlpull.v1.XmlPullParser;
47 41311 jjdelcerro
import org.xmlpull.v1.XmlPullParserException;
48 40435 jjdelcerro
49
/**
50 41311 jjdelcerro
 * Personalization of the program according to the file xml with the necessary
51
 * information.
52 40435 jjdelcerro
 *
53
 * @author Vicente Caballero Navarro
54
 */
55
public class Theme {
56 41311 jjdelcerro
57
    private static final Logger logger = LoggerFactory.getLogger(Theme.class);
58
59 40435 jjdelcerro
    public static final String CENTERED = "CENTERED";
60
    public static final String EXPAND = "EXPAND";
61
    public static final String MOSAIC = "MOSAIC";
62
63 41311 jjdelcerro
    private static final String ANDAMI_PROPERTIES = "AndamiProperties";
64 40435 jjdelcerro
    private static final String APPLICATION_IMAGES = "ApplicationImages";
65
    private static final String SPLASH_IMAGES = "SplashImages";
66
    private static final String SPLASH = "Splash";
67
    private static final String PATH = "path";
68
    private static final String TIMER = "timer";
69
    private static final String ICON = "Icon";
70
    private static final String APPLICATION_NAME = "ApplicationName";
71 41312 jjdelcerro
    private static final String PRIORITY = "priority";
72 40435 jjdelcerro
    private static final String VALUE = "value";
73 41311 jjdelcerro
    private static final String BACKGROUND_IMAGE = "BackgroundImage";
74
    private static final String WALLPAPER_TYPE = "WallpaperType";
75 40435 jjdelcerro
76 41311 jjdelcerro
    private static final String VERSION = "version";
77
    private static final String FONTPOSITIONX = "x";
78
    private static final String FONTPOSITIONY = "y";
79 40435 jjdelcerro
80 41311 jjdelcerro
    private static final String FONTSIZE = "fontsize";
81
    private static final String FONTCOLOR = "color";
82 40435 jjdelcerro
83 41311 jjdelcerro
    private ArrayList<String> images = new ArrayList<String>();
84 40435 jjdelcerro
    private String icon;
85
    private ArrayList<String> timers = new ArrayList<String>();
86
    private ArrayList<String> versions = new ArrayList<String>();
87
    private ArrayList<String> fontColors = new ArrayList<String>();
88
    private ArrayList<String> fontSizes = new ArrayList<String>();
89 41311 jjdelcerro
    private ArrayList<String> fontpositionsX = new ArrayList<String>();
90
    private ArrayList<String> fontpositionsY = new ArrayList<String>();
91 40435 jjdelcerro
92
    private String name = null;
93 41311 jjdelcerro
    private String backgroundimage;
94
    private String wallpaperType = CENTERED;
95 41312 jjdelcerro
    private int priority = 0;
96 40435 jjdelcerro
97 41311 jjdelcerro
    /**
98
     * Spurce file of the theme *
99
     */
100
    private File source = null;
101
    private StrSubstitutor strSubstitutor = null;
102
103
    public Theme() {
104
105 40435 jjdelcerro
    }
106
107 41311 jjdelcerro
    public Theme(Map vars) {
108
        this.strSubstitutor = new StrSubstitutor(vars);
109
    }
110
111 40435 jjdelcerro
    public File getSource() {
112 41311 jjdelcerro
        return source;
113 40435 jjdelcerro
    }
114 41311 jjdelcerro
115 40435 jjdelcerro
    /**
116
     * Read the file xml with the personalization.
117
     *
118
     * @param file xml
119
     */
120
    public void readTheme(File file) {
121 41311 jjdelcerro
        FileInputStream fis = null;
122 40435 jjdelcerro
        try {
123 41311 jjdelcerro
            String encoding = XMLUtils.getEncoding(file, "UTF-8");
124
125
            fis = new FileInputStream(file);
126 40435 jjdelcerro
            KXmlParser parser = new KXmlParser();
127 41311 jjdelcerro
            parser.setInput(fis, encoding);
128 40435 jjdelcerro
129
            int tag = parser.nextTag();
130
131 41311 jjdelcerro
            if ( parser.getEventType() != XmlPullParser.END_DOCUMENT ) {
132 40435 jjdelcerro
                parser.require(XmlPullParser.START_TAG, null, ANDAMI_PROPERTIES);
133
134 41311 jjdelcerro
                while ( tag != XmlPullParser.END_DOCUMENT ) {
135 40435 jjdelcerro
                    //parser.next();
136
                    switch (tag) {
137
                    case XmlPullParser.START_TAG:
138 41311 jjdelcerro
                        if ( parser.getName().compareTo(ANDAMI_PROPERTIES) == 0 ) {
139 40435 jjdelcerro
                            parser.nextTag();
140 41311 jjdelcerro
                            if ( parser.getName().compareTo(APPLICATION_IMAGES) == 0 ) {
141 40435 jjdelcerro
                                int splashtag = parser.nextTag();
142 41311 jjdelcerro
                                if ( parser.getName().compareTo(SPLASH_IMAGES) == 0 ) {
143
                                    splashtag = parser.nextTag();
144
                                    boolean endSplash = false;
145 40435 jjdelcerro
146 41311 jjdelcerro
                                    // parser.require(KXmlParser.START_TAG,null, SPLASH);
147
                                    while ( (splashtag != XmlPullParser.END_DOCUMENT)
148
                                            && !endSplash ) {
149
                                        if ( splashtag == XmlPullParser.END_TAG ) {
150
                                            splashtag = parser.nextTag();
151
                                            continue;
152
                                        }
153 40435 jjdelcerro
154 41311 jjdelcerro
                                        if ( parser.getName().compareTo(SPLASH) == 0 ) {
155
                                            images.add(fixPath(file, parser.getAttributeValue("", PATH)));
156
                                            timers.add(parser.getAttributeValue("", TIMER));
157
                                            versions.add(expand(parser.getAttributeValue("", VERSION)));
158
                                            fontpositionsX.add(parser.getAttributeValue("", FONTPOSITIONX));
159
                                            fontpositionsY.add(parser.getAttributeValue("", FONTPOSITIONY));
160
                                            fontSizes.add(parser.getAttributeValue("", FONTSIZE));
161
                                            fontColors.add(parser.getAttributeValue("", FONTCOLOR));
162 40435 jjdelcerro
163 41311 jjdelcerro
                                            splashtag = parser.nextTag();
164
                                        } else {
165
                                            endSplash = true;
166
                                        }
167
                                    }
168
                                }
169
                                int tagOptions = XmlPullParser.START_TAG;
170
                                while ( (tagOptions != XmlPullParser.END_TAG) ) {
171
                                    if ( parser.getName().compareTo(BACKGROUND_IMAGE) == 0 ) {
172
                                        backgroundimage = fixPath(file, parser.getAttributeValue("", PATH));
173
                                        tag = parser.next();
174
                                    } else if ( parser.getName().compareTo(ICON) == 0 ) {
175
                                        icon = fixPath(file, parser.getAttributeValue("", PATH));
176
                                        tag = parser.next();
177
                                    } else if ( parser.getName().compareTo(WALLPAPER_TYPE) == 0 ) {
178
                                        wallpaperType = parser.getAttributeValue("", VALUE);
179
                                        tag = parser.next();
180
                                    }
181
                                    tagOptions = parser.nextTag();
182 40435 jjdelcerro
183 41311 jjdelcerro
                                }
184
                            }
185 40435 jjdelcerro
                        }
186 41311 jjdelcerro
                        if ( parser.getName().compareTo(APPLICATION_NAME) == 0 ) {
187
                            name = expand(parser.getAttributeValue("", VALUE));
188
                            tag = parser.next();
189
                        }
190 41312 jjdelcerro
                        if ( parser.getName().compareTo(PRIORITY) == 0 ) {
191
                            String s = parser.getAttributeValue("", VALUE);
192
                            try {
193
                                this.priority = Integer.parseInt(s);
194
                            } catch(Exception ex) {
195
                                this.priority = 0;
196
                            }
197
                            tag = parser.next();
198
                        }
199 40435 jjdelcerro
                        break;
200
201
                    case XmlPullParser.END_TAG:
202
                        break;
203
204
                    case XmlPullParser.TEXT:
205
                        // System.out.println("[TEXT]["+kxmlParser.getText()+"]");
206
                        break;
207
                    }
208
                    tag = parser.next();
209
                }
210
            }
211
212
            parser.require(XmlPullParser.END_DOCUMENT, null, null);
213
            this.source = file;
214 41311 jjdelcerro
215
        } catch (FileNotFoundException ex) {
216
            logger.warn("Can't open theme file '" + file.getAbsolutePath() + "'.", ex);
217
218
        } catch (XmlPullParserException ex) {
219
            logger.warn("Can't parse theme file '" + file.getAbsolutePath() + "'.", ex);
220
221
        } catch (IOException ex) {
222
            logger.warn("Can't read theme file '" + file.getAbsolutePath() + "'.", ex);
223
224
        } finally {
225
            IOUtils.closeQuietly(fis);
226 40435 jjdelcerro
        }
227
    }
228
229
    private String fixPath(File theme, String path) {
230 41311 jjdelcerro
        path = path.replace("$GVSIG_INSTALL", Launcher.getApplicationDirectory());
231
        path = path.replace("$GVSIG_HOME", Launcher.getAppHomeDir());
232
        path = path.replace("$GVSIG_PLUGINS", Launcher.getAndamiConfig().getPluginsDirectory());
233 40435 jjdelcerro
        File parent = theme.getParentFile().getAbsoluteFile();
234
        File fpath = new File(path);
235 41311 jjdelcerro
        if ( !fpath.isAbsolute() ) {
236
            fpath = new File(parent, path);
237 40435 jjdelcerro
        }
238 41311 jjdelcerro
        return fpath.getAbsolutePath();
239 40435 jjdelcerro
    }
240 41311 jjdelcerro
241 40435 jjdelcerro
    /**
242 41311 jjdelcerro
     * Returns image to the splashwindow.
243
     *
244
     * @return ImageIcon[]
245
     */
246 40435 jjdelcerro
    public ImageIcon[] getSplashImages() {
247
        ImageIcon[] imgs = new ImageIcon[images.size()];
248
249 41311 jjdelcerro
        for ( int i = 0; i < images.size(); i++ ) {
250 40435 jjdelcerro
            imgs[i] = new ImageIcon(images.get(i));
251
        }
252
253
        return imgs;
254
    }
255
256
    public String getTypeDesktop() {
257 41311 jjdelcerro
        return wallpaperType;
258 40435 jjdelcerro
    }
259 41311 jjdelcerro
260 40435 jjdelcerro
    /**
261
     * Return the icon.
262
     *
263
     * @return ImageIcon
264
     */
265
    public ImageIcon getIcon() {
266 41311 jjdelcerro
        if ( icon == null ) {
267 40435 jjdelcerro
            return null;
268
        }
269
270
        try {
271
            return new ImageIcon(icon);
272
        } catch (Exception e) {
273
            return null;
274
        }
275
    }
276 41311 jjdelcerro
277 40435 jjdelcerro
    /**
278
     * Return the backgroundimage.
279
     *
280
     * @return ImageIcon
281
     */
282
    public ImageIcon getBackgroundImage() {
283 41311 jjdelcerro
        if ( backgroundimage == null ) {
284 40435 jjdelcerro
            return null;
285
        }
286
287
        try {
288
            return new ImageIcon(backgroundimage);
289
        } catch (Exception e) {
290
            return null;
291
        }
292
    }
293 41311 jjdelcerro
294 40435 jjdelcerro
    /**
295
     * Return the time of the splash images.
296
     *
297
     * @return long[]
298
     */
299
    public long[] getTimers() {
300
        long[] tms = new long[timers.size()];
301
302 41311 jjdelcerro
        for ( int i = 0; i < tms.length; i++ ) {
303 40435 jjdelcerro
            tms[i] = Long.parseLong(timers.get(i));
304
        }
305
306
        return tms;
307
    }
308
309
    /**
310
     * Return the name of program.
311
     *
312
     * @return String
313
     */
314
    public String getName() {
315
        return name;
316
    }
317 41311 jjdelcerro
318 40435 jjdelcerro
    /**
319
     * Return the text to overwrite the images.
320
     *
321
     * @return String[]
322
     */
323 41311 jjdelcerro
    public String[] getVersions() {
324
        return versions.toArray(new String[0]);
325
    }
326
327
    /**
328 40435 jjdelcerro
     * Return the position of text to overwrite the images.
329
     *
330
     * @return Point[]
331
     */
332 41311 jjdelcerro
    public Point[] getPositions() {
333
        Point[] points = new Point[fontpositionsX.size()];
334
        for ( int i = 0; i < points.length; i++ ) {
335
            try {
336
                points[i] = new Point(Integer.valueOf(fontpositionsX.get(i)), Integer.valueOf(fontpositionsY.get(i)));
337
            } catch (NumberFormatException e) {
338
                NotificationManager.addInfo(PluginServices.getText(this, "incorrect_position"), e);
339
            }
340
        }
341
        return points;
342
    }
343
344
    /**
345 40435 jjdelcerro
     * Return the font size text to overwrite the images.
346
     *
347
     * @return int[]
348
     */
349 41311 jjdelcerro
    public int[] getFontSizes() {
350
        int[] sizes = new int[fontSizes.size()];
351
        for ( int i = 0; i < sizes.length; i++ ) {
352
            try {
353
                sizes[i] = Integer.valueOf(fontSizes.get(i));
354
            } catch (NumberFormatException e) {
355
                NotificationManager.addInfo(PluginServices.getText(this, "incorrect_size"), e);
356
            }
357
        }
358
        return sizes;
359
    }
360
361
    /**
362 40435 jjdelcerro
     * Return the font color of text to overwrite the images.
363
     *
364
     * @return Color[]
365
     */
366 41311 jjdelcerro
    public Color[] getFontColors() {
367
        Color[] colors = new Color[fontColors.size()];
368
        for ( int i = 0; i < colors.length; i++ ) {
369
            try {
370
                String s = fontColors.get(i);
371
                String[] rgb = s.split(",");
372
                colors[i] = new Color(Integer.valueOf(rgb[0]), Integer.valueOf(rgb[1]), Integer.valueOf(rgb[2]));
373
            } catch (Exception e) {
374
                NotificationManager.addInfo(PluginServices.getText(this, "incorrect_color"), e);
375
            }
376
        }
377
        return colors;
378
    }
379
380
    private String expand(String value) {
381
        if ( this.strSubstitutor == null ) {
382
            return value;
383
        }
384
        return strSubstitutor.replace(value);
385
    }
386 41312 jjdelcerro
387
    public int getPriority() {
388
        return this.priority;
389
    }
390 40435 jjdelcerro
}