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 @ 41314

History | View | Annotate | Download (14.4 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 javax.swing.UIManager;
37
import org.apache.commons.io.IOUtils;
38
import org.apache.commons.lang.text.StrSubstitutor;
39
import org.apache.commons.lang3.StringUtils;
40

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

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

    
59
    private static final Logger logger = LoggerFactory.getLogger(Theme.class);
60

    
61
    public static final String CENTERED = "CENTERED";
62
    public static final String EXPAND = "EXPAND";
63
    public static final String MOSAIC = "MOSAIC";
64

    
65
    private static final String ANDAMI_PROPERTIES = "AndamiProperties";
66
    private static final String APPLICATION_IMAGES = "ApplicationImages";
67
    private static final String SPLASH_IMAGES = "SplashImages";
68
    private static final String SPLASH = "Splash";
69
    private static final String PATH = "path";
70
    private static final String TIMER = "timer";
71
    private static final String ICON = "Icon";
72
    private static final String APPLICATION_NAME = "ApplicationName";
73
    private static final String PRIORITY = "priority";
74
    private static final String VALUE = "value";
75
    private static final String BACKGROUND_IMAGE = "BackgroundImage";
76
    private static final String BACKGROUND_COLOR = "BackgroundColor";
77
    private static final String WALLPAPER_TYPE = "WallpaperType";
78

    
79
    private static final String VERSION = "version";
80
    private static final String FONTPOSITIONX = "x";
81
    private static final String FONTPOSITIONY = "y";
82

    
83
    private static final String FONTSIZE = "fontsize";
84
    private static final String FONTCOLOR = "color";
85

    
86
    private ArrayList<String> images = new ArrayList<String>();
87
    private String icon;
88
    private ArrayList<String> timers = new ArrayList<String>();
89
    private ArrayList<String> versions = new ArrayList<String>();
90
    private ArrayList<String> fontColors = new ArrayList<String>();
91
    private ArrayList<String> fontSizes = new ArrayList<String>();
92
    private ArrayList<String> fontpositionsX = new ArrayList<String>();
93
    private ArrayList<String> fontpositionsY = new ArrayList<String>();
94

    
95
    private String name = null;
96
    private String backgroundimage = null;
97
    private Color backgroundColor = null;
98
    private String wallpaperType = CENTERED;
99
    private int priority = 0;
100

    
101
    /**
102
     * Spurce file of the theme *
103
     */
104
    private File source = null;
105
    private StrSubstitutor strSubstitutor = null;
106

    
107
    public Theme() {
108
    }
109

    
110
    public Theme(Map vars) {
111
        this();
112
        this.strSubstitutor = new StrSubstitutor(vars);
113
    }
114

    
115
    public File getSource() {
116
        return source;
117
    }
118

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

    
129
            fis = new FileInputStream(file);
130
            KXmlParser parser = new KXmlParser();
131
            parser.setInput(fis, encoding);
132

    
133
            int tag = parser.nextTag();
134

    
135
            if ( parser.getEventType() != XmlPullParser.END_DOCUMENT ) {
136
                parser.require(XmlPullParser.START_TAG, null, ANDAMI_PROPERTIES);
137

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

    
150
                                    // parser.require(KXmlParser.START_TAG,null, SPLASH);
151
                                    while ( (splashtag != XmlPullParser.END_DOCUMENT)
152
                                            && !endSplash ) {
153
                                        if ( splashtag == XmlPullParser.END_TAG ) {
154
                                            splashtag = parser.nextTag();
155
                                            continue;
156
                                        }
157

    
158
                                        if ( parser.getName().compareTo(SPLASH) == 0 ) {
159
                                            images.add(fixPath(file, parser.getAttributeValue("", PATH)));
160
                                            timers.add(parser.getAttributeValue("", TIMER));
161
                                            versions.add(expand(parser.getAttributeValue("", VERSION)));
162
                                            fontpositionsX.add(parser.getAttributeValue("", FONTPOSITIONX));
163
                                            fontpositionsY.add(parser.getAttributeValue("", FONTPOSITIONY));
164
                                            fontSizes.add(parser.getAttributeValue("", FONTSIZE));
165
                                            fontColors.add(parser.getAttributeValue("", FONTCOLOR));
166

    
167
                                            splashtag = parser.nextTag();
168
                                        } else {
169
                                            endSplash = true;
170
                                        }
171
                                    }
172
                                }
173
                                int tagOptions = XmlPullParser.START_TAG;
174
                                while ( (tagOptions != XmlPullParser.END_TAG) ) {
175
                                    if ( parser.getName().compareTo(BACKGROUND_IMAGE) == 0 ) {
176
                                        backgroundimage = fixPath(file, parser.getAttributeValue("", PATH));
177
                                        tag = parser.next();
178
                                    } else if ( parser.getName().compareTo(BACKGROUND_COLOR) == 0 ) {
179
                                        backgroundColor = parseColor(parser.getAttributeValue("", "color"));
180
                                        tag = parser.next();
181
                                    } else if ( parser.getName().compareTo(ICON) == 0 ) {
182
                                        icon = fixPath(file, parser.getAttributeValue("", PATH));
183
                                        tag = parser.next();
184
                                    } else if ( parser.getName().compareTo(WALLPAPER_TYPE) == 0 ) {
185
                                        wallpaperType = parser.getAttributeValue("", VALUE);
186
                                        tag = parser.next();
187
                                    }
188
                                    tagOptions = parser.nextTag();
189

    
190
                                }
191
                            }
