KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > NonGui


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core;
21
22 import java.awt.EventQueue JavaDoc;
23 import java.awt.Frame JavaDoc;
24 import java.awt.Toolkit JavaDoc;
25 import java.awt.event.ComponentAdapter JavaDoc;
26 import java.awt.event.ComponentEvent JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import javax.swing.SwingUtilities JavaDoc;
32 import javax.swing.Timer JavaDoc;
33 import org.netbeans.TopSecurityManager;
34 import org.netbeans.core.startup.InstalledFileLocatorImpl;
35 import org.netbeans.core.startup.Main;
36 import org.netbeans.core.startup.ModuleSystem;
37 import org.netbeans.core.startup.Splash;
38 import org.netbeans.core.startup.StartLog;
39 import org.openide.awt.StatusDisplayer;
40 import org.openide.util.Lookup;
41 import org.openide.util.NbBundle;
42 import org.openide.windows.WindowManager;
43
44 /**
45  * Most of the NetBeans startup logic that is not closely tied to the GUI.
46  * The meat of the startup sequence is in {@link #run}.
47  */

48 public class NonGui extends NbTopManager
49 implements Runnable JavaDoc, org.netbeans.core.startup.RunLevel {
50     private static int count;
51     
52     public NonGui () {
53         assert count++ == 0 : "Only one instance allowed"; // NOI18N
54
}
55     
56     /** Everything is interactive */
57     public boolean isInteractive (int il) {
58         return true;
59     }
60
61     /** Initialization of the manager.
62     */

63     public void run () {
64         // -----------------------------------------------------------------------------------------------------
65
// 10. Loader pool loading
66
try {
67             LoaderPoolNode.load();
68         } catch (IOException JavaDoc ioe) {
69             Logger.getLogger(NonGui.class.getName()).log(Level.WARNING, null, ioe);
70         }
71         StartLog.logProgress ("LoaderPool loaded"); // NOI18N
72
Splash.getInstance().increment(10);
73
74         LoaderPoolNode.installationFinished ();
75         StartLog.logProgress ("LoaderPool notified"); // NOI18N
76
Splash.getInstance().increment(10);
77
78         // install java.net.ProxySelector
79
java.net.ProxySelector.setDefault (new NbProxySelector ());
80         
81         //---------------------------------------------------------------------------------------------------------
82
// initialize main window AFTER the setup wizard is finished
83

84         initializeMainWindow ();
85         StartLog.logProgress ("Main window initialized"); // NOI18N
86
Splash.getInstance().increment(1);
87
88         // -----------------------------------------------------------------------------------------------------
89
// 8. Advance Policy
90

91         // set security manager
92
SecurityManager JavaDoc secman = new TopSecurityManager();
93
94         System.setSecurityManager(secman);
95         TopSecurityManager.makeSwingUseSpecialClipboard(Lookup.getDefault().lookup(org.openide.util.datatransfer.ExClipboard.class));
96
97         // install java.net.Authenticator
98
java.net.Authenticator.setDefault (new NbAuthenticator ());
99         
100         StartLog.logProgress ("Security managers installed"); // NOI18N
101
Splash.getInstance().increment(1);
102         
103         
104         InstalledFileLocatorImpl.discardCache();
105     }
106
107
108     /** Method to initialize the main window.
109     */

110     protected void initializeMainWindow () {
111         if (!org.netbeans.core.startup.CLIOptions.isGui ()) {
112             return;
113         }
114         
115         StartLog.logStart ("Main window initialization"); //NOI18N
116

117         // #28536: make sure a JRE bug does not prevent the event queue from having
118
// the right context class loader
119
// and #35470: do it early, before any module-loaded AWT code might run
120
// and #36820: even that isn't always early enough, so we need to push
121
// a new EQ to enforce the context loader
122
// XXX this is a hack!
123
try {
124             org.openide.util.Mutex.EVENT.writeAccess (new Runnable JavaDoc() {
125                 public void run() {
126                     Thread.currentThread().setContextClassLoader(Main.getModuleSystem().getManager().getClassLoader());
127                     Toolkit.getDefaultToolkit().getSystemEventQueue().push(new EventQueue JavaDoc());
128                 }
129             });
130         } catch (Exception JavaDoc e) {
131             e.printStackTrace();
132         }
133
134         // -----------------------------------------------------------------------------------------------------
135
// 11. Initialization of main window
136
StatusDisplayer.getDefault().setStatusText (NbBundle.getMessage (NonGui.class, "MSG_MainWindowInit"));
137
138         // force to initialize timer
139
// sometimes happened that the timer thread was initialized under
140
// a TaskThreadGroup
141
// such task never ends or, if killed, timer is over
142
Timer JavaDoc timerInit = new Timer JavaDoc(0, new java.awt.event.ActionListener JavaDoc() {
143               public void actionPerformed(java.awt.event.ActionEvent JavaDoc ev) { }
144         });
145         timerInit.setRepeats(false);
146         timerInit.start();
147         Splash.getInstance().increment(10);
148         StartLog.logProgress ("Timer initialized"); // NOI18N
149

150         // -----------------------------------------------------------------------------------------------------
151
// 13. Initialize Shortcuts
152
ShortcutsFolder.initShortcuts();
153         Splash.getInstance().increment(1);
154         StartLog.logProgress ("Shortcuts initialized"); // NOI18N
155

156
157     // -----------------------------------------------------------------------------------------------------
158
// 14. Open main window
159
StatusDisplayer.getDefault().setStatusText (NbBundle.getMessage (NonGui.class, "MSG_WindowShowInit"));
160
161         // Starts GUI components to be created and shown on screen.
162
// I.e. main window + current workspace components.
163

164         // Access winsys from AWT thread only. In this case main thread wouldn't harm, just to be kosher.
165
SwingUtilities.invokeLater(new Runnable JavaDoc() {
166
167                                        public void run() {
168                                            StartLog.logProgress("Window system initialization");
169                                            if (System.getProperty("netbeans.warmup.skip") ==
170                                                null &&
171                                                System.getProperty("netbeans.close") ==
172                                                null) {
173                                                final Frame JavaDoc mainWindow = WindowManager.getDefault().getMainWindow();
174
175                                                mainWindow.addComponentListener(new ComponentAdapter JavaDoc() {
176
177                                                                                    public void componentShown(ComponentEvent JavaDoc evt) {
178                                                                                        mainWindow.removeComponentListener(this);
179                                                                                        WarmUpSupport.warmUp();
180                                                                                    }
181                                                                                });
182                                            }
183                                            NbTopManager.WindowSystem windowSystem = (NbTopManager.WindowSystem) Lookup.getDefault().lookup(NbTopManager.WindowSystem.class);
184
185                                            if (windowSystem != null) {
186                                                windowSystem.load();
187                                                StartLog.logProgress("Window system loaded");
188                                                if (StartLog.willLog()) {
189                                                    waitForMainWindowPaint();
190                                                }
191                                                windowSystem.show();
192                                            } else {
193                                                Logger.getLogger(NonGui.class.getName()).log(Level.WARNING,
194                                                                  null,
195                                                                  new NullPointerException JavaDoc("\n\n\nWindowSystem is not supplied!!!\\n\n"));
196                                            }
197                                            StartLog.logProgress("Window system shown");
198                                            if (!StartLog.willLog()) {
199                                                maybeDie(null);
200                                            }
201                                        }
202                                    });
203         StartLog.logEnd ("Main window initialization"); //NOI18N
204
}
205   
206     private static void waitForMainWindowPaint() {
207         // Waits for notification about processed paint event for main window
208
// require modified java.awt.EventQueue to run succesfully
209
Runnable JavaDoc r = new Runnable JavaDoc() {
210           public void run() {
211               try {
212                   Class JavaDoc clz = Class.forName("org.netbeans.performance.test.guitracker.LoggingRepaintManager"); // NOI18N
213
Method JavaDoc m = clz.getMethod("measureStartup", new Class JavaDoc[] {}); // NOI18N
214
Object JavaDoc o = m.invoke(null);
215                   endOfStartupMeasuring(o);
216               } catch (ClassNotFoundException JavaDoc e) {
217                   StartLog.logProgress(e.toString());
218               } catch (NoSuchMethodException JavaDoc e) {
219                   StartLog.logProgress(e.toString());
220         // } catch (InterruptedException e) {
221
// StartLog.logProgress(e.toString());
222
} catch (IllegalAccessException JavaDoc e) {
223                   StartLog.logProgress(e.toString());
224               } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
225                   StartLog.logProgress(e.toString());
226               }
227           }
228         };
229         new Thread JavaDoc(r).start();
230     }
231     private static void endOfStartupMeasuring(Object JavaDoc o) {
232       StartLog.logProgress("Startup memory and time measured"); // NOI18N
233
maybeDie(o);
234     }
235
236     private static void maybeDie(Object JavaDoc o) {
237         // finish starting
238
if (System.getProperty("netbeans.kill") != null) {
239             org.netbeans.TopSecurityManager.exit(5);
240         }
241
242         // close IDE
243
if (System.getProperty("netbeans.close") != null) {
244             if (Boolean.getBoolean("netbeans.warm.close")) {
245                 // Do other stuff related to startup, to measure the effect.
246
// Useful for performance testing.
247
new WarmUpSupport().run(); // synchronous
248
}
249             if(o!=null) StartLog.logMeasuredStartupTime(((Long JavaDoc)o).longValue());
250             org.openide.LifecycleManager.getDefault().exit();
251         }
252     }
253
254     /** Exits from the VM.
255     */

256     static void doExit (int code) {
257         TopSecurityManager.exit(code);
258     }
259
260     /** Get the module subsystem. */
261     public ModuleSystem getModuleSystem() {
262         return Main.getModuleSystem ();
263     }
264
265 }
266
Popular Tags