1 19 20 package org.netbeans.lib.lexer.inc; 21 22 import javax.swing.event.DocumentEvent ; 23 import javax.swing.event.DocumentListener ; 24 import javax.swing.text.Document ; 25 import org.netbeans.api.lexer.InputAttributes; 26 import org.netbeans.api.lexer.Language; 27 import org.netbeans.api.lexer.TokenId; 28 import org.netbeans.lib.editor.util.swing.DocumentListenerPriority; 29 import org.netbeans.lib.editor.util.swing.DocumentUtilities; 30 import org.netbeans.lib.lexer.LanguageManager; 31 import org.netbeans.spi.lexer.*; 32 33 45 46 public final class DocumentInput<D extends Document > 47 extends MutableTextInput<D> implements DocumentListener { 48 49 private static final String PROP_MIME_TYPE = "mimeType"; 51 public static <D extends Document > DocumentInput<D> get(D doc) { 52 @SuppressWarnings ("unchecked") 53 DocumentInput<D> di = (DocumentInput<D>)doc.getProperty(MutableTextInput.class); 54 if (di == null) { 55 di = new DocumentInput<D>(doc); 56 doc.putProperty(MutableTextInput.class, di); 57 } 58 return di; 59 } 60 61 private D doc; 62 63 private LanguageHierarchy languageHierarchy; 64 65 private CharSequence text; 66 67 public DocumentInput(D doc) { 68 this.doc = doc; 69 this.text = DocumentUtilities.getText(doc); 70 DocumentUtilities.addDocumentListener(doc, this, DocumentListenerPriority.LEXER); 72 } 73 74 public Language<? extends TokenId> language() { 75 Language<? extends TokenId> lang = (Language<? extends TokenId>) 76 doc.getProperty(Language.class); 77 78 if (lang == null) { 79 String mimeType = (String ) doc.getProperty(PROP_MIME_TYPE); 80 if (mimeType != null) { 81 lang = LanguageManager.getInstance().findLanguage(mimeType); 82 } 83 } 84 85 return lang; 86 } 87 88 public CharSequence text() { 89 return text; 90 } 91 92 public InputAttributes inputAttributes() { 93 return (InputAttributes)doc.getProperty(InputAttributes.class); 94 } 95 96 public D inputSource() { 97 return doc; 98 } 99 100 public void changedUpdate(DocumentEvent e) { 101 } 102 103 public void insertUpdate(DocumentEvent e) { 104 modified(true, e); 105 } 106 107 public void removeUpdate(DocumentEvent e) { 108 modified(false, e); 109 } 110 111 private void modified(boolean insert, DocumentEvent e) { 112 int offset = e.getOffset(); 113 int length = e.getLength(); 114 if (insert) { 115 tokenHierarchyControl().textModified(offset, 0, null, length); 116 } else { 117 tokenHierarchyControl().textModified(offset, length, 118 DocumentUtilities.getModificationText(e), 0); 119 } 120 } 121 122 } 123 | Popular Tags |