192
                        }
193
                        if ( parser.getName().compareTo(APPLICATION_NAME) == 0 ) {
194
                            name = expand(parser.getAttributeValue("", VALUE));
195
                            tag = parser.next();
196
                        }
197
                        if ( parser.getName().compareTo(PRIORITY) == 0 ) {
198
                            String s = parser.getAttributeValue("", VALUE);
199
                            try {
200
                                this.priority = Integer.parseInt(s);
201
                            } catch (Exception ex) {
202
                                this.priority = 0;
203
                            }
204
                            tag = parser.next();
205
                        }
206
                        break;
207

    
208
                    case XmlPullParser.END_TAG:
209
                        break;
210

    
211
                    case XmlPullParser.TEXT:
212
                        // System.out.println("[TEXT]["+kxmlParser.getText()+"]");
213
                        break;
214
                    }
215
                    tag = parser.next();
216
                }
217
            }
218

    
219
            parser.require(XmlPullParser.END_DOCUMENT, null, null);
220
            this.source = file;
221

    
222
        } catch (FileNotFoundException ex) {
223
            logger.warn("Can't open theme file '" + file.getAbsolutePath() + "'.", ex);
224

    
225
        } catch (XmlPullParserException ex) {
226
            logger.warn("Can't parse theme file '" + file.getAbsolutePath() + "'.", ex);
227

    
228
        } catch (IOException ex) {
229
            logger.warn("Can't read theme file '" + file.getAbsolutePath() + "'.", ex);
230

    
231
        } finally {
232
            IOUtils.closeQuietly(fis);
233
        }
234
    }
235

    
236
    private String fixPath(File theme, String path) {
237
        path = path.replace("$GVSIG_INSTALL", Launcher.getApplicationDirectory());
238
        path = path.replace("$GVSIG_HOME", Launcher.getAppHomeDir());
239
        path = path.replace("$GVSIG_PLUGINS", Launcher.getAndamiConfig().getPluginsDirectory());
240
        File parent = theme.getParentFile().getAbsoluteFile();
241
        File fpath = new File(path);
242
        if ( !fpath.isAbsolute() ) {
243
            fpath = new File(parent, path);
244
        }
245
        return fpath.getAbsolutePath();
246
    }
247

    
248
    /**
249
     * Returns image to the splashwindow.
250
     *
251
     * @return ImageIcon[]
252
     */
253
    public ImageIcon[] getSplashImages() {
254
        ImageIcon[] imgs = new ImageIcon[images.size()];
255

    
256
        for ( int i = 0; i < images.size(); i++ ) {
257
            imgs[i] = new ImageIcon(images.get(i));
258
        }
259

    
260
        return imgs;
261
    }
262

    
263
    public String getTypeDesktop() {
264
        return wallpaperType;
265
    }
