KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > inc > DocumentInput


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.lib.lexer.inc;
21
22 import javax.swing.event.DocumentEvent JavaDoc;
23 import javax.swing.event.DocumentListener JavaDoc;
24 import javax.swing.text.Document JavaDoc;
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 /**
34  * Control structure for managing of the lexer for a given document.
35  * <br>
36  * There is one structure for a document. It can be obtained by
37  * {@link #get(Document)}.
38  * <br>
39  * Each document that wants to use the lexer framework
40  * must be initialized by using {@link #init(Document, boolean, Object)}.
41  *
42  * @author Miloslav Metelka
43  * @version 1.00
44  */

45
46 public final class DocumentInput<D extends Document JavaDoc>
47 extends MutableTextInput<D> implements DocumentListener JavaDoc {
48
49     private static final String JavaDoc PROP_MIME_TYPE = "mimeType"; //NOI18N
50

51     public static <D extends Document JavaDoc> DocumentInput<D> get(D doc) {
52         @SuppressWarnings JavaDoc("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 JavaDoc text;
66     
67     public DocumentInput(D doc) {
68         this.doc = doc;
69         this.text = DocumentUtilities.getText(doc);
70         // Add document listener with the appropriate priority (if priority listening is supported)
71
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 JavaDoc mimeType = (String JavaDoc) 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 JavaDoc 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 JavaDoc e) {
101     }
102
103     public void insertUpdate(DocumentEvent JavaDoc e) {
104         modified(true, e);
105     }
106
107     public void removeUpdate(DocumentEvent JavaDoc e) {
108         modified(false, e);
109     }
110
111     private void modified(boolean insert, DocumentEvent JavaDoc 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