KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > lexer > editorbridge > LexerEditorKit


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.lexer.editorbridge;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import javax.swing.Action JavaDoc;
24 import javax.swing.JEditorPane JavaDoc;
25 import javax.swing.plaf.TextUI JavaDoc;
26 import javax.swing.text.Document JavaDoc;
27 import javax.swing.text.JTextComponent JavaDoc;
28 import javax.swing.text.TextAction JavaDoc;
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 JavaDoc tokenListDebugAction = "token-list-debug";
43     
44     public static final String JavaDoc UNKNOWN_MIME_TYPE = "text/x-unknown";
45     
46     public static LexerEditorKit create(FileObject fo) {
47         String JavaDoc mimeType = UNKNOWN_MIME_TYPE;
48         // Get mime-type from fo's name
49
fo = fo.getParent(); // get parent folder
50
String JavaDoc path = fo.getPath();
51         // Strip initial "Editors/"
52
if (path.startsWith("Editors/")) {
53             mimeType = path.substring("Editors/".length());
54         }
55         return new LexerEditorKit(mimeType);
56     }
57     
58     private String JavaDoc mimeType;
59     
60     public LexerEditorKit(String JavaDoc mimeType) {
61         this.mimeType = mimeType;
62     }
63     
64     /**
65      * @deprecated LexerEditorKit should not be used in longterm; it should be eliminated during 6.0 development
66      */

67     public LexerEditorKit() {
68         // Compatibility constructor - should not
69
}
70     
71     public String JavaDoc getContentType() {
72         return mimeType;
73     }
74
75     public void install(JEditorPane JavaDoc pane) {
76         super.install(pane);
77
78         TextUI JavaDoc 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 JavaDoc doc) {
85         return new PlainSyntax();
86     }
87
88     public SyntaxSupport createSyntaxSupport(BaseDocument doc) {
89         return new ExtSyntaxSupport(doc);
90     }
91
92     protected Action JavaDoc[] createActions() {
93         Action JavaDoc[] calcActions = new Action JavaDoc[] {
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 JavaDoc evt, final JTextComponent JavaDoc target) {
106             TokenHierarchy hi = TokenHierarchy.get(target.getDocument());
107             if (hi != null) {
108                 /*DEBUG*/System.err.println("Token list:\n" + hi.tokenSequence());
109             } else {
110                 /*DEBUG*/System.err.println("Token hierarchy is null.");
111             }
112         }
113     }
114
115     /**
116      * Possibly change the original coloring name.
117      */

118     protected String JavaDoc updateColoringName(String JavaDoc coloringName) {
119         return coloringName;
120     }
121
122 }
123
124
Popular Tags