1 19 20 package org.netbeans.modules.lexer.demo; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.InputEvent ; 24 import java.awt.event.KeyEvent ; 25 import java.io.File ; 26 import java.io.FileReader ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.InputStreamReader ; 30 import java.io.Reader ; 31 import javax.swing.JFrame ; 32 import javax.swing.JEditorPane ; 33 import javax.swing.KeyStroke ; 34 import javax.swing.text.BadLocationException ; 35 import javax.swing.text.Document ; 36 import javax.swing.text.JTextComponent ; 37 import javax.swing.text.PlainDocument ; 38 import javax.swing.text.TextAction ; 39 import javax.swing.event.DocumentListener ; 40 import javax.swing.event.DocumentEvent ; 41 import org.netbeans.api.lexer.Language; 42 import org.netbeans.api.lexer.TokenUpdater; 43 44 51 52 public class EditorPaneDemo extends DemoTokenUpdater { 53 54 private boolean elementChangeDump; 55 56 public EditorPaneDemo(Language language, boolean maintainLookbacks, 57 String initialContent) { 58 59 super(new PlainDocument (), language, maintainLookbacks); 60 61 JFrame frame = new JFrame (); 62 frame.addWindowListener(new java.awt.event.WindowAdapter () { 63 public void windowClosing(java.awt.event.WindowEvent 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 jep = new JEditorPane (); 72 73 Document doc = getDocument(); 74 jep.setDocument(doc); 75 try { 77 if (initialContent != null) { 78 doc.insertString(0, initialContent, null); 79 } 80 } catch (BadLocationException e) { 81 e.printStackTrace(); 82 return; 83 } 84 85 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 dump(); 104 105 frame.setSize(400, 300); 106 frame.setVisible(true); 107 108 } 109 110 private static String [] splitClassName(String classFullName) { 111 int lastDotIndex = classFullName.lastIndexOf('.'); 112 return new String [] { 113 (lastDotIndex >= 0) ? classFullName.substring(0, lastDotIndex) : "", classFullName.substring(lastDotIndex + 1) }; 116 } 117 118 private void dump() { 119 System.err.println(allTokensToString()); 120 } 121 122 private class DumpAction extends TextAction { 123 124 DumpAction() { 125 super("dump"); 126 } 127 128 public void actionPerformed(ActionEvent evt) { 129 dump(); 130 } 131 132 } 133 134 private class DebugTokenChangesAction extends TextAction { 135 136 DebugTokenChangesAction() { 137 super("debugTokenChanges"); 138 } 139 140 public void actionPerformed(ActionEvent 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 152 public static void main (String [] 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 langCls = Class.forName (args[0]); 161 162 java.lang.reflect.Method m = langCls.getDeclaredMethod("get", new Class [0]); 163 Language language = (Language)m.invoke (null, new Object [0]); 164 165 String content = null; 166 167 if (args.length > 1) { 168 String contentFileName = args[1]; 169 File contentFile = new File (contentFileName); 170 if (contentFile.exists()) { 171 Reader reader = new FileReader (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) { break; 179 } 180 totalReadCount += readCount; 181 } 182 183 content = new String (contentChars, 0, totalReadCount); 184 185 } else { System.err.println("Input file NOT FOUND:\n" 187 + contentFile); 188 } 189 } 190 191 new EditorPaneDemo(language, false, content); 192 193 } catch (Exception ex) { 194 ex.printStackTrace(); 195 } 196 } 197 198 } 199 200 | Popular Tags |