KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > UserDataStorage


1 /***
2  * FractalGUI: a graphical tool to edit Fractal component configurations.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  *
21  * Authors: Eric Bruneton, Patrice Fauvel
22  */

23
24 package org.objectweb.fractal.gui;
25
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.io.InputStreamReader JavaDoc;
30 import java.io.BufferedReader JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.Hashtable JavaDoc;
34
35 public class UserDataStorage implements UserData
36 {
37   static String JavaDoc FS = new String JavaDoc (System.getProperty("file.separator"));
38   static String JavaDoc UH = new String JavaDoc (System.getProperty("user.home"));
39   static String JavaDoc UN = new String JavaDoc (System.getProperty("user.name"));
40   String JavaDoc filename = UH+FS+UN+".frtlgui";
41
42   int currentdepth = 0;
43   int currentwidth = 780;
44   int currentheight = 660;
45   int currentstatus = UserData.NO_MODIFIED;
46   private Integer JavaDoc ZERO = new Integer JavaDoc (0);
47   private int current_id = 0;
48   UFProject ufp = null;
49   Hashtable JavaDoc hfp = new Hashtable JavaDoc ();
50
51   public UserDataStorage () {
52     open();
53   }
54
55   /**
56    * Open the file which contains all registered User Data. If this file doesn't
57    * exist, a new empty file is created.
58    *
59    */

60   public void open () {
61     try {
62       FileInputStream JavaDoc fin = new FileInputStream JavaDoc (filename);
63       BufferedReader JavaDoc brd = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(fin));
64       String JavaDoc st = new String JavaDoc ("");
65       UFProject cufp = new UFProject ();
66
67       while (st != null) {
68         st = brd.readLine();
69         if (st == null) break;
70         int i = st.indexOf('=');
71         if (st.length() == i+1) continue;
72         Integer JavaDoc val = new Integer JavaDoc (st.substring(0, i));
73
74         int vali = val.intValue() % 100;
75         int valp = val.intValue() / 100;
76
77         if (current_id < valp) current_id = valp+1;
78         ufp = (UFProject)getProject(valp);
79         if (ufp == null) {
80           ufp = new UFProject ();
81           ufp.setId(valp);
82         }
83
84         switch (vali) {
85           case (UserData.CURRENT_DEPTH) :
86             currentdepth = intValue (st); break;
87           case (UserData.CURRENT_WIDTH) :
88             currentwidth = intValue (st); break;
89           case (UserData.CURRENT_HEIGHT) :
90             currentheight = intValue (st); break;
91           case (UserData.CURRENT_CONFIG) :
92             currentstatus = intValue (st); break;
93           default :
94             ufp.setStringData(vali, st.substring(i+1));
95             cufp.setStringData(vali, st.substring(i+1));
96           break;
97         }
98         addProject(ufp);
99       }
100       brd.close();
101       if (hfp.size() == 0) { }
102     }
103     catch (Exception JavaDoc ex) { }
104   }
105
106   // -----
107

108   private int intValue (String JavaDoc st)
109   {
110     int i = st.indexOf('=');
111     Integer JavaDoc val = new Integer JavaDoc (st.substring(i+1));
112     return val.intValue();
113   }
114
115  /**
116   * Clean the user's data
117   */

118   public void clean () {
119     hfp.clear();
120     save ();
121   }
122
123  /**
124   * Save all registered data in the file of the user. If this file doesn't exist, a new empty file
125   * is created.
126   *
127   */

128   public void save () {
129     try {
130       FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc (filename);
131       PrintWriter JavaDoc os = new PrintWriter JavaDoc (new OutputStreamWriter JavaDoc (fos));
132
133       os.println (UserData.CURRENT_DEPTH+"="+currentdepth);
134       os.println (UserData.CURRENT_WIDTH+"="+currentwidth);
135       os.println (UserData.CURRENT_HEIGHT+"="+currentheight);
136
137       for (Enumeration JavaDoc en = hfp.keys(); en.hasMoreElements() ;) {
138         Integer JavaDoc key = (Integer JavaDoc)en.nextElement();
139               UFProject fp = (UFProject)hfp.get(key);
140         int ind = fp.getId();
141         for (int i = 0; i < UserData.NB_DIR; i++) {
142           String JavaDoc field = fp.getStringData(i);
143           if (field != null) {
144             os.println (((100*ind)+i+UserData.START_INDEX)+"="+field);
145           } else { os.println (((100*ind)+i+UserData.START_INDEX)+"="); }
146         }
147       }
148       os.close();
149     }
150     catch (Exception JavaDoc ex) { }
151   }
152
153  /**
154   * set the value for a given type. The type must belong to the CURRENT_
155   * family type or else an Exceptioon is thrown.
156   *
157   * @param typ is the Data type.
158   * @param v is the int value for the type.
159   */

160   public void setIntData (int typ, int v) throws Exception JavaDoc {
161     if (typ == UserData.CURRENT_DEPTH) currentdepth = v;
162     else if (typ == UserData.CURRENT_WIDTH) currentwidth = v;
163     else if (typ == UserData.CURRENT_HEIGHT) currentheight = v;
164     else throw new IllegalArgumentException JavaDoc();
165   }
166
167  /**
168   * get the int value for a given type. The type must belong to the CURRENT_
169   * family type or else an Exception is thrown.
170   *
171   * @param typ is the Data type.
172   */

