1 19 20 package org.netbeans.core; 21 22 import java.awt.Dialog ; 23 import java.awt.Toolkit ; 24 import java.awt.Window ; 25 import java.io.IOException ; 26 import java.lang.reflect.Method ; 27 import java.net.URL ; 28 import java.text.MessageFormat ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.logging.Level ; 33 import java.util.logging.Logger ; 34 import java.util.prefs.PreferenceChangeEvent ; 35 import java.util.prefs.PreferenceChangeListener ; 36 import javax.swing.JDialog ; 37 import javax.swing.SwingUtilities ; 38 import javax.swing.event.ChangeEvent ; 39 import javax.swing.event.ChangeListener ; 40 import org.netbeans.TopSecurityManager; 41 import org.netbeans.core.startup.MainLookup; 42 import org.netbeans.core.startup.ModuleSystem; 43 import org.netbeans.core.startup.layers.SessionManager; 44 import org.netbeans.core.ui.SwingBrowser; 45 import org.openide.LifecycleManager; 46 import org.openide.NotifyDescriptor; 47 import org.openide.awt.HtmlBrowser; 48 import org.openide.cookies.SaveCookie; 49 import org.openide.loaders.DataObject; 50 import org.openide.util.Exceptions; 51 import org.openide.util.HelpCtx; 52 import org.openide.util.Lookup; 53 import org.openide.util.LookupEvent; 54 import org.openide.util.LookupListener; 55 import org.openide.util.Mutex; 56 import org.openide.util.NbBundle; 57 import org.openide.util.RequestProcessor; 58 import org.openide.util.Task; 59 import org.openide.util.lookup.InstanceContent; 60 import org.openide.windows.Mode; 61 import org.openide.windows.TopComponent; 62 import org.openide.windows.WindowManager; 63 64 68 public abstract class NbTopManager { 69 70 71 74 public static final int IL_MAIN_WINDOW = 0x0001; 75 78 public static final int IL_WINDOWS = 0x0002; 79 81 public static final int IL_WORKSPACES = 0x0004; 82 83 84 86 public static final int IL_ALL = 0xffff; 87 88 90 public NbTopManager() { 91 assert defaultTopManager == null : "Only one instance allowed"; defaultTopManager = this; 93 94 Lookup lookup = Lookup.getDefault(); 95 if (!(lookup instanceof MainLookup)) { 96 throw new ClassCastException ("Wrong Lookup impl found: " + lookup); 97 } 98 ((MainLookup)lookup).startedNbTopManager(); 99 } 100 101 103 public static NbTopManager get () { 104 assert defaultTopManager != null : "Must be initialized already"; return defaultTopManager; 106 } 107 108 114 public static NbTopManager getUninitialized() { 115 return get(); 116 } 117 118 private static NbTopManager defaultTopManager; 119 120 124 public static synchronized boolean isInitialized () { 125 return defaultTopManager != null; 126 } 127 128 133 public abstract boolean isInteractive (int il); 134 135 139 141 public final void register (Object obj) { 142 MainLookup.register (obj); 143 } 144 145 149 public final <T,R> void register(T obj, InstanceContent.Convertor<T,R> conv) { 150 MainLookup.register (obj, conv); 151 } 152 153 155 public final void unregister (Object obj) { 156 MainLookup.unregister (obj); 157 } 158 160 public final <T,R> void unregister (T obj, InstanceContent.Convertor<T,R> conv) { 161 MainLookup.unregister (obj, conv); 162 } 163 164 165 169 173 public void showHelp(HelpCtx helpCtx) { 174 try { 176 Class <?> c = (Lookup.getDefault().lookup(ClassLoader .class)).loadClass("org.netbeans.api.javahelp.Help"); Object o = Lookup.getDefault().lookup(c); 178 if (o != null) { 179 Method m = c.getMethod("showHelp", new Class [] {HelpCtx.class}); m.invoke(o, new Object [] {helpCtx}); 181 return; 182 } 183 } catch (ClassNotFoundException cnfe) { 184 } catch (Exception e) { 186 Logger.getLogger(NbTopManager.class.getName()).log(Level.WARNING, null, e); 188 } 189 Toolkit.getDefaultToolkit().beep(); 191 } 192 193 196 public static final class NbURLDisplayer extends HtmlBrowser.URLDisplayer { 197 198 public NbURLDisplayer() {} 199 200 private NbBrowser htmlViewer; 201 public void showURL(final URL u) { 202 Mutex.EVENT.readAccess(new Runnable () { 203 public void run() { 204 if (htmlViewer == null) { 205 htmlViewer = new NbBrowser(); 206 } 207 htmlViewer.showUrl(u); 208 } 209 }); 210 } 211 } 212 213 214 217 public static final class NbStatusDisplayer extends org.openide.awt.StatusDisplayer { 218 219 public NbStatusDisplayer() {} 220 private List <ChangeListener > listeners = null; 221 private String text = ""; public void setStatusText(String text) { 223 ChangeListener [] _listeners; 224 synchronized (this) { 225 this.text = text; 226 if (listeners == null || listeners.isEmpty()) { 227 return; 228 } else { 229 _listeners = listeners.toArray(new ChangeListener [listeners.size()]); 230 } 231 } 232 ChangeEvent e = new ChangeEvent (this); 233 for (int i = 0; i < _listeners.length; i++) { 234 _listeners[i].stateChanged(e); 235 } 236 Logger.getLogger(NbStatusDisplayer.class.getName()).log(Level.FINE, "Status text updated: {0}", text); 237 } 238 public synchronized String getStatusText() { 239 return text; 240 } 241 public synchronized void addChangeListener(ChangeListener l) { 242 if (listeners == null) listeners = new ArrayList <ChangeListener >(); 243 listeners.add(l); 244 } 245 public synchronized void removeChangeListener(ChangeListener l) { 246 listeners.remove(l); 247 } 248 } 249 250 251 private static void saveAll () { 252 DataObject dobj = null; 253 ArrayList <DataObject> bad = new ArrayList <DataObject> (); 254 DataObject[] modifs = DataObject.getRegistry ().getModified (); 255 if (modifs.length == 0) { 256 return; 258 } 259 for (int i = 0; i < modifs.length; i++) { 260 try { 261 dobj = modifs[i]; 262 SaveCookie sc = (SaveCookie) dobj.getCookie(SaveCookie.class); 263 264 if (sc != null) { 265 org.openide.awt.StatusDisplayer.getDefault().setStatusText( 266 MessageFormat.format( 267 NbBundle.getBundle(NbTopManager.class).getString( 268 "CTL_FMT_SavingMessage" 269 ), new Object []{dobj.getName()} 270 )); 271 sc.save(); 272 } 273 } 274 catch (IOException ex) { 275 Logger.getLogger(NbTopManager.class.getName()).log(Level.WARNING, null, ex); 276 bad.add(dobj); 277 } 278 } 279 NotifyDescriptor descriptor; 280 Iterator <DataObject> ee = bad.iterator (); 282 while (ee.hasNext ()) { 283 descriptor = new NotifyDescriptor.Message( 284 MessageFormat.format ( 285 NbBundle.getBundle (NbTopManager.class).getString("CTL_Cannot_save"), 286 new Object [] { ee.next().getPrimaryFile().getName() } 287 ) 288 ); 289 org.openide.DialogDisplayer.getDefault().notify (descriptor); 290 } 291 org.openide.awt.StatusDisplayer.getDefault().setStatusText( 293 NbBundle.getBundle (NbTopManager.class).getString ("MSG_AllSaved")); 294 } 295 296 298 public interface WindowSystem { 299 void show(); 300 void hide(); 301 void load(); 302 void save(); 303 void clear(); 304 } 306 public static boolean isModalDialogPresent() { 307 return hasModalDialog(WindowManager.getDefault().getMainWindow()) 308 || hasModalDialog(new JDialog ().getOwner()); 310 } 311 312 private static boolean hasModalDialog(Window w) { 313 Window [] ws = w.getOwnedWindows(); 314 for(int i = 0; i < ws.length; i++) { 315 if(ws[i] instanceof Dialog && ((Dialog )ws[i]).isModal()) { 316 return true; 317 } else if(hasModalDialog(ws[i])) { 318 return true; 319 } 320 } 321 322 return false; 323 } 324 325 326 327 private static boolean doingExit=false; 328 public static void exit ( ) { 329 if(SwingUtilities.isEventDispatchThread()) { 332 doExit(); 333 } else { 334 SwingUtilities.invokeLater(new Runnable () { 335 public void run() { 336 doExit(); 337 } 338 }); 339 } 340 } 341 342 345 public static boolean isExiting() { 346 return doingExit; 347 } 348 349 private static void doExit() { 350 if (doingExit) { 351 return ; 352 } 353 doingExit = true; 354 try { 356 if ( System.getProperty ("netbeans.close") != null || ExitDialog.showDialog() ) { 357 358 final WindowSystem windowSystem = Lookup.getDefault().lookup(WindowSystem.class); 359 360 Runnable hideFrames = new Runnable () { 362 public void run() { 363 org.netbeans.CLIHandler.stopServer (); 364 365 if(windowSystem != null) { 366 windowSystem.hide(); 367 windowSystem.save(); 368 } 369 if (Boolean.getBoolean("netbeans.close.when.invisible")) { 370 TopSecurityManager.exit(0); 372 } 373 } 374 }; 375 376 if (org.netbeans.core.startup.Main.getModuleSystem().shutDown(hideFrames)) { 377 try { 378 try { 379 LoaderPoolNode.store(); 380 } catch (IOException ioe) { 381 Logger.getLogger(NbTopManager.class.getName()).log(Level.WARNING, null, ioe); 382 } 383 try { 390 ((MainLookup)Lookup.getDefault()).storeCache(); 391 } catch (IOException ioe) { 392 Logger.getLogger(NbTopManager.class.getName()).log(Level.WARNING, null, ioe); 393 } 394 SessionManager.getDefault().close(); 395 } catch (ThreadDeath td) { 396 throw td; 397 } catch (Throwable t) { 398 Exceptions.printStackTrace(t); 401 } 402 Task exitTask = new Task(new Runnable () { 407 public void run() { 408 TopSecurityManager.exit(0); 409 } 410 }); 411 RequestProcessor.getDefault().post(exitTask); 412 exitTask.waitFinished(); 413 } 414 } 415 } finally { 416 doingExit = false; 417 } 418 } 419 420 424 public static final class NbLifecycleManager extends LifecycleManager { 425 426 public NbLifecycleManager() {} 427 public void saveAll() { 428 NbTopManager.saveAll(); 429 } 430 public void exit() { 431 NbTopManager.exit(); 432 } 433 } 434 435 436 public abstract ModuleSystem getModuleSystem(); 437 438 public static Lookup getModuleLookup() { 439 return org.netbeans.core.startup.Main.getModuleSystem().getManager().getModuleLookup(); 440 } 441 442 public static List getModuleJars() { 443 return org.netbeans.core.startup.Main.getModuleSystem().getModuleJars(); 444 } 445 446 449 public static class NbBrowser { 450 451 private HtmlBrowserComponent brComp; 452 private PreferenceChangeListener idePCL; 453 private static Lookup.Result factoryResult; 454 455 static { 456 factoryResult = Lookup.getDefault().lookupResult(HtmlBrowser.Factory.class); 457 factoryResult.allItems(); 458 factoryResult.addLookupListener(new LookupListener() { 459 public void resultChanged(LookupEvent ev) { 460 ((NbURLDisplayer)org.openide.awt.HtmlBrowser.URLDisplayer.getDefault()).htmlViewer = null; 461 } 462 }); 463 } 464 465 public NbBrowser() { 466 HtmlBrowser.Factory browser = IDESettings.getWWWBrowser(); 467 if (browser == null) { 468 browser = new SwingBrowser(); 470 } 471 if (browser.createHtmlBrowserImpl().getComponent() != null) { 474 brComp = findOpenedBrowserComponent(); 475 } 476 if (brComp == null) { 477 brComp = new HtmlBrowserComponent(browser, true, true); 478 brComp.putClientProperty("TabPolicy", "HideWhenAlone"); } 480 setListener(); 481 } 482 483 487 private HtmlBrowserComponent findOpenedBrowserComponent() { 488 for (Iterator it = WindowManager.getDefault().getModes().iterator(); it.hasNext(); ) { 489 Mode m = (Mode) it.next(); 490 if ("editor".equals(m.getName())) { TopComponent[] tcs = m.getTopComponents(); 492 for (int i = 0; i < tcs.length; i++) { 493 if (tcs[i] instanceof HtmlBrowserComponent) { 494 return (HtmlBrowserComponent) tcs[i]; 495 } 496 } 497 break; 498 } 499 } 500 return null; 501 } 502 503 506 private void showUrl(URL url) { 507 brComp.open(); 508 brComp.requestActive(); 509 brComp.setURL(url); 510 } 511 512 515 private void setListener () { 516 if (idePCL != null) { 517 return; 518 } 519 try { 520 idePCL = new PreferenceChangeListener () { 522 public void preferenceChange(PreferenceChangeEvent evt) { 523 if (IDESettings.PROP_WWWBROWSER.equals(evt.getKey())) { 524 ((NbURLDisplayer) HtmlBrowser.URLDisplayer.getDefault()).htmlViewer = null; 525 if (idePCL != null) { 526 IDESettings.getPreferences().removePreferenceChangeListener(idePCL); 527 idePCL = null; 528 brComp = null; 529 } 530 } 531 } 532 }; 533 IDESettings.getPreferences().addPreferenceChangeListener(idePCL); 534 } 535 catch (Exception ex) { 536 Exceptions.printStackTrace(ex); 537 } 538 } 539 } 540 } 541 | Popular Tags |