1 23 24 package org.objectweb.fractal.gui; 25 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.OutputStreamWriter ; 29 import java.io.InputStreamReader ; 30 import java.io.BufferedReader ; 31 import java.io.PrintWriter ; 32 import java.util.Enumeration ; 33 import java.util.Hashtable ; 34 35 public class UserDataStorage implements UserData 36 { 37 static String FS = new String (System.getProperty("file.separator")); 38 static String UH = new String (System.getProperty("user.home")); 39 static String UN = new String (System.getProperty("user.name")); 40 String 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 ZERO = new Integer (0); 47 private int current_id = 0; 48 UFProject ufp = null; 49 Hashtable hfp = new Hashtable (); 50 51 public UserDataStorage () { 52 open(); 53 } 54 55 60 public void open () { 61 try { 62 FileInputStream fin = new FileInputStream (filename); 63 BufferedReader brd = new BufferedReader (new InputStreamReader (fin)); 64 String st = new String (""); 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 val = new Integer (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 ex) { } 104 } 105 106 108 private int intValue (String st) 109 { 110 int i = st.indexOf('='); 111 Integer val = new Integer (st.substring(i+1)); 112 return val.intValue(); 113 } 114 115 118 public void clean () { 119 hfp.clear(); 120 save (); 121 } 122 123 128 public void save () { 129 try { 130 FileOutputStream fos = new FileOutputStream (filename); 131 PrintWriter os = new PrintWriter (new OutputStreamWriter (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 en = hfp.keys(); en.hasMoreElements() ;) { 138 Integer key = (Integer )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 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 ex) { } 151 } 152 153 160 public void setIntData (int typ, int v) throws Exception { 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 (); 165 } 166 167 173 public int getIntData (int typ) throws Exception { 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 (); 178 } 179 180 187 public void setStringData (int typ, String s) throws Exception { 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 (); 200 ufp.setStringData(typ, s); 201 } 202 203 209 public String getStringData (int typ) throws Exception { 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 (); 215 return ufp.getStringData(typ-UserData.START_INDEX); 216 } 217 218 225 public boolean projectExists (int id) { 226 Object fp = hfp.get(new Integer (id)); 227 if (fp == null) return false; 228 else return true; 229 } 230 231 238 public void addProject (UserData.FProject proj) throws Exception { 239 int i = proj.getId(); 240 if (i < 0) throw new IllegalArgumentException (); 241 Integer in = new Integer (i); 242 if (hfp.get(in) != null) { 243 in = new Integer (current_id); 244 proj.setId(current_id); 245 } 246 hfp.put(in, proj); 247 return; 248 } 249 250 256 public void removeProject (int id) { 257 } 258 259 267 public UserData.FProject getProject (int id) { 268 UserData.FProject fp = (UserData.FProject)hfp.get(new Integer (id)); 269 return fp; 270 } 271 272 273 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 [] fields = new String [UserData.NB_DIR]; 289 private int ident = -1; 290 291 296 public void setId (int i) { ident = i; } 297 298 303 public int getId () { return ident; } 304 305 312 public void setStringData (int typ, String 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 (s); 316 } 317 318 324 public String getStringData (int typ) { 325 if ((typ < 0) || (typ > UserData.NB_DIR-1)) return null; 326 return fields[typ]; 327 } 328 } 329 } 330 | Popular Tags |