1 19 20 package org.netbeans.modules.java.editor; 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.View ; 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 54 55 public class JavaEditorWarmUpTask implements Runnable { 56 57 64 private static final int ARTIFICIAL_DOCUMENT_LINE_COUNT = 1510; 65 66 70 private static final int VIEW_HIERARCHY_CREATION_COUNT = 1; 71 72 75 private static final int IMAGE_WIDTH = 600; 76 77 80 private static final int IMAGE_HEIGHT = 400; 81 82 85 private static final int PAINT_COUNT = 1; 86 87 88 private static final boolean debug 89 = Boolean.getBoolean("netbeans.debug.editor.warmup"); 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 pane; 101 private JFrame frame; 102 private Document emptyDoc; 103 private Document longDoc; 104 private Graphics 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 javaKit = BaseKit.getKit(JavaKit.class); 119 120 javaKit.getActions(); 122 123 124 if (debug) { 127 System.out.println("Kit instances initialized: " + (System.currentTimeMillis()-startTime)); 129 startTime = System.currentTimeMillis(); 130 } 131 132 Iterator componentIterator = Registry.getComponentIterator(); 133 if (!componentIterator.hasNext()) { status = STATUS_CREATE_PANE; 135 SwingUtilities.invokeLater(this); } break; 138 139 case STATUS_CREATE_PANE: assert SwingUtilities.isEventDispatchThread(); 142 pane = new JEditorPane (); 143 pane.setEditorKit(javaKit); 144 145 EditorUI editorUI = Utilities.getEditorUI(pane); 147 if (editorUI != null) { 148 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 emptyDoc = javaKit.createDefaultDocument(); 162 longDoc = pane.getDocument(); 163 164 try { 165 StringBuffer sb = new StringBuffer (); 169 for (int i = ARTIFICIAL_DOCUMENT_LINE_COUNT; i > 0; i--) { 170 sb.append("int ident = 1; // comment\n"); } 172 longDoc.insertString(0, sb.toString(), null); 173 174 status = STATUS_SWITCH_DOCUMENTS; 175 SwingUtilities.invokeLater(this); 176 177 } catch (BadLocationException e) { 178 ErrorManager.getDefault().notify(e); 179 } 180 break; 181 182 case STATUS_SWITCH_DOCUMENTS: 183 for (int i = 0; i < VIEW_HIERARCHY_CREATION_COUNT; i++) { 186 pane.setDocument(emptyDoc); 187 188 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 BufferedImage bImage = new BufferedImage ( 200 IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB); 201 bGraphics = bImage.getGraphics(); 202 bGraphics.setClip(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT); 203 204 AbstractDocument doc = (AbstractDocument )pane.getDocument(); 206 doc.readLock(); 207 try { 208 final View rootView = Utilities.getDocumentView(pane); 209 LockView lockView = LockView.get(rootView); 210 lockView.lock(); 211 try { 212 int viewCount = rootView.getViewCount(); 213 214 Runnable resetChildrenEstimatedSpans = new Runnable () { 216 public void run() { 217 int cnt = rootView.getViewCount(); 218 for (int j = 0; j < cnt; j++) { 219 View 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 { resetChildrenEstimatedSpans.run(); 231 } 232 233 for (int j = 0; j < viewCount; j++) { 235 Rectangle alloc = new Rectangle (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 if (false) { float rootViewYSpan = rootView.getPreferredSpan(View.Y_AXIS); 245 float maybeLineSpan = rootViewYSpan / viewCount; 246 Point point = new Point (); 247 point.x = 5; 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 alloc = new Rectangle (0, 0, rootViewWidth, rootViewHeight); 259 260 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 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 (); 281 EditorUI ui = Utilities.getEditorUI(pane); 282 JComponent mainComp = null; 283 if (ui != null) { 284 mainComp = ui.getExtComponent(); 285 } 286 if (mainComp == null) { 287 mainComp = new javax.swing.JScrollPane (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 298 if (debug) { 299 System.out.println("View hierarchy initialized: " + (System.currentTimeMillis()-startTime)); 301 startTime = System.currentTimeMillis(); 302 } 303 break; 304 305 default: 306 throw new IllegalStateException (); 307 } 308 } 309 310 } 311 | Popular Tags |