1 package rero.util; 2 3 import java.util.*; 10 import java.io.*; 11 import java.net.*; 12 13 import java.awt.*; 14 import javax.swing.*; 15 16 import rero.config.*; 17 18 import text.*; 19 import rero.dialogs.*; 20 21 public class ClientUtils 22 { 23 public static void invokeLater(Runnable doIt) 24 { 25 if (SwingUtilities.isEventDispatchThread()) 26 doIt.run(); 27 else 28 SwingUtilities.invokeLater(doIt); 29 } 30 31 public static JFrame getFrameForComponent(JComponent c) 32 { 33 Component temp = (Component)c.getParent(); 34 while (temp != null) 35 { 36 if (temp instanceof JFrame) 37 { 38 return (JFrame)temp; 39 } 40 41 temp = temp.getParent(); 42 } 43 44 return null; 45 } 46 47 public static String strip(String text) 48 { 49 return AttributedString.CreateAttributedString(text).getText(); 50 } 51 52 public static String ShowVersion() 53 { 54 String tagline = ClientUtils.tagline(); 55 56 return "jIRCii " + ClientState.getClientState().getString("version.string", ClientDefaults.version_string) + " " + System.getProperty("os.name").replaceAll(" ", "") + " : " + ClientState.getClientState().getString("version.addons", ClientUtils.tagline()); 57 } 58 59 public static String tagline() 60 { 61 String taglines[] = { 62 "highly caffeinated", 63 "Fat butane, grubbin' on French fries", 64 "Clean. Christian. Comprehensive.", 65 "If idiots could fly, IRC would be an airport", 66 "I'm a *p0rn* star" 67 }; 68 int r = ctime() % taglines.length; 69 return taglines[r]; 70 } 71 72 public static String formatTime(long seconds) 73 { 74 StringBuffer rv = new StringBuffer (); 75 76 if (seconds < 60) 77 { 78 return seconds + " seconds"; 79 } 80 81 if (seconds < (60 * 60)) 82 { 83 return (seconds / 60) + " minutes"; 84 } 85 86 if (seconds < (60 * 60 * 24)) 87 { 88 return (seconds / (60 * 60)) + " hours"; 89 } 90 91 return (seconds / (60 * 60 * 24)) + " days"; 92 } 93 94 public static String formatTime2(long secs) 95 { 96 StringBuffer rv = new StringBuffer (); 97 98 int seconds = (int)secs, minutes = 0, hours = 0, days = 0; 99 100 days = seconds / (60 * 60 * 24); 101 hours = seconds / (60 * 60) % 24; 102 minutes = (seconds / 60) % 60; 103 seconds = seconds % 60; 104 105 if (days > 0) { 106 rv.append(days); 107 rv.append(":"); 108 } 109 110 if (hours > 0) { 111 if (hours < 10) rv.append("0"); 112 rv.append(hours); 113 114 rv.append(":"); 115 } 116 117 if (minutes > 0) { 118 if (minutes < 10) rv.append("0"); 119 rv.append(minutes); 120 121 rv.append(":"); 122 } 123 124 if (seconds < 10) rv.append("0"); 125 rv.append(seconds); 126 127 return rv.toString(); 128 } 129 130 public static String formatTime3(long secs) { 131 int seconds = (int) secs; 132 int minutes = 0; 133 int hours = 0; 134 int days = 0; 135 136 days = seconds / (60 * 60 * 24); 137 hours = seconds / (60 * 60) % 24; 138 minutes = (seconds / 60) % 60; 139 seconds %= 60; 140 141 StringBuffer buf = new StringBuffer (); 142 if (days > 0) 143 buf.append(days).append(days != 1 ? " days, " : " day, "); 144 if (days > 0 || hours > 0) 145 buf.append(hours).append(hours != 1 ? " hours, " : " hour, "); 146 if (hours > 0 || minutes > 0) 147 buf.append(minutes).append(minutes != 1 ? " minutes, " : " minute, "); 148 149 buf.append(seconds).append(seconds != 1 ? " seconds" : " second"); 150 151 return buf.toString(); 152 } 153 154 public static String formatBytes(long bytes) 155 { 156 if (bytes < 1024) 157 { 158 return bytes + "b"; 159 } 160 161 bytes = bytes / 1024; 162 163 if (bytes < 1024) 164 { 165 return bytes + "kb"; 166 } 167 168 bytes = bytes / 1024; 169 170 if (bytes < 1024) 171 { 172 return bytes + "mb"; 173 } 174 175 bytes = bytes / 1024; 176 177 return bytes + "GB"; 178 } 179 180 public static String encodeFont(Font value) 181 { 182 StringBuffer saved = new StringBuffer (); 183 saved.append(value.getFamily()); 184 saved.append("-"); 185 186 if (value.isBold()) 187 { 188 saved.append("BOLD"); 189 } 190 else if (value.isItalic()) 191 { 192 saved.append("ITALIC"); 193 } 194 else 195 { 196 saved.append("PLAIN"); 197 } 198 199 saved.append("-"); 200 saved.append(value.getSize()); 201 202 return saved.toString(); 203 } 204 205 public static void openURL(String location) 206 { 207 try 208 { 209 Runtime.getRuntime().exec(ClientState.getClientState().getString("ui.openfiles", ClientDefaults.ui_openfiles) + " " + location); 210 } 211 catch(Exception ex) 212 { 213 ex.printStackTrace(); 214 } 215 } 216 217 public static String mask(String address, int type) 218 { 219 if (address.length() == 0 || address.indexOf('!') < 1 || address.indexOf('@') < 1 || address.indexOf('.') < 1) 220 { 221 return "<bad address, use nick!user@host format>"; 222 } 223 224 String n, u, h, d; 225 n = address.substring(0, address.indexOf('!')); 226 u = address.substring(address.indexOf('!')+1, address.indexOf('@')); 227 h = address.substring(address.indexOf('@')+1, address.length()); 228 if (address.lastIndexOf('.', address.lastIndexOf('.') - 1) > -1) 229 { 230 d = "*"+address.substring(address.lastIndexOf('.', address.lastIndexOf('.') - 1), address.length()); 231 } 232 else 233 { 234 d = h; 235 } 236 237 switch (type) 238 { 239 case 0: return "*!"+u+"@"+h; 240 case 1: return "*!*"+u+"@"+h; 241 case 2: return "*!*@"+h; 242 case 3: return "*!*"+u+"@"+d; 243 case 4: return "*!*@"+d; 244 case 5: return n+"!"+u+"@"+h; 245 case 6: return n+"!*"+u+"@"+h; 246 case 7: return n+"!*@"+h; 247 case 8: return n+"!*"+u+"@"+d; 248 case 9: return n+"!*@"+d; 249 } 250 return address; 251 } 252 253 public static String longip(String l) 254 { 255 TokenizedString s = new TokenizedString(l); 258 s.tokenize("."); 259 260 long a, b, c, d, x; 261 262 if (s.getTotalTokens() == 4) 263 { 264 a = Long.parseLong(s.getToken(0)); 265 b = Long.parseLong(s.getToken(1)); 266 c = Long.parseLong(s.getToken(2)); 267 d = Long.parseLong(s.getToken(3)); 268 269 x = (a << 24) + (b << 16) + (c << 8) + d; 270 return x+""; 271 } 272 else 273 { 274 x = Long.parseLong(l); 275 a = (x & 0xff000000) >> 24; 276 b = (x & 0x00ff0000) >> 16; 277 c = (x & 0x0000ff00) >> 8; 278 d = (x & 0x000000ff); 279 280 return a+"."+b+"."+c+"."+d; 281 } 282 } 283 284 public static String TimeStamp() 285 { 286 String rv; 287 288 Date rightNow = new Date(); 289 String am_pm = "am"; 290 int hours = rightNow.getHours(); 291 int minutes = rightNow.getMinutes(); 292 293 if (hours == 0) 294 { 295 hours = 12; 296 am_pm = "am"; 297 } 298 else if (hours > 12) { hours -= 12; am_pm = "pm"; } 299 else if (hours == 12) { am_pm = "pm"; } 300 301 if (minutes <= 9) 302 { 303 rv = hours+":0"+minutes+am_pm; 304 } 305 else 306 { 307 rv = hours+":"+minutes+am_pm; 308 } 309 310 return rv; 311 } 312 313 public static HashMap getEventHashMap(String target, String parms) 314 { 315 HashMap temp = new HashMap(); 316 temp.put("$data", target + " " + parms); 317 temp.put("$parms", parms); 318 319 return temp; 320 } 321 322 public static String TimeDateStamp(long l) 323 { 324 Date temp = new Date(l * 1000); 325 String am_pm = "am"; 326 int hours = temp.getHours(); 327 int minutes = temp.getMinutes(); 328 329 StringBuffer value = new StringBuffer (""); 330 StringBuffer rv = new StringBuffer (""); 331 332 if (hours >= 12) 333 { 334 hours -= 12; 335 am_pm = "pm"; 336 337 if (hours == 0) 338 { 339 hours = 12; 340 } 341 } 342 if (minutes <= 9) 343 { 344 value.append(hours); 345 value.append(":0"); 346 value.append(minutes); 347 value.append(am_pm); 348 } 349 else 350 { 351 value.append(hours); 352 value.append(":"); 353 value.append(minutes); 354 value.append(am_pm); 355 } 356 rv.append(intToDay(temp.getDay())); 358 rv.append(" "); 359 rv.append(intToMonth(temp.getMonth())); 360 rv.append(" "); 361 rv.append(temp.getDate()); 362 rv.append(" "); 363 rv.append(temp.getYear() + 1900); 364 rv.append(" "); 365 rv.append(value.toString()); 366 367 return rv.toString(); 368 } 369 370 public static String intToDay(int d) 371 { 372 d = d % 7; 373 374 switch (d) 375 { 376 case 0: 377 return "Sun"; 378 case 1: 379 return "Mon"; 380 case 2: 381 return "Tues"; 382 case 3: 383 return "Wed"; 384 case 4: 385 return "Thurs"; 386 case 5: 387 return "Fri"; 388 case 6: 389 return "Sat"; 390 } 391 return "Unknown Day: "+d; 392 } 393 394 public static boolean isNumeric(String n) 395 { 396 return (Character.isDigit(n.charAt(0))); 397 } 398 399 public static String intToMonth(int m) 400 { 401 switch (m) 402 { 403 case 0: 404 return "Jan"; 405 case 1: 406 return "Feb"; 407 case 2: 408 return "Mar"; 409 case 3: 410 return "Apr"; 411 case 4: 412 return "May"; 413 case 5: 414 return "Jun"; 415 case 6: 416 return "Jul"; 417 case 7: 418 return "Aug"; 419 case 8: 420 return "Sep"; 421 case 9: 422 return "Oct"; 423 case 10: 424 return "Nov"; 425 case 11: 426 return "Dec"; 427 } 428 return "Unknown Month: "+m; 429 } 430 431 public static boolean isChannel(String target) 432 { 433 return (target.length() > 0 && (target.charAt(0) == '#' || target.charAt(0) == '&')); 434 } 435 436 public static int ctime() 437 { 438 Long temp = new Long (System.currentTimeMillis() / 1000); 439 return temp.intValue(); 440 } 441 442 private static String CP437TABLE = "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\n\u000B\u000C\r\u000E\u000F\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F\u0020\u0021\"\u0023\u0024\u0025\u0026'\u0028\u0029\u002A\u002B\u002C\u002D\u002E\u002F\u0030\u0031\u0032\u0033\u0034\u0035\u0036\u0037\u0038\u0039\u003A\u003B\u003C\u003D\u003E\u003F\u0040\u0041\u0042\u0043\u0044\u0045\u0046\u0047\u0048\u0049\u004A\u004B\u004C\u004D\u004E\u004F\u0050\u0051\u0052\u0053\u0054\u0055\u0056\u0057\u0058\u0059\u005A\u005B\\\u005D\u005E\u005F\u0060\u0061\u0062\u0063\u0064\u0065\u0066\u0067\u0068\u0069\u006A\u006B\u006C\u006D\u006E\u006F\u0070\u0071\u0072\u0073\u0074\u0075\u0076\u0077\u0078\u0079\u007A\u007B\u007C\u007D\u007E\u007F\u00C7\u00FC\u00E9\u00E2\u00E4\u00E0\u00E5\u00E7\u00EA\u00EB\u00E8\u00EF\u00EE\u00EC\u00C4\u00C5\u00C9\u00E6\u00C6\u00F4\u00F6\u00F2\u00FB\u00F9\u00FF\u00D6\u00DC\u00A2\u00A3\u00A5\u20A7\u0192\u00E1\u00ED\u00F3\u00FA\u00F1\u00D1\u00AA\u00BA\u00BF\u2310\u00AC\u00BD\u00BC\u00A1\u00AB\u00BB\u2591\u2592\u2593\u2502\u2524\u2561\u2562\u2556\u2555\u2563\u2551\u2557\u255D\u255C\u255B\u2510\u2514\u2534\u252C\u251C\u2500\u253C\u255E\u255F\u255A\u2554\u2569\u2566\u2560\u2550\u256C\u2567\u2568\u2564\u2565\u2559\u2558\u2552\u2553\u256B\u256A\u2518\u250C\u2588\u2584\u258C\u2590\u2580\u03B1\u00DF\u0393\u03C0\u03A3\u03C3\u00B5\u03C4\u03A6\u0398\u03A9\u03B4\u221E\u03C6\u03B5\u2229\u2261\u00B1\u2265\u2264\u2320\u2321\u00F7\u2248\u00B0\u2219\u00B7\u221A\u207F\u00B2\u25A0 "; 443 444 public static String BuildCP437String(String text) 445 { 446 char[] temp = text.toCharArray(); 447 for (int x = 0; x < temp.length; x++) 448 { 449 int value = (int)temp[x]; 450 451 if (value < CP437TABLE.length()) 452 temp[x] = CP437TABLE.charAt(value); 453 } 454 455 return new String (temp); 456 } 457 458 public static void dump2 (HashMap data) 459 { 460 Iterator i = data.keySet().iterator(); 461 while (i.hasNext()) 462 { 463 String key = (String )i.next(); 464 System.out.println(key + "=> " + data.get(key)); 465 } 466 System.out.println(" ... Done ..."); 467 } 468 469 public static String formatLongAsDecimal (long l) 470 { 471 String rv = l+""; 472 if (rv.length() < 3) 473 { 474 while (rv.length() < 3) 475 { 476 rv = "0" + rv; 477 } 478 479 return "." + rv; 480 } 481 482 return rv.substring(0, rv.length() - 3) + "." + rv.substring(rv.length() - 3, rv.length()); 483 } 484 485 public static void removeAll(Collection source, Set remove) 486 { 487 Iterator i = remove.iterator(); 488 while (i.hasNext()) 489 { 490 source.remove(i.next()); 491 } 492 } 493 494 public static String generateThemeScript(String name) 495 { 496 if (name == null || name.length() == 0) 497 { 498 File file = DialogUtilities.showSaveDialog("Save Theme Script"); 499 if (file != null) 500 name = file.getAbsolutePath(); 501 } 502 503 if (name == null || name.length() == 0) 504 return null; 505 506 StringBuffer temp = new StringBuffer (); 507 508 temp.append("# jIRCii Theme File, scripters feel free to edit this file to export more settings\n"); 509 temp.append("# by default only color settings are exported.\n\n"); 510 511 temp.append("# some miscellaneous colors\n"); 512 temp.append(generateThemeLine("statusbar.color", null)); 513 temp.append(generateThemeLine("window.color", null)); 514 temp.append(generateThemeLine("desktop.color", null)); 515 temp.append(generateThemeLine("switchbar.color", null)); 516 temp.append(generateThemeLine("ui.editcolor", null)); 517 518 temp.append("\n"); 519 520 temp.append("# the actual color map\n"); 521 522 for (int x = 0; x < 100; x++) 523 { 524 temp.append("setMappedColor("); 525 temp.append(x); 526 temp.append(", \""); 527 temp.append(TextSource.colorTable[x].getRGB()); 528 temp.append("\");\n"); 529 } 530 531 temp.append("\n# force jIRCii to update settings right away\n"); 532 temp.append("setProperty(\"desktop\", -1);\n"); 533 temp.append("setProperty(\"statusbar\", -1);\n"); 534 temp.append("setProperty(\"window\", -1);\n"); 535 536 temp.append("\nsaveColorMap();\n"); 537 538 try 539 { 540 PrintWriter output = new PrintWriter(new FileOutputStream(new File(name), false)); 541 output.println(temp.toString()); 542 output.close(); 543 } 544 catch (Exception ex) 545 { 546 ex.printStackTrace(); 547 } 548 549 return (new File(name)).getName(); 550 } 551 552 private static String generateThemeLine(String var, String comment) 553 { 554 StringBuffer temp = new StringBuffer (); 555 556 if (ClientState.getClientState().getString(var, null) != null) 557 { 558 if (comment != null) 559 temp.append("# " + comment + "\n"); 560 561 temp.append("setProperty(\""); 562 temp.append(var); 563 temp.append("\", \""); 564 temp.append(ClientState.getClientState().getString(var, null)); 565 temp.append("\");\n"); 566 } 567 568 return temp.toString(); 569 } 570 571 public static File getFile(String file) 572 { 573 if (file.length() > 0 && file.charAt(0) == '~' && System.getProperty("user.home") != null) 574 { 575 return new File(System.getProperty("user.home").toString(), file.substring(1)); 576 } 577 578 return new File(file); 579 } 580 581 public static LinkedList fileCompleteAll(String pfile) 582 { 583 LinkedList rv = new LinkedList(); 584 585 File cwd; 586 String cwf = null; 587 588 File temp = getFile(pfile); 589 if (temp.isDirectory()) 590 { 591 cwd = temp; 592 cwf = null; 593 } 594 else if (temp.getParentFile() != null && temp.getParentFile().isDirectory()) 595 { 596 cwd = temp.getParentFile(); 597 cwf = temp.getName(); 598 } 599 else 600 { 601 cwd = new File(new File("").getAbsolutePath()); 602 cwf = pfile; 603 } 604 605 File[] files = cwd.listFiles(); 606 for (int x = 0; x < files.length; x++) 607 { 608 String tempn = files[x].getName(); 609 610 if (cwf == null) 611 { 612 rv.add(files[x].getAbsolutePath()); 613 } 614 else if (tempn.length() >= cwf.length() && tempn.toUpperCase().substring(0, cwf.length()).equals(cwf.toUpperCase())) 615 { 616 rv.add(0, files[x].getAbsolutePath()); 617 } 618 else if (tempn.toUpperCase().indexOf(cwf.toUpperCase()) > -1) 619 { 620 rv.add(files[x].getAbsolutePath()); 621 } 622 } 623 624 rv.add(pfile); 625 626 return rv; 627 } 628 } 629 | Popular Tags |