KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > providers > SuggestionContext


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.providers;
21
22 import org.openide.loaders.DataObject;
23 import org.openide.cookies.EditorCookie;
24 import org.openide.filesystems.FileObject;
25 import org.openide.ErrorManager;
26
27 import javax.swing.text.Document JavaDoc;
28 import javax.swing.text.BadLocationException JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 /**
32  * Passes working environment to suggestion provides.
33  *
34  * @author Petr Kuzel
35  * @deprecated Experimental SPI
36  * @since 1.3
37  */

38 public final class SuggestionContext {
39
40     private final DataObject dataObject;
41
42     private String JavaDoc cachedString;
43
44     // we have soft runtime dependency on java module
45
private static boolean linkageError;
46
47     /**
48      * For internal framework purposes only!
49      */

50     SuggestionContext(DataObject dataObject) {
51         this.dataObject = dataObject;
52     }
53
54     /**
55      * @return read-only snapshot of context representation
56      */

57     public CharSequence JavaDoc getCharSequence() {
58
59         if (cachedString == null) {
60
61             FileObject fo = getFileObject();
62             if (linkageError == false && fo.hasExt("java")) { // NOI18N
63
// use faster direct access to file for java sources
64
// I measured 10% speedup
65
try {
66                     // XXX it does not normalize line separators to \n
67
cachedString = JavaSuggestionContext.getContent(fo);
68                     return cachedString;
69                 } catch (LinkageError JavaDoc link) {
70                     // use EditorCookie below
71
link.printStackTrace();
72                     linkageError = true;
73                 }
74             }
75
76             if (fo.hasExt("properties") && dataObject.isModified() == false) { // NOI18N
77
cachedString = PropertiesSuggestionContext.getContent(fo);
78                 return cachedString;
79             }
80
81             if ("xml".equalsIgnoreCase(fo.getExt()) && dataObject.isModified() == false) { // NOI18N
82
cachedString = XMLSuggestionContext.getContent(fo);
83                 if (cachedString != null) return cachedString;
84             }
85
86             EditorCookie edit =
87                 (EditorCookie) dataObject.getCookie(EditorCookie.class);
88             if (edit != null) {
89                 Document JavaDoc doc;
90                 try {
91                     doc = edit.openDocument(); // DOES block
92
cachedString = extractString(doc);
93                 } catch (IOException JavaDoc e) {
94                     ErrorManager.getDefault().notify(e);
95                 }
96             }
97         }
98         return cachedString;
99     }
100
101     /**
102      * @return read/write live in-memory context representation
103      */

104     public Document JavaDoc getDocument() {
105         EditorCookie edit =
106             (EditorCookie) dataObject.getCookie(EditorCookie.class);
107         if (edit != null) {
108             try {
109                 return edit.openDocument(); // DOES block
110
} catch (IOException JavaDoc e) {
111                 // XXX
112
e.printStackTrace();
113             }
114         }
115         return null;
116     }
117
118     /**
119      * @return filesystem context representation
120      */

121     public FileObject getFileObject() {
122         return dataObject.getPrimaryFile();
123     }
124
125     /**
126      * Extracts document content as a string
127      * @param doc source document (never null)
128      * @return extracted text
129      */

130     private static String JavaDoc extractString(final Document JavaDoc doc) {
131         final String JavaDoc text[] = new String JavaDoc[1];
132         doc.render(new Runnable JavaDoc () {
133             public void run() {
134                 try {
135                     text[0] = doc.getText(0, doc.getLength());
136                 } catch (BadLocationException JavaDoc ex) {
137                     assert false : ex;
138                 }
139             }
140         });
141         return text[0];
142     }
143
144 }
145
Popular Tags