KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > JavaEditorWarmUpTask


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.modules.java.editor;
21
22 import java.awt.Graphics JavaDoc;
23 import java.awt.Point JavaDoc;
24 import java.awt.Rectangle JavaDoc;
25 import java.awt.image.BufferedImage JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import javax.swing.JComponent JavaDoc;
28 import javax.swing.JEditorPane JavaDoc;
29 import javax.swing.JFrame JavaDoc;
30 import javax.swing.SwingUtilities JavaDoc;
31 import javax.swing.text.AbstractDocument JavaDoc;
32 import javax.swing.text.BadLocationException JavaDoc;
33 import javax.swing.text.Document JavaDoc;
34 import javax.swing.text.View JavaDoc;
35 import org.netbeans.editor.BaseKit;
36 import org.netbeans.editor.EditorUI;
37 import org.netbeans.editor.Utilities;
38 import org.netbeans.editor.Registry;
39 import org.netbeans.editor.view.spi.EstimatedSpanView;
40 import org.netbeans.editor.view.spi.LockView;
41 import org.netbeans.modules.editor.java.JavaKit;
42 import org.openide.ErrorManager;
43 import org.openide.util.RequestProcessor;
44
45 /**
46  * "Warm-up" task for editor. Executed after IDE startup, it should
47  * pre-initialize some suitable parts of the module to improve first time usage
48  * experience - which might suffer from long response time due to class loading
49  * and various initialization.
50  * See {@link org.netbeans.core.AfterStartWarmUp} for details about how the task is run.
51  *
52  * @author Tomas Pavek, Martin Roskanin
53  */

