KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > text > completion > XMLCompletionProvider


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.xml.text.completion;
21
22 import java.util.Collections JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.text.BadLocationException JavaDoc;
26 import javax.swing.text.Document JavaDoc;
27 import javax.swing.text.JTextComponent JavaDoc;
28 import org.netbeans.api.editor.completion.Completion;
29 import org.netbeans.editor.BaseDocument;
30 import org.netbeans.editor.TokenItem;
31 import org.netbeans.modules.xml.text.api.XMLDefaultTokenContext;
32 import org.netbeans.modules.xml.text.syntax.XMLKit;
33 import org.netbeans.modules.xml.text.syntax.XMLSyntaxSupport;
34 import org.netbeans.editor.Utilities;
35 import org.netbeans.editor.ext.CompletionQuery;
36 import org.netbeans.editor.ext.CompletionQuery.ResultItem;
37 import org.netbeans.editor.ext.ExtEditorUI;
38 import org.netbeans.editor.ext.ExtSyntaxSupport;
39 import org.netbeans.modules.xml.text.syntax.XMLTokenContext;
40 import org.netbeans.spi.editor.completion.CompletionItem;
41 import org.netbeans.spi.editor.completion.CompletionResultSet;
42 import org.netbeans.spi.editor.completion.support.AsyncCompletionQuery;
43 import org.netbeans.spi.editor.completion.support.AsyncCompletionTask;
44 import org.netbeans.spi.editor.completion.CompletionProvider;
45 import org.netbeans.spi.editor.completion.CompletionTask;
46
47 /**
48  *
49  * @author Samaresh (Samaresh.Panda@Sun.Com)
50  */

51 public class XMLCompletionProvider implements CompletionProvider {
52     
53     private static final boolean ENABLED = true;
54     
55     /**
56      * Creates a new instance of XMLCompletionProvider
57      */

58     public XMLCompletionProvider() {
59     }
60     
61     public int getAutoQueryTypes(JTextComponent JavaDoc component, String JavaDoc typedText) {
62         int type = ((XMLSyntaxSupport)Utilities.getDocument(component).
63                 getSyntaxSupport()).checkCompletion(component, typedText, false);
64         
65         if(type == ExtSyntaxSupport.COMPLETION_POPUP)
66             return COMPLETION_QUERY_TYPE;
67         else return 0;
68     }
69     
70     public CompletionTask createTask(int queryType, JTextComponent JavaDoc component) {
71         if (queryType == COMPLETION_QUERY_TYPE) {
72             return new AsyncCompletionTask(new Query(), component);
73         }
74         
75         return null;
76     }
77     
78     static class Query extends AbstractQuery {
79         
80         private JTextComponent JavaDoc component;
81         
82         protected void prepareQuery(JTextComponent JavaDoc component) {
83             this.component = component;
84         }
85         
86         protected void doQuery(CompletionResultSet resultSet, Document JavaDoc doc, int caretOffset) {
87             XMLCompletionQuery.XMLCompletionResult res = queryImpl(component, caretOffset);
88             if(res != null) {
89                 List JavaDoc/*<CompletionItem>*/ results = res.getData();
90                 resultSet.addAllItems(results);
91                 resultSet.setTitle(res.getTitle());
92                 resultSet.setAnchorOffset(res.getSubstituteOffset());
93             }
94         }
95     }
96     
97     private static XMLCompletionQuery.XMLCompletionResult queryImpl(JTextComponent JavaDoc component, int offset) {
98         if (!ENABLED) return null;
99         
100         Class JavaDoc kitClass = Utilities.getKitClass(component);
101         if (kitClass != null) {
102             ExtEditorUI eeui = (ExtEditorUI)Utilities.getEditorUI(component);
103             org.netbeans.editor.ext.Completion compl = ((XMLKit)Utilities.getKit(component)).createCompletionForProvider(eeui);
104             XMLSyntaxSupport support = (XMLSyntaxSupport)Utilities.getSyntaxSupport(component);
105             return (XMLCompletionQuery.XMLCompletionResult)compl.getQuery().query(component, offset, support);
106         }
107         
108         return null;
109     }
110     
111     private static abstract class AbstractQuery extends AsyncCompletionQuery {
112         
113         protected void preQueryUpdate(JTextComponent JavaDoc component) {
114             int caretOffset = component.getCaretPosition();
115             Document JavaDoc doc = component.getDocument();
116             checkHideCompletion((BaseDocument)doc, caretOffset);
117         }
118         
119         protected void query(CompletionResultSet resultSet, Document JavaDoc doc, int caretOffset) {
120             if(doc != null) {
121                 checkHideCompletion((BaseDocument)doc, caretOffset);
122             }
123             doQuery(resultSet, doc, caretOffset);
124             resultSet.finish();
125         }
126         
127         abstract void doQuery(CompletionResultSet resultSet, Document JavaDoc doc, int caretOffset);
128         
129     }
130     
131     private static void checkHideCompletion(BaseDocument doc, int caretOffset) {
132         //test whether we are just in text and eventually close the opened completion
133
//this is handy after end tag autocompletion when user doesn't complete the
134
//end tag and just types a text
135
XMLSyntaxSupport sup = (XMLSyntaxSupport)doc.getSyntaxSupport().get(XMLSyntaxSupport.class);
136         try {
137             TokenItem ti = sup.getTokenChain(caretOffset <= 0 ? 0 : caretOffset - 1, caretOffset);
138             if(ti != null && ti.getTokenID() == XMLDefaultTokenContext.TEXT && !ti.getImage().startsWith("<") && !ti.getImage().startsWith("&")) {
139                 hideCompletion();
140             }
141         }catch(BadLocationException JavaDoc e) {
142             //do nothing
143
}
144     }
145     
146     private static void hideCompletion() {
147         Completion.get().hideCompletion();
148     }
149     
150 }
151
Popular Tags