1 package org.netbeans.modules.gsf.tools.lexing; 2 3 import java.io.Serializable ; 4 import javax.swing.JEditorPane ; 5 import javax.swing.event.CaretEvent ; 6 import javax.swing.event.CaretListener ; 7 import javax.swing.text.Document ; 8 import org.netbeans.api.lexer.Language; 9 import org.netbeans.api.lexer.Token; 10 import org.netbeans.api.lexer.TokenHierarchy; 11 import org.netbeans.api.lexer.TokenId; 12 import org.netbeans.api.lexer.TokenSequence; 13 import org.netbeans.modules.editor.NbEditorDocument; 14 import org.openide.ErrorManager; 15 import org.openide.cookies.EditorCookie; 16 import org.openide.loaders.DataObject; 17 import org.openide.nodes.Node; 18 import org.openide.util.NbBundle; 19 import org.openide.windows.TopComponent; 20 import org.openide.windows.WindowManager; 21 23 26 final class TokenSpyTopComponent extends TopComponent { 27 private CaretListener caretListener; 28 private JEditorPane lastPane; 29 private Document lastDocument; 30 31 private static TokenSpyTopComponent instance; 32 33 35 private static final String PREFERRED_ID = "TokenSpyTopComponent"; 36 37 private TokenSpyTopComponent() { 38 initComponents(); 39 setName(NbBundle.getMessage(TokenSpyTopComponent.class, "CTL_TokenSpyTopComponent")); 40 setToolTipText(NbBundle.getMessage(TokenSpyTopComponent.class, "HINT_TokenSpyTopComponent")); 41 } 43 44 49 private void initComponents() { 51 52 jLabel1 = new javax.swing.JLabel (); 53 idLabel = new javax.swing.JLabel (); 54 jLabel3 = new javax.swing.JLabel (); 55 offsetLabel = new javax.swing.JLabel (); 56 jLabel5 = new javax.swing.JLabel (); 57 lengthLabel = new javax.swing.JLabel (); 58 jLabel7 = new javax.swing.JLabel (); 59 textLabel = new javax.swing.JLabel (); 60 jLabel9 = new javax.swing.JLabel (); 61 languageLabel = new javax.swing.JLabel (); 62 jLabel4 = new javax.swing.JLabel (); 63 parentId = new javax.swing.JLabel (); 64 parentOffset = new javax.swing.JLabel (); 65 jLabel10 = new javax.swing.JLabel (); 66 parentLength = new javax.swing.JLabel (); 67 jLabel12 = new javax.swing.JLabel (); 68 jLabel2 = new javax.swing.JLabel (); 69 parentText = new javax.swing.JLabel (); 70 parentLanguage = new javax.swing.JLabel (); 71 jLabel6 = new javax.swing.JLabel (); 72 jLabel8 = new javax.swing.JLabel (); 73 caretOffset = new javax.swing.JLabel (); 74 75 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, "Token Id:"); 76 77 org.openide.awt.Mnemonics.setLocalizedText(jLabel3, "Offset:"); 78 79 org.openide.awt.Mnemonics.setLocalizedText(jLabel5, "Length:"); 80 81 org.openide.awt.Mnemonics.setLocalizedText(jLabel7, "Text:"); 82 83 org.openide.awt.Mnemonics.setLocalizedText(jLabel9, "Language:"); 84 85 org.openide.awt.Mnemonics.setLocalizedText(jLabel4, "Embedded Offset:"); 86 87 org.openide.awt.Mnemonics.setLocalizedText(parentId, " "); 88 89 org.openide.awt.Mnemonics.setLocalizedText(parentOffset, " "); 90 91 org.openide.awt.Mnemonics.setLocalizedText(jLabel10, "Embedded Length:"); 92 93 org.openide.awt.Mnemonics.setLocalizedText(parentLength, " "); 94 95 org.openide.awt.Mnemonics.setLocalizedText(jLabel12, "Embedded Id:"); 96 97 org.openide.awt.Mnemonics.setLocalizedText(jLabel2, "Embedded Text:"); 98 99 org.openide.awt.Mnemonics.setLocalizedText(parentText, " "); 100 101 org.openide.awt.Mnemonics.setLocalizedText(parentLanguage, " "); 102 103 org.openide.awt.Mnemonics.setLocalizedText(jLabel6, "Embedded Language:"); 104 105 org.openide.awt.Mnemonics.setLocalizedText(jLabel8, "CARET OFFSET:"); 106 107 org.openide.awt.Mnemonics.setLocalizedText(caretOffset, " "); 108 109 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 110 this.setLayout(layout); 111 layout.setHorizontalGroup( 112 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 113 .add(layout.createSequentialGroup() 114 .addContainerGap() 115 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 116 .add(layout.createSequentialGroup() 117 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 118 .add(jLabel1) 119 .add(jLabel3) 120 .add(jLabel5) 121 .add(jLabel7) 122 .add(jLabel9)) 123 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) 124 .add(layout.createSequentialGroup() 125 .add(26, 26, 26) 126 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) 127 .add(layout.createSequentialGroup() 128 .add(12, 12, 12) 129 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) 130 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) 131 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) 132 .add(layout.createSequentialGroup() 133 .add(50, 50, 50) 134 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 135 .add(languageLabel) 136 .add(textLabel) 137 .add(lengthLabel) 138 .add(offsetLabel) 139 .add(idLabel)) 140 .addContainerGap(342, Short.MAX_VALUE)) 141 .add(layout.createSequentialGroup() 142 .add(jLabel2) 143 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 144 .add(parentText) 145 .addContainerGap())) 146 .add(layout.createSequentialGroup() 147 .add(jLabel4) 148 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 149 .add(parentOffset) 150 .addContainerGap())) 151 .add(layout.createSequentialGroup() 152 .add(jLabel12) 153 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 154 .add(parentId) 155 .addContainerGap()))) 156 .add(layout.createSequentialGroup() 157 .add(jLabel10) 158 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 159 .add(parentLength) 160 .addContainerGap()))) 161 .add(layout.createSequentialGroup() 162 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 163 .add(jLabel6) 164 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 165 .add(parentLanguage) 166 .addContainerGap()))) 167 .add(layout.createSequentialGroup() 168 .add(jLabel8) 169 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 170 .add(caretOffset) 171 .addContainerGap(379, Short.MAX_VALUE)))) 172 ); 173 layout.setVerticalGroup( 174 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 175 .add(layout.createSequentialGroup() 176 .addContainerGap() 177 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 178 .add(jLabel1) 179 .add(idLabel) 180 .add(parentId) 181 .add(jLabel12)) 182 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 183 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 184 .add(jLabel3) 185 .add(offsetLabel) 186 .add(parentOffset) 187 .add(jLabel4)) 188 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 189 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 190 .add(jLabel5) 191 .add(lengthLabel) 192 .add(parentLength) 193 .add(jLabel10)) 194 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 195 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 196 .add(jLabel7) 197 .add(textLabel) 198 .add(parentText) 199 .add(jLabel2)) 200 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 201 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 202 .add(jLabel9) 203 .add(languageLabel) 204 .add(parentLanguage) 205 .add(jLabel6)) 206 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 207 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 208 .add(jLabel8) 209 .add(caretOffset)) 210 .addContainerGap(144, Short.MAX_VALUE)) 211 ); 212 } 214 215 private javax.swing.JLabel caretOffset; 217 private javax.swing.JLabel idLabel; 218 private javax.swing.JLabel jLabel1; 219 private javax.swing.JLabel jLabel10; 220 private javax.swing.JLabel jLabel12; 221 private javax.swing.JLabel jLabel2; 222 private javax.swing.JLabel jLabel3; 223 private javax.swing.JLabel jLabel4; 224 private javax.swing.JLabel jLabel5; 225 private javax.swing.JLabel jLabel6; 226 private javax.swing.JLabel jLabel7; 227 private javax.swing.JLabel jLabel8; 228 private javax.swing.JLabel jLabel9; 229 private javax.swing.JLabel languageLabel; 230 private javax.swing.JLabel lengthLabel; 231 private javax.swing.JLabel offsetLabel; 232 private javax.swing.JLabel parentId; 233 private javax.swing.JLabel parentLanguage; 234 private javax.swing.JLabel parentLength; 235 private javax.swing.JLabel parentOffset; 236 private javax.swing.JLabel parentText; 237 private javax.swing.JLabel textLabel; 238 240 245 public static synchronized TokenSpyTopComponent getDefault() { 246 if (instance == null) { 247 instance = new TokenSpyTopComponent(); 248 } 249 return instance; 250 } 251 252 255 public static synchronized TokenSpyTopComponent findInstance() { 256 TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID); 257 if (win == null) { 258 ErrorManager.getDefault().log(ErrorManager.WARNING, 259 "Cannot find MyWindow component. It will not be located properly in the window system."); 260 return getDefault(); 261 } 262 if (win instanceof TokenSpyTopComponent) { 263 return (TokenSpyTopComponent)win; 264 } 265 ErrorManager.getDefault().log(ErrorManager.WARNING, 266 "There seem to be multiple components with the '" + PREFERRED_ID + 267 "' ID. That is a potential source of errors and unexpected behavior."); 268 return getDefault(); 269 } 270 271 public int getPersistenceType() { 272 return TopComponent.PERSISTENCE_NEVER; 273 } 274 275 public void componentOpened() { 276 } 277 278 public void componentClosed() { 279 } 280 281 @Override 282 public void componentShowing() { 283 Node[] ns = TopComponent.getRegistry().getActivatedNodes(); 284 285 if (ns.length != 1) { 286 return; 287 } 288 289 EditorCookie editorCookie = (EditorCookie)ns[0].getLookup().lookup(EditorCookie.class); 291 292 if (editorCookie == null) { 293 return; 294 } 295 296 if (editorCookie.getOpenedPanes() == null) { 297 return; 298 } 299 300 if (editorCookie.getOpenedPanes().length < 1) { 301 return; 302 } 303 304 JEditorPane pane = editorCookie.getOpenedPanes()[0]; 305 306 if (caretListener == null) { 307 caretListener = new CListener(); 308 } 309 310 if ((lastPane != null) && (lastPane != pane)) { 311 lastPane.removeCaretListener(caretListener); 312 lastPane = null; 313 lastDocument = null; 314 } 315 316 if (lastPane == null) { 317 pane.addCaretListener(caretListener); 318 lastPane = pane; 319 lastDocument = (NbEditorDocument)pane.getDocument(); 320 } 321 322 final Document doc = editorCookie.getDocument(); 323 324 if ((doc == null) || !(doc instanceof NbEditorDocument)) { 325 return; 326 } 327 } 328 329 @Override 330 public void componentHidden() { 331 if (lastPane != null) { 332 lastPane.removeCaretListener(caretListener); 333 lastPane = null; 334 lastDocument = null; 335 org.openide.awt.StatusDisplayer.getDefault().setStatusText(""); 336 } 337 } 338 339 340 public Object writeReplace() { 341 return new ResolvableHelper(); 342 } 343 344 protected String preferredID() { 345 return PREFERRED_ID; 346 } 347 348 final static class ResolvableHelper implements Serializable { 349 private static final long serialVersionUID = 1L; 350 public Object readResolve() { 351 return TokenSpyTopComponent.getDefault(); 352 } 353 } 354 355 private void show(int caret) { 356 TokenHierarchy th = null; 357 caretOffset.setText(Integer.toString(caret)); 358 if (lastDocument != null) { 359 th = TokenHierarchy.get(lastDocument); 360 TokenSequence ts = th.tokenSequence(); 361 ts.move(caret); 362 if (ts.moveNext() || ts.movePrevious()) { 363 Token token = ts.token(); 364 TokenId id = token.id(); 365 idLabel.setText(id.name() + " : " + id.primaryCategory()); 366 lengthLabel.setText(Integer.toString(token.length())); 367 String text = token.text().toString(); 368 textLabel.setText(text); 369 offsetLabel.setText(Integer.toString(ts.offset())); 370 languageLabel.setText(ts.language().toString()); 371 372 if (ts.embedded() != null) { 373 ts = ts.embedded(); 374 ts.move(caret); 375 if (ts.moveNext() || ts.movePrevious()) { 376 token = ts.token(); 377 id = token.id(); 378 parentId.setText(id.name() + " : " + id.primaryCategory()); 379 parentLength.setText(Integer.toString(token.length())); 380 text = token.text().toString(); 381 parentText.setText(text); 382 parentOffset.setText(Integer.toString(ts.offset())); 383 parentLanguage.setText(ts.language().toString()); 384 } 385 } else { 386 parentId.setText(""); 387 parentLength.setText(""); 388 parentOffset.setText(""); 389 parentText.setText(""); 390 parentLanguage.setText(""); 391 } 392 return; 393 } 394 } 395 idLabel.setText(""); 396 lengthLabel.setText(""); 397 offsetLabel.setText(""); 398 textLabel.setText(""); 399 languageLabel.setText(""); 400 401 parentId.setText(""); 402 parentLength.setText(""); 403 parentOffset.setText(""); 404 parentText.setText(""); 405 } 406 407 private class CListener implements CaretListener { 408 public void caretUpdate(CaretEvent ev) { 409 show(ev.getDot()); 410 } 411 } 412 413 } 414 | Popular Tags |