1 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 43 public final class SetExecutionUriAction extends NodeAction { 44 public static final String ATTR_EXECUTION_URI = "execution.uri"; 46 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 [] urlPatterns = getServletMappings(webModule, servletFo); 56 if (urlPatterns!=null && urlPatterns.length>0) { 57 String oldUri = (String )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 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 ex){} 66 } else return; 67 } else { 68 String mes = java.text.MessageFormat.format ( 69 NbBundle.getMessage (SetExecutionUriAction.class, "TXT_missingServletMappings"), 70 new Object [] {servletFo.getName()}); 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 mimetype = data.getPrimaryFile().getMIMEType(); 85 Boolean servletAttr = (Boolean )data.getPrimaryFile().getAttribute("org.netbeans.modules.web.IsServletFile"); return "text/x-java".equals(mimetype) && Boolean.TRUE.equals(servletAttr); } 88 } 89 } 90 return false; 91 } 92 93 96 public HelpCtx getHelpCtx() { 97 return new HelpCtx (SetExecutionUriAction.class); 98 } 99 100 104 public String getName() { 105 return NbBundle.getMessage(SetExecutionUriAction.class, "LBL_serveltExecutionUriAction"); 106 } 107 108 111 protected String iconResource () { 112 return "org/netbeans/modules/web/project/ui/resources/servletUri.gif"; } 114 115 protected boolean asynchronous() { 116 return false; 117 } 118 119 public static String [] 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"); if (fo==null) return null; 125 ClassPath classPath = ClassPath.getClassPath (javaClass, ClassPath.SOURCE); 126 String className = classPath.getResourceName(javaClass,'.',false); 127 try { 128 WebApp webApp = DDProvider.getDefault().getDDRoot(fo); 129 Servlet[] servlets = webApp.getServlet(); 130 java.util.List mappingList = new java.util.ArrayList (); 131 for (int i=0;i<servlets.length;i++) { 132 if (className.equals(servlets[i].getServletClass())) { 133 String 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 urlPattern = maps[j].getUrlPattern(); 138 if (urlPattern!=null) 139 mappingList.add(urlPattern); 140 } 141 } 142 } 143 } 144 String [] mappings = new String [mappingList.size()]; 145 mappingList.toArray(mappings); 146 return mappings; 147 } catch (java.io.IOException ex) {return null;} 148 } 149 150 } 151 | Popular Tags |