KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > ui > SetExecutionUriAction


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.project.ui;
21
22 import org.openide.util.NbBundle;
23 import org.openide.util.HelpCtx;
24 import org.openide.util.actions.NodeAction;
25 import org.openide.filesystems.FileObject;
26 import org.openide.loaders.DataObject;
27 import org.openide.nodes.Node;
28
29 import org.netbeans.modules.web.api.webmodule.WebModule;
30 import org.netbeans.modules.j2ee.dd.api.web.DDProvider;
31 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
32 import org.netbeans.modules.j2ee.dd.api.web.Servlet;
33 import org.netbeans.modules.j2ee.dd.api.web.ServletMapping;
34 import org.netbeans.api.java.classpath.ClassPath;
35
36 import org.openide.DialogDescriptor;
37 import org.openide.DialogDisplayer;
38 import org.openide.NotifyDescriptor;
39 /**
40 *
41 * @author Milan Kuchtiak
42 */

43 public final class SetExecutionUriAction extends NodeAction {
44     public static final String JavaDoc ATTR_EXECUTION_URI = "execution.uri"; //NOI18N
45

46     /** Creates and starts a thread for generating documentation
47     */

48     protected void performAction(Node[] activatedNodes) {
49         if ((activatedNodes != null) && (activatedNodes.length == 1)) {
50             if (activatedNodes[0] != null) {
51                 DataObject data = (DataObject)activatedNodes[0].getCookie(DataObject.class);
52                 if (data != null) {
53                     FileObject servletFo = data.getPrimaryFile();
54                     WebModule webModule = WebModule.getWebModule(servletFo);
55                     String JavaDoc[] urlPatterns = getServletMappings(webModule, servletFo);
56                     if (urlPatterns!=null && urlPatterns.length>0) {
57                         String JavaDoc oldUri = (String JavaDoc)servletFo.getAttribute(ATTR_EXECUTION_URI);
58                         ServletUriPanel uriPanel = new ServletUriPanel(urlPatterns,oldUri,false);
59                         DialogDescriptor desc = new DialogDescriptor(uriPanel,
60                             NbBundle.getMessage (SetExecutionUriAction.class, "TTL_setServletExecutionUri"));
61                         Object JavaDoc res = DialogDisplayer.getDefault().notify(desc);
62                         if (res.equals(NotifyDescriptor.YES_OPTION)) {
63                             try {
64                                 servletFo.setAttribute(ATTR_EXECUTION_URI,uriPanel.getServletUri());
65                             } catch (java.io.IOException JavaDoc ex){}
66                         } else return;
67                     } else {
68                         String JavaDoc mes = java.text.MessageFormat.format (
69                                 NbBundle.getMessage (SetExecutionUriAction.class, "TXT_missingServletMappings"),
70                                 new Object JavaDoc [] {servletFo.getName()}); //NOI18N
71
NotifyDescriptor desc = new NotifyDescriptor.Message(mes,NotifyDescriptor.Message.ERROR_MESSAGE);
72                         DialogDisplayer.getDefault().notify(desc);
73                     }
74                 }
75             }
76         }
77     }
78     
79     protected boolean enable (Node[] activatedNodes) {
80         if ((activatedNodes != null) && (activatedNodes.length == 1)) {
81             if (activatedNodes[0] != null) {
82                 DataObject data = (DataObject)activatedNodes[0].getCookie(DataObject.class);
83                 if (data != null) {
84                     String JavaDoc mimetype = data.getPrimaryFile().getMIMEType();
85                     Boolean JavaDoc servletAttr = (Boolean JavaDoc)data.getPrimaryFile().getAttribute("org.netbeans.modules.web.IsServletFile"); //NOI18N
86
return "text/x-java".equals(mimetype) && Boolean.TRUE.equals(servletAttr); //NOI18N
87
}
88             }
89         }
90         return false;
91     }
92
93     /* Help context where to find more about the action.
94     * @return the help context for this action
95     */

96     public HelpCtx getHelpCtx() {
97         return new HelpCtx (SetExecutionUriAction.class);
98     }
99
100     /* Human presentable name of the action. This should be
101     * presented as an item in a menu.
102     * @return the name of the action
103     */

104     public String JavaDoc getName() {
105         return NbBundle.getMessage(SetExecutionUriAction.class, "LBL_serveltExecutionUriAction");
106     }
107     
108     /** The action's icon location.
109     * @return the action's icon location
110     */

111     protected String JavaDoc iconResource () {
112         return "org/netbeans/modules/web/project/ui/resources/servletUri.gif"; // NOI18N
113
}
114     
115     protected boolean asynchronous() {
116         return false;
117     }
118     
119     public static String JavaDoc[] getServletMappings(WebModule webModule, FileObject javaClass) {
120         if (webModule==null) return null;
121         FileObject webDir = webModule.getDocumentBase ();
122         if (webDir==null) return null;
123         FileObject fo = webDir.getFileObject("WEB-INF/web.xml"); //NOI18N
124
if (fo==null) return null;
125         ClassPath classPath = ClassPath.getClassPath (javaClass, ClassPath.SOURCE);
126         String JavaDoc className = classPath.getResourceName(javaClass,'.',false);
127         try {
128             WebApp webApp = DDProvider.getDefault().getDDRoot(fo);
129             Servlet[] servlets = webApp.getServlet();
130             java.util.List JavaDoc mappingList = new java.util.ArrayList JavaDoc();
131             for (int i=0;i<servlets.length;i++) {
132                 if (className.equals(servlets[i].getServletClass())) {
133                     String JavaDoc servletName=servlets[i].getServletName();
134                     ServletMapping[] maps = webApp.getServletMapping();
135                     for (int j=0;j<maps.length;j++) {
136                         if (maps[j].getServletName().equals(servletName)) {
137                             String JavaDoc urlPattern = maps[j].getUrlPattern();
138                             if (urlPattern!=null)
139                                 mappingList.add(urlPattern);
140                         }
141                     }
142                 }
143             }
144             String JavaDoc[] mappings = new String JavaDoc[mappingList.size()];
145             mappingList.toArray(mappings);
146             return mappings;
147         } catch (java.io.IOException JavaDoc ex) {return null;}
148     }
149     
150 }
151
Popular Tags