1 19 20 package org.netbeans.modules.web.core; 21 22 import java.awt.Graphics ; 23 import java.awt.Point ; 24 import java.awt.Rectangle ; 25 import java.awt.image.BufferedImage ; 26 import java.util.Iterator ; 27 import javax.swing.JComponent ; 28 import javax.swing.JEditorPane ; 29 import javax.swing.JFrame ; 30 import javax.swing.SwingUtilities ; 31 import javax.swing.text.AbstractDocument ; 32 import javax.swing.text.BadLocationException ; 33 import javax.swing.text.Document ; 34 import javax.swing.text.EditorKit ; 35 import javax.swing.text.View ; 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 56 57 public class JspEditorWarmUpTask implements Runnable { 58 59 66 private static final int ARTIFICIAL_DOCUMENT_LINE_COUNT = 1510; 67 68 72 private static final int VIEW_HIERARCHY_CREATION_COUNT = 1; 73 74 77 private static final int IMAGE_WIDTH = 600; 78 79 82 private static final int IMAGE_HEIGHT = 400; 83 84 87 private static final int PAINT_COUNT = 1; 88 89 90 private static final boolean debug 91 = Boolean.getBoolean("netbeans.debug.editor.warmup"); 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 pane; 104 private JFrame frame; 105 private Document emptyDoc; 106 private Document longDoc; 107 private Graphics bGraphics; 108 109 private EditorKit jspKit; 110 111 private long startTime; 112 113 public static boolean ALREADY_RUN = false; 115 116 public void run() { 117 switch (status) { 118 case STATUS_INIT: 119 if(!isWebProjectOpened()) return ; 121 122 if (debug) { 123 startTime = System.currentTimeMillis(); 124 } 125 126 Iterator componentIterator = Registry.getComponentIterator(); 129 if (!componentIterator.hasNext()) { status = STATUS_CREATE_PANE; 131 SwingUtilities.invokeLater(this); } break; 134 135 case STATUS_CREATE_PANE: assert SwingUtilities.isEventDispatchThread(); 138 jspKit = JEditorPane.createEditorKitForContentType("text/x-jsp"); 141 jspKit.getActions(); 143 144 pane = new JEditorPane (); 145 pane.setEditorKit(jspKit); 146 147 EditorUI editorUI = Utilities.getEditorUI(pane); 149 if (editorUI != null) { 150 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 emptyDoc = jspKit.createDefaultDocument(); 164 longDoc = pane.getDocument(); 165 166 try { 167 StringBuffer sb = new StringBuffer (); 171 for (int i = ARTIFICIAL_DOCUMENT_LINE_COUNT; i > 0; i--) { 172 sb.append("hello"); } 174 longDoc.insertString(0, sb.toString(), null); 175 176 status = STATUS_SWITCH_DOCUMENTS; 177 SwingUtilities.invokeLater(this); 178 179 } catch (BadLocationException e) { 180 ErrorManager.getDefault().notify(e); 181 } 182 break; 183 184 case STATUS_SWITCH_DOCUMENTS: 185 for (int i = 0; i < VIEW_HIERARCHY_CREATION_COUNT; i++) { 188 pane.setDocument(emptyDoc); 189 190 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 BufferedImage bImage = new BufferedImage ( 202 IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB); 203 bGraphics = bImage.getGraphics(); 204 bGraphics.setClip(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT); 205 206 AbstractDocument doc = (AbstractDocument )pane.getDocument(); 208 doc.readLock(); 209 try { 210 final View rootView = Utilities.getDocumentView(pane); 211 LockView lockView = LockView.get(rootView); 212 lockView.lock(); 213 try { 214 int viewCount = rootView.getViewCount(); 215 216 Runnable resetChildrenEstimatedSpans = new Runnable () { 218 public void run() { 219 int cnt = rootView.getViewCount(); 220 for (int j = 0; j < cnt; j++) { 221 View 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 { resetChildrenEstimatedSpans.run(); 233 } 234 235 for (int j = 0; j < viewCount; j++) { 237 Rectangle alloc = new Rectangle (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 if (false) { float rootViewYSpan = rootView.getPreferredSpan(View.Y_AXIS); 247 float maybeLineSpan = rootViewYSpan / viewCount; 248 Point point = new Point (); 249 point.x = 5; 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 alloc = new Rectangle (0, 0, rootViewWidth, rootViewHeight); 261 262 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 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 (); 283 EditorUI ui = Utilities.getEditorUI(pane); 284 JComponent mainComp = null; 285 if (ui != null) { 286 mainComp = ui.getExtComponent(); 287 } 288 if (mainComp == null) { 289 mainComp = new javax.swing.JScrollPane (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 300 if (debug) { 301 System.out.println("View hierarchy initialized: " + (System.currentTimeMillis()-startTime)); 303 startTime = System.currentTimeMillis(); 304 } 305 break; 306 default: 307 throw new IllegalStateException (); 308 } 309 } 310 311 private static boolean isWebProjectOpened() { 312 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 |