KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > completion > CompletionQuery


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 package org.netbeans.modules.xml.schema.completion;
20
21 import java.util.List JavaDoc;
22 import javax.swing.text.Document JavaDoc;
23 import javax.swing.text.JTextComponent JavaDoc;
24 import org.netbeans.editor.BaseDocument;
25 import org.netbeans.modules.xml.schema.completion.util.CompletionContextImpl;
26 import org.netbeans.modules.xml.schema.completion.util.CompletionUtil;
27 import org.netbeans.modules.xml.text.syntax.XMLSyntaxSupport;
28 import org.netbeans.spi.editor.completion.CompletionResultSet;
29 import org.netbeans.spi.editor.completion.support.AsyncCompletionQuery;
30 import org.openide.filesystems.FileObject;
31
32 /**
33  *
34  * @author Samaresh (Samaresh.Panda@Sun.Com)
35  */

36 public class CompletionQuery extends AsyncCompletionQuery {
37         
38     /**
39      * Creates a new instance of CompletionQuery
40      */

41     public CompletionQuery(FileObject primaryFile) {
42         this.primaryFile = primaryFile;
43     }
44     
45     /**
46      *
47      */

48     protected void prepareQuery(JTextComponent JavaDoc component) {
49         this.component = component;
50     }
51     
52     /**
53      *
54      */

55     protected void query(CompletionResultSet resultSet,
56             Document JavaDoc doc, int caretOffset) {
57         List JavaDoc<CompletionResultItem> items = getCompletionItems(doc, caretOffset);
58         if(items != null) resultSet.addAllItems(items);
59         resultSet.finish();
60     }
61     
62     
63     /**
64      * This method is needed for unit testing purposes.
65      */

66     List JavaDoc<CompletionResultItem> getCompletionItems(Document JavaDoc doc, int caretOffset) {
67         List JavaDoc<CompletionResultItem> completionItems = null;
68         
69         //Step 1: create a context
70
XMLSyntaxSupport support = (XMLSyntaxSupport) ((BaseDocument)doc).getSyntaxSupport();
71         context = new CompletionContextImpl(primaryFile, support, caretOffset);
72         
73         //Step 2: Accumulate all models and initialize the context
74
if(!context.initModels() || !context.initContext()) {
75             return null;
76         }
77                 
78         //Step 3: Query
79
switch (context.getCompletionType()) {
80             case COMPLETION_TYPE_ELEMENT:
81                 completionItems = CompletionUtil.getElements(context);
82                 break;
83                 
84             case COMPLETION_TYPE_ATTRIBUTE:
85                 completionItems = CompletionUtil.getAttributes(context);
86                 break;
87             
88             case COMPLETION_TYPE_VALUE:
89                 break;
90             
91             case COMPLETION_TYPE_ENTITY:
92                 break;
93             
94             case COMPLETION_TYPE_NOTATION:
95                 break;
96                 
97             default:
98                 break;
99         }
100         
101         return completionItems;
102     }
103             
104     private JTextComponent JavaDoc component;
105     private FileObject primaryFile;
106     private CompletionContextImpl context;
107 }
108
Popular Tags