1 7 8 package org.jdesktop.swing; 9 10 import java.applet.Applet ; 11 import java.applet.AppletContext ; 12 13 import java.awt.Component ; 14 import java.awt.Container ; 15 import java.awt.Image ; 16 import java.awt.Window ; 17 18 import java.beans.Beans ; 19 20 import java.io.File ; 21 22 import java.lang.reflect.Method ; 23 24 import java.net.URL ; 25 import java.net.MalformedURLException ; 26 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.Hashtable ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.Vector ; 34 35 import javax.swing.ActionMap ; 36 import javax.swing.JApplet ; 37 import javax.swing.JRootPane ; 38 import javax.swing.Icon ; 39 import javax.swing.ImageIcon ; 40 import javax.swing.SwingUtilities ; 41 42 import org.jdesktop.swing.event.SelectionListener; 43 44 import org.jdesktop.swing.actions.ActionManager; 45 46 import org.jdesktop.swing.utils.SwingWorker; 47 62 public class Application { 63 64 private static final int WINDOWS = 0; 66 private static final int APPLETS = 1; 67 68 private static Map appMap = new Hashtable (); 69 private static Map imageCache = new HashMap (); 70 71 private ActionMap actionMap; 72 73 private ActionManager manager; 76 77 private String title = "JDNC Application"; 78 private String versionString = ""; 79 80 private Image splashImage = null; 81 private Image titleBarImage = null; 82 83 private URL baseURL; 84 private Vector toplevel[] = new Vector [2]; 85 86 private List selectionListeners; 87 88 100 public static Application getInstance() { 101 return getInstance("theone"); 102 } 103 104 113 public static Application getInstance(Object key) { 114 Application app = null; 115 synchronized(appMap) { 116 app = (Application) appMap.get(key); 117 if (app == null) { 118 try { 119 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 120 app = (Application)Beans.instantiate(cl, 121 "org.jdesktop.swing.Application"); 122 appMap.put(key, app); 123 } catch (Exception ex) { 124 ex.printStackTrace(); 126 } 127 } 129 } 130 return app; 131 } 132 133 142 public static Application getApp(Component c) { 143 Application app = null; 144 Container parent = c instanceof Container ? (Container )c : c.getParent(); 145 while (parent != null) { 146 if (parent instanceof Window ) { 147 app = findApp(WINDOWS, parent); 148 break; 149 } 150 else if (parent instanceof Applet ) { 151 app = findApp(APPLETS, parent); 152 break; 153 } 154 else { 155 parent = parent.getParent(); 156 } 157 } 158 if (app == null) { 161 Component p = SwingUtilities.getRoot(c); 162 if (p != null) { 163 app = Application.getInstance(p); 164 if (p instanceof Applet ) { 165 app.registerApplet((Applet )p); 166 } else { 167 app.registerWindow((Window )p); 168 } 169 } 170 } 171 return app; 172 } 173 174 private static Application findApp(int type, Component c) { 175 Application app = null; 176 Iterator apps = appMap.values().iterator(); 177 while (app == null && apps.hasNext()) { 178 Application a = (Application) apps.next(); 179 if (a.toplevel[type] != null) { 180 if (a.toplevel[type].contains(c)) { 181 app = a; 182 break; 183 } 184 } 185 } 186 return app; 187 } 188 189 194 public ActionManager getActionManager() { 195 if (manager == null) { 196 manager = new ActionManager(); 197 } 198 return manager; 199 } 200 201 205 public void setBaseURL(URL baseURL) { 206 this.baseURL = baseURL; 207 } 208 209 212 public URL getBaseURL() { 213 return baseURL; 214 } 215 216 221 public static URL getBaseURL(Object obj) { 222 URL url = null; 223 if (obj instanceof Component ) { 224 Container parent = SwingUtilities.getAncestorOfClass(JApplet .class, 225 (Component )obj); 226 if (parent != null) { 227 JApplet applet = (JApplet )parent; 228 url = applet.getDocumentBase(); 229 } else { 230 WebStartContext context = WebStartContext.getInstance(); 231 url = context.getDocumentBase(); 233 } 234 if (url == null) { 235 Application app = Application.getApp((Component )obj); 236 if (app != null) { 237 url = app.getBaseURL(); 238 } 239 } 240 } 241 if (url == null) { 242 url = Application.getInstance().getBaseURL(); 243 } 244 return url; 245 } 246 247 253 public static URL getURL(String value, Object obj) { 254 URL url = getURLResource(value, obj); 255 if (url == null) { 256 try { 257 url = new URL (value); 258 } catch (MalformedURLException ex) { 259 } 261 } 262 if (url == null) { 263 URL base = Application.getBaseURL(obj); 264 if (base != null) { 265 try { 266 url = new URL (base, value); 267 } catch (MalformedURLException e) { 268 } 270 } 271 } 272 if (url == null) { 273 } 275 return url; 276 } 277 278 public static URL getURLResource(String value, Object obj) { 279 return obj.getClass().getResource(value); 280 } 281 282 283 public static Image getImage(String name, Object obj) { 284 Icon icon = getIcon(name, obj); 285 if (icon != null) { 286 return ((ImageIcon )icon).getImage(); 287 } else { 288 return null; 289 } 290 } 291 292 public static Icon getIcon(String name, Object obj) { 293 Icon icon = null; 294 295 if (name == null || (icon = (Icon )imageCache.get(name)) != null) { 296 return icon; 297 } 298 299 URL fileLoc = getURL(name, obj); 300 if (fileLoc != null && (icon = new ImageIcon (fileLoc)) != null) { 301 imageCache.put(name, icon); 302 } 303 return icon; 304 } 305 306 321 public void showDocument(final URL url, final String target) { 322 if (isRunningApplet()) { 323 Iterator iter = getApplets(); 325 if (iter != null) { 326 Applet applet = (Applet )iter.next(); 328 final AppletContext context = applet.getAppletContext(); 329 SwingWorker worker = new SwingWorker() { 332 public Object construct() { 333 context.showDocument(url, target); 334 return null; 335 } 336 }; 337 worker.start(); 338 } 339 } else if (isRunningWebStart()) { 340 final WebStartContext context = WebStartContext.getInstance(); 341 SwingWorker worker = new SwingWorker() { 342 public Object construct() { 343 context.showDocument(url, target); 344 return null; 345 } 346 }; 347 worker.start(); 348 } else { 349 String [] command = null; 351 String os = System.getProperty("os.name").toLowerCase(); 352 if (os.indexOf("os x") >= 0) { 353 command = new String [] { "open", url.toExternalForm() }; 355 } 356 else if (os.indexOf("wind") >= 0) { 357 command = new String [] { "rundll32 url.dll,FileProtocolHandler", 359 url.toExternalForm() }; 360 } 361 else { 362 command = new String [] { "mozilla", "-remote", 364 "openurl(" + url.toExternalForm() + ")"}; 365 } 366 executeCommand(command); 367 } 368 } 369 370 373 void executeCommand(final String [] command) { 374 SwingWorker worker = new SwingWorker() { 375 public Object construct() { 376 try { 377 Runtime.getRuntime().exec(command); 378 } catch (Exception ex) { 379 } 380 return null; 381 } 382 }; 383 worker.start(); 384 } 385 386 393 public void setSplashImage(Image splashImage) { 394 this.splashImage = splashImage; 395 } 396 397 400 public Image getSplashImage() { 401 return splashImage; 402 } 403 404 408 public void setTitle(String title) { 409 this.title = title; 410 } 411 412 415 public String getTitle() { 416 return title; 417 } 418 419 426 public void setTitleBarImage(Image titleBarImage) { 427 this.titleBarImage = titleBarImage; 428 } 429 430 433 public Image getTitleBarImage() { 434 return titleBarImage; 435 } 436 437 441 public void setVersionString(String versionString) { 442 this.versionString = versionString; 443 } 444 445 448 public String getVersionString() { 449 return versionString; 450 } 451 452 458 public boolean isStandAlone() { 459 return getApplets() == null; 460 } 461 462 468 public boolean isRunningInSandbox() { 469 boolean inSandbox = false; 470 try { 471 new File ("."); 472 } catch (SecurityException e) { 473 inSandbox = true; 474 } 475 return inSandbox; 476 } 477 478 483 public boolean isRunningApplet() { 484 return getApplets() != null; 485 } 486 487 493 public boolean isRunningWebStart() { 494 try { 495 Class service = Class.forName("javax.jnlp.ServiceManager"); 497 Method lookup = service.getMethod("lookup", new Class [] { String .class }); 498 499 Object basic = lookup.invoke(null, 500 new Object [] { "javax.jnlp.BasicService" }); 501 502 return true; 504 } catch (Exception ex) { 505 } 507 return false; 508 } 509 510 513 public void registerWindow(Window window) { 514 register(WINDOWS, window); 515 } 516 517 public void unregisterWindow(Window window) { 518 unregister(WINDOWS, window); 519 } 520 521 public void registerApplet(Applet applet) { 522 register(APPLETS, applet); 523 } 524 525 public void unregisterApplet(Applet applet) { 526 unregister(APPLETS, applet); 527 } 528 529 533 public ActionMap getActionMap() { 534 if (actionMap == null) { 535 actionMap = new ActionMap (); 536 } 537 return actionMap; 538 } 539 540 544 public Iterator getApplets() { 545 return getToplevels(APPLETS); 546 } 547 548 552 public Iterator getWindows() { 553 return getToplevels(WINDOWS); 554 } 555 556 public void addSelectionListener(SelectionListener l) { 557 if (selectionListeners == null) { 558 selectionListeners = new ArrayList (); 559 } 560 selectionListeners.add(l); 561 } 562 563 public void removeSelectionListener(SelectionListener l) { 564 if (selectionListeners != null) { 565 selectionListeners.remove(l); 566 } 567 } 568 569 public SelectionListener[] getSelectionListeners() { 570 if (selectionListeners != null) { 571 return (SelectionListener[])selectionListeners.toArray( 572 new SelectionListener[1]); 573 } 574 return new SelectionListener[0]; 575 } 576 577 private void register(int type, Component c) { 578 if (toplevel[type] == null) { 579 toplevel[type] = new Vector (); 580 } 581 toplevel[type].add(c); 582 } 583 584 private void unregister(int type, Component c) { 585 if (toplevel[type] != null) { 586 toplevel[type].remove(c); 587 } 588 } 589 590 private Iterator getToplevels(int type) { 591 if (toplevel[type] == null) { 592 return null; 593 } 594 return toplevel[type].iterator(); 595 } 596 } 597 | Popular Tags |