266

    
267
    /**
268
     * Return the icon.
269
     *
270
     * @return ImageIcon
271
     */
272
    public ImageIcon getIcon() {
273
        if ( icon == null ) {
274
            return null;
275
        }
276

    
277
        try {
278
            return new ImageIcon(icon);
279
        } catch (Exception e) {
280
            return null;
281
        }
282
    }
283

    
284
    /**
285
     * Return the backgroundimage.
286
     *
287
     * @return ImageIcon
288
     */
289
    public ImageIcon getBackgroundImage() {
290
        if ( backgroundimage == null ) {
291
            return null;
292
        }
293

    
294
        try {
295
            return new ImageIcon(backgroundimage);
296
        } catch (Exception e) {
297
            return null;
298
        }
299
    }
300

    
301
    public Color getBackgroundColor() {
302
        if ( this.backgroundColor == null ) {
303
            this.backgroundColor = (Color) UIManager.get("Desktop.background");
304
        }
305
        return this.backgroundColor;
306
    }
307

    
308
    /**
309
     * Return the time of the splash images.
310
     *
311
     * @return long[]
312
     */
313
    public long[] getTimers() {
314
        long[] tms = new long[timers.size()];
315

    
316
        for ( int i = 0; i < tms.length; i++ ) {
317
            tms[i] = Long.parseLong(timers.get(i));
318
        }
319

    
320
        return tms;
321
    }
322

    
323
    /**
324
     * Return the name of program.
325
     *
326
     * @return String
327
     */
328
    public String getName() {
329
        return name;
330
    }
331

    
332
    /**
333
     * Return the text to overwrite the images.
334
     *
335
     * @return String[]
336
     */
337
    public String[] getVersions() {
338
        return versions.toArray(new String[0]);
339
    }
340

    
341
    /**
342
     * Return the position of text to overwrite the images.
343
     *
344
     * @return Point[]
345
     */
346
    public Point[] getPositions() {
347
        Point[] points = new Point[fontpositionsX.size()];
348
        for ( int i = 0; i < points.length; i++ ) {
349
            try {
350
                points[i] = new Point(Integer.valueOf(fontpositionsX.get(i)), Integer.valueOf(fontpositionsY.get(i)));
351
            } catch (NumberFormatException e) {
352
                NotificationManager.addInfo(PluginServices.getText(this, "incorrect_position"), e);
353
            }
354
        }
355
        return points;
356
    }
357

    
358
    /**
359
     * Return the font size text to overwrite the images.
360
     *
361
     * @return int[]
362
     */
363
    public int[] getFontSizes() {
364
        int[] sizes = new int[fontSizes.size()];
365
        for ( int i = 0; i < sizes.length; i++ ) {
366
            try {
367
                sizes[i] = Integer.valueOf(fontSizes.get(i));
368
            } catch (NumberFormatException e) {
369
                NotificationManager.addInfo(PluginServices.getText(this, "incorrect_size"), e);
370
            }
371
        }
372
        return sizes;
373
    }
374

    
375
    /**
376
     * Return the font color of text to overwrite the images.
377
     *
378
     * @return Color[]
379
     */
380
    public Color[] getFontColors() {
381
        Color[] colors = new Color[fontColors.size()];
382
        for ( int i = 0; i < colors.length; i++ ) {
383
            try {
384
                String s = fontColors.get(i);
385
                colors[i] = parseColor(s);
386
            } catch (Exception e) {
387
                NotificationManager.addInfo(PluginServices.getText(this, "incorrect_color"), e);
388
            }
389
        }
390
        return colors;
391
    }
392

    
393
    private Color parseColor(String s) {
394
        if ( StringUtils.isEmpty(s) ) {
395
            return null;
396
        }
397
        String[] rgb = s.split(",");
398
        Color color = new Color(Integer.valueOf(rgb[0]), Integer.valueOf(rgb[1]), Integer.valueOf(rgb[2]));
399
        return color;
400
    }
401

    
402
    private String expand(String value) {
403
        if ( this.strSubstitutor == null ) {
404
            return value;
405
        }
406
        return strSubstitutor.replace(value);
407
    }
408

    
409
    public int getPriority() {
410
        return this.priority;
411
    }
412
}