Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / frameworks / _fwAndami / src / com / iver / andami / ui / theme / Theme.java @ 9661

History | View | Annotate | Download (7.18 KB)

1
package com.iver.andami.ui.theme;
2

    
3
import org.kxml2.io.KXmlParser;
4

    
5
import org.xmlpull.v1.XmlPullParser;
6

    
7
import com.iver.andami.messages.NotificationManager;
8

    
9
import java.io.BufferedReader;
10
import java.io.File;
11
import java.io.FileInputStream;
12
import java.io.FileNotFoundException;
13
import java.io.FileReader;
14
import java.io.IOException;
15

    
16
import java.util.ArrayList;
17

    
18
import javax.swing.ImageIcon;
19

    
20

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

    
31
        private static String encoding = "UTF-8";
32
        private static final String ANDAMI_PROPERTIES = "AndamiProperties";
33
    private static final String APPLICATION_IMAGES = "ApplicationImages";
34
    private static final String SPLASH_IMAGES = "SplashImages";
35
    private static final String SPLASH = "Splash";
36
    private static final String PATH = "path";
37
    private static final String TIMER = "timer";
38
    private static final String ICON = "Icon";
39
    private static final String APPLICATION_NAME = "ApplicationName";
40
    private static final String VALUE = "value";
41
        private static final String BACKGROUND_IMAGE = "BackgroundImage";
42
        private static final String WALLPAPER_TYPE = "WallpaperType";
43
    private ArrayList images = new ArrayList();
44
    private String icon;
45
    private ArrayList timers = new ArrayList();
46
    private String name = null;
47
        private String backgroundimage;
48
        private String wallpaperType=CENTERED;
49

    
50
    public static void main(String[] args) {
51
        Theme theme = new Theme();
52
        theme.readTheme(new File(
53
                "c:/workspace/_fwAndami/theme/andami-theme.xml"));
54
    }
55

    
56
    /**
57
     * Read the file xml with the personalization.
58
     *
59
     * @param file xml
60
     */
