root / branches / v2_0_0_prep / frameworks / _fwAndami / src / org / gvsig / andami / ui / theme / Theme.java @ 29593
History | View | Annotate | Download (9.91 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.PluginServices; |
16 |
import org.gvsig.andami.messages.NotificationManager; |
17 |
import org.kxml2.io.KXmlParser; |
18 |
import org.xmlpull.v1.XmlPullParser; |
19 |
|
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 |
private ArrayList<String> images = new ArrayList<String>(); |
53 |
private String icon; |
54 |
private ArrayList<String> timers = new ArrayList<String>(); |
55 |
private ArrayList<String> versions = new ArrayList<String>(); |
56 |
private ArrayList<String> fontColors = new ArrayList<String>(); |
57 |
private ArrayList<String> fontSizes = new ArrayList<String>(); |
58 |
private ArrayList<String> fontpositionsX=new ArrayList<String>(); |
59 |
private ArrayList<String> fontpositionsY=new ArrayList<String>(); |
60 |
|
61 |
private String name = null; |
62 |
private String backgroundimage; |
63 |
private String wallpaperType=CENTERED; |
64 |
|
65 |
public static void main(String[] args) { |
66 |
Theme theme = new Theme();
|
67 |
theme.readTheme(new File( |
68 |
"c:/workspace/_fwAndami/theme/andami-theme.xml"));
|
69 |
} |
70 |
|
71 |
/**
|
72 |
* Read the file xml with the personalization.
|
73 |
*
|
74 |
* @param file xml
|
75 |
*/
|
76 |
public void readTheme(File file) { |
77 |
try {
|
78 |
FileReader fr;
|
79 |
|
80 |
try {
|
81 |
fr = new FileReader(file); |
82 |
|
83 |
BufferedReader br = new BufferedReader(fr); |
84 |
char[] buffer = new char[100]; |
85 |
br.read(buffer); |
86 |
|
87 |
StringBuffer st = new StringBuffer(new String(buffer)); |
88 |
String searchText = "encoding=\""; |
89 |
int index = st.indexOf(searchText);
|
90 |
|
91 |
if (index > -1) { |
92 |
st.delete(0, index + searchText.length());
|
93 |
encoding = st.substring(0, st.indexOf("\"")); |
94 |
} |
95 |
} catch (FileNotFoundException ex) { |
96 |
ex.printStackTrace(); |
97 |
} catch (IOException e) { |
98 |
e.printStackTrace(); |
99 |
} |
100 |
|
101 |
fr = new FileReader(file); |
102 |
|
103 |
KXmlParser parser = new KXmlParser();
|
104 |
parser.setInput(new FileInputStream(file), encoding); |
105 |
|
106 |
int tag = parser.nextTag();
|
107 |
|
108 |
if (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
|
109 |
parser.require(XmlPullParser.START_TAG, null, ANDAMI_PROPERTIES);
|
110 |
|
111 |
while (tag != XmlPullParser.END_DOCUMENT) {
|
112 |
//parser.next();
|
113 |
switch (tag) {
|
114 |
case XmlPullParser.START_TAG:
|
115 |
|
116 |
if (parser.getName().compareTo(ANDAMI_PROPERTIES) == 0) { |
117 |
parser.nextTag(); |
118 |
if (parser.getName().compareTo(APPLICATION_IMAGES) == 0) { |
119 |
int splashtag = parser.nextTag();
|
120 |
if (parser.getName().compareTo(SPLASH_IMAGES) == 0) { |
121 |
splashtag = parser.nextTag(); |
122 |
boolean endSplash = false; |
123 |
|
124 |
// parser.require(KXmlParser.START_TAG,
|
125 |
// null, SPLASH);
|
126 |
while ((splashtag != XmlPullParser.END_DOCUMENT)
|
127 |
&& !endSplash) { |
128 |
if (splashtag == XmlPullParser.END_TAG) {
|
129 |
splashtag = parser.nextTag(); |
130 |
|
131 |
continue;
|
132 |
} |
133 |
|
134 |
if (parser.getName().compareTo(SPLASH) == 0) { |
135 |
images.add(parser |
136 |
.getAttributeValue("",
|
137 |
PATH)); |
138 |
timers.add(parser |
139 |
.getAttributeValue("",
|
140 |
TIMER)); |
141 |
versions.add(parser |
142 |
.getAttributeValue("",
|
143 |
VERSION)); |
144 |
fontpositionsX.add(parser |
145 |
.getAttributeValue("",
|
146 |
FONTPOSITIONX)); |
147 |
fontpositionsY.add(parser |
148 |
.getAttributeValue("",
|
149 |
FONTPOSITIONY)); |
150 |
fontSizes.add(parser |
151 |
.getAttributeValue("",
|
152 |
FONTSIZE)); |
153 |
fontColors.add(parser |
154 |
.getAttributeValue("",
|
155 |
FONTCOLOR)); |
156 |
|
157 |
splashtag = parser.nextTag(); |
158 |
} else {
|
159 |
endSplash = true;
|
160 |
} |
161 |
} |
162 |
} |
163 |
int tagOptions = XmlPullParser.START_TAG;
|
164 |
while ((tagOptions != XmlPullParser.END_TAG)) {
|
165 |
if (parser.getName().compareTo(
|
166 |
BACKGROUND_IMAGE) == 0) {
|
167 |
backgroundimage = parser |
168 |
.getAttributeValue("", PATH);
|
169 |
tag = parser.next(); |
170 |
} else if (parser.getName().compareTo(ICON) == 0) { |
171 |
icon = parser.getAttributeValue("",
|
172 |
PATH); |
173 |
tag = parser.next(); |
174 |
} else if (parser.getName().compareTo( |
175 |
WALLPAPER_TYPE) == 0) {
|
176 |
wallpaperType = parser.getAttributeValue( |
177 |
"", VALUE);
|
178 |
tag = parser.next(); |
179 |
} |
180 |
tagOptions = parser.nextTag(); |
181 |
|
182 |
} |
183 |
} |
184 |
} |
185 |
if (parser.getName().compareTo(APPLICATION_NAME) == 0) { |
186 |
name = parser.getAttributeValue("",
|
187 |
VALUE); |
188 |
tag = parser.next(); |
189 |
} |
190 |
break;
|
191 |
|
192 |
case XmlPullParser.END_TAG:
|
193 |
break;
|
194 |
|
195 |
case XmlPullParser.TEXT:
|
196 |
|
197 |
// System.out.println("[TEXT]["+kxmlParser.getText()+"]");
|
198 |
break;
|
199 |
} |
200 |
|
201 |
tag = parser.next(); |
202 |
} |
203 |
} |
204 |
|
205 |
parser.require(XmlPullParser.END_DOCUMENT, null, null); |
206 |
|
207 |
// }
|
208 |
} catch (Exception e) { |
209 |
NotificationManager.addError("Error reading theme: "+file.getAbsolutePath(), e);
|
210 |
} |
211 |
} |
212 |
|
213 |
/**
|
214 |
* Returns image to the splashwindow.
|
215 |
*
|
216 |
* @return ImageIcon[]
|
217 |
*/
|
218 |
public ImageIcon[] getSplashImages() { |
219 |
ImageIcon[] imgs = new ImageIcon[images.size()]; |
220 |
|
221 |
for (int i = 0; i < images.size(); i++) { |
222 |
imgs[i] = new ImageIcon(images.get(i)); |
223 |
} |
224 |
|
225 |
return imgs;
|
226 |
} |
227 |
|
228 |
public String getTypeDesktop() { |
229 |
return wallpaperType;
|
230 |
} |
231 |
/**
|
232 |
* Return the icon.
|
233 |
*
|
234 |
* @return ImageIcon
|
235 |
*/
|
236 |
public ImageIcon getIcon() { |
237 |
if (icon == null) { |
238 |
return null; |
239 |
} |
240 |
|
241 |
try {
|
242 |
return new ImageIcon(icon); |
243 |
} catch (Exception e) { |
244 |
return null; |
245 |
} |
246 |
} |
247 |
/**
|
248 |
* Return the backgroundimage.
|
249 |
*
|
250 |
* @return ImageIcon
|
251 |
*/
|
252 |
public ImageIcon getBackgroundImage() { |
253 |
if (backgroundimage == null) { |
254 |
return null; |
255 |
} |
256 |
|
257 |
try {
|
258 |
return new ImageIcon(backgroundimage); |
259 |
} catch (Exception e) { |
260 |
return null; |
261 |
} |
262 |
} |
263 |
/**
|
264 |
* Return the time of the splash images.
|
265 |
*
|
266 |
* @return long[]
|
267 |
*/
|
268 |
public long[] getTimers() { |
269 |
long[] tms = new long[timers.size()]; |
270 |
|
271 |
for (int i = 0; i < tms.length; i++) { |
272 |
tms[i] = Long.parseLong(timers.get(i));
|
273 |
} |
274 |
|
275 |
return tms;
|
276 |
} |
277 |
|
278 |
/**
|
279 |
* Return the name of program.
|
280 |
*
|
281 |
* @return String
|
282 |
*/
|
283 |
public String getName() { |
284 |
return name;
|
285 |
} |
286 |
/**
|
287 |
* Return the text to overwrite the images.
|
288 |
*
|
289 |
* @return String[]
|
290 |
*/
|
291 |
public String[] getVersions() { |
292 |
return versions.toArray(new String[0]); |
293 |
} |
294 |
/**
|
295 |
* Return the position of text to overwrite the images.
|
296 |
*
|
297 |
* @return Point[]
|
298 |
*/
|
299 |
public Point[] getPositions() { |
300 |
Point[] points=new Point[fontpositionsX.size()]; |
301 |
for (int i=0;i<points.length;i++) { |
302 |
try {
|
303 |
points[i]=new Point(Integer.valueOf(fontpositionsX.get(i)),Integer.valueOf(fontpositionsY.get(i))); |
304 |
}catch (NumberFormatException e) { |
305 |
NotificationManager.addInfo(PluginServices.getText(this,"incorrect_position"),e); |
306 |
} |
307 |
} |
308 |
return points;
|
309 |
} |
310 |
/**
|
311 |
* Return the font size text to overwrite the images.
|
312 |
*
|
313 |
* @return int[]
|
314 |
*/
|
315 |
public int[] getFontSizes() { |
316 |
int[] sizes=new int[fontSizes.size()]; |
317 |
for (int i=0;i<sizes.length;i++) { |
318 |
try {
|
319 |
sizes[i]=Integer.valueOf(fontSizes.get(i));
|
320 |
}catch (NumberFormatException e) { |
321 |
NotificationManager.addInfo(PluginServices.getText(this,"incorrect_size"),e); |
322 |
} |
323 |
} |
324 |
return sizes;
|
325 |
} |
326 |
/**
|
327 |
* Return the font color of text to overwrite the images.
|
328 |
*
|
329 |
* @return Color[]
|
330 |
*/
|
331 |
public Color[] getFontColors() { |
332 |
Color[] colors=new Color[fontColors.size()]; |
333 |
for (int i=0;i<colors.length;i++) { |
334 |
try {
|
335 |
String s=fontColors.get(i);
|
336 |
String[] rgb=s.split(","); |
337 |
colors[i]=new Color(Integer.valueOf(rgb[0]),Integer.valueOf(rgb[1]),Integer.valueOf(rgb[2])); |
338 |
}catch (Exception e) { |
339 |
NotificationManager.addInfo(PluginServices.getText(this,"incorrect_color"),e); |
340 |
} |
341 |
} |
342 |
return colors;
|
343 |
} |
344 |
} |