KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > editor > JSFConfigHyperlinkProvider


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 package org.netbeans.modules.web.jsf.editor;
20
21 import java.io.IOException JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import javax.lang.model.element.TypeElement;
26 import javax.lang.model.util.Elements;
27 import javax.swing.text.BadLocationException JavaDoc;
28 import javax.swing.text.JTextComponent JavaDoc;
29 import org.netbeans.api.java.source.ClasspathInfo;
30 import org.netbeans.api.java.source.CompilationController;
31 import org.netbeans.api.java.source.JavaSource;
32 import org.netbeans.api.java.source.UiUtils;
33 import org.netbeans.editor.BaseDocument;
34 import org.netbeans.editor.TokenItem;
35 import org.netbeans.editor.Utilities;
36 import org.netbeans.editor.ext.ExtSyntaxSupport;
37 //import org.netbeans.jmi.javamodel.JavaClass;
38
import org.netbeans.lib.editor.hyperlink.spi.HyperlinkProvider;
39 import org.netbeans.modules.editor.NbEditorUtilities;
40 import org.netbeans.modules.j2ee.common.source.AbstractTask;
41 //import org.netbeans.modules.editor.java.JMIUtils;
42
import org.netbeans.modules.web.api.webmodule.WebModule;
43 import org.netbeans.modules.web.jsf.JSFConfigDataObject;
44 //import org.netbeans.modules.web.jsf.JSFConfigUtilities;
45
import org.openide.ErrorManager;
46 import org.openide.cookies.OpenCookie;
47 import org.openide.filesystems.FileObject;
48 import org.openide.loaders.DataObject;
49 import org.openide.loaders.DataObjectNotFoundException;
50 import org.openide.nodes.Node;
51 import org.openide.util.NbBundle;
52
53 /**
54  *
55  * @author petr
56  */

