Statistics
| Revision:

root / trunk / frameworks / _fwAndami / src / com / iver / andami / ui / theme / Theme.java @ 8925

History | View | Annotate | Download (7.13 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 java.io.BufferedReader;
8
import java.io.File;
9
import java.io.FileInputStream;
10
import java.io.FileNotFoundException;
11
import java.io.FileReader;
12
import java.io.IOException;
13

    
14
import java.util.ArrayList;
15

    
16
import javax.swing.ImageIcon;
17

    
18

    
19
/**
20
 * Personalization of the program according to the file xml with the necessary information.
21
 *
22
 * @author Vicente Caballero Navarro
23
 */
24
public class Theme {
25
    private static String encoding = "UTF-8";
26
    private static final String IMAGES = "Images";
27
    private static final String SPLASHIMAGES = "Splashimages";
28
    private static final String SPLASH = "Splash";
29
    private static final String PATH = "path";
30
    private static final String TIMER = "timer";
31
    private static final String ICON = "Icon";
32
    private static final String NAME = "Name";
33
    private static final String VALUE = "value";
34
        private static final Object BACKGROUNDIMAGE = "Backgroundimage";
35
    private ArrayList images = new ArrayList();
36
    private String icon;
37
    private ArrayList timers = new ArrayList();
38
    private String name = "";
39
        private String backgroundimage;
40

    
41
    public static void main(String[] args) {
42
        Theme theme = new Theme();
43
        theme.readTheme(new File(
44
                "c:/workspace/_fwAndami/theme/andami-theme.xml"));
45
    }
46

    
47
    /**
48
     * Read the file xml with the personalization.
49
     *
50
     * @param file xml
51
     */
52
    public void readTheme(File file) {
53
        try {
54
            FileReader fr;
55

    
56
            try {
57
                fr = new FileReader(file);
58

    
59
                BufferedReader br = new BufferedReader(fr);
60
                char[] buffer = new char[100];
61
                br.read(buffer);
62

    
63
                StringBuffer st = new StringBuffer(new String(buffer));
64
                String searchText = "encoding=\"";
65
                int index = st.indexOf(searchText);
66

    
67
                if (index > -1) {
68
                    st.delete(0, index + searchText.length());
69
                    encoding = st.substring(0, st.indexOf("\""));
70
                }
71
            } catch (FileNotFoundException ex) {
72
                ex.printStackTrace();
73
            } catch (IOException e) {
74
                e.printStackTrace();
75
            }
76

    
77
            fr = new FileReader(file);
78

    
79
            KXmlParser parser = new KXmlParser();
80
            parser.setInput(new FileInputStream(file), encoding);
81

    
82
            int tag = parser.nextTag();
83

    
84
            if (parser.getEventType() != KXmlParser.END_DOCUMENT) {
85
                parser.require(KXmlParser.START_TAG, null, IMAGES);
86

    
87
                while (tag != KXmlParser.END_DOCUMENT) {
88
                    //parser.next();
89
                    switch (tag) {
90
                    case KXmlParser.START_TAG:
91

    
92
                        if (parser.getName().compareTo(IMAGES) == 0) {
93
                            parser.nextTag();
94

    
95
                            if (parser.getName().compareTo(SPLASHIMAGES) == 0) {
96
                                int splashtag = parser.nextTag();
97
                                boolean endSplash = false;
98

    
99
                                //parser.require(KXmlParser.START_TAG, null, SPLASH);
100
                                while ((splashtag != KXmlParser.END_DOCUMENT) &&
101
                                        !endSplash) {
102
                                    if (splashtag == KXmlParser.END_TAG) {
103
                                        splashtag = parser.nextTag();
104

    
105
                                        continue;
106
                                    }
107

    
108
                                    if (parser.getName().compareTo(SPLASH) == 0) {
109
                                        images.add(parser.getAttributeValue(
110
                                                "", PATH));
111
                                        timers.add(parser.getAttributeValue(
112
                                                "", TIMER));
113
                                        System.out.println(images.size() + " " +
114
                                            timers.size());
115
                                        splashtag = parser.nextTag();
116
                                    } else {
117
                                        endSplash = true;
118
                                    }
119
                                }
120
                            }
121
                            int tagOptions=KXmlParser.START_TAG;
122
                            while ((tagOptions != KXmlParser.END_TAG)) {
123
                            if (parser.getName().compareTo(BACKGROUNDIMAGE) == 0) {
124
                                backgroundimage = parser.getAttributeValue("", PATH);
125
                                tag=parser.next();
126
                            }else if (parser.getName().compareTo(ICON) == 0) {
127
                                icon = parser.getAttributeValue("", PATH);
128
                                tag=parser.next();
129
                            }else if (parser.getName().compareTo(NAME) == 0) {
130
                                name = parser.getAttributeValue("", VALUE);
131
                                tag=parser.next();
132
                            }
133
                                    tagOptions = parser.nextTag();
134

    
135
                            }
136
                        }
137

    
138
                        break;
139

    
140
                    case KXmlParser.END_TAG:
141
                        break;
142

    
143
                    case KXmlParser.TEXT:
144

    
145
                        //System.out.println("[TEXT]["+kxmlParser.getText()+"]");
146
                        break;
147
                    }
148

    
149
                    tag = parser.next();
150
                }
151
            }
152

    
153
            parser.require(XmlPullParser.END_DOCUMENT, null, null);
154

    
155
            //}
156
        } catch (Exception e) {
157
            e.printStackTrace();
158
            System.out.println("error");
159
        }
160
    }
161

    
162
    /**
163
     * Returns image to the splashwindow.
164
     *
165
     * @return ImageIcon[]
166
     */
167
    public ImageIcon[] getSplashImages() {
168
        ImageIcon[] imgs = new ImageIcon[images.size()];
169

    
170
        for (int i = 0; i < images.size(); i++) {
171
            imgs[i] = new ImageIcon((String) images.get(i));
172
        }
173

    
174
        return imgs;
175
    }
176

    
177
    /**
178
     * Return the icon.
179
     *
180
     * @return ImageIcon
181
     */
182
    public ImageIcon getIcon() {
183
        if (icon == null) {
184
            return null;
185
        }
186

    
187
        try {
188
            return new ImageIcon(icon);
189
        } catch (Exception e) {
190
            return null;
191
        }
192
    }
193
    /**
194
     * Return the backgroundimage.
195
     *
196
     * @return ImageIcon
197
     */
198
    public ImageIcon getBackgroundImage() {
199
        if (backgroundimage == null) {
200
            return null;
201
        }
202

    
203
        try {
204
            return new ImageIcon(backgroundimage);
205
        } catch (Exception e) {
206
            return null;
207
        }
208
    }
209
    /**
210
     * Return the time of the splash images.
211
     *
212
     * @return long[]
213
     */
214
    public long[] getTimers() {
215
        long[] tms = new long[timers.size()];
216

    
217
        for (int i = 0; i < tms.length; i++) {
218
            tms[i] = Long.parseLong((String) timers.get(i));
219
        }
220

    
221
        return tms;
222
    }
223

    
224
    /**
225
     * Return the name of program.
226
     *
227
     * @return String
228
     */
229
    public String getName() {
230
        return name;
231
    }
232
}