KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > core > JspEditorWarmUpTask


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.web.core;
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.EditorKit JavaDoc;
35 import javax.swing.text.View JavaDoc;
36 import org.netbeans.api.project.Project;
37 import org.netbeans.api.project.ui.OpenProjects;
38 import org.netbeans.editor.EditorUI;
39 import org.netbeans.editor.Utilities;
40 import org.netbeans.editor.Registry;
41 import org.netbeans.editor.view.spi.EstimatedSpanView;
42 import org.netbeans.editor.view.spi.LockView;
43 import org.netbeans.modules.web.spi.webmodule.WebModuleImplementation;
44 import org.openide.ErrorManager;
45 import org.openide.util.RequestProcessor;
46
47 /**
48  * "Warm-up" task for editor. Executed after IDE startup, it should
49  * pre-initialize some suitable parts of the module to improve first time usage
50  * experience - which might suffer from long response time due to class loading
51  * and various initialization.
52  * See {@link org.netbeans.core.AfterStartWarmUp} for details about how the task is run.
53  *
54  * @author Tomas Pavek, Marek Fukala
55  */

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

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

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

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

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

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

94     private static final int STATUS_INIT = 0;
95     private static final int STATUS_CREATE_PANE = 1;
96     private static final int STATUS_CREATE_DOCUMENTS = 2;
97     private static final int STATUS_SWITCH_DOCUMENTS = 3;
98     private static final int STATUS_TRAVERSE_VIEWS = 4;
99     private static final int STATUS_RENDER_FRAME = 5;
100     
101     private int status = STATUS_INIT;
102
103     private JEditorPane JavaDoc pane;
104     private JFrame JavaDoc frame;
105     private Document JavaDoc emptyDoc;
106     private Document JavaDoc longDoc;
107     private Graphics JavaDoc bGraphics;
108     
109     private EditorKit JavaDoc jspKit;
110
111     private long startTime;
112     
113     //signals whether the warmuptask has already been performed
114
public static boolean ALREADY_RUN = false;
115     
116     public void run() {
117         switch (status) {
118             case STATUS_INIT:
119                 //test whether a WebProject is opened
120
if(!isWebProjectOpened()) return ;
121         
122                 if (debug) {
123                     startTime = System.currentTimeMillis();
124                 }
125         
126                 // Start of a code block that tries to force hotspot to compile
127
// the view hierarchy and related classes for faster performance
128
Iterator JavaDoc componentIterator = Registry.getComponentIterator();
129                 if (!componentIterator.hasNext()) { // no components opened yet
130
status = STATUS_CREATE_PANE;
131                     SwingUtilities.invokeLater(this); // must run in AWT
132
} // otherwise stop because editor pane(s) already opened (optimized)
133
break;
134                 
135             case STATUS_CREATE_PANE: // now create editor component and assign a kit to it
136
assert SwingUtilities.isEventDispatchThread(); // This part must run in AWT
137

138                 // Init of JSPKit and JSPOptions
139
jspKit = JEditorPane.createEditorKitForContentType("text/x-jsp"); //NOI18N
140

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

300                 if (debug) {
301                     System.out.println("View hierarchy initialized: " // NOI18N
302
+ (System.currentTimeMillis()-startTime));
303                     startTime = System.currentTimeMillis();
304                 }
305                 break;
306             default:
307                 throw new IllegalStateException JavaDoc();
308         }
309     }
310     
311     private static boolean isWebProjectOpened() {
312         //init jasper for all opened projects
313
Project[] openedProjects = OpenProjects.getDefault().getOpenProjects();
314         for (int i = 0; i < openedProjects.length; i++) {
315             WebModuleImplementation wmImpl = (WebModuleImplementation)openedProjects[i].getLookup().lookup(WebModuleImplementation.class);
316             if(wmImpl != null) return true;
317         }
318         return false;
319     }
320     
321 }
322
Popular Tags