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
/**
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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * 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
 * MA 02110-1301, USA.
20
 *
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
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
import java.util.Map;
34

    
35
import javax.swing.ImageIcon;
36
import org.apache.commons.io.IOUtils;
37
import org.apache.commons.lang.text.StrSubstitutor;
38

    
39
import org.gvsig.andami.Launcher;
40
import org.gvsig.andami.PluginServices;
41
import org.gvsig.andami.messages.NotificationManager;
42
import org.gvsig.tools.util.XMLUtils;
43
import org.kxml2.io.KXmlParser;
44
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
46
import org.xmlpull.v1.XmlPullParser;
47
import org.xmlpull.v1.XmlPullParserException;
48

    
49
/**
50
 * Personalization of the program according to the file xml with the necessary
51
 * information.
52
 *
53
 * @author Vicente Caballero Navarro
54
 */
55
public class Theme {
56

    
57
    private static final Logger logger = LoggerFactory.getLogger(Theme.class);
58

    
59
    public static final String CENTERED = "CENTERED";
60
    public static final String EXPAND = "EXPAND";
61
    public static final String MOSAIC = "MOSAIC";
62

    
63
    private static final String ANDAMI_PROPERTIES = "AndamiProperties";
64
    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
    private static final String PRIORITY = "priority";
72
    private static final String VALUE = "value";
73
    private static final String BACKGROUND_IMAGE = "BackgroundImage";
74
    private static final String WALLPAPER_TYPE = "WallpaperType";
75

    
76
    private static final String VERSION = "version";
77
    private static final String FONTPOSITIONX = "x";
78
    private static final String FONTPOSITIONY = "y";
79

    
80
    private static final String FONTSIZE = "fontsize";
81
    private static final String FONTCOLOR = "color";
82

    
83
    private ArrayList<String> images = new ArrayList<String>();
84
    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
    private ArrayList<String> fontpositionsX = new ArrayList<String>();
90
    private ArrayList<String> fontpositionsY = new ArrayList<String>();
91

    
92
    private String name = null;
93
    private String backgroundimage;
94
    private String wallpaperType = CENTERED;
95
    private int priority = 0;
96

    
97
    /**
98
     * Spurce file of the theme *
99
     */
100
    private File source = null;
101
    private StrSubstitutor strSubstitutor = null;
102

    
103
    public Theme() {
104

    
105
    }
106

    
107
    public Theme(Map vars) {
108
        this.strSubstitutor = new StrSubstitutor(vars);
109
    }
110

    
111
    public File getSource() {
112
        return source;
113
    }
114

    
115
    /**
116
     * Read the file xml with the personalization.
117
     *
118
     * @param file xml
119
     */
120
    public void readTheme(File file) {
121
        FileInputStream fis = null;
122
        try {
123
            String encoding = XMLUtils.getEncoding(file, "UTF-8");
124
            
125
            fis = new FileInputStream(file);
126
            KXmlParser parser = new KXmlParser();
127
            parser.setInput(fis, encoding);
128

    
129
            int tag = parser.nextTag();
130

    
131
            if ( parser.getEventType() != XmlPullParser.END_DOCUMENT ) {
132
                parser.require(XmlPullParser.START_TAG, null, ANDAMI_PROPERTIES);
133

    
134
                while ( tag != XmlPullParser.END_DOCUMENT ) {
135
                    //parser.next();
136
                    switch (tag) {
137
                    case XmlPullParser.START_TAG:
138
                        if ( parser.getName().compareTo(ANDAMI_PROPERTIES) == 0 ) {
139
                            parser.nextTag();
140
                            if ( parser.getName().compareTo(APPLICATION_IMAGES) == 0 ) {
141
                                int splashtag = parser.nextTag();
142
                                if ( parser.getName().compareTo(SPLASH_IMAGES) == 0 ) {
143
                                    splashtag = parser.nextTag();
144
                                    boolean endSplash = false;
145

    
146
                                    // 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

    
154
                                        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

    
163
                                            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

    
183
                                }
184
                            }
185
                        }
186
                        if ( parser.getName().compareTo(APPLICATION_NAME) == 0 ) {
187
                            name = expand(parser.getAttributeValue("", VALUE));
188
                            tag = parser.next();
189
                        }
190
                        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
                        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

    
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
        }
227
    }
228

    
229
    private String fixPath(File theme, String path) {
230
        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
        File parent = theme.getParentFile().getAbsoluteFile();
234
        File fpath = new File(path);
235
        if ( !fpath.isAbsolute() ) {
236
            fpath = new File(parent, path);
237
        }
238
        return fpath.getAbsolutePath();
239
    }
240

    
241
    /**
242
     * Returns image to the splashwindow.
243
     *
244
     * @return ImageIcon[]
245
     */
246
    public ImageIcon[] getSplashImages() {
247
        ImageIcon[] imgs = new ImageIcon[images.size()];
248

    
249
        for ( int i = 0; i < images.size(); i++ ) {
250
            imgs[i] = new ImageIcon(images.get(i));
251
        }
252

    
253
        return imgs;
254
    }
255

    
256
    public String getTypeDesktop() {
257
        return wallpaperType;
258
    }
259

    
260
    /**
261
     * Return the icon.
262
     *
263
     * @return ImageIcon
264
     */
265
    public ImageIcon getIcon() {
266
        if ( icon == null ) {
267
            return null;
268
        }
269

    
270
        try {
271
            return new ImageIcon(icon);
272
        } catch (Exception e) {
273
            return null;
274
        }
275
    }
276

    
277
    /**
278
     * Return the backgroundimage.
279
     *
280
     * @return ImageIcon
281
     */
282
    public ImageIcon getBackgroundImage() {
283
        if ( backgroundimage == null ) {
284
            return null;
285
        }
286

    
287
        try {
288
            return new ImageIcon(backgroundimage);
289
        } catch (Exception e) {
290
            return null;
291
        }
292
    }
293

    
294
    /**
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
        for ( int i = 0; i < tms.length; i++ ) {
303
            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

    
318
    /**
319
     * Return the text to overwrite the images.
320
     *
321
     * @return String[]
322
     */
323
    public String[] getVersions() {
324
        return versions.toArray(new String[0]);
325
    }
326

    
327
    /**
328
     * Return the position of text to overwrite the images.
329
     *
330
     * @return Point[]
331
     */
332
    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
     * Return the font size text to overwrite the images.
346
     *
347
     * @return int[]
348
     */
349
    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
     * Return the font color of text to overwrite the images.
363
     *
364
     * @return Color[]
365
     */
366
    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
    
387
    public int getPriority() {
388
        return this.priority;
389
    }
390
}