2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.xml.text.completion;
44
import java.util.List;
45
import javax.swing.text.BadLocationException;
46
import javax.swing.text.Document;
47
import javax.swing.text.JTextComponent;
48
import org.netbeans.api.editor.completion.Completion;
49
import org.netbeans.editor.BaseDocument;
50
import org.netbeans.editor.TokenItem;
51
import org.netbeans.modules.xml.text.api.XMLDefaultTokenContext;
52
import org.netbeans.modules.xml.text.syntax.XMLKit;
53
import org.netbeans.modules.xml.text.syntax.XMLSyntaxSupport;
54
import org.netbeans.editor.Utilities;
55
import org.netbeans.editor.ext.ExtEditorUI;
56
import org.netbeans.editor.ext.ExtSyntaxSupport;
57
import org.netbeans.spi.editor.completion.CompletionResultSet;
58
import org.netbeans.spi.editor.completion.support.AsyncCompletionQuery;
59
import org.netbeans.spi.editor.completion.support.AsyncCompletionTask;
60
import org.netbeans.spi.editor.completion.CompletionProvider;
61
import org.netbeans.spi.editor.completion.CompletionTask;
65
* @author Samaresh (Samaresh.Panda@Sun.Com)
67
public class XMLCompletionProvider implements CompletionProvider {
69
private static final boolean ENABLED = true;
72
* Creates a new instance of XMLCompletionProvider
74
public XMLCompletionProvider() {
77
public int getAutoQueryTypes(JTextComponent component, String typedText) {
78
int type = ((XMLSyntaxSupport)Utilities.getDocument(component).
79
getSyntaxSupport()).checkCompletion(component, typedText, false);
81
if(type == ExtSyntaxSupport.COMPLETION_POPUP)
82
return COMPLETION_QUERY_TYPE;
87
public CompletionTask createTask(int queryType, JTextComponent component) {
88
if (queryType == COMPLETION_QUERY_TYPE) {
89
return new AsyncCompletionTask(new Query(), component);
95
static class Query extends AsyncCompletionQuery {
97
private JTextComponent component;
100
protected void prepareQuery(JTextComponent component) {
101
this.component = component;
104
protected boolean doQuery(CompletionResultSet resultSet, Document doc, int caretOffset) {
105
XMLCompletionQuery.XMLCompletionResult res = queryImpl(component, caretOffset);
107
List/*<CompletionItem>*/ results = res.getData();
108
resultSet.addAllItems(results);
109
resultSet.setTitle(res.getTitle());
110
resultSet.setAnchorOffset(res.getSubstituteOffset());
111
return results.size() == 0;
116
protected void query(CompletionResultSet resultSet, Document doc, int caretOffset) {
117
boolean noResults = doQuery(resultSet, doc, caretOffset);
118
if(doc != null && noResults) {
119
checkHideCompletion((BaseDocument)doc, caretOffset);
126
private static XMLCompletionQuery.XMLCompletionResult queryImpl(JTextComponent component, int offset) {
127
if (!ENABLED) return null;
129
Class kitClass = Utilities.getKitClass(component);
130
if (kitClass != null) {
131
ExtEditorUI eeui = (ExtEditorUI)Utilities.getEditorUI(component);
132
org.netbeans.editor.ext.Completion compl = ((XMLKit)Utilities.getKit(component)).createCompletionForProvider(eeui);
133
XMLSyntaxSupport support = (XMLSyntaxSupport)Utilities.getSyntaxSupport(component);
134
return (XMLCompletionQuery.XMLCompletionResult)compl.getQuery().query(component, offset, support);
140
private static void checkHideCompletion(BaseDocument doc, int caretOffset) {
141
//test whether we are just in text and eventually close the opened completion
142
//this is handy after end tag autocompletion when user doesn't complete the
143
//end tag and just types a text
144
XMLSyntaxSupport sup = (XMLSyntaxSupport)doc.getSyntaxSupport().get(XMLSyntaxSupport.class);
146
TokenItem ti = sup.getTokenChain(caretOffset <= 0 ? 0 : caretOffset - 1, caretOffset);
147
if(ti != null && ti.getTokenID() == XMLDefaultTokenContext.TEXT && !ti.getImage().startsWith("<") && !ti.getImage().startsWith("&")) {
150
}catch(BadLocationException e) {
155
private static void hideCompletion() {
156
Completion.get().hideCompletion();