1 19 20 package org.netbeans.modules.lexer.editorbridge; 21 22 import java.awt.event.ActionEvent ; 23 import javax.swing.Action ; 24 import javax.swing.JEditorPane ; 25 import javax.swing.plaf.TextUI ; 26 import javax.swing.text.Document ; 27 import javax.swing.text.JTextComponent ; 28 import javax.swing.text.TextAction ; 29 import org.netbeans.api.lexer.TokenHierarchy; 30 import org.netbeans.editor.BaseAction; 31 import org.netbeans.editor.BaseDocument; 32 import org.netbeans.editor.BaseTextUI; 33 import org.netbeans.editor.Syntax; 34 import org.netbeans.editor.SyntaxSupport; 35 import org.netbeans.editor.ext.ExtSyntaxSupport; 36 import org.netbeans.editor.ext.plain.PlainSyntax; 37 import org.netbeans.modules.editor.NbEditorKit; 38 import org.openide.filesystems.FileObject; 39 40 public class LexerEditorKit extends NbEditorKit { 41 42 public static final String tokenListDebugAction = "token-list-debug"; 43 44 public static final String UNKNOWN_MIME_TYPE = "text/x-unknown"; 45 46 public static LexerEditorKit create(FileObject fo) { 47 String mimeType = UNKNOWN_MIME_TYPE; 48 fo = fo.getParent(); String path = fo.getPath(); 51 if (path.startsWith("Editors/")) { 53 mimeType = path.substring("Editors/".length()); 54 } 55 return new LexerEditorKit(mimeType); 56 } 57 58 private String mimeType; 59 60 public LexerEditorKit(String mimeType) { 61 this.mimeType = mimeType; 62 } 63 64 67 public LexerEditorKit() { 68 } 70 71 public String getContentType() { 72 return mimeType; 73 } 74 75 public void install(JEditorPane pane) { 76 super.install(pane); 77 78 TextUI ui = pane.getUI(); 79 if (ui instanceof BaseTextUI) { 80 ((BaseTextUI)ui).getEditorUI().addLayer(new LexerLayer(pane), LexerLayer.VISIBILITY); 81 } 82 } 83 84 public Syntax createSyntax(Document doc) { 85 return new PlainSyntax(); 86 } 87 88 public SyntaxSupport createSyntaxSupport(BaseDocument doc) { 89 return new ExtSyntaxSupport(doc); 90 } 91 92 protected Action [] createActions() { 93 Action [] calcActions = new Action [] { 94 new TokenListDebugAction(), 95 }; 96 return TextAction.augmentList(super.createActions(), calcActions); 97 } 98 99 static class TokenListDebugAction extends BaseAction { 100 101 public TokenListDebugAction() { 102 super(tokenListDebugAction, 0); 103 } 104 105 public void actionPerformed(final ActionEvent evt, final JTextComponent target) { 106 TokenHierarchy hi = TokenHierarchy.get(target.getDocument()); 107 if (hi != null) { 108 System.err.println("Token list:\n" + hi.tokenSequence()); 109 } else { 110 System.err.println("Token hierarchy is null."); 111 } 112 } 113 } 114 115 118 protected String updateColoringName(String coloringName) { 119 return coloringName; 120 } 121 122 } 123 124 | Popular Tags |