173   public int getIntData (int typ) throws Exception JavaDoc {
174     if (typ == UserData.CURRENT_DEPTH) return currentdepth;
175     else if (typ == UserData.CURRENT_WIDTH) return currentwidth;
176     else if (typ == UserData.CURRENT_HEIGHT) return currentheight;
177     else throw new IllegalArgumentException JavaDoc();
178   }
179
180  /**
181   * set the value for a given type. The type must belong to the LAST_
182   * family type.
183   *
184   * @param typ is the Data type.
185   * @param s is the String value for the type.
186   */

187   public void setStringData (int typ, String JavaDoc s) throws Exception JavaDoc {
188     UFProject ufp = (UFProject)hfp.get(ZERO);
189     if (ufp == null) {
190       ufp = new UFProject ();
191       ufp.setId(0);
192       for (int i = 0; i < UserData.NB_DIR; i++)
193         ufp.setStringData(i+UserData.START_INDEX, "");
194       hfp.put(ZERO, ufp);
195       save ();
196     }
197     if ((typ < UserData.START_INDEX) ||
198         (typ > UserData.NB_DIR+UserData.START_INDEX-1)) throw
199       new IllegalArgumentException JavaDoc();
200     ufp.setStringData(typ, s);
201   }
202
203  /**
204   * get the int value for a given type. The type must belong to the LAST_
205   * family type or else an Exception is thrown.
206   *
207   * @param typ is the Data type.
208   */

209   public String JavaDoc getStringData (int typ) throws Exception JavaDoc {
210     UFProject ufp = (UFProject)hfp.get(ZERO);
211     if (ufp == null) return null;
212     if ((typ < UserData.START_INDEX) ||
213         (typ > UserData.NB_DIR+UserData.START_INDEX-1)) throw
214       new IllegalArgumentException JavaDoc();
215     return ufp.getStringData(typ-UserData.START_INDEX);
216   }
217
218  /**
219   * Adds a new FProject to the project list. If the project already exists,
220   * nothing is done.
221   *
222   * @param id is the Data type.
223   * @return <b>true</b> if the FProject exist, <b>false</b> otherwise.
224   */

225   public boolean projectExists (int id) {
226     Object JavaDoc fp = hfp.get(new Integer JavaDoc(id));
227     if (fp == null) return false;
228     else return true;
229   }
230
231  /**
232   * Adds a new FProject to the project list. If the project already exists,
233   * nothing is done.
234   * If thomething goes wrong an Exception is thrown.
235   *
236   * @param proj is the FProject to add.
237   */

238   public void addProject (UserData.FProject proj) throws Exception JavaDoc {
239     int i = proj.getId();
240     if (i < 0) throw new IllegalArgumentException JavaDoc();
241     Integer JavaDoc in = new Integer JavaDoc(i);
242     if (hfp.get(in) != null) {
243       in = new Integer JavaDoc (current_id);
244       proj.setId(current_id);
245     }
246     hfp.put(in, proj);
247     return;
248   }
249
250  /**
251   * Removes the FProject with specified <b>id</b> from the project list.
252   * If the project doesn't exist, nothing is done.
253   *
254   * @param id is the FProject id to remove.
255   */

256   public void removeProject (int id) {
257   }
258
259   /**
260    * Adds a new FProject to the project list. If the project already exists,
261    * nothing is done.
262    * If thomething goes wrong an Exception is thrown.
263    *
264    * @param id is the FProject identifier.
265    * @return the FProject if exists or null otherwise.
266    */

267   public UserData.FProject getProject (int id) {
268     UserData.FProject fp = (UserData.FProject)hfp.get(new Integer JavaDoc(id));
269     return fp;
270   }
271
272
273   /**
274    * Asks for a new FProject.
275    *
276    * @return the new FProject
277    */

278   public UserData.FProject getNewProject () {
279     UFProject fp = new UFProject ();
280     fp.setId(current_id++);
281     return fp;
282   }
283
284
285   /* ---------------------------------------------------------- */
286
287   public class UFProject implements UserData.FProject {
288     String JavaDoc [] fields = new String JavaDoc[UserData.NB_DIR];
289     private int ident = -1;
290
291     /**
292      * set the id for the project
293      *
294      * @param i is the Data type.
295      */

296     public void setId (int i) { ident = i; }
297
298     /**
299      * get the id of the project
300      *
301      * @return the identifier of the FProject.
302      */

303     public int getId () { return ident; }
304
305     /**
306      * set the value for a given type. The type must belong to the LAST_ family
307      * type.
308      *
309      * @param typ is the Data type.
310      * @param s is the String value for the type.
311      */

312     public void setStringData (int typ, String JavaDoc s) {
313     if ((typ < UserData.START_INDEX) ||
314         (typ > UserData.NB_DIR+UserData.START_INDEX-1)) return;
315       fields[typ-UserData.START_INDEX] = new String JavaDoc (s);
316     }
317
318     /**
319      * get the int value for a given type. The type must belong to the LAST_
320      * family type or else an Exception is thrown.
321      *
322      * @param typ is the Data type.
323      */

324     public String JavaDoc getStringData (int typ) {
325       if ((typ < 0) || (typ > UserData.NB_DIR-1)) return null;
326       return fields[typ];
327     }
328   }
329 }
330
Popular Tags