1 package snow.utils.storage; 2 3 import java.util.*; 4 import java.io.*; 5 import java.awt.Component ; 6 import java.awt.Font ; 7 import java.awt.Color ; 8 import java.awt.Toolkit ; 9 10 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 e) 30 { 31 e.printStackTrace(); 32 } 33 finally 34 { 35 if(fis!=null) try{ fis.close(); } catch(Exception 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 e) 48 { 49 e.printStackTrace(); 50 } 51 finally 52 { 53 if(fos!=null) try{ fos.close(); } catch(Exception ex) {} 54 } 55 } 56 57 public void setArrayProperty(String key, String [] 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 [] getArrayProperty(String key, String [] defaults) 65 { 66 String found = getProperty(key+"length", "no"); 67 if(found.equals("no")) return defaults; 68 int n = Integer.parseInt(found); 69 String [] props = new String [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 key, int val) 78 { 79 this.put(key, ""+val); 80 } 81 82 public void setLong(String key, long val) 83 { 84 this.put(key, ""+val); 85 } 86 87 public void setDouble(String key, double val) 88 { 89 this.put(key, ""+val); 90 } 91 92 public Font getFont(String key, Font def) 93 { 94 String 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 (name, style, size); 101 } 102 103 public Color getColor(String key, Color 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 (r,g,b,a); 111 } 112 113 public void setColor(String key, Color 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 key, Font 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 150 public void setStringLCK(String key, String val) 151 { 152 this.put(key.toLowerCase(), val); 153 } 154 155 public int getInteger(String key, int def) 156 { 157 String 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 e) 163 { 164 return def; 165 } 166 } 167 168 public long getLong(String key, long def) 169 { 170 String 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 e) 176 { 177 return def; 178 } 179 } 180 181 public double getDouble(String key, double def) 182 { 183 String 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 e) 189 { 190 return def; 191 } 192 } 193 194 public void setBoolean(String key, boolean val) 195 { 196 this.put(key, (val?"true":"false")); 197 } 198 199 public boolean getBoolean(String key, boolean def) 200 { 201 String found = getProperty(key, "no"); 202 if(found.equals("no")) return def; 203 return (found.equals("true")); 204 } 205 206 208 public String getStringLCK(String key, String def) 209 { 210 String found = getProperty(key.toLowerCase(), "no"); 211 if(found.equals("no")) return def; 212 return found; 213 } 214 215 216 218 public synchronized void setComponentSizeFromINIFile( Component comp, String 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 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 237 public synchronized void saveComponentSizeInINIFile(Component comp, String 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 247 public synchronized void setComponentLocationFromINIFile( Component comp, String 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 267 public synchronized void saveComponentLocationInINIFile(Component comp, String 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 > getVectorRepresentation() 274 { 275 Vector<Object > v = new Vector<Object >(); 276 v.add(1); v.add(this.size()); 278 Enumeration enum2 = this.keys(); 279 while(enum2.hasMoreElements()) 280 { 281 Object obj = enum2.nextElement(); 282 v.addElement(""+ obj); 283 v.addElement(getProperty((String ) obj)); 284 } 285 return v; 286 } 287 288 public void createFromVectorRepresentation(Vector<Object > v) 289 { 290 int ver = (Integer ) v.elementAt(0); 291 if(ver!=1) throw new RuntimeException ("version ="+ver+" not supported"); 292 int size = (Integer ) v.elementAt(1); 293 for(int i=0; i<size; i++) 294 { 295 this.setProperty((String ) v.elementAt(2*i+2), (String ) v.elementAt(2*i+3)); 296 } 297 } 298 299 300 } | Popular Tags |