61
    public void readTheme(File file) {
62
        try {
63
            FileReader fr;
64

    
65
            try {
66
                fr = new FileReader(file);
67

    
68
                BufferedReader br = new BufferedReader(fr);
69
                char[] buffer = new char[100];
70
                br.read(buffer);
71

    
72
                StringBuffer st = new StringBuffer(new String(buffer));
73
                String searchText = "encoding=\"";
74
                int index = st.indexOf(searchText);
75

    
76
                if (index > -1) {
77
                    st.delete(0, index + searchText.length());
78
                    encoding = st.substring(0, st.indexOf("\""));
79
                }
80
            } catch (FileNotFoundException ex) {
81
                ex.printStackTrace();
82
            } catch (IOException e) {
83
                e.printStackTrace();
84
            }
85

    
86
            fr = new FileReader(file);
87

    
88
            KXmlParser parser = new KXmlParser();
89
            parser.setInput(new FileInputStream(file), encoding);
90

    
91
            int tag = parser.nextTag();
92

    
93
            if (parser.getEventType() != KXmlParser.END_DOCUMENT) {
94
                parser.require(KXmlParser.START_TAG, null, ANDAMI_PROPERTIES);
95

    
96
                while (tag != KXmlParser.END_DOCUMENT) {
97
                    //parser.next();
98
                    switch (tag) {
99
                    case KXmlParser.START_TAG:
100

    
101
                        if (parser.getName().compareTo(ANDAMI_PROPERTIES) == 0) {
102
                            parser.nextTag();
103
                            if (parser.getName().compareTo(APPLICATION_IMAGES) == 0) {
104
                                int splashtag = parser.nextTag();
105
                            if (parser.getName().compareTo(SPLASH_IMAGES) == 0) {
106
                                                                        splashtag = parser.nextTag();
107
                                                                        boolean endSplash = false;
108

    
109
                                                                        // parser.require(KXmlParser.START_TAG,
110
                                                                        // null, SPLASH);
111
                                                                        while ((splashtag != KXmlParser.END_DOCUMENT)
112
                                                                                        && !endSplash) {
113
                                                                                if (splashtag == KXmlParser.END_TAG) {
114
                                                                                        splashtag = parser.nextTag();
115

    
116
                                                                                        continue;
117
                                                                                }
118

    
119
                                                                                if (parser.getName().compareTo(SPLASH) == 0) {
120
                                                                                        images
121
                                                                                                        .add(parser
122
                                                                                                                        .getAttributeValue(
123
                                                                                                                                        "", PATH));
124
                                                                                        timers.add(parser
125
                                                                                                        .getAttributeValue("",
126
                                                                                                                        TIMER));
127
                                                                                        splashtag = parser.nextTag();
128
                                                                                } else {
129
                                                                                        endSplash = true;
130
                                                                                }
131
                                                                        }
132
                                                                }
133
                                                                int tagOptions = KXmlParser.START_TAG;
134
                                                                while ((tagOptions != KXmlParser.END_TAG)) {
135
                                                                        if (parser.getName().compareTo(
136
                                                                                        BACKGROUND_IMAGE) == 0) {
137
                                                                                backgroundimage = parser
138
                                                                                                .getAttributeValue("", PATH);
139
                                                                                tag = parser.next();
140
                                                                        } else if (parser.getName().compareTo(ICON) == 0) {
141
                                                                                icon = parser.getAttributeValue("",
142
                                                                                                PATH);
143
                                                                                tag = parser.next();
144
                                                                        } else if (parser.getName().compareTo(
145
                                                                                        WALLPAPER_TYPE) == 0) {
146
                                                                                wallpaperType = parser.getAttributeValue(
147
                                                                                                "", VALUE);
148
                                                                                tag = parser.next();
149
                                                                        }
150
                                                                        tagOptions = parser.nextTag();
151

    
152
                                                                }
153
                                                        }
154
                        }
155
                        if (parser.getName().compareTo(APPLICATION_NAME) == 0) {
156
                                                        name = parser.getAttributeValue("",
157
                                                                        VALUE);
158
                                                        tag = parser.next();
159
                                                }
160
                        break;
161

    
162
                    case KXmlParser.END_TAG:
163
                        break;
164

    
165
                    case KXmlParser.TEXT:
166

    
167
                        // System.out.println("[TEXT]["+kxmlParser.getText()+"]");
168
                        break;
169
                    }
170

    
171
                    tag = parser.next();
172
                }
173
            }
174

    
175
            parser.require(XmlPullParser.END_DOCUMENT, null, null);
176

    
177
            // }
178
        } catch (Exception e) {
179
            NotificationManager.addError("Error reading theme: "+file.getAbsolutePath(), e);
180
        }
181
    }
182

    
183
    /**
184
         * Returns image to the splashwindow.
185
         *
186
         * @return ImageIcon[]
187
         */
188
    public ImageIcon[] getSplashImages() {
189
        ImageIcon[] imgs = new ImageIcon[images.size()];
190

    
191
        for (int i = 0; i < images.size(); i++) {
192
            imgs[i] = new ImageIcon((String) images.get(i));
193
        }
194

    
195
        return imgs;
196
    }
197

    
198
    public String getTypeDesktop() {
199
            return wallpaperType;
200
    }
201
    /**
202
     * Return the icon.
203
     *
204
     * @return ImageIcon
205
     */
206
    public ImageIcon getIcon() {
207
        if (icon == null) {
208
            return null;
209
        }
210

    
211
        try {
212
            return new ImageIcon(icon);
213
        } catch (Exception e) {
214
            return null;
215
        }
216
    }
217
    /**
218
     * Return the backgroundimage.
219
     *
220
     * @return ImageIcon
221
     */
222
    public ImageIcon getBackgroundImage() {
223
        if (backgroundimage == null) {
224
            return null;
225
        }
226

    
227
        try {
228
            return new ImageIcon(backgroundimage);
229
        } catch (Exception e) {
230
            return null;
231
        }
232
    }
233
    /**
234
     * Return the time of the splash images.
235
     *
236
     * @return long[]
237
     */
238
    public long[] getTimers() {
239
        long[] tms = new long[timers.size()];
240

    
241
        for (int i = 0; i < tms.length; i++) {
242
            tms[i] = Long.parseLong((String) timers.get(i));
243
        }
244

    
245
        return tms;
246
    }
247

    
248
    /**
249
     * Return the name of program.
250
     *
251
     * @return String
252
     */
253
    public String getName() {
254
        return name;
255
    }
256
}