1 19 20 package org.netbeans.editor.example; 21 22 import java.io.*; 23 import java.awt.event.KeyEvent ; 24 import java.awt.event.InputEvent ; 25 import java.awt.event.ActionEvent ; 26 import java.net.URL ; 27 import java.text.MessageFormat ; 28 29 import java.util.Map ; 30 import java.util.List ; 31 import java.util.ResourceBundle ; 32 import java.util.MissingResourceException ; 33 import javax.swing.KeyStroke ; 34 import javax.swing.JEditorPane ; 35 import javax.swing.JMenuItem ; 36 import javax.swing.Action ; 37 import javax.swing.text.Document ; 38 import javax.swing.text.JTextComponent ; 39 import javax.swing.text.TextAction ; 40 import javax.swing.text.BadLocationException ; 41 import org.netbeans.editor.*; 42 import org.netbeans.editor.ext.*; 43 import org.netbeans.editor.ext.java.*; 44 45 51 52 public class JavaKit extends ExtKit { 53 54 public static final String JAVA_MIME_TYPE = "text/x-java"; 56 private static final String [] getSetIsPrefixes = new String [] { 57 "get", "set", "is" }; 59 60 63 public static final String makeGetterAction = "make-getter"; 65 68 public static final String makeSetterAction = "make-setter"; 70 73 public static final String makeIsAction = "make-is"; 75 76 public static final String abbrevDebugLineAction = "abbrev-debug-line"; 78 static final long serialVersionUID =-5445829962533684922L; 79 80 static { 81 Settings.addInitializer( new JavaSettingsInitializer( JavaKit.class ) ); 82 Settings.addInitializer( new SaJavaSettingsInitializer() ); 83 Settings.reset(); 84 85 ResourceBundle settings = ResourceBundle.getBundle( "settings" ); String jcPath = null; 87 try { 88 jcPath = settings.getString( "Java_Completion" ); 89 } catch( MissingResourceException exc ) {} 90 91 if( jcPath != null ) { 92 URL skeleton = JavaKit.class.getResource("/" + jcPath + ".jcs"); 93 URL body = JavaKit.class.getResource("/" + jcPath + ".jcb"); 94 95 if (skeleton == null || body == null) { 96 System.err.println("Warning: Java parser databases not found. Ignoring."); 97 } else { 98 DAFileProvider provider = new DAFileProvider( 99 new URLAccessor(skeleton), 100 new URLAccessor(body) 101 ); 102 103 JCBaseFinder finder = new JCBaseFinder(); 104 105 finder.append( provider ); 106 JavaCompletion.setFinder( finder ); 107 } 108 } 109 } 110 111 public String getContentType() { 112 return JAVA_MIME_TYPE; 113 } 114 115 119 public Syntax createSyntax(Document doc) { 120 return new JavaSyntax(); 121 } 122 123 124 public SyntaxSupport createSyntaxSupport(BaseDocument doc) { 125 return new JavaSyntaxSupport(doc); 126 } 127 128 public Completion createCompletion(ExtEditorUI extEditorUI) { 129 return new JavaCompletion(extEditorUI); 130 } 131 132 133 public Formatter createFormatter() { 134 return new JavaFormatter(this.getClass()); 135 } 136 137 protected EditorUI createEditorUI() { 138 return new ExtEditorUI(); 139 } 140 141 protected void initDocument(BaseDocument doc) { 142 doc.addLayer(new JavaDrawLayerFactory.JavaLayer(), 143 JavaDrawLayerFactory.JAVA_LAYER_VISIBILITY); 144 doc.addDocumentListener(new JavaDrawLayerFactory.LParenWatcher()); 145 } 146 147 protected Action [] createActions() { 148 Action [] javaActions = new Action [] { 149 new JavaDefaultKeyTypedAction(), 150 new PrefixMakerAction(makeGetterAction, "get", getSetIsPrefixes), new PrefixMakerAction(makeSetterAction, "set", getSetIsPrefixes), new PrefixMakerAction(makeIsAction, "is", getSetIsPrefixes), new AbbrevDebugLineAction(), 154 }; 155 return TextAction.augmentList(super.createActions(), javaActions); 156 } 157 158 159 public static class JavaDefaultKeyTypedAction extends ExtDefaultKeyTypedAction { 160 161 protected void checkIndentHotChars(JTextComponent target, String typedText) { 162 boolean reindent = false; 163 164 BaseDocument doc = Utilities.getDocument(target); 165 int dotPos = target.getCaret().getDot(); 166 if (doc != null) { 167 170 if ("e".equals(typedText)) { try { 172 int fnw = Utilities.getRowFirstNonWhite(doc, dotPos); 173 if (fnw >= 0 && fnw + 4 == dotPos 174 && "else".equals(doc.getText(fnw, 4)) ) { 176 reindent = true; 177 } 178 } catch (BadLocationException e) { 179 } 180 181 } else if (":".equals(typedText)) { try { 183 int fnw = Utilities.getRowFirstNonWhite(doc, dotPos); 184 if (fnw >= 0 && fnw + 4 <= doc.getLength() 185 && "case".equals(doc.getText(fnw, 4)) ) { 187 reindent = true; 188 } 189 } catch (BadLocationException e) { 190 } 191 } 192 193 if (reindent) { 195 try { 196 Utilities.reformatLine(doc, dotPos); 197 } catch (BadLocationException e) { 198 } 199 } 200 } 201 202 super.checkIndentHotChars(target, typedText); 203 } 204 205 } 206 207 208 209 public static class AbbrevDebugLineAction extends BaseAction { 210 211 public AbbrevDebugLineAction() { 212 super(abbrevDebugLineAction); 213 } 214 215 public void actionPerformed(ActionEvent evt, JTextComponent target) { 216 if (target != null) { 217 if (!target.isEditable() || !target.isEnabled()) { 218 target.getToolkit().beep(); 219 return; 220 } 221 BaseDocument doc = (BaseDocument)target.getDocument(); 222 StringBuffer sb = new StringBuffer ("System.err.println(\""); File file = (File)doc.getProperty( "file" ); 224 if (file != null) { 225 sb.append( file.getAbsolutePath() ); 226 sb.append(':'); 227 } 228 try { 229 sb.append(Utilities.getLineOffset(doc, target.getCaret().getDot()) + 1); 230 } catch (BadLocationException e) { 231 } 232 sb.append(' '); 233 234 BaseKit kit = Utilities.getKit(target); 235 Action a = kit.getActionByName(BaseKit.insertContentAction); 236 if (a != null) { 237 Utilities.performAction( 238 a, 239 new ActionEvent (target, ActionEvent.ACTION_PERFORMED, sb.toString()), 240 target 241 ); 242 } 243 } 244 } 245 } 246 247 248 private static class SaJavaSettingsInitializer extends Settings.AbstractInitializer { 249 public SaJavaSettingsInitializer() { 250 super( "sa-java-settings-initializer" ); } 252 253 254 255 public void updateSettingsMap(Class kitClass, Map settingsMap) { 256 if (kitClass == JavaKit.class) { 257 SettingsUtil.updateListSetting(settingsMap, SettingsNames.KEY_BINDING_LIST, getJavaKeyBindings()); 258 } 259 260 } 261 262 public MultiKeyBinding[] getJavaKeyBindings() { 263 return new MultiKeyBinding[] { 264 new MultiKeyBinding( 265 new KeyStroke [] { 266 KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.ALT_MASK), 267 KeyStroke.getKeyStroke(KeyEvent.VK_G, 0) 268 }, 269 JavaKit.makeGetterAction 270 ), 271 new MultiKeyBinding( 272 new KeyStroke [] { 273 KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.ALT_MASK), 274 KeyStroke.getKeyStroke(KeyEvent.VK_S, 0) 275 }, 276 JavaKit.makeSetterAction 277 ), 278 new MultiKeyBinding( 279 new KeyStroke [] { 280 KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.ALT_MASK), 281 KeyStroke.getKeyStroke(KeyEvent.VK_I, 0) 282 }, 283 JavaKit.makeIsAction 284 ), 285 new MultiKeyBinding( 286 KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.ALT_MASK), 287 JavaKit.abbrevDebugLineAction 288 ) 289 }; 290 } 291 } 292 293 294 299 public static class URLAccessor implements DataAccessor { 300 301 URL url; 302 InputStream stream; 303 int streamOff; 304 int actOff; 305 306 public URLAccessor(URL url) { 307 this.url = url; 308 } 309 310 312 public void append(byte[] buffer, int off, int len) throws IOException { 313 throw new IllegalArgumentException ("read only!"); } 315 316 327 public void read(byte[] buffer, int off, int len) throws IOException { 328 InputStream str = getStream(actOff); 329 while (len > 0) { 330 int count = str.read(buffer, off, len); 331 streamOff += count; 332 off += count; 333 len -= count; 334 } 335 } 336 337 340 public void open(boolean requestWrite) throws IOException { 341 if(requestWrite) throw new IllegalArgumentException ("read only!"); } 343 344 345 public void close() throws IOException { 346 if (stream!=null) { 347 stream.close(); 348 stream = null; 349 } 350 } 351 352 358 public long getFilePointer() throws IOException { 359 return actOff; 360 } 361 362 363 public void resetFile() throws IOException { 364 throw new IllegalArgumentException ("read only!"); } 366 367 371 public void seek(long pos) throws IOException { 372 actOff = (int)pos; 373 } 374 375 376 private InputStream getStream(int off) throws IOException { 377 if (streamOff > off && stream != null) { 378 stream.close(); 379 stream = null; 380 } 381 382 if(stream == null) { 383 stream = url.openStream(); 384 streamOff = 0; 385 } 386 387 while (streamOff < off) { 388 long len = stream.skip(off - streamOff); 389 streamOff += (int)len; 390 if (len == 0) throw new IOException("EOF"); } 392 393 return stream; 394 } 395 396 public int getFileLength() { 397 try { 398 int l = url.openConnection().getContentLength(); 399 return l; 400 } catch (IOException e) { 401 return 0; 402 } 403 } 404 405 public String toString() { 406 return url.toString(); 407 } 408 } 409 } 410 | Popular Tags |