KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > editor > jspel > JSFELCompletionProvider


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.web.jsf.editor.jspel;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.text.Document JavaDoc;
25 import javax.swing.text.JTextComponent JavaDoc;
26 import org.netbeans.editor.Utilities;
27 import org.netbeans.editor.ext.ExtSyntaxSupport;
28 import org.netbeans.modules.editor.NbEditorUtilities;
29 import org.netbeans.modules.web.api.webmodule.WebModule;
30 import org.netbeans.modules.web.core.syntax.JspSyntaxSupport;
31 import org.netbeans.modules.web.core.syntax.completion.JspCompletionItem;
32 import org.netbeans.modules.web.jsf.api.facesmodel.ManagedBean;
33 import org.netbeans.spi.editor.completion.*;
34 import org.netbeans.spi.editor.completion.support.AsyncCompletionQuery;
35 import org.netbeans.spi.editor.completion.support.AsyncCompletionTask;
36 import org.openide.filesystems.FileObject;
37 /**
38  *
39  * @author Petr Pisl
40  */

41 public class JSFELCompletionProvider implements CompletionProvider{
42     
43     /** Creates a new instance of JSFELCompletionProvider */
44     public JSFELCompletionProvider() {
45     }
46     
47     public CompletionTask createTask(int queryType, JTextComponent JavaDoc component) {
48         if (queryType == COMPLETION_QUERY_TYPE)
49             return new AsyncCompletionTask(new CCQuery(component.getCaret().getDot()), component);
50 // else if (queryType == DOCUMENTATION_QUERY_TYPE)
51
// return new AsyncCompletionTask(new DocQuery(/*null*/), component);
52
return null;
53     }
54     
55     public int getAutoQueryTypes(JTextComponent JavaDoc component, String JavaDoc typedText) {
56         return 0;
57     }
58     
59     static final class CCQuery extends AsyncCompletionQuery {
60         private int creationCaretOffset;
61         private JTextComponent JavaDoc component;
62         
63         CCQuery(int caretOffset) {
64             this.creationCaretOffset = caretOffset;
65         }
66         
67         protected void query(CompletionResultSet resultSet, Document JavaDoc doc, int offset) {
68             ExtSyntaxSupport sup = (ExtSyntaxSupport)Utilities.getSyntaxSupport(component);
69             FileObject fObject = NbEditorUtilities.getFileObject(doc);
70             WebModule wm = null;
71             if (fObject != null)
72                 wm = WebModule.getWebModule(fObject);
73             if (sup instanceof JspSyntaxSupport && wm != null){
74                 JSFELExpression elExpr = new JSFELExpression (wm, (JspSyntaxSupport)sup);
75                 ArrayList JavaDoc complItems = new ArrayList JavaDoc();
76
77                 switch (elExpr.parse(offset)){
78                     case JSFELExpression.EL_START:
79                         List JavaDoc <ManagedBean>beans = JSFBeanCache.getBeans(wm);
80                         
81                         for (ManagedBean bean : beans){
82                             if (bean.getManagedBeanName().startsWith(elExpr.getReplace())){
83                                 complItems.add(new JSFResultItem.JSFBean(bean.getManagedBeanName(), bean.getManagedBeanClass()));
84                             }
85                         }
86                         break;
87                     case JSFELExpression.EL_JSF_BEAN:
88                         List JavaDoc<CompletionItem> items = elExpr.getPropertyCompletionItems(elExpr.getObjectClass());
89                         complItems.addAll(items);
90                         break;
91                 }
92                 for (int i = 0; i < complItems.size(); i++)
93                     ((JspCompletionItem.JspResultItem)complItems.get(i)).setSubstituteOffset(offset - elExpr.getReplace().length());
94                 resultSet.addAllItems(complItems);
95             }
96             resultSet.finish();
97         }
98         
99         protected void prepareQuery(JTextComponent JavaDoc component) {
100             this.component = component;
101         }
102     }
103    
104     
105     static class DocQuery extends AsyncCompletionQuery {
106         protected void query(CompletionResultSet resultSet, Document JavaDoc doc, int caretOffset) {
107         }
108     }
109 }
110
Popular Tags