1 19 20 package org.netbeans.modules.j2ee.ddloaders.web.multiview; 21 22 import org.netbeans.core.spi.multiview.*; 23 import org.openide.nodes.*; 24 import org.netbeans.modules.j2ee.dd.api.web.*; 25 import org.netbeans.modules.j2ee.ddloaders.web.*; 26 import org.netbeans.modules.xml.multiview.ui.*; 27 import org.netbeans.modules.xml.multiview.ToolBarMultiViewElement; 28 import org.netbeans.modules.xml.multiview.Error; 29 import org.openide.util.NbBundle; 30 import org.openide.util.RequestProcessor; 31 import org.openide.util.HelpCtx; 32 36 public class ServletsMultiViewElement extends ToolBarMultiViewElement implements java.beans.PropertyChangeListener { 37 private SectionView view; 38 private ToolBarDesignEditor comp; 39 private DDDataObject dObj; 40 private WebApp webApp; 41 private ServletPanelFactory factory; 42 private javax.swing.Action addAction, removeAction; 43 private boolean needInit=true; 44 private int index; 45 private RequestProcessor.Task repaintingTask; 46 private static final String SERVLET_MV_ID=DDDataObject.DD_MULTIVIEW_PREFIX+DDDataObject.MULTIVIEW_SERVLETS; 47 private static final String HELP_ID_PREFIX=DDDataObject.HELP_ID_PREFIX_SERVLETS; 48 49 50 public ServletsMultiViewElement(final DDDataObject dObj, int index) { 51 super(dObj); 52 this.dObj=dObj; 53 this.index=index; 54 comp = new ToolBarDesignEditor(); 55 factory = new ServletPanelFactory(comp, dObj); 56 addAction = new AddAction(dObj, NbBundle.getMessage(ServletsMultiViewElement.class,"LBL_addServlet")); 57 removeAction = new RemoveAction(NbBundle.getMessage(ServletsMultiViewElement.class,"LBL_remove")); 58 setVisualEditor(comp); 59 repaintingTask = RequestProcessor.getDefault().create(new Runnable () { 60 public void run() { 61 javax.swing.SwingUtilities.invokeLater(new Runnable () { 62 public void run() { 63 repaintView(); 64 } 65 }); 66 } 67 }); 68 } 69 70 public SectionView getSectionView() { 71 return view; 72 } 73 74 public void componentShowing() { 75 super.componentShowing(); 76 dObj.setLastOpenView(index); 77 if (needInit || !dObj.isDocumentParseable()) { 78 repaintView(); 79 needInit=false; 80 } 81 } 82 83 public void componentOpened() { 84 super.componentOpened(); 85 dObj.getWebApp().addPropertyChangeListener(this); 86 } 87 88 public void componentClosed() { 89 super.componentClosed(); 90 dObj.getWebApp().removePropertyChangeListener(this); 91 } 92 93 public void propertyChange(java.beans.PropertyChangeEvent evt) { 94 if (!dObj.isChangedFromUI()) { 95 String name = evt.getPropertyName(); 96 if ( name.indexOf("Servlet")>0 ) { if (SERVLET_MV_ID.equals(dObj.getSelectedPerspective().preferredID())) { 99 repaintingTask.schedule(100); 100 } else { 101 needInit=true; 102 } 103 } 104 } 105 } 106 107 private void repaintView() { 108 webApp = dObj.getWebApp(); 109 view = new ServletsView(webApp); 110 comp.setContentView(view); 111 Object lastActive = comp.getLastActive(); 112 if (lastActive!=null) { 113 ((SectionView)view).openPanel(lastActive); 114 } else { 115 ServletsView servletsView = (ServletsView)view; 116 Node initialNode = servletsView.getServletsNode(); 117 Children ch = initialNode.getChildren(); 118 if (ch.getNodesCount()>0) 119 initialNode = ch.getNodes()[0]; 120 servletsView.selectNode(initialNode); 121 } 122 view.checkValidity(); 123 dObj.checkParseable(); 124 } 125 126 class ServletsView extends SectionView { 127 private SectionContainer servletsCont; 128 private Node servletsNode; 129 130 ServletsView (WebApp webApp) { 131 super(factory); 132 Servlet[] servlets = webApp.getServlet(); 133 Node[] nodes = new Node[servlets.length]; 134 Children ch = new Children.Array(); 135 for (int i=0;i<nodes.length;i++) { 136 nodes[i] = new ServletNode(this,webApp,servlets[i]); 137 } 138 ch.add(nodes); 139 servletsNode = new SectionContainerNode(ch); 140 servletsCont = new SectionContainer(this,servletsNode, 141 NbBundle.getMessage(ServletsMultiViewElement.class,"TTL_servlets"),false); 142 servletsCont.setHeaderActions(new javax.swing.Action []{addAction}); 143 Children servletsChildren = new Children.Array(); 144 servletsChildren.add(new Node[]{servletsNode}); 145 AbstractNode root = new AbstractNode(servletsChildren); 146 SectionPanel[] pan = new SectionPanel[servlets.length]; 147 for (int i=0;i<nodes.length;i++) { 148 pan[i] = new SectionPanel(this, nodes[i], getServletTitle(servlets[i]),servlets[i]); 149 pan[i].setHeaderActions(new javax.swing.Action []{removeAction}); 150 servletsCont.addSection(pan[i]); 151 } 152 addSection(servletsCont); 153 servletsNode.setDisplayName(NbBundle.getMessage(ServletsMultiViewElement.class,"TTL_servlets")); 155 servletsNode.setName(HELP_ID_PREFIX+"servletsNode"); setRoot(root); 157 } 158 159 Node getServletsNode() { 160 return servletsNode; 161 } 162 163 SectionContainer getServletsContainer(){ 164 return servletsCont; 165 } 166 167 String getServletTitle(Servlet servlet) { 168 String servletName=servlet.getServletName(); 169 if (servletName==null) servletName=""; 170 String mappings = DDUtils.urlPatternList(DDUtils.getUrlPatterns(webApp,servlet)); 171 return NbBundle.getMessage(ServletsMultiViewElement.class,"TTL_servletPanel",servletName,mappings); 172 } 173 174 public Error validateView() { 175 Error error = getSectionView().getErrorPanel().getError(); 176 if (error != null){ 177 return error; 178 } 179 return SectionValidator.validateServlets(webApp); 180 } 181 } 182 183 private class ServletNode extends org.openide.nodes.AbstractNode { 184 private Servlet servlet; 185 private WebApp webApp; 186 private SectionView view; 187 ServletNode(SectionView view, WebApp webApp, Servlet servlet) { 188 super(org.openide.nodes.Children.LEAF); 189 this.servlet=servlet; 190 this.webApp=webApp; 191 this.view=view; 192 setDisplayName(servlet.getServletName()); 193 setIconBaseWithExtension("org/netbeans/modules/j2ee/ddloaders/web/multiview/resources/class.gif"); } 195 public HelpCtx getHelpCtx() { 196 return new HelpCtx(HELP_ID_PREFIX+"servletNode"); } 198 } 199 200 private class AddAction extends javax.swing.AbstractAction { 201 202 AddAction(final DDDataObject dObj, String actionName) { 203 super(actionName); 204 char mnem = NbBundle.getMessage(ServletsMultiViewElement.class,"LBL_addServlet_mnem").charAt(0); 205 putValue(MNEMONIC_KEY,new Integer ((int)mnem)); 206 } 207 public void actionPerformed(java.awt.event.ActionEvent evt) { 208 209 String [] labels = new String []{ 210 NbBundle.getMessage(ServletsMultiViewElement.class,"LBL_servletName"), 211 NbBundle.getMessage(ServletsMultiViewElement.class,"LBL_servletClass"), 212 NbBundle.getMessage(ServletsMultiViewElement.class,"LBL_jspFile"), 213 NbBundle.getMessage(ServletsMultiViewElement.class,"LBL_description"), 214 NbBundle.getMessage(ServletsMultiViewElement.class,"LBL_urlPatterns") 215 }; 216 char[] mnem = new char[] { 217 NbBundle.getMessage(ServletsMultiViewElement.class,"LBL_servletName_mnem").charAt(0), 218 NbBundle.getMessage(ServletsMultiViewElement.class,"LBL_servletClass_mnem").charAt(0), 219 NbBundle.getMessage(ServletsMultiViewElement.class,"LBL_jspFile_mnem").charAt(0), 220 NbBundle.getMessage(ServletsMultiViewElement.class,"LBL_description_mnem").charAt(0), 221 NbBundle.getMessage(ServletsMultiViewElement.class,"LBL_urlPatterns_mnem").charAt(0) 222 }; 223 boolean[] buttons = new boolean[]{false,true,true,false,false}; 224 SimpleDialogPanel.DialogDescriptor descriptor = new SimpleDialogPanel.DialogDescriptor(labels); 225 descriptor.setMnemonics(mnem); 226 descriptor.setButtons(buttons); 227 descriptor.setTextField(new boolean[]{true,true,true,false,true}); 228 229 final SimpleDialogPanel dialogPanel = new SimpleDialogPanel(descriptor); 230 dialogPanel.getCustomizerButtons()[0].addActionListener(new java.awt.event.ActionListener () { 231 public void actionPerformed(java.awt.event.ActionEvent evt) { 232 try { 233 org.netbeans.api.project.SourceGroup[] groups = DDUtils.getJavaSourceGroups(dObj); 234 org.openide.filesystems.FileObject fo = BrowseFolders.showDialog(groups); 235 if (fo!=null) { 236 String className = DDUtils.getResourcePath(groups,fo); 237 dialogPanel.getTextComponents()[1].setText(className); 238 } 239 } catch (java.io.IOException ex) {} 240 } 241 }); 242 dialogPanel.getCustomizerButtons()[1].addActionListener(new java.awt.event.ActionListener () { 243 public void actionPerformed(java.awt.event.ActionEvent evt) { 244 try { 245 org.netbeans.api.project.SourceGroup[] groups = DDUtils.getDocBaseGroups(dObj); 246 org.openide.filesystems.FileObject fo = BrowseFolders.showDialog(groups); 247 if (fo!=null) { 248 String res = "/"+DDUtils.getResourcePath(groups,fo,'/',true); 249 dialogPanel.getTextComponents()[2].setText(res); 250 } 251 } catch (java.io.IOException ex) {} 252 } 253 }); 254 EditDialog dialog = new EditDialog(dialogPanel,NbBundle.getMessage(ServletsMultiViewElement.class,"TTL_Servlet"),true) { 255 protected String validate() { 256 String [] values = dialogPanel.getValues(); 257 String servletName = values[0].trim(); 258 String servletClass = values[1].trim(); 259 String jspFile = values[2].trim(); 260 String urlPatterns = values[4]; 261 return SectionValidator.validateNewServlet(dObj.getWebApp(), servletName, servletClass, jspFile, urlPatterns); 262 } 263 }; 264 dialog.setValid(false); 266 javax.swing.event.DocumentListener docListener = new EditDialog.DocListener(dialog); 267 dialogPanel.getTextComponents()[0].getDocument().addDocumentListener(docListener); 268 dialogPanel.getTextComponents()[1].getDocument().addDocumentListener(docListener); 269 dialogPanel.getTextComponents()[2].getDocument().addDocumentListener(docListener); 270 dialogPanel.getTextComponents()[4].getDocument().addDocumentListener(docListener); 271 272 java.awt.Dialog d = org.openide.DialogDisplayer.getDefault().createDialog(dialog); 273 d.setVisible(true); 274 dialogPanel.getTextComponents()[0].getDocument().removeDocumentListener(docListener); 275 dialogPanel.getTextComponents()[1].getDocument().removeDocumentListener(docListener); 276 dialogPanel.getTextComponents()[2].getDocument().removeDocumentListener(docListener); 277 dialogPanel.getTextComponents()[4].getDocument().removeDocumentListener(docListener); 278 279 if (dialog.getValue().equals(EditDialog.OK_OPTION)) { 280 String [] values = dialogPanel.getValues(); 281 dObj.modelUpdatedFromUI(); 282 dObj.setChangedFromUI(true); 283 try { 284 Servlet servlet = (Servlet)webApp.createBean("Servlet"); servlet.setServletName(values[0].trim()); 286 String servletClass = values[1].trim(); 287 if (servletClass.length()>0) servlet.setServletClass(servletClass); 288 else servlet.setJspFile(values[2].trim()); 289 String desc = values[3].trim(); 290 if (desc.length()>0) servlet.setDescription(desc); 291 webApp.addServlet(servlet); 292 293 String [] patterns = DDUtils.getStringArray(values[4]); 294 DDUtils.addServletMappings(webApp,servlet,patterns); 295 296 ServletsView view = (ServletsView)comp.getContentView(); 297 Node node = new ServletNode(view, webApp, servlet); 298 view.getServletsNode().getChildren().add(new Node[]{node}); 299 300 SectionPanel pan = new SectionPanel(view, node, view.getServletTitle(servlet), servlet); 301 pan.setHeaderActions(new javax.swing.Action []{removeAction}); 302 view.getServletsContainer().addSection(pan, true); 303 } catch (ClassNotFoundException ex){} 304 finally { 305 dObj.setChangedFromUI(false); 306 } 307 } 308 } 309 } 310 private class RemoveAction extends javax.swing.AbstractAction { 311 312 RemoveAction(String actionName) { 313 super(actionName); 314 char mnem = NbBundle.getMessage(ServletsMultiViewElement.class,"LBL_remove_mnem").charAt(0); 315 putValue(MNEMONIC_KEY,new Integer ((int)mnem)); 316 } 317 318 public void actionPerformed(java.awt.event.ActionEvent evt) { 319 org.openide.DialogDescriptor desc = new ConfirmDialog( 320 NbBundle.getMessage(ServletsMultiViewElement.class,"TXT_removeServletConfirm")); 321 java.awt.Dialog dialog = org.openide.DialogDisplayer.getDefault().createDialog(desc); 322 dialog.setVisible(true); 323 if (org.openide.DialogDescriptor.OK_OPTION.equals(desc.getValue())) { 324 SectionPanel sectionPanel = ((SectionPanel.HeaderButton)evt.getSource()).getSectionPanel(); 325 Servlet servlet = (Servlet)sectionPanel.getKey(); 326 dObj.modelUpdatedFromUI(); 328 dObj.setChangedFromUI(true); 329 try { 330 DDUtils.removeServletMappings(webApp,servlet.getServletName()); 331 DDUtils.removeFilterMappingsForServlet(webApp,servlet.getServletName()); 332 webApp.removeServlet(servlet); 333 334 sectionPanel.getSectionView().removeSection(sectionPanel.getNode()); 336 } finally { 337 dObj.setChangedFromUI(false); 338 } 339 } 340 } 341 } 342 } 343 | Popular Tags |