1 13 14 15 package swingwtx.swing; 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.widgets.*; 19 20 import java.io.*; 21 import java.net.*; 22 import java.util.*; 23 24 32 public abstract class SwingWTUtils { 33 34 private static boolean isDebug = true; 35 public static boolean showInternalSWTExceptions = true; 36 37 private final static String VERSION = "0.83 (070504)"; 38 private final static String COPYRIGHT = "This is SwingWT (http://swingwt.sourceforge.net)\n" + 39 "Version: " + VERSION + "\n" + 40 "Copyright(c)2003-2004, R.Rawson-Tetley and other contributors.\n\n" + 41 "This library is distributed in the hope that it will be useful,\n" + 42 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" + 43 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n" + 44 "Lesser General Public Licence for more details."; 45 46 private static Display display = null; 47 48 49 private static boolean eventsDispatching = false; 50 51 private static Object eventsDispatchOwner = null; 52 53 private static boolean setDeleteTempOnShutdown = false; 54 55 private static Vector tempFileList = new Vector(); 56 57 private static boolean retardEventDispatch = false; 58 private static int retardationms = 2000; 59 60 private static int windowReferenceCount = 0; 61 62 private static String tempDirectory = System.getProperty("user.home") + File.separator + "tmp" + File.separator + "swingwt"; 63 64 public static String getVersion() { return VERSION; } 65 66 public static synchronized void incrementWindowReferences() { 67 windowReferenceCount++; 68 } 69 70 public static synchronized void decrementWindowReferences() { 71 windowReferenceCount--; 72 if (windowReferenceCount == 0) stopEventDispatchRunning(); 73 } 74 75 public static void setShowSwingWTInfoOnStartup(boolean b) { 76 isDebug = b; 77 } 78 79 80 public static synchronized void setRetardDispatchThread(boolean b) { 81 retardEventDispatch = b; 82 } 83 84 public static synchronized void setRetardDispatchThread(boolean b, int ms) { 85 retardEventDispatch = b; 86 retardationms = ms; 87 } 88 89 96 public synchronized static void setEclipsePlugin(boolean b) { 97 if (b) { 98 eventsDispatching = true; 99 display = Display.getDefault(); 100 } 101 else { 102 eventsDispatching = false; 103 } 104 } 105 106 110 public synchronized static void checkEventDispatcher() { 111 if (!eventsDispatching) { 112 113 eventsDispatching = true; 114 115 checkShutdownHook(); 118 119 new Thread () { 121 public void run() { 122 123 display = Display.getDefault(); 125 126 if (isDebug) 128 System.out.println(COPYRIGHT); 129 130 while (eventsDispatching) { 131 try { 132 if (!display.readAndDispatch()) { 133 display.sleep(); 136 } 137 if (SwingWTUtils.isRetardDispatchThread()) { 139 try { 140 Thread.sleep(SwingWTUtils.getRetardationInterval()); 141 } 142 catch (InterruptedException e) {} 143 } 144 } 145 catch (org.eclipse.swt.SWTException e) { 150 if (showInternalSWTExceptions) 151 e.printStackTrace(); 152 } 153 catch (Error e) { 154 e.printStackTrace(); 155 } 156 catch (Exception e) { 157 e.printStackTrace(); 158 } 159 } 160 } 161 }.start(); 162 } 163 } 164 165 166 public static boolean isRetardDispatchThread() { return retardEventDispatch; } 167 168 public static int getRetardationInterval() { return retardationms; } 169 170 public static synchronized Display getDisplay() { 171 172 checkEventDispatcher(); 175 176 while (display == null) { 178 try { 179 Thread.sleep(50); 180 } 181 catch (InterruptedException e) {} 182 } 183 184 return display; 185 } 186 187 191 private static void checkForTempDirectory() throws IOException { 192 File f = new File(tempDirectory); 193 if (!f.exists()) 194 f.mkdirs(); 195 } 196 197 199 public static boolean isSWTControlAvailable(org.eclipse.swt.widgets.Control c) { 200 if (c == null) return false; 201 if (c.isDisposed()) return false; 202 return true; 203 } 204 205 207 public static boolean isSWTMenuControlAvailable(org.eclipse.swt.widgets.MenuItem c) { 208 if (c == null) return false; 209 if (c.isDisposed()) return false; 210 return true; 211 } 212 213 public synchronized static boolean isEventDispatchRunning() { 214 return eventsDispatching; 215 } 216 217 221 public static synchronized void stopEventDispatchRunning() { 222 eventsDispatching = false; 223 } 224 225 232 public static URL stringToTempFile(byte[] contents, String type) throws IOException { 233 234 checkForTempDirectory(); 236 237 File f = null; 239 String fName = null; 240 do { 241 fName = tempDirectory + File.separator + 242 ((int) (Math.random() * 10000000)) + "." + type; 243 f = new File(fName); 244 } while (f.exists()); 245 246 System.out.println("TEMP: Creating temp file " + fName); 247 FileOutputStream out = new FileOutputStream(f); 248 out.write(contents); 249 out.close(); 250 251 tempFileList.add(fName); 253 254 return new URL("file://" + fName); 255 } 256 257 public static void clearTempDirectory() { 258 if (tempFileList.size() == 0) return; 260 Iterator i = tempFileList.iterator(); 261 while (i.hasNext()) { 262 try { 263 File f = new File((String ) i.next()); 264 f.delete(); 265 } 266 catch (Exception e) { e.printStackTrace(); } 267 } 268 } 269 270 public static void checkShutdownHook() { 271 if (!setDeleteTempOnShutdown) { 272 Runtime.getRuntime().addShutdownHook( new Thread () { 273 public void run() { 274 275 clearTempDirectory(); 276 277 282 297 System.runFinalization(); 301 System.gc(); 302 303 } 304 }); 305 setDeleteTempOnShutdown = true; 306 } 307 } 308 309 private static boolean checkedOS = false; 310 private static boolean isWin = false; 311 312 public static boolean isWindows() { 313 if (!checkedOS) 314 isWin = System.getProperty("os.name").toLowerCase().indexOf("windows") != -1; 315 return isWin; 316 } 317 318 325 public static void saveImageToJPG(swingwt.awt.Image image, OutputStream stream) { 326 org.eclipse.swt.graphics.ImageLoader il = new org.eclipse.swt.graphics.ImageLoader(); 327 il.data = new org.eclipse.swt.graphics.ImageData[] { image.image.getImageData() }; 328 il.save(stream, org.eclipse.swt.SWT.IMAGE_JPEG); 329 il = null; 330 } 331 332 339 public static void saveImageToGIF(swingwt.awt.Image image, OutputStream stream) { 340 org.eclipse.swt.graphics.ImageLoader il = new org.eclipse.swt.graphics.ImageLoader(); 341 il.data = new org.eclipse.swt.graphics.ImageData[] { image.image.getImageData() }; 342 il.save(stream, org.eclipse.swt.SWT.IMAGE_GIF); 343 il = null; 344 } 345 346 353 public static void saveImageToPNG(swingwt.awt.Image image, OutputStream stream) { 354 org.eclipse.swt.graphics.ImageLoader il = new org.eclipse.swt.graphics.ImageLoader(); 355 il.data = new org.eclipse.swt.graphics.ImageData[] { image.image.getImageData() }; 356 il.save(stream, org.eclipse.swt.SWT.IMAGE_PNG); 357 il = null; 358 } 359 360 361 private static int intretval = 0; 362 367 public static int getRenderStringWidth(final String text) { 368 SwingUtilities.invokeSync( new Runnable () { 369 public void run() { 370 org.eclipse.swt.graphics.GC gc = null; 371 org.eclipse.swt.graphics.Image im = null; 372 if (getDisplay().getActiveShell() == null) { 373 im = new org.eclipse.swt.graphics.Image(getDisplay(), getDisplay().getBounds()); 374 gc = new org.eclipse.swt.graphics.GC(im); 375 } 376 else 377 gc = new org.eclipse.swt.graphics.GC(getDisplay().getActiveShell()); 378 379 org.eclipse.swt.graphics.Point p = null; 383 if (isWindows()) 384 p = gc.stringExtent(text + "WWW"); 385 else 386 p = gc.stringExtent(text + "W"); 387 int width = p.x; 388 gc.dispose(); 389 gc = null; 390 if (im != null) im.dispose(); 391 im = null; 392 intretval = width; 393 } 394 }); 395 return intretval; 396 } 397 398 403 public static int getRenderStringHeight(final String text) { 404 SwingUtilities.invokeSync( new Runnable () { 405 public void run() { 406 org.eclipse.swt.graphics.GC gc = null; 407 org.eclipse.swt.graphics.Image im = null; 408 if (getDisplay().getActiveShell() == null) { 409 im = new org.eclipse.swt.graphics.Image(getDisplay(), getDisplay().getBounds()); 410 gc = new org.eclipse.swt.graphics.GC(im); 411 } 412 else 413 gc = new org.eclipse.swt.graphics.GC(getDisplay().getActiveShell()); 414 415 org.eclipse.swt.graphics.Point p = gc.stringExtent(text + "W"); 416 int height = p.y; 417 gc.dispose(); 418 gc = null; 419 if (im != null) im.dispose(); 420 im = null; 421 intretval = height; 422 } 423 }); 424 return intretval; 425 } 426 427 434 public static int getStringBufferIndexOf(StringBuffer buffer, String string) { 435 return buffer.toString().indexOf(string); 436 } 437 445 public static int getStringBufferIndexOf(StringBuffer buffer, String string, int fromIndex) { 446 return buffer.toString().indexOf(string, fromIndex); 447 } 448 449 459 public static org.eclipse.swt.graphics.Image getSWTImageFromSwingIcon(swingwt.awt.Component c, swingwtx.swing.Icon icon) { 460 461 if (icon == null) return null; 462 463 if (icon instanceof ImageIcon) 466 return ((ImageIcon) icon).getImage().image; 467 468 470 org.eclipse.swt.graphics.Image img = new org.eclipse.swt.graphics.Image(getDisplay(), icon.getIconWidth(), icon.getIconHeight()); 472 org.eclipse.swt.graphics.GC gc = new org.eclipse.swt.graphics.GC(img); 473 swingwt.awt.SWTGraphics2DRenderer g = new swingwt.awt.SWTGraphics2DRenderer(gc, false); 474 475 icon.paintIcon(c, g, 0, 0); 477 478 g.dispose(); 480 481 return img; 483 484 } 485 486 487 public static int translateSwingAlignmentConstant(int constant) { 488 int ret = 0; 489 switch (constant) { 490 case (SwingConstants.CENTER): ret = SWT.CENTER; break; 491 case (SwingConstants.TOP): ret = SWT.TOP; break; 492 case (SwingConstants.LEFT): ret = SWT.LEFT; break; 493 case (SwingConstants.BOTTOM): ret = SWT.BOTTOM; break; 494 case (SwingConstants.RIGHT): ret = SWT.RIGHT; break; 495 case (SwingConstants.LEADING): ret = SWT.LEFT; break; 496 case (SwingConstants.TRAILING): ret = SWT.RIGHT; break; 497 } 498 return ret; 499 } 500 501 502 public static int translateSwingOrientationConstant(int constant) { 503 int ret = 0; 504 switch (constant) { 505 case (SwingConstants.HORIZONTAL): ret = SWT.HORIZONTAL; break; 506 case (SwingConstants.VERTICAL): ret = SWT.VERTICAL; break; 507 } 508 return ret; 509 } 510 511 514 public static String removeHTML(String s) { 515 516 int opener = s.indexOf("<"); 519 int closer = s.indexOf(">"); 520 if (opener != -1 && closer == -1) 521 return s; 522 523 int i = s.indexOf("<"); 524 while (i != -1) { 525 int e = s.indexOf(">", i); 527 if (e == -1) e = s.length(); 528 s = s.substring(0, i) + 530 ( e < s.length() ? s.substring(e + 1, s.length()) 531 : "" ); 532 i = s.indexOf("<"); 534 } 535 536 s = replace(s, " ", " "); 538 s = replace(s, " ", " "); 539 s = replace(s, "&", " "); 540 s = replace(s, "<", "<"); 541 s = replace(s, ">", ">"); 542 543 s = s.trim(); 545 StringBuffer o = new StringBuffer (); 546 boolean lastWasSpace = false; 547 for (i = 0; i < s.length(); i++) { 548 if (s.substring(i, i + 1).equals(" ")) { 549 if (!lastWasSpace) { 550 lastWasSpace = true; 551 o.append(" "); 552 } 553 } 554 else { 555 o.append(s.substring(i, i + 1)); 556 lastWasSpace = false; 557 } 558 } 559 s = o.toString(); 560 561 return s; 562 } 563 564 565 571 public static String replace(String findin, String find, String replacewith) { 572 573 StringBuffer sb = new StringBuffer (findin); 574 int i = 0; 575 try { 576 while (i <= sb.length() - find.length()) { 577 if (sb.substring(i, i + find.length()).equalsIgnoreCase(find)) { 578 sb.replace(i, i + find.length(), replacewith); 579 } 580 i++; 581 } 582 } 583 catch (StringIndexOutOfBoundsException e) { 584 } 586 587 return sb.toString(); 588 } 589 590 } 591 592 777 | Popular Tags |