KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > storage > AppProperties


1 package snow.utils.storage;
2
3 import java.util.*;
4 import java.io.*;
5 import java.awt.Component JavaDoc;
6 import java.awt.Font JavaDoc;
7 import java.awt.Color JavaDoc;
8 import java.awt.Toolkit JavaDoc;
9
10 /** useful for some general purpose properties as components sizes and locations.
11 */

12 public final class AppProperties extends Properties {
13
14     public AppProperties()
15     {
16         super();
17     }
18
19     public void load_XML(File f)
20     {
21        if(!f.exists()) return;
22
23        FileInputStream fis = null;
24        try
25        {
26          fis = new FileInputStream(f);
27          this.loadFromXML( fis );
28        }
29        catch(Exception JavaDoc e)
30        {
31          e.printStackTrace();
32        }
33        finally
34        {
35          if(fis!=null) try{ fis.close(); } catch(Exception JavaDoc ex) {}
36        }
37     }
38
39     public void save_XML(File f)
40     {
41        FileOutputStream fos = null;
42        try
43        {
44          fos = new FileOutputStream(f);
45          this.storeToXML(fos, "AppProperties" );
46        }
47        catch(Exception JavaDoc e)
48        {
49          e.printStackTrace();
50        }
51        finally
52        {
53          if(fos!=null) try{ fos.close(); } catch(Exception JavaDoc ex) {}
54        }
55     }
56
57     public void setArrayProperty(String JavaDoc key, String JavaDoc[] values)
58     {
59         setProperty(key+"length", ""+values.length);
60         for(int i=0; i<values.length; i++)
61         setProperty( key+(i), values[i]);
62     }
63
64     public String JavaDoc[] getArrayProperty(String JavaDoc key, String JavaDoc[] defaults)
65     {
66         String JavaDoc found = getProperty(key+"length", "no");
67         if(found.equals("no")) return defaults;
68         int n = Integer.parseInt(found);
69         String JavaDoc[] props = new String JavaDoc[n];
70         for(int i=0; i<n; i++)
71         {
72             props[i] = getProperty( key+(i), "oh la la la la");
73         }
74         return props;
75     }
76
77     public void setInteger(String JavaDoc key, int val)
78     {
79         this.put(key, ""+val);
80     }
81
82     public void setLong(String JavaDoc key, long val)
83     {
84         this.put(key, ""+val);
85     }
86
87     public void setDouble(String JavaDoc key, double val)
88     {
89         this.put(key, ""+val);
90     }
91
92     public Font JavaDoc getFont(String JavaDoc key, Font JavaDoc def)
93     {
94        String JavaDoc name = this.getProperty(key+"_name", "?");
95        if(name.equals("?")) return def;
96
97        int style = this.getInteger(key+"_style", Font.PLAIN);
98        int size = this.getInteger(key+"_size", 12);
99
100        return new Font JavaDoc(name, style, size);
101     }
102
103     public Color JavaDoc getColor(String JavaDoc key, Color JavaDoc def)
104     {
105        int r = this.getInteger(key+"_r", -1);
106        if(r==-1) return def;
107        int g = this.getInteger(key+"_g", 0);
108        int b = this.getInteger(key+"_b", 0);
109        int a = this.getInteger(key+"_a", 0);
110        return new Color JavaDoc(r,g,b,a);
111     }
112
113     public void setColor(String JavaDoc key, Color JavaDoc val)
114     {
115        if(val==null)
116        {
117         this.remove(key+"_r");
118         this.remove(key+"_g");
119         this.remove(key+"_b");
120         this.remove(key+"_a");
121
122        }
123        else
124        {
125         this.setInteger(key+"_r", val.getRed());
126         this.setInteger(key+"_g", val.getGreen());
127         this.setInteger(key+"_b", val.getBlue());
128         this.setInteger(key+"_a", val.getAlpha());
129        }
130     }
131
132     public void setFont(String JavaDoc key, Font JavaDoc font)
133     {
134        if(font==null)
135        {
136         this.remove(key+"_name");
137         this.remove(key+"_style");
138         this.remove(key+"_size");
139        }
140        else
141        {
142         this.put(key+"_name", font.getName());
143         this.setInteger(key+"_style", font.getStyle());
144         this.setInteger(key+"_size", font.getSize());
145        }
146     }
147
148     /** this set the key
149     */

150     public void setStringLCK(String JavaDoc key, String JavaDoc val)
151     {
152         this.put(key.toLowerCase(), val);
153     }
154
155     public int getInteger(String JavaDoc key, int def)
156     {
157         String JavaDoc found = getProperty(key, "no");
158         if(found.equals("no")) return def;
159         try{
160             int n = Integer.parseInt(found);
161             return n;
162         } catch(Exception JavaDoc e)
163         {
164           return def;
165         }
166     }
167
168     public long getLong(String JavaDoc key, long def)
169     {
170         String JavaDoc found = getProperty(key, "no");
171         if(found.equals("no")) return def;
172         try{
173             long n = Long.parseLong(found);
174             return n;
175         } catch(Exception JavaDoc e)
176         {
177           return def;
178         }
179     }
180
181     public double getDouble(String JavaDoc key, double def)
182     {
183         String JavaDoc found = getProperty(key, "no");
184         if(found.equals("no")) return def;
185         try{
186             double n = Double.parseDouble(found);
187             return n;
188         } catch(Exception JavaDoc e)
189         {
190           return def;
191         }
192     }
193
194     public void setBoolean(String JavaDoc key, boolean val)
195     {
196         this.put(key, (val?"true":"false"));
197     }
198
199     public boolean getBoolean(String JavaDoc key, boolean def)
200     {
201         String JavaDoc found = getProperty(key, "no");
202         if(found.equals("no")) return def;
203         return (found.equals("true"));
204     }
205
206     /** lowercase key
207     */

208     public String JavaDoc getStringLCK(String JavaDoc key, String JavaDoc def)
209     {
210         String JavaDoc found = getProperty(key.toLowerCase(), "no");
211         if(found.equals("no")) return def;
212         return found;
213     }
214
215
216    /** set the component size and location
217     */

218     public synchronized void setComponentSizeFromINIFile( Component JavaDoc comp, String JavaDoc key,
219          int defWidth, int defHeight, int defPosX, int defPosY)
220     {
221         int w = getInteger(key+".width", defWidth);
222         int h = getInteger(key+".height", defHeight);
223
224         // ###
225
if(w<=defWidth/10 || h<=defHeight/10)
226         {
227           w=defWidth;
228           h=defHeight;
229         }
230         comp.setSize(w, h);
231
232         setComponentLocationFromINIFile(comp, key, defPosX, defPosY);
233     }
234
235   /** save the component size and location
236   */

237     public synchronized void saveComponentSizeInINIFile(Component JavaDoc comp, String JavaDoc key)
238     {
239         this.setInteger(key+".width", (int) comp.getSize().getWidth());
240         this.setInteger(key+".height", (int) comp.getSize().getHeight());
241         this.setInteger(key+".posx", (int) comp.getLocation().getX());
242         this.setInteger(key+".posy", (int) comp.getLocation().getY());
243     }
244
245    /** set the component location
246     */

247     public synchronized void setComponentLocationFromINIFile( Component JavaDoc comp, String JavaDoc key,
248          int defPosX, int defPosY)
249     {
250         int x = getInteger(key+".posx", defPosX);
251         int y = getInteger(key+".posy", defPosY);
252
253         if(x<0) x=0;
254         if(y<0) y=0;
255
256         int screenW = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
257         int screenH = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
258
259         if(x > screenW-20) x = screenW - 20;
260         if(y > screenH-20) y = screenH - 20;
261
262         comp.setLocation(x, y);
263     }
264
265   /** save the component location
266   */

267     public synchronized void saveComponentLocationInINIFile(Component JavaDoc comp, String JavaDoc key)
268     {
269         this.setInteger(key+".posx", (int) comp.getLocation().getX());
270         this.setInteger(key+".posy", (int) comp.getLocation().getY());
271     }
272
273     public Vector<Object JavaDoc> getVectorRepresentation()
274     {
275         Vector<Object JavaDoc> v = new Vector<Object JavaDoc>();
276         v.add(1); // version
277
v.add(this.size());
278         Enumeration enum2 = this.keys();
279         while(enum2.hasMoreElements())
280         {
281             Object JavaDoc obj = enum2.nextElement();
282             v.addElement(""+ obj);
283             v.addElement(getProperty((String JavaDoc) obj));
284         }
285         return v;
286     }
287
288     public void createFromVectorRepresentation(Vector<Object JavaDoc> v)
289     {
290         int ver = (Integer JavaDoc) v.elementAt(0);
291         if(ver!=1) throw new RuntimeException JavaDoc("version ="+ver+" not supported");
292         int size = (Integer JavaDoc) v.elementAt(1);
293         for(int i=0; i<size; i++)
294         {
295             this.setProperty((String JavaDoc) v.elementAt(2*i+2), (String JavaDoc) v.elementAt(2*i+3));
296         }
297     }
298
299
300 }
Popular Tags