KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > web > multiview > ServletsMultiViewElement


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.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 /**
33  *
34  * @author mkuchtiak
35  */

36 public class ServletsMultiViewElement extends ToolBarMultiViewElement implements java.beans.PropertyChangeListener JavaDoc {
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 JavaDoc addAction, removeAction;
43     private boolean needInit=true;
44     private int index;
45     private RequestProcessor.Task repaintingTask;
46     private static final String JavaDoc SERVLET_MV_ID=DDDataObject.DD_MULTIVIEW_PREFIX+DDDataObject.MULTIVIEW_SERVLETS;
47     private static final String JavaDoc HELP_ID_PREFIX=DDDataObject.HELP_ID_PREFIX_SERVLETS;
48     
49     /** Creates a new instance of DDMultiViewElement */
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 JavaDoc() {
60             public void run() {
61                 javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
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 JavaDoc evt) {
94         if (!dObj.isChangedFromUI()) {
95             String JavaDoc name = evt.getPropertyName();
96             if ( name.indexOf("Servlet")>0 ) { //NOI18
97
// repaint view if the wiew is active and something is changed with servlets
98
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 JavaDoc 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 JavaDoc[]{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 JavaDoc[]{removeAction});
150                 servletsCont.addSection(pan[i]);
151             }
152             addSection(servletsCont);
153             //root.setDisplayName("<Servlets>");
154
servletsNode.setDisplayName(NbBundle.getMessage(ServletsMultiViewElement.class,"TTL_servlets"));
155             servletsNode.setName(HELP_ID_PREFIX+"servletsNode"); //NOI18N
156
setRoot(root);
157         }
158         
159         Node getServletsNode() {
160             return servletsNode;
161         }
162         
163         SectionContainer getServletsContainer(){
164             return servletsCont;
165         }
166         
167         String JavaDoc getServletTitle(Servlet servlet) {
168             String JavaDoc servletName=servlet.getServletName();
169             if (servletName==null) servletName="";
170             String JavaDoc mappings = DDUtils.urlPatternList(DDUtils.getUrlPatterns(webApp,servlet));
171             return NbBundle.getMessage(ServletsMultiViewElement.class,"TTL_servletPanel",servletName,mappings);
172         }
173         
174         public Error JavaDoc validateView() {
175             Error JavaDoc 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"); //NOI18N
194
}
195         public HelpCtx getHelpCtx() {
196             return new HelpCtx(HELP_ID_PREFIX+"servletNode"); //NOI18N
197
}
198     }
199
200     private class AddAction extends javax.swing.AbstractAction JavaDoc {
201         
202         AddAction(final DDDataObject dObj, String JavaDoc actionName) {
203             super(actionName);
204             char mnem = NbBundle.getMessage(ServletsMultiViewElement.class,"LBL_addServlet_mnem").charAt(0);
205             putValue(MNEMONIC_KEY,new Integer JavaDoc((int)mnem));
206         }
207         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
208
209             String JavaDoc[] labels = new String JavaDoc[]{
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 JavaDoc() {
231                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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 JavaDoc className = DDUtils.getResourcePath(groups,fo);
237                             dialogPanel.getTextComponents()[1].setText(className);
238                         }
239                     } catch (java.io.IOException JavaDoc ex) {}
240                 }
241             });
242             dialogPanel.getCustomizerButtons()[1].addActionListener(new java.awt.event.ActionListener JavaDoc() {
243                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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 JavaDoc res = "/"+DDUtils.getResourcePath(groups,fo,'/',true);
249                             dialogPanel.getTextComponents()[2].setText(res);
250                         }
251                     } catch (java.io.IOException JavaDoc ex) {}
252                 }
253             });
254             EditDialog dialog = new EditDialog(dialogPanel,NbBundle.getMessage(ServletsMultiViewElement.class,"TTL_Servlet"),true) {
255                 protected String JavaDoc validate() {
256                     String JavaDoc[] values = dialogPanel.getValues();
257                     String JavaDoc servletName = values[0].trim();
258                     String JavaDoc servletClass = values[1].trim();
259                     String JavaDoc jspFile = values[2].trim();
260                     String JavaDoc urlPatterns = values[4];
261                     return SectionValidator.validateNewServlet(dObj.getWebApp(), servletName, servletClass, jspFile, urlPatterns);
262                 }
263             };
264             dialog.setValid(false); // disable OK button
265

266             javax.swing.event.DocumentListener JavaDoc 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 JavaDoc 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 JavaDoc[] values = dialogPanel.getValues();
281                 dObj.modelUpdatedFromUI();
282                 dObj.setChangedFromUI(true);
283                 try {
284                     Servlet servlet = (Servlet)webApp.createBean("Servlet"); //NOI18N
285
servlet.setServletName(values[0].trim());
286                     String JavaDoc servletClass = values[1].trim();
287                     if (servletClass.length()>0) servlet.setServletClass(servletClass);
288                     else servlet.setJspFile(values[2].trim());
289                     String JavaDoc desc = values[3].trim();
290                     if (desc.length()>0) servlet.setDescription(desc);
291                     webApp.addServlet(servlet);
292                     
293                     String JavaDoc[] 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 JavaDoc[]{removeAction});
302                     view.getServletsContainer().addSection(pan, true);
303                 } catch (ClassNotFoundException JavaDoc ex){}
304                 finally {
305                     dObj.setChangedFromUI(false);
306                 }
307             }
308         }
309     }
310     private class RemoveAction extends javax.swing.AbstractAction JavaDoc {
311         
312         RemoveAction(String JavaDoc actionName) {
313             super(actionName);
314             char mnem = NbBundle.getMessage(ServletsMultiViewElement.class,"LBL_remove_mnem").charAt(0);
315             putValue(MNEMONIC_KEY,new Integer JavaDoc((int)mnem));
316         }
317         
318         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
319             org.openide.DialogDescriptor desc = new ConfirmDialog(
320                 NbBundle.getMessage(ServletsMultiViewElement.class,"TXT_removeServletConfirm"));
321             java.awt.Dialog JavaDoc 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                 // updating data model
327
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                     // removing section
335
sectionPanel.getSectionView().removeSection(sectionPanel.getNode());
336                 } finally {
337                     dObj.setChangedFromUI(false);
338                 }
339             }
340         }
341     }
342 }
343
Popular Tags