001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.editor.html;
043:
044: import java.util.List;
045: import javax.swing.text.Document;
046: import javax.swing.text.JTextComponent;
047: import org.netbeans.api.editor.completion.Completion;
048: import org.netbeans.api.html.lexer.HTMLTokenId;
049: import org.netbeans.api.lexer.Token;
050: import org.netbeans.api.lexer.TokenHierarchy;
051: import org.netbeans.api.lexer.TokenSequence;
052: import org.netbeans.editor.BaseDocument;
053: import org.netbeans.editor.Utilities;
054: import org.netbeans.editor.ext.CompletionQuery;
055: import org.netbeans.editor.ext.CompletionQuery.ResultItem;
056: import org.netbeans.editor.ext.ExtSyntaxSupport;
057: import org.netbeans.editor.ext.html.HTMLCompletionQuery;
058: import org.netbeans.editor.ext.html.HTMLCompletionQuery.HTMLResultItem;
059: import org.netbeans.editor.ext.html.HTMLSyntaxSupport;
060: import org.netbeans.spi.editor.completion.CompletionItem;
061: import org.netbeans.spi.editor.completion.CompletionResultSet;
062: import org.netbeans.spi.editor.completion.CompletionProvider;
063: import org.netbeans.spi.editor.completion.CompletionTask;
064: import org.netbeans.spi.editor.completion.support.AsyncCompletionQuery;
065: import org.netbeans.spi.editor.completion.support.AsyncCompletionTask;
066:
067: /**
068: * Implementation of {@link CompletionProvider} for HTML documents.
069: *
070: * @author Marek Fukala
071: */
072: public class HTMLCompletionProvider implements CompletionProvider {
073:
074: /** Creates a new instance of JavaDocCompletionProvider */
075: public HTMLCompletionProvider() {
076: }
077:
078: public int getAutoQueryTypes(JTextComponent component,
079: String typedText) {
080: int type = HTMLSyntaxSupport.get(component.getDocument())
081: .checkCompletion(component, typedText, false);
082: return type == ExtSyntaxSupport.COMPLETION_POPUP ? COMPLETION_QUERY_TYPE
083: + DOCUMENTATION_QUERY_TYPE
084: : 0;
085: }
086:
087: public CompletionTask createTask(int queryType,
088: JTextComponent component) {
089: AsyncCompletionTask task = null;
090: if ((queryType & COMPLETION_QUERY_TYPE & COMPLETION_ALL_QUERY_TYPE) != 0) {
091: task = new AsyncCompletionTask(new Query(), component);
092: } else if (queryType == DOCUMENTATION_QUERY_TYPE) {
093: task = new AsyncCompletionTask(new DocQuery(null),
094: component);
095: }
096: return task;
097: }
098:
099: static class Query extends AbstractQuery {
100:
101: private JTextComponent component;
102:
103: @Override
104: protected void prepareQuery(JTextComponent component) {
105: this .component = component;
106: }
107:
108: protected void doQuery(CompletionResultSet resultSet,
109: Document doc, int caretOffset) {
110: List<CompletionItem> items = HTMLCompletionQuery
111: .getDefault().query(component, caretOffset);
112: if (items == null) {
113: return;
114: }
115: resultSet.addAllItems(items);
116: // resultSet.setAnchorOffset(res.getSubstituteOffset());
117: }
118: }
119:
120: static class DocQuery extends AbstractQuery {
121:
122: private JTextComponent component;
123: private CompletionItem item;
124:
125: DocQuery(HTMLResultItem item) {
126: this .item = item;
127: }
128:
129: @Override
130: protected void prepareQuery(JTextComponent component) {
131: this .component = component;
132: }
133:
134: protected void doQuery(CompletionResultSet resultSet,
135: Document doc, int caretOffset) {
136: List<CompletionItem> res = null;
137: if (item == null) {
138: //item == null means that the DocQuery is invoked
139: //based on the explicit documentation opening request
140: //(not ivoked by selecting a completion item in the list)
141: res = HTMLCompletionQuery.getDefault().query(component,
142: caretOffset);
143: if (res != null && res.size() > 0) {
144: item = res.get(0);
145: }
146: }
147: HTMLResultItem htmlItem = (HTMLResultItem) item;
148: if (htmlItem != null && htmlItem.getHelpID() != null) {
149: resultSet
150: .setDocumentation(new HTMLCompletionQuery.DocItem(
151: htmlItem));
152: }
153: }
154: }
155:
156: private static abstract class AbstractQuery extends
157: AsyncCompletionQuery {
158:
159: protected void preQueryUpdate(JTextComponent component) {
160: int caretOffset = component.getCaretPosition();
161: Document doc = component.getDocument();
162: checkHideCompletion((BaseDocument) doc, caretOffset);
163: }
164:
165: protected void query(CompletionResultSet resultSet,
166: Document doc, int caretOffset) {
167: doQuery(resultSet, doc, caretOffset);
168: resultSet.finish();
169: }
170:
171: abstract void doQuery(CompletionResultSet resultSet,
172: Document doc, int caretOffset);
173:
174: }
175:
176: private static void checkHideCompletion(BaseDocument doc,
177: int caretOffset) {
178: //test whether we are just in text and eventually close the opened completion
179: //this is handy after end tag autocompletion when user doesn't complete the
180: //end tag and just types a text
181: //test whether the user typed an ending quotation in the attribute value
182: doc.readLock();
183: try {
184: TokenHierarchy tokenHierarchy = TokenHierarchy.get(doc);
185: TokenSequence tokenSequence = tokenHierarchy
186: .tokenSequence();
187:
188: tokenSequence.move(caretOffset == 0 ? 0 : caretOffset - 1);
189: if (!tokenSequence.moveNext())
190: return;
191:
192: Token tokenItem = tokenSequence.token();
193: if (tokenItem.id() == HTMLTokenId.TEXT
194: && !tokenItem.text().toString().startsWith("<")
195: && !tokenItem.text().toString().startsWith("&")) {
196: hideCompletion();
197: }
198:
199: } finally {
200: doc.readUnlock();
201: }
202: }
203:
204: private static void hideCompletion() {
205: Completion.get().hideCompletion();
206: Completion.get().hideDocumentation();
207: }
208: }
|