KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > config > ClientState


1 package rero.config;
2
3 import java.awt.image.*;
4 import javax.swing.*;
5 import java.awt.*;
6
7 import java.io.*;
8 import java.net.URL JavaDoc;
9
10 import java.util.*;
11
12 import java.lang.ref.*;
13
14 public class ClientState
15 {
16    protected static HashMap listeners = new HashMap();
17    // ^-- container for the listeners for property changes. This way
18
// components won't be checking themselves all the time, they
19
// can be notified when something happens.
20
// listeners{"property.name"} = LinkedList ( WeakReference(listener1),
21
// WeakReference(null),
22
// WeakReference(listener2))
23
// The weak references are for a server connection that gets closed,
24
// this way those can be garbage collected. Trying to make jIRC more
25
// memory friendly.
26

27    // related to backup and restore procedures
28
protected Set changes; // keeps track of changes made to this version of the "state"
29
protected Properties backup; // backup of the client state.
30

31
32    protected static File baseDirectory = new File(System.getProperty("user.home"), ".jIRC");
33
34    protected Properties state; // all of the properties we're going to load from the jerk.cfg file.
35

36    public static void setBaseDirectory(String JavaDoc directory)
37    {
38       baseDirectory = new File(directory);
39    }
40
41    public void fireChange(String JavaDoc property)
42    {
43       fireChange(property, null);
44    }
45
46    public void addClientStateListener(String JavaDoc property, ClientStateListener l)
47    {
48       LinkedList temp = (LinkedList)listeners.get(property);
49       if (temp == null)
50       {
51          temp = new LinkedList();
52          listeners.put(property, temp);
53       }
54
55       temp.add(new WeakReference(l));
56    }
57
58    public void fireChange(String JavaDoc property, String JavaDoc parameter)
59    {
60       if (listeners.get(property) == null)
61       {
62          return; // ain't no thang sh'bang.
63
}
64
65       Iterator i = ((LinkedList)listeners.get(property)).iterator();
66       while (i.hasNext())
67       {
68          WeakReference temp = (WeakReference)i.next();
69          if (temp.get() == null)
70          {
71              i.remove();
72          }
73          else
74          {
75              ClientStateListener l = (ClientStateListener)temp.get();
76              l.propertyChanged(property, parameter);
77          }
78       }
79    }
80
81    public static InputStreamReader getProperInputStream(InputStream stream)
82    {
83       if (ClientState.getClientState().getString("client.encoding", rero.dck.items.CharsetInput.DEFAULT_CHARSET).equals(rero.dck.items.CharsetInput.DEFAULT_CHARSET))
84       {
85          return new InputStreamReader(stream);
86       }
87       else
88       {
89          try
90          {
91             return new InputStreamReader(stream, ClientState.getClientState().getString("client.encoding", rero.dck.items.CharsetInput.DEFAULT_CHARSET));
92          }
93          catch (Exception JavaDoc ex)
94          {
95             ex.printStackTrace();
96             return new InputStreamReader(stream);
97          }
98       }
99    }
100
101    public static PrintStream getProperPrintStream(OutputStream stream)
102    {
103       if (ClientState.getClientState().getString("client.encoding", rero.dck.items.CharsetInput.DEFAULT_CHARSET).equals(rero.dck.items.CharsetInput.DEFAULT_CHARSET))
104       {
105          return new PrintStream(stream, true);
106       }
107       else
108       {
109          try
110          {
111             return new PrintStream(stream, true, ClientState.getClientState().getString("client.encoding", rero.dck.items.CharsetInput.DEFAULT_CHARSET));
112          }
113          catch (Exception JavaDoc ex)
114          {
115             ex.printStackTrace();
116             return new PrintStream(stream, true);
117          }
118       }
119    }
120
121    public static File getBaseDirectory()
122    {
123       if (!baseDirectory.exists() || !baseDirectory.isDirectory())
124       {
125          baseDirectory.delete();
126          baseDirectory.mkdirs();
127       }
128
129       return baseDirectory;
130    }
131
132    protected static ClientState clientState = null;
133
134    public static ClientState getClientState()
135    {
136       if (clientState == null)
137       {
138          clientState = new ClientState();
139       }
140
141       return clientState;
142    }
143
144    public ClientState()
145    {
146       state = new Properties();
147       try
148       {
149           FileInputStream istream = new FileInputStream(new File(getBaseDirectory(), "jirc.prop"));
150           state.load(istream);
151           istream.close();
152       }
153       catch (Exception JavaDoc ex) { }
154
155       changes = new HashSet();
156    }
157
158    /** performs a backup of the properties before making changes, it is the responsability of the changing class to call this function */
159    public void backup()
160    {
161       backup = (Properties)(state.clone());
162       changes.clear();
163    }
164
165    /** performs a restore of the properties effectively undoing everything since the last backup, it is the responsability of the changing class to call this function */
166    public void restore()
167    {
168       state = backup;
169       sync();
170
171       Iterator i = changes.iterator();
172
173       while (i.hasNext())
174       {
175          String JavaDoc temp = (String JavaDoc)i.next();
176          fireChange(temp);
177       }
178    }
179
180    /** sync the file system config file with the current client state */
181    public void sync()
182    {
183       try
184       {
185
186          FileOutputStream ostream = new FileOutputStream(new File(getBaseDirectory(), "jirc.prop"));
187          state.save(ostream, "Java IRC Configuration");
188          ostream.close();
189       }
190       catch (Exception JavaDoc ex) { ex.printStackTrace(); }
191    }
192
193    public Properties getProperties()
194    {
195       return state;
196    }
197
198    public void setString(String JavaDoc key, String JavaDoc value)
199    {
200       state.setProperty(key, value);
201       fireChange(key);
202    }
203
204    public String JavaDoc getString(String JavaDoc key, String JavaDoc defaultValue)
205    {
206       String JavaDoc temp = state.getProperty(key);
207
208       if (temp == null || temp.length() == 0)
209       {
210          return defaultValue;
211       }
212
213       return temp;
214    }
215
216    public Rectangle getBounds(String JavaDoc key, Dimension areaSize, Dimension mySize)
217    {
218       String JavaDoc temp = state.getProperty(key);
219
220       if (temp == null)
221       {
222          int x = (int)(areaSize.getWidth() - mySize.getWidth()) / 2;
223          int y = (int)(areaSize.getHeight() - mySize.getHeight()) / 2;
224
225          if (x <= 0 || y <= 0)
226          {
227             x = 0;
228             y = 0;
229          }
230
231          return new Rectangle(x, y, (int)mySize.getWidth(), (int)mySize.getHeight());
232       }
233
234       String JavaDoc[] values = temp.split("x");
235
236       Rectangle tempr = new Rectangle(Integer.parseInt(values[0]), Integer.parseInt(values[1]), Integer.parseInt(values[2]), Integer.parseInt(values[3]));
237       return tempr;
238    }
239
240    public float getFloat(String JavaDoc key, float defaultValue)
241    {
242       String JavaDoc temp = state.getProperty(key);
243
244       if (temp == null)
245       {
246          return defaultValue;
247       }
248
249       return Float.parseFloat(temp);
250    }
251
252    public void setFloat(String JavaDoc key, float value)
253    {
254       setString(key, value+"");
255    }
256
257    public int getInteger(String JavaDoc key, int defaultValue)
258    {
259       String JavaDoc temp = state.getProperty(key);
260
261       if (temp == null)
262       {
263          return defaultValue;
264       }
265
266       return Integer.parseInt(temp);
267    }
268
269    public void setInteger(String JavaDoc key, int value)
270    {
271       setString(key, value+"");
272    }
273
274    public void setOption(String JavaDoc key, boolean value)
275    {
276       if (value)
277       {
278          setString(key, "true");
279       }
280       else
281       {
282          setString(key, "false");
283       }
284       fireChange(key);
285    }
286
287    public boolean isOption(String JavaDoc key, boolean defaultBoolean)
288    {
289       String JavaDoc temp = getString(key, null);
290
291       if (temp == null)
292       {
293          return defaultBoolean;
294       }
295
296       if (temp.equals("true"))
297       {
298          return true;
299       }
300
301       return false;
302    }
303
304    public Color getColor(String JavaDoc key, Color defaultColor)
305    {
306       String JavaDoc temp = getString(key, null);
307
308       if (temp == null)
309       {
310          return defaultColor;
311       }
312
313       return Color.decode(temp);
314    }
315
316    public void setColor(String JavaDoc key, Color color)
317    {
318 /* long value = 0;
319       value = (color.getRed() << 16) | value;
320       value = (color.getGreen() << 8) | value;
321       value = (color.getBlue() << 0) | value; */

322
323       setString(key, color.getRGB()+"");
324    }
325
326    public StringList getStringList(String JavaDoc key)
327    {
328       return new StringList(key);
329    }
330
331    public boolean isValue(String JavaDoc key, String JavaDoc item)
332    {
333       return getStringList(key).isValue(item);
334    }
335
336    public static File getFile(String JavaDoc filename)
337    {
338       return new File(getBaseDirectory(), filename);
339    }
340
341    public URL JavaDoc getResource(String JavaDoc fileName)
342    {
343       return getPackagedResource(fileName, "resource");
344 // return this.getClass().getResource("/resource/"+fileName);
345
}
346
347    public String JavaDoc getHelpString(String JavaDoc topic)
348    {
349       topic = topic.replaceAll("\\'", "").replaceAll("\\?", "").replaceAll(" ", "_");
350
351       try
352       {
353          URL JavaDoc url = getPackagedResource(topic, "help");
354
355          if (url == null) { return null; }
356
357          BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
358
359          StringBuffer JavaDoc temp = new StringBuffer JavaDoc();
360
361          String JavaDoc text;
362          while ((text = in.readLine()) != null)
363          {
364             temp.append(text);
365             temp.append("\n");
366          }
367
368          return temp.toString();
369       }
370       catch (Exception JavaDoc ex)
371       {
372          ex.printStackTrace();
373       }
374
375       return null;
376    }
377
378    public URL JavaDoc getPackagedResource(String JavaDoc fileName, String JavaDoc subDir)
379    {
380       try
381       {
382          File check = new File(getBaseDirectory(), fileName);
383          if (check.exists())
384          {
385             return check.toURL();
386          }
387       }
388       catch (Exception JavaDoc ex)
389       {
390          ex.printStackTrace();
391       }
392
393       return this.getClass().getResource("/"+subDir+"/"+fileName);
394    }
395
396    public InputStream getResourceAsStream(String JavaDoc fileName)
397    {
398       try
399       {
400          File realf = new File(fileName);
401          if (realf.exists())
402          {
403             return realf.toURL().openStream();
404          }
405
406          File check = new File(getBaseDirectory(), fileName);
407          if (check.exists())
408          {
409             return check.toURL().openStream();
410          }
411       }
412       catch (Exception JavaDoc ex)
413       {
414          ex.printStackTrace();
415       }
416
417       URL JavaDoc temp = getResource(fileName);
418       if (temp == null)
419       {
420          return null;
421       }
422
423       try
424       {
425          return temp.openStream();
426       }
427       catch (Exception JavaDoc ex) { ex.printStackTrace(); }
428
429       return null;
430    }
431
432    public Font getFont(String JavaDoc key, Font defaultValue)
433    {
434       String JavaDoc fname = getString(key, null);
435
436       if (fname == null)
437       {
438          return defaultValue;
439       }
440
441       return Font.decode(fname);
442    }
443
444    public void setFont(String JavaDoc key, Font value)
445    {
446       setString(key, rero.util.ClientUtils.encodeFont(value));
447    }
448
449    public ImageIcon getIcon(String JavaDoc key, String JavaDoc defaultResource)
450    {
451       String JavaDoc temp = getString(key, null);
452
453       if (temp == null)
454       {
455          return new ImageIcon(getResource(defaultResource));
456       }
457       else
458       {
459          return new ImageIcon(Toolkit.getDefaultToolkit().getImage(temp));
460       }
461    }
462
463    public void setBounds(String JavaDoc key, Rectangle value)
464    {
465       StringBuffer JavaDoc bounds = new StringBuffer JavaDoc();
466       bounds.append((int)value.getX());
467       bounds.append('x');
468       bounds.append((int)value.getY());
469       bounds.append('x');
470       bounds.append((int)value.getWidth());
471       bounds.append('x');
472       bounds.append((int)value.getHeight());
473
474       setString(key, bounds.toString());
475    }
476
477 }
478
479
Popular Tags