Statistics
| Revision:

svn-gvsig-desktop / tags / J2ME_compat_v1_2_Build_1209 / frameworks / _fwAndami / src / com / iver / andami / ui / theme / Theme.java @ 19520

History | View | Annotate | Download (9.95 KB)

1
package com.iver.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.kxml2.io.KXmlParser;
16
import org.xmlpull.v1.XmlPullParser;
17

    
18
import com.iver.andami.PluginServices;
19
import com.iver.andami.messages.NotificationManager;
20

    
21

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

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

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

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

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

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

    
66
    public static void main(String[] args) {
67
        Theme theme = new Theme();
68
        theme.readTheme(new File(
69
                "c:/workspace/_fwAndami/theme/andami-theme.xml"));
70
    }
71

    
72
    /**
73
     * Read the file xml with the personalization.
74
     *
75
     * @param file xml
76
     */
77
    public void readTheme(File file) {
78
        try {
79
            FileReader fr;
80

    
81
            try {
82
                fr = new FileReader(file);
83

    
84
                BufferedReader br = new BufferedReader(fr);
85
                char[] buffer = new char[100];
86
                br.read(buffer);
87

    
88
                StringBuffer st = new StringBuffer(new String(buffer));
89
                String searchText = "encoding=\"";
90
                int index = st.indexOf(searchText);
91

    
92
                if (index > -1) {
93
                    st.delete(0, index + searchText.length());
94
                    encoding = st.substring(0, st.indexOf("\""));
95
                }
96
            } catch (FileNotFoundException ex) {
97
                ex.printStackTrace();
98
            } catch (IOException e) {
99
                e.printStackTrace();
100
            }
101

    
102
            fr = new FileReader(file);
103

    
104
            KXmlParser parser = new KXmlParser();
105
            parser.setInput(new FileInputStream(file), encoding);
106

    
107
            int tag = parser.nextTag();
108

    
109
            if (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
110
                parser.require(XmlPullParser.START_TAG, null, ANDAMI_PROPERTIES);
111

    
112
                while (tag != XmlPullParser.END_DOCUMENT) {
113
                    //parser.next();
114
                    switch (tag) {
115
                    case XmlPullParser.START_TAG:
116

    
117
                        if (parser.getName().compareTo(ANDAMI_PROPERTIES) == 0) {
118
                            parser.nextTag();
119
                            if (parser.getName().compareTo(APPLICATION_IMAGES) == 0) {
120
                                int splashtag = parser.nextTag();
121
                            if (parser.getName().compareTo(SPLASH_IMAGES) == 0) {
122
                                                                        splashtag = parser.nextTag();
123
                                                                        boolean endSplash = false;
124

    
125
                                                                        // parser.require(KXmlParser.START_TAG,
126
                                                                        // null, SPLASH);
127
                                                                        while ((splashtag != XmlPullParser.END_DOCUMENT)
128
                                                                                        && !endSplash) {
129
                                                                                if (splashtag == XmlPullParser.END_TAG) {
130
                                                                                        splashtag = parser.nextTag();
131

    
132
                                                                                        continue;
133
                                                                                }
134

    
135
                                                                                if (parser.getName().compareTo(SPLASH) == 0) {
136
                                                                                        images.add(parser
137
                                                                                                        .getAttributeValue("",
138
                                                                                                                        PATH));
139
                                                                                        timers.add(parser
140
                                                                                                        .getAttributeValue("",
141
                                                                                                                        TIMER));
142
                                                                                        versions.add(parser
143
                                                                                                        .getAttributeValue("",
144
                                                                                                                        VERSION));
145
                                                                                        fontpositionsX.add(parser
146
                                                                                                        .getAttributeValue("",
147
                                                                                                                        FONTPOSITIONX));
148
                                                                                        fontpositionsY.add(parser
149
                                                                                                        .getAttributeValue("",
150
                                                                                                                        FONTPOSITIONY));
151
                                                                                        fontSizes.add(parser
152
                                                                                                        .getAttributeValue("",
153
                                                                                                                        FONTSIZE));
154
                                                                                        fontColors.add(parser
155
                                                                                                        .getAttributeValue("",
156
                                                                                                                        FONTCOLOR));
157

    
158
                                                                                        splashtag = parser.nextTag();
159
                                                                                } else {
160
                                                                                        endSplash = true;
161
                                                                                }
162
                                                                        }
163
                                                                }
164
                                                                int tagOptions = XmlPullParser.START_TAG;
165
                                                                while ((tagOptions != XmlPullParser.END_TAG)) {
166
                                                                        if (parser.getName().compareTo(
167
                                                                                        BACKGROUND_IMAGE) == 0) {
168
                                                                                backgroundimage = parser
169
                                                                                                .getAttributeValue("", PATH);
170
                                                                                tag = parser.next();
171
                                                                        } else if (parser.getName().compareTo(ICON) == 0) {
172
                                                                                icon = parser.getAttributeValue("",
173
                                                                                                PATH);
174
                                                                                tag = parser.next();
175
                                                                        } else if (parser.getName().compareTo(
176
                                                                                        WALLPAPER_TYPE) == 0) {
177
                                                                                wallpaperType = parser.getAttributeValue(
178
                                                                                                "", VALUE);
179
                                                                                tag = parser.next();
180
                                                                        }
181
                                                                        tagOptions = parser.nextTag();
182

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

    
193
                    case XmlPullParser.END_TAG:
194
                        break;
195

    
196
                    case XmlPullParser.TEXT:
197

    
198
                        // System.out.println("[TEXT]["+kxmlParser.getText()+"]");
199
                        break;
200
                    }
201

    
202
                    tag = parser.next();
203
                }
204
            }
205

    
206
            parser.require(XmlPullParser.END_DOCUMENT, null, null);
207

    
208
            // }
209
        } catch (Exception e) {
210
                 NotificationManager.addError("Error reading theme: "+file.getAbsolutePath(), e);
211
        }
212
    }
213

    
214
    /**
215
         * Returns image to the splashwindow.
216
         *
217
         * @return ImageIcon[]
218
         */
219
    public ImageIcon[] getSplashImages() {
220
        ImageIcon[] imgs = new ImageIcon[images.size()];
221

    
222
        for (int i = 0; i < images.size(); i++) {
223
            imgs[i] = new ImageIcon((String) images.get(i));
224
        }
225

    
226
        return imgs;
227
    }
228

    
229
    public String getTypeDesktop() {
230
            return wallpaperType;
231
    }
232
    /**
233
     * Return the icon.
234
     *
235
     * @return ImageIcon
236
     */
237
    public ImageIcon getIcon() {
238
        if (icon == null) {
239
            return null;
240
        }
241

    
242
        try {
243
            return new ImageIcon(icon);
244
        } catch (Exception e) {
245
            return null;
246
        }
247
    }
248
    /**
249
     * Return the backgroundimage.
250
     *
251
     * @return ImageIcon
252
     */
253
    public ImageIcon getBackgroundImage() {
254
        if (backgroundimage == null) {
255
            return null;
256
        }
257

    
258
        try {
259
            return new ImageIcon(backgroundimage);
260
        } catch (Exception e) {
261
            return null;
262
        }
263
    }
264
    /**
265
     * Return the time of the splash images.
266
     *
267
     * @return long[]
268
     */
269
    public long[] getTimers() {
270
        long[] tms = new long[timers.size()];
271

    
272
        for (int i = 0; i < tms.length; i++) {
273
            tms[i] = Long.parseLong((String) timers.get(i));
274
        }
275

    
276
        return tms;
277
    }
278

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