KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > lexer > demo > EditorPaneDemo


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.demo;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.InputEvent JavaDoc;
24 import java.awt.event.KeyEvent JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileReader JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.InputStreamReader JavaDoc;
30 import java.io.Reader JavaDoc;
31 import javax.swing.JFrame JavaDoc;
32 import javax.swing.JEditorPane JavaDoc;
33 import javax.swing.KeyStroke JavaDoc;
34 import javax.swing.text.BadLocationException JavaDoc;
35 import javax.swing.text.Document JavaDoc;
36 import javax.swing.text.JTextComponent JavaDoc;
37 import javax.swing.text.PlainDocument JavaDoc;
38 import javax.swing.text.TextAction JavaDoc;
39 import javax.swing.event.DocumentListener JavaDoc;
40 import javax.swing.event.DocumentEvent JavaDoc;
41 import org.netbeans.api.lexer.Language;
42 import org.netbeans.api.lexer.TokenUpdater;
43
44 /**
45  * Example of using the lexer framework.
46  * <BR><CODE>createLexer()</CODE> can be overriden if necessary.
47  *
48  * @author Miloslav Metelka
49  * @version 1.00
50  */

51
52 public class EditorPaneDemo extends DemoTokenUpdater {
53     
54     private boolean elementChangeDump;
55
56     public EditorPaneDemo(Language language, boolean maintainLookbacks,
57     String JavaDoc initialContent) {
58
59         super(new PlainDocument JavaDoc(), language, maintainLookbacks);
60         
61         JFrame JavaDoc frame = new JFrame JavaDoc();
62         frame.addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
63             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
64                 System.exit(0);
65             }
66         });
67
68         frame.setTitle("Test of " + splitClassName(language.getClass().getName())[1]
69             + " - Use Ctrl+L to dump tokens");
70
71         JEditorPane JavaDoc jep = new JEditorPane JavaDoc();
72         
73         Document JavaDoc doc = getDocument();
74         jep.setDocument(doc);
75         // Insert initial content string
76
try {
77             if (initialContent != null) {
78                 doc.insertString(0, initialContent, null);
79             }
80         } catch (BadLocationException JavaDoc e) {
81             e.printStackTrace();
82             return;
83         }
84         
85         // Initially debug token changes
86
setDebugTokenChanges(true);
87
88         frame.getContentPane().add(jep);
89         
90         DumpAction da = new DumpAction();
91         jep.registerKeyboardAction(da,
92             KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK), 0);
93         
94         DebugTokenChangesAction dtca = new DebugTokenChangesAction();
95         jep.registerKeyboardAction(dtca,
96             KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_MASK), 0);
97         
98         
99         System.err.println("NOTE: Press Ctrl+L to dump the document's token list.\n");
100         System.err.println(" Press Ctrl+T to toggle debugging of token changes.\n");
101         
102         // Debug initially
103
dump();
104
105         frame.setSize(400, 300);
106         frame.setVisible(true);
107         
108     }
109     
110     private static String JavaDoc[] splitClassName(String JavaDoc classFullName) {
111         int lastDotIndex = classFullName.lastIndexOf('.');
112         return new String JavaDoc[] {
113             (lastDotIndex >= 0) ? classFullName.substring(0, lastDotIndex) : "", // pkg name
114
classFullName.substring(lastDotIndex + 1) // class name
115
};
116     }
117     
118     private void dump() {
119         System.err.println(allTokensToString());
120     }
121     
122     private class DumpAction extends TextAction JavaDoc {
123  
124         DumpAction() {
125             super("dump");
126         }
127         
128         public void actionPerformed(ActionEvent JavaDoc evt) {
129             dump();
130         }
131         
132     }
133     
134     private class DebugTokenChangesAction extends TextAction JavaDoc {
135         
136         DebugTokenChangesAction() {
137             super("debugTokenChanges");
138         }
139         
140         public void actionPerformed(ActionEvent JavaDoc evt) {
141             boolean debugTokenChanges = !getDebugTokenChanges();
142             setDebugTokenChanges(debugTokenChanges);
143             System.out.println("Debugging of token changes turned "
144                 + (debugTokenChanges ? "on" : "off")
145             );
146         }
147         
148     }
149     
150     /** Tests language by opening it in a editor.
151      */

152     public static void main (String JavaDoc[] args) {
153         try {
154             if (args.length == 0) {
155                 System.err.println("Usage: java " + EditorPaneDemo.class.getName ()
156                     + " <language-class-name> [file-to-load]");
157                 System.exit (1);
158             }
159
160             Class JavaDoc langCls = Class.forName (args[0]);
161
162             java.lang.reflect.Method JavaDoc m = langCls.getDeclaredMethod("get", new Class JavaDoc[0]);
163             Language language = (Language)m.invoke (null, new Object JavaDoc[0]);
164
165             String JavaDoc content = null;
166
167             if (args.length > 1) {
168                 String JavaDoc contentFileName = args[1];
169                 File JavaDoc contentFile = new File JavaDoc(contentFileName);
170                 if (contentFile.exists()) {
171                     Reader JavaDoc reader = new FileReader JavaDoc(contentFile);
172                     char[] contentChars = new char[(int)contentFile.length() + 1];
173                     int totalReadCount = 0;
174                     while (true) {
175                         int readCount = reader.read(contentChars, totalReadCount,
176                             contentChars.length - totalReadCount);
177                         if (readCount == -1) { // no more chars
178
break;
179                         }
180                         totalReadCount += readCount;
181                     }
182
183                     content = new String JavaDoc(contentChars, 0, totalReadCount);
184
185                 } else { // content file does not exist
186
System.err.println("Input file NOT FOUND:\n"
187                         + contentFile);
188                 }
189             }
190             
191             new EditorPaneDemo(language, false, content);
192
193         } catch (Exception JavaDoc ex) {
194             ex.printStackTrace();
195         }
196     }
197
198 }
199
200
Popular Tags