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 ; 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 27 protected Set changes; protected Properties backup; 31 32 protected static File baseDirectory = new File(System.getProperty("user.home"), ".jIRC"); 33 34 protected Properties state; 36 public static void setBaseDirectory(String directory) 37 { 38 baseDirectory = new File(directory); 39 } 40 41 public void fireChange(String property) 42 { 43 fireChange(property, null); 44 } 45 46 public void addClientStateListener(String 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 property, String parameter) 59 { 60 if (listeners.get(property) == null) 61 { 62 return; } 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 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 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 ex) { } 154 155 changes = new HashSet(); 156 } 157 158 159 public void backup() 160 { 161 backup = (Properties)(state.clone()); 162 changes.clear(); 163 } 164 165 166 public void restore() 167 { 168 state = backup; 169 sync(); 170 171 Iterator i = changes.iterator(); 172 173 while (i.hasNext()) 174 { 175 String temp = (String )i.next(); 176 fireChange(temp); 177 } 178 } 179 180 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 ex) { ex.printStackTrace(); } 191 } 192 193 public Properties getProperties() 194 { 195 return state; 196 } 197 198 public void setString(String key, String value) 199 { 200 state.setProperty(key, value); 201 fireChange(key); 202 } 203 204 public String getString(String key, String defaultValue) 205 { 206 String 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 key, Dimension areaSize, Dimension mySize) 217 { 218 String 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 [] 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 key, float defaultValue) 241 { 242 String 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 key, float value) 253 { 254 setString(key, value+""); 255 } 256 257 public int getInteger(String key, int defaultValue) 258 { 259 String 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 key, int value) 270 { 271 setString(key, value+""); 272 } 273 274 public void setOption(String 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 key, boolean defaultBoolean) 288 { 289 String 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 key, Color defaultColor) 305 { 306 String 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 key, Color color) 317 { 318 322 323 setString(key, color.getRGB()+""); 324 } 325 326 public StringList getStringList(String key) 327 { 328 return new StringList(key); 329 } 330 331 public boolean isValue(String key, String item) 332 { 333 return getStringList(key).isValue(item); 334 } 335 336 public static File getFile(String filename) 337 { 338 return new File(getBaseDirectory(), filename); 339 } 340 341 public URL getResource(String fileName) 342 { 343 return getPackagedResource(fileName, "resource"); 344 } 346 347 public String getHelpString(String topic) 348 { 349 topic = topic.replaceAll("\\'", "").replaceAll("\\?", "").replaceAll(" ", "_"); 350 351 try 352 { 353 URL url = getPackagedResource(topic, "help"); 354 355 if (url == null) { return null; } 356 357 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 358 359 StringBuffer temp = new StringBuffer (); 360 361 String 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 ex) 371 { 372 ex.printStackTrace(); 373 } 374 375 return null; 376 } 377 378 public URL getPackagedResource(String fileName, String 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 ex) 389 { 390 ex.printStackTrace(); 391 } 392 393 return this.getClass().getResource("/"+subDir+"/"+fileName); 394 } 395 396 public InputStream getResourceAsStream(String 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 ex) 413 { 414 ex.printStackTrace(); 415 } 416 417 URL temp = getResource(fileName); 418 if (temp == null) 419 { 420 return null; 421 } 422 423 try 424 { 425 return temp.openStream(); 426 } 427 catch (Exception ex) { ex.printStackTrace(); } 428 429 return null; 430 } 431 432 public Font getFont(String key, Font defaultValue) 433 { 434 String 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 key, Font value) 445 { 446 setString(key, rero.util.ClientUtils.encodeFont(value)); 447 } 448 449 public ImageIcon getIcon(String key, String defaultResource) 450 { 451 String 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 key, Rectangle value) 464 { 465 StringBuffer bounds = new StringBuffer (); 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 |