57 public class JSFConfigHyperlinkProvider implements HyperlinkProvider {
58     
59     static private boolean debug = false;
60     private static Hashtable JavaDoc hyperlinkTable;
61     
62     private final int JAVA_CLASS = 0;
63     private final int RESOURCE_PATH = 2;
64     
65     {
66         hyperlinkTable = new Hashtable JavaDoc();
67         hyperlinkTable.put("managed-bean-class", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
68
hyperlinkTable.put("component-class", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
69
hyperlinkTable.put("renderer-class", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
70
hyperlinkTable.put("property-class", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
71
hyperlinkTable.put("validator-class", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
72
hyperlinkTable.put("attribute-class", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
73
hyperlinkTable.put("message-bundle", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
74
hyperlinkTable.put("action-listener", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
75
hyperlinkTable.put("application-factory", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
76
hyperlinkTable.put("converter-class", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
77
hyperlinkTable.put("converter-for-class", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
78
hyperlinkTable.put("faces-context-factory", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
79
hyperlinkTable.put("key-class", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
80
hyperlinkTable.put("lifecycle-factory", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
81
hyperlinkTable.put("navigation-handler", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
82
hyperlinkTable.put("phase-listener", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
83
hyperlinkTable.put("property-resolver", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
84
hyperlinkTable.put("referenced-bean-class", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
85
hyperlinkTable.put("render-kit-class", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
86
hyperlinkTable.put("render-kit-factory", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
87
hyperlinkTable.put("value-class", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
88
hyperlinkTable.put("variable-resolver", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
89
hyperlinkTable.put("from-view-id", new Integer JavaDoc(RESOURCE_PATH)); //NOI18N
90
hyperlinkTable.put("to-view-id", new Integer JavaDoc(RESOURCE_PATH)); //NOI18N
91
}
92     
93     private int valueOffset;
94     private String JavaDoc [] ev = null;
95     /** Creates a new instance of StrutsHyperlinkProvider */
96     public JSFConfigHyperlinkProvider() {
97     }
98
99     public int[] getHyperlinkSpan(javax.swing.text.Document JavaDoc doc, int offset) {
100         if (debug) debug (":: getHyperlinkSpan");
101         if (ev != null){
102             return new int []{valueOffset, valueOffset + ev[1].length() -1};
103         }
104         return null;
105     }
106
107     public boolean isHyperlinkPoint(javax.swing.text.Document JavaDoc doc, int offset) {
108         if (debug) debug (":: isHyperlinkSpan - offset: " + offset); //NOI18N
109

110         // PENDING - this check should be removed, when
111
// the issue #61704 is solved.
112
DataObject dObject = NbEditorUtilities.getDataObject(doc);
113         if (! (dObject instanceof JSFConfigDataObject))
114             return false;
115         ev = getElementValue(doc, offset);
116         if (ev != null){
117             if (hyperlinkTable.get(ev[0])!= null)
118                 return true;
119         }
120         return false;
121     }
122
123     public void performClickAction(javax.swing.text.Document JavaDoc doc, int offset) {
124         if (debug) debug (":: performClickAction");
125         
126         if (hyperlinkTable.get(ev[0])!= null){
127             int type = ((Integer JavaDoc)hyperlinkTable.get(ev[0])).intValue();
128             switch (type){
129                 case JAVA_CLASS: findJavaClass(ev[1], doc); break;
130                 case RESOURCE_PATH: findResourcePath(ev[1], (BaseDocument)doc);break;
131             }
132         }
133     }
134     
135     static void debug(String JavaDoc message){
136         System.out.println("JSFConfigHyperlinkProvider: " + message); //NoI18N
137
}
138     
139     private String JavaDoc[] getElementValue(javax.swing.text.Document JavaDoc doc, int offset){
140         String JavaDoc tag = null;
141         String JavaDoc value = null;
142         
143         try {
144             BaseDocument bdoc = (BaseDocument) doc;
145             JTextComponent JavaDoc target = Utilities.getFocusedComponent();
146             
147             if (target == null || target.getDocument() != bdoc)
148                 return null;
149             ExtSyntaxSupport sup = (ExtSyntaxSupport)bdoc.getSyntaxSupport();
150             TokenItem token = sup.getTokenChain(offset, offset+1);
151             if (token == null || token.getTokenID().getNumericID() != JSFEditorUtilities.XML_TEXT)
152                 return null;
153             value = token.getImage();
154             if (value != null){
155                 String JavaDoc original = value;
156                 value = value.trim();
157                 valueOffset = token.getOffset()+(original.indexOf(value));
158             }
159             // Find element
160
// 4 - tag
161
while(token != null
162                     && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
163                     && !token.getImage().equals(">")))
164                 token = token.getPrevious();
165             if (token == null)
166                 return null;
167             tag = token.getImage().substring(1);
168             if (debug) debug ("element: " + tag ); // NOI18N
169
if (debug) debug ("value: " + value ); //NOI18N
170
return new String JavaDoc[]{tag, value};
171             
172         }
173         catch (BadLocationException JavaDoc e) {
174             ErrorManager.getDefault().notify(e);
175         }
176         return null;
177     }
178     
179     private boolean isWhiteChar(char c){
180         return (c == ' ' || c == '\n' || c == '\t' || c == '\r');
181     }
182     
183     private void findJavaClass(final String JavaDoc fqn, javax.swing.text.Document JavaDoc doc){
184         FileObject fo = NbEditorUtilities.getFileObject(doc);
185         if (fo != null){
186             WebModule wm = WebModule.getWebModule(fo);
187             if (wm != null){
188                 try {
189                     final ClasspathInfo cpi = ClasspathInfo.create(wm.getDocumentBase());
190                     JavaSource js = JavaSource.create(cpi, Collections.EMPTY_LIST);
191                     
192                     js.runUserActionTask(new AbstractTask<CompilationController>() {
193                         
194                         public void run(CompilationController cc) throws Exception JavaDoc {
195                             Elements elements = cc.getElements();
196                             TypeElement element = elements.getTypeElement(fqn.trim());
197                             if (element != null) {
198                                 if (!UiUtils.open(cpi, element)){
199                                     String JavaDoc key = "goto_source_not_found"; // NOI18N
200
String JavaDoc msg = NbBundle.getBundle(JSFConfigHyperlinkProvider.class).getString(key);
201                                     org.openide.awt.StatusDisplayer.getDefault().setStatusText(MessageFormat.format(msg, new Object JavaDoc [] { fqn } ));
202                                     
203                                 }
204                             }
205                         }
206                     }, false);
207                 } catch (IOException JavaDoc ex) {
208                     java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE,
209                             ex.getMessage(), ex);
210                 };
211             }
212         }
213     }
214    
215     
216     
217     private void findResourcePath(String JavaDoc path, BaseDocument doc){
218         //normalize path
219
int qmIndex = path.indexOf('?');
220         if(qmIndex == 0) {
221             return ; //filter sg. like '?id=val'
222
}
223         if(qmIndex != -1) {
224             //trim the path
225
path = path.substring(0, qmIndex);
226         }
227         
228         WebModule wm = WebModule.getWebModule(NbEditorUtilities.getFileObject(doc));
229         if (wm != null){
230             FileObject docBase= wm.getDocumentBase();
231             FileObject fo = docBase.getFileObject(path);
232             if (fo != null)
233                 openInEditor(fo);
234         }
235     }
236     
237     
238     
239     private void openInEditor(FileObject fObj){
240         if (fObj != null){
241             DataObject dobj = null;
242             try{
243                 dobj = DataObject.find(fObj);
244             }
245             catch (DataObjectNotFoundException e){
246                ErrorManager.getDefault().notify(e);
247                return;
248             }
249             if (dobj != null){
250                 Node.Cookie cookie = dobj.getCookie(OpenCookie.class);
251                 if (cookie != null)
252                     ((OpenCookie)cookie).open();
253             }
254         }
255     }
256 }
257
Popular Tags