Statistics
| Revision:

root / branches / v2_0_0_prep / frameworks / _fwAndami / src / org / gvsig / andami / ui / theme / Theme.java @ 35978

History | View | Annotate | Download (10.6 KB)

1
package org.gvsig.andami.ui.theme;
2

    
3
import java.awt.Color;
4
import java.awt.Point;
5
import java.io.BufferedReader;
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.FileNotFoundException;
9
import java.io.FileReader;
10
import java.io.IOException;
11
import java.util.ArrayList;
12

    
13
import javax.swing.ImageIcon;
14

    
15
import org.gvsig.andami.Launcher;
16
import org.gvsig.andami.PluginServices;
17
import org.gvsig.andami.messages.NotificationManager;
18
import org.kxml2.io.KXmlParser;
19
import org.xmlpull.v1.XmlPullParser;
20

    
21

    
22

    
23
/**
24
 * Personalization of the program according to the file xml with the necessary information.
25
 *
26
 * @author Vicente Caballero Navarro
27
 */
28
public class Theme {
29
    public static final String CENTERED = "CENTERED";
30
    public static final String EXPAND = "EXPAND";
31
    public static final String MOSAIC = "MOSAIC";
32

    
33
        private static String encoding = "UTF-8";
34
        private static final String ANDAMI_PROPERTIES = "AndamiProperties";
35
    private static final String APPLICATION_IMAGES = "ApplicationImages";
36
    private static final String SPLASH_IMAGES = "SplashImages";
37
    private static final String SPLASH = "Splash";
38
    private static final String PATH = "path";
39
    private static final String TIMER = "timer";
40
    private static final String ICON = "Icon";
41
    private static final String APPLICATION_NAME = "ApplicationName";
42
    private static final String VALUE = "value";
43
        private static final String BACKGROUND_IMAGE = "BackgroundImage";
44
        private static final String WALLPAPER_TYPE = "WallpaperType";
45

    
46
        private static final String VERSION="version";
47
        private static final String FONTPOSITIONX="x";
48
        private static final String FONTPOSITIONY="y";
49

    
50
        private static final String FONTSIZE="fontsize";
51
        private static final String FONTCOLOR="color";
52

    
53
        private ArrayList<String> images = new ArrayList<String>();
54
    private String icon;
55
    private ArrayList<String> timers = new ArrayList<String>();
56
    private ArrayList<String> versions = new ArrayList<String>();
57
    private ArrayList<String> fontColors = new ArrayList<String>();
58
    private ArrayList<String> fontSizes = new ArrayList<String>();
59
    private ArrayList<String> fontpositionsX=new ArrayList<String>();
60
    private ArrayList<String> fontpositionsY=new ArrayList<String>();
61

    
62
    private String name = null;
63
        private String backgroundimage;
64
        private String wallpaperType=CENTERED;
65

    
66
        /** Spurce file of the theme **/
67
        private File source = null;
68
        
69
    public static void main(String[] args) {
70
        Theme theme = new Theme();
71
        theme.readTheme(new File(
72
                "c:/workspace/_fwAndami/theme/andami-theme.xml"));
73
    }
74

    
75
    public File getSource() {
76
            return source;
77
    }
78
    
79
    /**
80
     * Read the file xml with the personalization.
81
     *
82
     * @param file xml
83
     */
84
    public void readTheme(File file) {
85
        try {
86
            FileReader fr;
87

    
88
            try {
89
                fr = new FileReader(file);
90

    
91
                BufferedReader br = new BufferedReader(fr);
92
                char[] buffer = new char[100];
93
                br.read(buffer);
94

    
95
                StringBuffer st = new StringBuffer(new String(buffer));
96
                String searchText = "encoding=\"";
97
                int index = st.indexOf(searchText);
98

    
99
                if (index > -1) {
100
                    st.delete(0, index + searchText.length());
101
                    encoding = st.substring(0, st.indexOf("\""));
102
                }
103
            } catch (FileNotFoundException ex) {
104
                ex.printStackTrace();
105
            } catch (IOException e) {
106
                e.printStackTrace();
107
            }
108

    
109
            fr = new FileReader(file);
110

    
111
            KXmlParser parser = new KXmlParser();
112
            parser.setInput(new FileInputStream(file), encoding);
113

    
114
            int tag = parser.nextTag();
115

    
116
            if (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
117
                parser.require(XmlPullParser.START_TAG, null, ANDAMI_PROPERTIES);
118

    
119
                while (tag != XmlPullParser.END_DOCUMENT) {
120
                    //parser.next();
121
                    switch (tag) {
122
                    case XmlPullParser.START_TAG:
123

    
124
                        if (parser.getName().compareTo(ANDAMI_PROPERTIES) == 0) {
125
                            parser.nextTag();
126
                            if (parser.getName().compareTo(APPLICATION_IMAGES) == 0) {
127
                                int splashtag = parser.nextTag();
128
                            if (parser.getName().compareTo(SPLASH_IMAGES) == 0) {
129
                                                                        splashtag = parser.nextTag();
130
                                                                        boolean endSplash = false;
131

    
132
                                                                        // parser.require(KXmlParser.START_TAG,
133
                                                                        // null, SPLASH);
134
                                                                        while ((splashtag != XmlPullParser.END_DOCUMENT)
135
                                                                                        && !endSplash) {
136
                                                                                if (splashtag == XmlPullParser.END_TAG) {
137
                                                                                        splashtag = parser.nextTag();
138

    
139
                                                                                        continue;
140
                                                                                }
141

    
142
                                                                                if (parser.getName().compareTo(SPLASH) == 0) {
143
                                                                                        images.add(fixPath(file, parser.getAttributeValue("",        PATH)));
144
                                                                                        timers.add(parser
145
                                                                                                        .getAttributeValue("",
146
                                                                                                                        TIMER));
147
                                                                                        versions.add(parser
148
                                                                                                        .getAttributeValue("",
149
                                                                                                                        VERSION));
150
                                                                                        fontpositionsX.add(parser
151
                                                                                                        .getAttributeValue("",
152
                                                                                                                        FONTPOSITIONX));
153
                                                                                        fontpositionsY.add(parser
154
                                                                                                        .getAttributeValue("",
155
                                                                                                                        FONTPOSITIONY));
156
                                                                                        fontSizes.add(parser
157
                                                                                                        .getAttributeValue("",
158
                                                                                                                        FONTSIZE));
159
                                                                                        fontColors.add(parser
160
                                                                                                        .getAttributeValue("",
161
                                                                                                                        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(
172
                                                                                        BACKGROUND_IMAGE) == 0) {
173
                                                                                backgroundimage = fixPath(file, parser.getAttributeValue("", PATH));
174
                                                                                tag = parser.next();
175
                                                                        } else if (parser.getName().compareTo(ICON) == 0) {
176
                                                                                icon = fixPath(file, parser.getAttributeValue("",PATH));
177
                                                                                tag = parser.next();
178
                                                                        } else if (parser.getName().compareTo(
179
                                                                                        WALLPAPER_TYPE) == 0) {
180
                                                                                wallpaperType = parser.getAttributeValue(
181
                                                                                                "", VALUE);
182
                                                                                tag = parser.next();
183
                                                                        }
184
                                                                        tagOptions = parser.nextTag();
185

    
186
                                                                }
187
                                                        }
188
                        }
189
                        if (parser.getName().compareTo(APPLICATION_NAME) == 0) {
190
                                                        name = parser.getAttributeValue("",
191
                                                                        VALUE);
192
                                                        tag = parser.next();
193
                                                }
194
                        break;
195

    
196
                    case XmlPullParser.END_TAG:
197
                        break;
198

    
199
                    case XmlPullParser.TEXT:
200

    
201
                        // System.out.println("[TEXT]["+kxmlParser.getText()+"]");
202
                        break;
203
                    }
204

    
205
                    tag = parser.next();
206
                }
207
            }
208

    
209
            parser.require(XmlPullParser.END_DOCUMENT, null, null);
210
            this.source = file;
211
            // }
212
        } catch (Exception e) {
213
                 NotificationManager.addError("Error reading theme: "+file.getAbsolutePath(), e);
214
        }
215
    }
216

    
217
    private String fixPath(File theme, String path) {
218
            path = path.replace("$GVSIG_INSTALL",Launcher.getApplicationDirectory());
219
            path = path.replace("$GVSIG_HOME", Launcher.getAppHomeDir());
220
            path = path.replace("$GVSIG_PLUGINS", Launcher.getAndamiConfig().getPluginsDirectory());
221
        File parent = theme.getParentFile().getAbsoluteFile();
222
        File fpath = new File(path);
223
        if( ! fpath.isAbsolute() ) {
224
            fpath = new File(parent,path);
225
        }
226
            return fpath.getAbsolutePath();
227
    }
228
    
229
    /**
230
         * Returns image to the splashwindow.
231
         *
232
         * @return ImageIcon[]
233
         */
234
    public ImageIcon[] getSplashImages() {
235
        ImageIcon[] imgs = new ImageIcon[images.size()];
236

    
237
        for (int i = 0; i < images.size(); i++) {
238
            imgs[i] = new ImageIcon(images.get(i));
239
        }
240

    
241
        return imgs;
242
    }
243

    
244
    public String getTypeDesktop() {
245
            return wallpaperType;
246
    }
247
    /**
248
     * Return the icon.
249
     *
250
     * @return ImageIcon
251
     */
252
    public ImageIcon getIcon() {
253
        if (icon == null) {
254
            return null;
255
        }
256

    
257
        try {
258
            return new ImageIcon(icon);
259
        } catch (Exception e) {
260
            return null;
261
        }
262
    }
263
    /**
264
     * Return the backgroundimage.
265
     *
266
     * @return ImageIcon
267
     */
268
    public ImageIcon getBackgroundImage() {
269
        if (backgroundimage == null) {
270
            return null;
271
        }
272

    
273
        try {
274
            return new ImageIcon(backgroundimage);
275
        } catch (Exception e) {
276
            return null;
277
        }
278
    }
279
    /**
280
     * Return the time of the splash images.
281
     *
282
     * @return long[]
283
     */
284
    public long[] getTimers() {
285
        long[] tms = new long[timers.size()];
286

    
287
        for (int i = 0; i < tms.length; i++) {
288
            tms[i] = Long.parseLong(timers.get(i));
289
        }
290

    
291
        return tms;
292
    }
293

    
294
    /**
295
     * Return the name of program.
296
     *
297
     * @return String
298
     */
299
    public String getName() {
300
        return name;
301
    }
302
    /**
303
     * Return the text to overwrite the images.
304
     *
305
     * @return String[]
306
     */
307
        public String[] getVersions() {
308
                return versions.toArray(new String[0]);
309
        }
310
        /**
311
     * Return the position of text to overwrite the images.
312
     *
313
     * @return Point[]
314
     */
315
        public Point[] getPositions() {
316
                Point[] points=new Point[fontpositionsX.size()];
317
                for (int i=0;i<points.length;i++) {
318
                        try {
319
                                points[i]=new Point(Integer.valueOf(fontpositionsX.get(i)),Integer.valueOf(fontpositionsY.get(i)));
320
                        }catch (NumberFormatException e) {
321
                                NotificationManager.addInfo(PluginServices.getText(this,"incorrect_position"),e);
322
                        }
323
                }
324
                return points;
325
        }
326
        /**
327
     * Return the font size text to overwrite the images.
328
     *
329
     * @return int[]
330
     */
331
        public int[] getFontSizes() {
332
                int[] sizes=new int[fontSizes.size()];
333
                for (int i=0;i<sizes.length;i++) {
334
                        try {
335
                                sizes[i]=Integer.valueOf(fontSizes.get(i));
336
                        }catch (NumberFormatException e) {
337
                                NotificationManager.addInfo(PluginServices.getText(this,"incorrect_size"),e);
338
                        }
339
                }
340
                return sizes;
341
        }
342
        /**
343
     * Return the font color of text to overwrite the images.
344
     *
345
     * @return Color[]
346
     */
347
        public Color[] getFontColors() {
348
                Color[] colors=new Color[fontColors.size()];
349
                for (int i=0;i<colors.length;i++) {
350
                        try {
351
                        String s=fontColors.get(i);
352
                        String[] rgb=s.split(",");
353
                        colors[i]=new Color(Integer.valueOf(rgb[0]),Integer.valueOf(rgb[1]),Integer.valueOf(rgb[2]));
354
                        }catch (Exception e) {
355
                                NotificationManager.addInfo(PluginServices.getText(this,"incorrect_color"),e);
356
                        }
357
                }
358
                return colors;
359
        }
360
}