54
55 public class JavaEditorWarmUpTask implements Runnable JavaDoc{
56     
57     /**
58      * Number of lines that an artificial document
59      * for view hierarchy code optimization will have.
60      * <br>
61      * The default threshold for hotspot method compilation
62      * is 1500 invocations.
63      */

64     private static final int ARTIFICIAL_DOCUMENT_LINE_COUNT = 1510;
65
66     /**
67      * Number of times a long document is assigned to the editor pane
68      * which causes the view hierarchy for it to be (re)built.
69      */

70     private static final int VIEW_HIERARCHY_CREATION_COUNT = 1;
71     
72     /**
73      * Width of buffered image area.
74      */

75     private static final int IMAGE_WIDTH = 600;
76     
77     /**
78      * Height of buffered image area.
79      */

80     private static final int IMAGE_HEIGHT = 400;
81     
82     /**
83      * Number of paints to be simulated.
84      */

85     private static final int PAINT_COUNT = 1;
86     
87
88     private static final boolean debug
89         = Boolean.getBoolean("netbeans.debug.editor.warmup"); // NOI18N
90

91     private static final int STATUS_INIT = 0;
92     private static final int STATUS_CREATE_PANE = 1;
93     private static final int STATUS_CREATE_DOCUMENTS = 2;
94     private static final int STATUS_SWITCH_DOCUMENTS = 3;
95     private static final int STATUS_TRAVERSE_VIEWS = 4;
96     private static final int STATUS_RENDER_FRAME = 5;
97     
98     private int status = STATUS_INIT;
99
100     private JEditorPane JavaDoc pane;
101     private JFrame JavaDoc frame;
102     private Document JavaDoc emptyDoc;
103     private Document JavaDoc longDoc;
104     private Graphics JavaDoc bGraphics;
105     
106     private BaseKit javaKit;
107
108     private long startTime;
109     
110     public void run() {
111         switch (status) {
112             case STATUS_INIT:
113                 if (debug) {
114                     startTime = System.currentTimeMillis();
115                 }
116         
117                 // Init of JavaKit and JavaOptions
118
javaKit = BaseKit.getKit(JavaKit.class);
119         
120                 //creating actions instances
121
javaKit.getActions();
122         
123
124                 // Start of a code block that tries to force hotspot to compile
125
// the view hierarchy and related classes for faster performance
126
if (debug) {
127                     System.out.println("Kit instances initialized: " // NOI18N
128
+ (System.currentTimeMillis()-startTime));
129                     startTime = System.currentTimeMillis();
130                 }
131
132                 Iterator JavaDoc componentIterator = Registry.getComponentIterator();
133                 if (!componentIterator.hasNext()) { // no components opened yet
134
status = STATUS_CREATE_PANE;
135                     SwingUtilities.invokeLater(this); // must run in AWT
136
} // otherwise stop because editor pane(s) already opened (optimized)
137
break;
138                 
139             case STATUS_CREATE_PANE: // now create editor component and assign a kit to it
140
assert SwingUtilities.isEventDispatchThread(); // This part must run in AWT
141

142                 pane = new JEditorPane JavaDoc();
143                 pane.setEditorKit(javaKit);
144
145                 // Obtain extended component (with editor's toolbar and scrollpane)
146
EditorUI editorUI = Utilities.getEditorUI(pane);
147                 if (editorUI != null) {
148                     // Make sure extended component necessary classes get loaded
149
editorUI.getExtComponent();
150                 }
151
152                 Registry.removeComponent(pane);
153
154                 status = STATUS_CREATE_DOCUMENTS;
155                 RequestProcessor.getDefault().post(this);
156                 break;
157                 
158             case STATUS_CREATE_DOCUMENTS:
159
160                 // Have two documents - one empty and another one filled with many lines
161
emptyDoc = javaKit.createDefaultDocument();
162                 longDoc = pane.getDocument();
163
164                 try {
165                     // Fill the document with data.
166
// Number of lines is more important here than number of columns in a line
167
// Do one big insert instead of many small inserts
168
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
169                     for (int i = ARTIFICIAL_DOCUMENT_LINE_COUNT; i > 0; i--) {
170                         sb.append("int ident = 1; // comment\n"); // NOI18N
171
}
172                     longDoc.insertString(0, sb.toString(), null);
173
174                     status = STATUS_SWITCH_DOCUMENTS;
175                     SwingUtilities.invokeLater(this);
176
177                 } catch (BadLocationException JavaDoc e) {
178                     ErrorManager.getDefault().notify(e);
179                 }
180                 break;
181
182             case STATUS_SWITCH_DOCUMENTS:
183                 // Switch between empty doc and long several times
184
// to force view hierarchy creation
185
for (int i = 0; i < VIEW_HIERARCHY_CREATION_COUNT; i++) {
186                     pane.setDocument(emptyDoc);
187
188                     // Set long doc - causes view hierarchy to be rebuilt
189
pane.setDocument(longDoc);
190                 }
191                 
192                 status = STATUS_TRAVERSE_VIEWS;
193                 RequestProcessor.getDefault().post(this);
194                 break;
195                 
196             case STATUS_TRAVERSE_VIEWS:
197                 try {
198                     // Create buffered image for painting simulation
199
BufferedImage JavaDoc bImage = new BufferedImage JavaDoc(
200                         IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
201                     bGraphics = bImage.getGraphics();
202                     bGraphics.setClip(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
203
204                     // Do view-related operations
205
AbstractDocument JavaDoc doc = (AbstractDocument JavaDoc)pane.getDocument();
206                     doc.readLock();
207                     try {
208                         final View JavaDoc rootView = Utilities.getDocumentView(pane);
209                         LockView lockView = LockView.get(rootView);
210                         lockView.lock();
211                         try {
212                             int viewCount = rootView.getViewCount();
213
214                             // Force switch the line views from estimated spans to exact measurements
215
Runnable JavaDoc resetChildrenEstimatedSpans = new Runnable JavaDoc() {
216                                 public void run() {
217                                     int cnt = rootView.getViewCount();
218                                     for (int j = 0; j < cnt; j++) {
219                                         View JavaDoc v = rootView.getView(j);
220                                         if (v instanceof EstimatedSpanView) {
221                                             ((EstimatedSpanView)v).setEstimatedSpan(false);
222                                         }
223                                     }
224                                 }
225                             };
226                             if (rootView instanceof org.netbeans.lib.editor.view.GapDocumentView) {
227                                 ((org.netbeans.lib.editor.view.GapDocumentView)rootView).
228                                     renderWithUpdateLayout(resetChildrenEstimatedSpans);
229                             } else { // not specialized instance => run normally
230
resetChildrenEstimatedSpans.run();
231                             }
232
233                             // Get child allocation for each line
234
for (int j = 0; j < viewCount; j++) {
235                                 Rectangle JavaDoc alloc = new Rectangle JavaDoc(0, 0,
236                                     (int)rootView.getPreferredSpan(View.X_AXIS),
237                                     (int)rootView.getPreferredSpan(View.Y_AXIS)
238                                 );
239                                 rootView.getChildAllocation(j, alloc);
240                             }
241
242                             // Test modelToView and viewToModel
243
if (false) { // Disabled because of #
244
float rootViewYSpan = rootView.getPreferredSpan(View.Y_AXIS);
245                                 float maybeLineSpan = rootViewYSpan / viewCount;
246                                 Point JavaDoc point = new Point JavaDoc();
247                                 point.x = 5; // likely somewhere inside the first char on the line
248
for (int j = 0; j < viewCount; j++) {
249                                     pane.modelToView(rootView.getView(j).getStartOffset());
250
251                                     point.y = (int)(j * maybeLineSpan);
252                                     int pos = pane.viewToModel(point);
253                                 }
254                             }
255
256                             int rootViewWidth = (int)rootView.getPreferredSpan(View.X_AXIS);
257                             int rootViewHeight = (int)rootView.getPreferredSpan(View.Y_AXIS);
258                             Rectangle JavaDoc alloc = new Rectangle JavaDoc(0, 0, rootViewWidth, rootViewHeight);
259
260                             // Paint into buffered image
261
for (int i = PAINT_COUNT - 1; i >= 0; i--) {
262                                 rootView.paint(bGraphics, alloc);
263                             }
264
265                         } finally {
266                             lockView.unlock();
267                         }
268                     } finally {
269                         doc.readUnlock();
270                     }
271                 } catch (BadLocationException JavaDoc e) {
272                     ErrorManager.getDefault().notify(e);
273                 }
274                     
275                 status = STATUS_RENDER_FRAME;
276                 SwingUtilities.invokeLater(this);
277                 break;
278
279             case STATUS_RENDER_FRAME:
280                 frame = new JFrame JavaDoc();
281                 EditorUI ui = Utilities.getEditorUI(pane);
282                 JComponent JavaDoc mainComp = null;
283                 if (ui != null) {
284                     mainComp = ui.getExtComponent();
285                 }
286                 if (mainComp == null) {
287                     mainComp = new javax.swing.JScrollPane JavaDoc(pane);
288                 }
289                 frame.getContentPane().add(mainComp);
290                 frame.pack();
291                 frame.paint(bGraphics);
292                 frame.getContentPane().removeAll();
293                 frame.dispose();
294                 pane.setEditorKit(null);
295
296                 // Candidates Annotations.getLineAnnotations()
297

298                 if (debug) {
299                     System.out.println("View hierarchy initialized: " // NOI18N
300
+ (System.currentTimeMillis()-startTime));
301                     startTime = System.currentTimeMillis();
302                 }
303                 break;
304                 
305             default:
306                 throw new IllegalStateException JavaDoc();
307         }
308     }
309     
310 }
311
Popular Tags