KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > web > DDDataObject


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;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.awt.event.*;
25 import java.beans.*;
26 import java.io.*;
27 import java.util.*;
28 import javax.swing.JButton JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30
31 import org.openide.DialogDescriptor;
32 import org.openide.filesystems.*;
33 import org.openide.loaders.*;
34 import org.openide.util.HelpCtx;
35 import org.openide.util.RequestProcessor;
36
37 import org.xml.sax.*;
38 import org.openide.ErrorManager;
39 import org.openide.util.NbBundle;
40
41 import org.netbeans.modules.j2ee.ddloaders.web.event.*;
42 import org.netbeans.modules.j2ee.dd.api.web.*;
43 import org.netbeans.modules.j2ee.dd.api.common.InitParam;
44 import org.netbeans.modules.j2ee.dd.api.common.RootInterface;
45 import org.netbeans.api.xml.cookies.ValidateXMLCookie;
46 import org.netbeans.api.xml.cookies.CheckXMLCookie;
47 import org.netbeans.spi.xml.cookies.*;
48 import org.openide.DialogDisplayer;
49 import org.netbeans.modules.j2ee.dd.impl.web.WebAppProxy;
50 import org.netbeans.modules.j2ee.dd.impl.web.WebParseUtils;
51 import org.netbeans.api.java.project.JavaProjectConstants;
52 import org.netbeans.api.project.FileOwnerQuery;
53 import org.netbeans.api.project.Project;
54 import org.netbeans.api.project.ProjectUtils;
55
56 import org.netbeans.modules.web.api.webmodule.WebModule;
57 import org.netbeans.api.project.SourceGroup;
58 import org.netbeans.api.project.Sources;
59
60 import org.netbeans.modules.j2ee.ddloaders.web.multiview.*;
61 import org.netbeans.modules.j2ee.ddloaders.multiview.DDMultiViewDataObject;
62 import org.netbeans.modules.xml.multiview.DesignMultiViewDesc;
63 import org.netbeans.modules.xml.multiview.ToolBarMultiViewElement;
64
65 /** Represents a DD object in the Repository.
66  *
67  * @author mkuchtiak
68  */

69 public class DDDataObject extends DDMultiViewDataObject
70     implements DDChangeListener, ChangeListener JavaDoc, PropertyChangeListener {
71     private transient WebApp webApp;
72     private transient FileObject srcRoots[];
73     protected transient final static RequestProcessor RP = new RequestProcessor("XML Parsing"); // NOI18N
74
protected boolean changedFromUI;
75
76     private static final long serialVersionUID = 8857563089355069362L;
77
78     /** Property name for documentDTD property */
79     public static final String JavaDoc PROP_DOCUMENT_DTD = "documentDTD"; // NOI18N
80
public static final String JavaDoc HELP_ID_PREFIX_OVERVIEW="dd_multiview_overview_"; //NOI18N
81
public static final String JavaDoc HELP_ID_PREFIX_SERVLETS="dd_multiview_servlets_"; //NOI18N
82
public static final String JavaDoc HELP_ID_PREFIX_FILTERS="dd_multiview_filters_"; //NOI18N
83
public static final String JavaDoc HELP_ID_PREFIX_PAGES="dd_multiview_pages_"; //NOI18N
84
public static final String JavaDoc HELP_ID_PREFIX_REFERENCES="dd_multiview_references_"; //NOI18N
85
public static final String JavaDoc HELP_ID_PREFIX_SECURITY="dd_multiview_security_"; //NOI18N
86

87
88     /** Holder of documentDTD property value */
89     private String JavaDoc documentDTD;
90
91     /** List of updates to servlets that should be processed */
92     private Vector updates;
93
94     private transient RequestProcessor.Task updateTask;
95     private transient FileObjectObserver fileListener;
96
97     public DDDataObject (FileObject pf, DDDataLoader loader) throws DataObjectExistsException {
98         super (pf, loader);
99         init (pf,loader);
100     }
101
102     private void init (FileObject fo,DDDataLoader loader) {
103         // added ValidateXMLCookie
104
InputSource in = DataObjectAdapters.inputSource(this);
105         CheckXMLCookie checkCookie = new CheckXMLSupport(in);
106         getCookieSet().add(checkCookie);
107         ValidateXMLCookie validateCookie = new ValidateXMLSupport(in);
108         getCookieSet().add(validateCookie);
109
110         fileListener = new FileObjectObserver(fo);
111
112         Project project = FileOwnerQuery.getOwner (getPrimaryFile ());
113         if (project != null) {
114             Sources sources = ProjectUtils.getSources(project);
115             sources.addChangeListener (this);
116         }
117         refreshSourceFolders ();
118         addPropertyChangeListener(this);
119     }
120
121     private void refreshSourceFolders () {
122         ArrayList srcRootList = new ArrayList ();
123
124         Project project = FileOwnerQuery.getOwner (getPrimaryFile ());
125         if (project != null) {
126             Sources sources = ProjectUtils.getSources(project);
127             SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
128             for (int i = 0; i < groups.length; i++) {
129                 if (WebModule.getWebModule (groups [i].getRootFolder ()) != null) {
130                     srcRootList.add (groups [i].getRootFolder ());
131                     DataLoaderPool.getDefault().removeOperationListener(operationListener); //avoid being added multiple times
132
DataLoaderPool.getDefault().addOperationListener(operationListener);
133                 }
134             }
135         }
136         srcRoots = (FileObject []) srcRootList.toArray (new FileObject [srcRootList.size ()]);
137     }
138
139     private String JavaDoc getPackageName (FileObject clazz) {
140         for (int i = 0; i < srcRoots.length; i++) {
141             String JavaDoc rp = FileUtil.getRelativePath (srcRoots [i], clazz);
142             if (rp != null) {
143                 if (clazz.getExt ().length () > 0) {
144                     rp = rp.substring (0, rp.length () - clazz.getExt ().length () - 1);
145                 }
146                 return rp.replace ('/', '.');
147             }
148         }
149         return null;
150     }
151
152     public WebApp getWebApp() {
153         if (webApp == null) {
154             try {
155                 webApp = createWebApp();
156             } catch (IOException ex) {
157                 ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
158             }
159         }
160         return webApp;
161     }
162
163     public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
164         if (DDDataObject.PROP_DOCUMENT_VALID.equals (evt.getPropertyName ())) {
165             ((DDDataNode)getNodeDelegate()).iconChanged();
166         }
167     }
168
169     private WebApp createWebApp() throws java.io.IOException JavaDoc {
170         WebApp webApp = DDProvider.getDefault().getDDRoot(getPrimaryFile());
171         if (webApp != null) {
172             setSaxError(webApp.getError());
173         }
174         return webApp;
175     }
176
177     protected org.openide.nodes.Node createNodeDelegate () {
178         return new DDDataNode(this);
179     }
180
181     /**
182      * Sets only reasonable mappings (mappings with existing servlet element
183      * @param mappings - all mappings
184      */

185     public void setReasonableMappings(ServletMapping[] mappings) {
186         List newMappings = new ArrayList();
187         Servlet[] servlets = webApp.getServlet();
188         for (int i=0;i<mappings.length;i++) {
189             for (int j=0;j<servlets.length;j++) {
190                 if (servlets[j].getServletName().equals(mappings[i].getServletName())) {
191                     newMappings.add(mappings[i]);
192                     break;
193                 }
194             }
195         }
196         ServletMapping[] maps = new ServletMapping[newMappings.size()];
197         newMappings.toArray(maps);
198         webApp.setServletMapping(maps);
199         //setNodeDirty(true);
200
//modelUpdatedFromUI();
201
}
202
203     protected void parseDocument() throws IOException {
204         if (webApp == null || ((WebAppProxy) webApp).getOriginal() == null) {
205             try {
206                 webApp = DDProvider.getDefault().getDDRoot(getPrimaryFile());
207             } catch (IOException e) {
208                 if (webApp == null) {
209                     webApp = new WebAppProxy(null, null);
210                 }
211             }
212         }
213         // update model with the document
214
parseDocument(true);
215     }
216
217     protected void validateDocument() throws IOException {
218         // parse document without updating model
219
parseDocument(false);
220     }
221
222     private void parseDocument(boolean updateWebApp) throws IOException {
223         WebAppProxy webAppProxy = (WebAppProxy) webApp;
224         try {
225             // preparsing
226
SAXParseException error = WebParseUtils.parse(new InputSource(createReader()));
227             setSaxError(error);
228
229             String JavaDoc version = WebParseUtils.getVersion(new InputSource(createReader()));
230             // creating model
231
WebAppProxy app = new WebAppProxy(org.netbeans.modules.j2ee.dd.impl.common.DDUtils.createWebApp(
232                     createInputStream(), version), version);
233             if (updateWebApp) {
234                 if (version.equals(webAppProxy.getVersion()) && webAppProxy.getOriginal() != null) {
235                     webApp.merge(app, WebApp.MERGE_UPDATE);
236                 } else if (app.getOriginal() != null) {
237                     webApp = webAppProxy = app;
238                 }
239             }
240             webAppProxy.setStatus(error != null ? WebApp.STATE_INVALID_PARSABLE : WebApp.STATE_VALID);
241             webAppProxy.setError(error);
242         } catch (SAXException ex) {
243             webAppProxy.setStatus(WebApp.STATE_INVALID_UNPARSABLE);
244             if (ex instanceof SAXParseException) {
245                 webAppProxy.setError((SAXParseException) ex);
246             } else if (ex.getException() instanceof SAXParseException) {
247                 webAppProxy.setError((SAXParseException) ex.getException());
248             }
249             setSaxError(ex);
250         }
251     }
252
253     protected RootInterface getDDModel() {
254         return getWebApp();
255     }
256
257     public boolean isDocumentParseable() {
258         return WebApp.STATE_INVALID_UNPARSABLE != getWebApp().getStatus();
259     }
260
261     protected String JavaDoc getPrefixMark() {
262         return "<web-app";
263     }
264
265     /**
266      * Adds servlet and servlet-mapping elements to map servlet.
267      *
268      * One servlet element and one matching servlet-mapping element. The servlet-name is
269      * set to Servlet_&lt clazz&gt by default.
270      *
271      * @param clazz class name of servlet
272      * @param urlPattern path to servlet class (pkg/foo/Bar)
273      */

274     private void createDefaultServletConfiguration (String JavaDoc clazz, String JavaDoc urlPattern) {
275         // PENDING: should be synchronized
276
WebApp wappTo = getWebApp ();
277         try {
278             Servlet newSrvlt = (Servlet)webApp.createBean("Servlet");
279             newSrvlt.setServletClass (clazz);
280             String JavaDoc name = DDUtils.findFreeName (wappTo.getServlet (), "ServletName" , "Servlet_"+clazz); // NOI18N
281
newSrvlt.setServletName (name);
282             newSrvlt.setDescription (NbBundle.getMessage (DDDataObject.class, "TXT_newServletElementDescription"));
283             newSrvlt.setDisplayName ("Servlet "+clazz); // NOI18N
284
wappTo.addServlet (newSrvlt);
285
286             ServletMapping newSM = (ServletMapping)webApp.createBean("ServletMapping");
287             newSM.setServletName (name);
288             newSM.setUrlPattern (urlPattern);
289             wappTo.addServletMapping (newSM);
290
291             //setNodeDirty (true);
292
//modelUpdatedFromUI();
293
} catch (ClassNotFoundException JavaDoc ex) {}
294     }
295
296     protected DataObject handleCopy(DataFolder f) throws IOException {
297         DataObject dObj = super.handleCopy(f);
298         try { dObj.setValid(false); }catch(java.beans.PropertyVetoException JavaDoc e){}
299         return dObj;
300     }
301
302     protected void dispose () {
303         // no more changes in DD
304
synchronized (this) {
305             updates = null;
306             if (updateTask != null) {
307                 updateTask.cancel();
308             }
309         }
310         super.dispose ();
311     }
312
313     /** Getter for property documentDTD.
314      * @return Value of property documentDTD or <CODE>null</CODE> if documentDTD cannot be obtained.
315      */

316     public String JavaDoc getDocumentDTD () {
317         if (documentDTD == null) {
318             WebApp wa = getWebApp ();
319         }
320         return documentDTD;
321     }
322
323     /** This methods gets called when servlet is changed
324      * @param evt - object that describes the change.
325      */

326     public void deploymentChange (DDChangeEvent evt) {
327         // fix of #28542, don't add servlet, if it's already defined in DD
328
if (evt.getType() == DDChangeEvent.SERVLET_ADDED && servletDefined(evt.getNewValue())) {
329             return;
330         }
331
332         // these are now handled in j2ee/refactoring - see #70389.
333
else if (evt.getType() == DDChangeEvent.LISTENER_DELETED
334                 || evt.getType() == DDChangeEvent.FILTER_DELETED
335                 || evt.getType() == DDChangeEvent.SERVLET_DELETED){
336             
337             return;
338         }
339         
340         synchronized (this) {
341             if (updates == null) {
342                 updates = new Vector ();
343             }
344             updates.addElement (evt);
345         }
346
347         // schedule processDDChangeEvent
348
if (updateTask == null) {
349             updateTask = RequestProcessor.getDefault().post (new Runnable JavaDoc () {
350                 public void run () {
351                     java.util.List JavaDoc changes = null;
352                     synchronized (DDDataObject.this) {
353                         if (!DDDataObject.this.isValid()) {
354                             return;
355                         }
356                         if (updates != null) {
357                             changes = updates;
358                             updates = null;
359                         }
360                     }
361                     if (changes != null) {
362                         showDDChangesDialog(changes);
363                     }
364                 }
365             }, 2000, Thread.MIN_PRIORITY);
366         }
367         else {
368             updateTask.schedule (2000);
369         }
370     }
371
372     private boolean servletDefined(String JavaDoc classname) {
373         WebApp webApp = getWebApp();
374         if (webApp == null) {
375             return true;
376         }
377         Servlet[] servlets = webApp.getServlet();
378         for ( int i = 0; i < servlets.length; i++ ) {
379             if (servlets[i].getServletClass() != null && servlets[i].getServletClass().equals(classname)) {
380                 return true;
381             }
382         }
383         return false;
384     }
385
386     private void showDDChangesDialog (List changes) {
387         final JButton JavaDoc processButton;
388         final JButton JavaDoc processAllButton;
389         final JButton JavaDoc closeButton;
390         final DDChangesPanel connectionPanel;
391         final DialogDescriptor confirmChangesDescriptor;
392         final Dialog JavaDoc confirmChangesDialog[] = { null };
393
394         processButton = new JButton JavaDoc (NbBundle.getMessage (DDDataObject.class, "LAB_processButton"));
395         processButton.setMnemonic (NbBundle.getMessage (DDDataObject.class, "LAB_processButton_Mnemonic").charAt (0));
396         processButton.setToolTipText (NbBundle.getMessage (DDDataObject.class, "ACS_processButtonA11yDesc"));
397         processAllButton = new JButton JavaDoc (NbBundle.getMessage (DDDataObject.class, "LAB_processAllButton"));
398         processAllButton.setMnemonic (NbBundle.getMessage (DDDataObject.class, "LAB_processAllButton_Mnemonic").charAt (0));
399         processAllButton.setToolTipText (NbBundle.getMessage (DDDataObject.class, "ACS_processAllButtonA11yDesc"));
400         closeButton = new JButton JavaDoc (NbBundle.getMessage (DDDataObject.class, "LAB_closeButton"));
401         closeButton.setMnemonic (NbBundle.getMessage (DDDataObject.class, "LAB_closeButton_Mnemonic").charAt (0));
402         closeButton.setToolTipText (NbBundle.getMessage (DDDataObject.class, "ACS_closeButtonA11yDesc"));
403         final Object JavaDoc [] options = new Object JavaDoc [] {
404             processButton,
405             processAllButton
406         };
407         final Object JavaDoc [] additionalOptions = new Object JavaDoc [] {
408             closeButton
409         };
410         WebModule wm = WebModule.getWebModule(getPrimaryFile ());
411         String JavaDoc fsname=""; // NOI18N
412
if (wm!=null) {
413             fsname=wm.getContextPath();
414         }
415         String JavaDoc caption = NbBundle.getMessage (DDDataObject.class, "MSG_SynchronizeCaption", fsname);
416         connectionPanel = new DDChangesPanel (caption, processButton);
417         confirmChangesDescriptor = new DialogDescriptor (
418             connectionPanel,
419             NbBundle.getMessage (DDDataObject.class, "LAB_ConfirmDialog"),
420             true,
421             options,
422             processButton,
423             DialogDescriptor.RIGHT_ALIGN,
424             HelpCtx.DEFAULT_HELP,
425             new ActionListener () {
426                 public void actionPerformed (ActionEvent e) {
427                     if (e.getSource () instanceof Component JavaDoc) {
428                         Component JavaDoc root;
429
430                         // hack to avoid multiple calls for disposed dialogs:
431
root = javax.swing.SwingUtilities.getRoot ((Component JavaDoc)e.getSource ());
432                         if (!root.isDisplayable ()) {
433                             return;
434                         }
435                     }
436                     if (options[0].equals (e.getSource ())) {
437                         int min = connectionPanel.changesList.getMinSelectionIndex ();
438                         int max = connectionPanel.changesList.getMaxSelectionIndex ();
439                         for (int i = max; i >= min; i--) {
440                             if (connectionPanel.changesList.isSelectedIndex (i)) {
441                                 final DDChangeEvent ev = (DDChangeEvent)connectionPanel.listModel.getElementAt (i);
442                                 processDDChangeEvent (ev);
443                                 connectionPanel.listModel.removeElementAt (i);
444                             }
445                         }
446                         if (connectionPanel.listModel.isEmpty ()) {
447                             confirmChangesDialog[0].setVisible (false);
448                         }
449                         else {
450                             processButton.setEnabled (false);
451                         }
452                     }
453                     else if (options[1].equals (e.getSource ())) {
454                         Enumeration en = connectionPanel.listModel.elements ();
455                         while (en.hasMoreElements ()) {
456                             processDDChangeEvent ((DDChangeEvent)en.nextElement ());
457                         }
458                         confirmChangesDialog[0].setVisible (false);
459                         connectionPanel.setChanges (null);
460                     }
461                     else if (additionalOptions[0].equals (e.getSource ())) {
462                         confirmChangesDialog[0].setVisible (false);
463                         connectionPanel.setChanges (null);
464                     }
465                 }
466             }
467         );
468         confirmChangesDescriptor.setAdditionalOptions (additionalOptions);
469
470         processButton.setEnabled (false);
471         processAllButton.requestFocus ();
472         connectionPanel.setChanges (changes);
473
474         try {
475             confirmChangesDialog[0] = DialogDisplayer.getDefault ().createDialog (confirmChangesDescriptor);
476             confirmChangesDialog[0].setVisible(true);
477         } finally {
478             confirmChangesDialog[0].dispose ();
479         }
480     }
481
482     private void processDDChangeEvent (DDChangeEvent evt) {
483         if (!isValid()) {
484             return;
485         }
486
487         if (evt.getType () == DDChangeEvent.SERVLET_ADDED) {
488             String JavaDoc clz = evt.getNewValue ();
489
490             // new from template or copy of another servlet
491
String JavaDoc urimapping = "/servlet/"+clz; // NOI18N
492
createDefaultServletConfiguration (clz, urimapping);
493         }
494         else if (evt.getType () == DDChangeEvent.SERVLET_CHANGED) {
495             // update servlet-class in servlet element
496
String JavaDoc old = evt.getOldValue ();
497             if (old == null) {
498                 return;
499             }
500
501             Servlet [] servlets = getWebApp ().getServlet ();
502             for (int i=0; i<servlets.length; i++) {
503                 if (old.equals (servlets[i].getServletClass ())) {
504                     servlets[i].setServletClass ((String JavaDoc)evt.getNewValue ());
505                 }
506             }
507         }
508         else if (evt.getType () == DDChangeEvent.SERVLET_DELETED) {
509             // delete servlet and matching servlet-mappings
510
String JavaDoc clz = evt.getNewValue ();
511             if (clz == null) {
512                 return;
513             }
514
515             WebApp wa = getWebApp ();
516             Servlet [] servlets = wa.getServlet ();
517             java.util.Vector JavaDoc servletNames = new java.util.Vector JavaDoc ();
518             for (int i=0; i<servlets.length; i++) {
519                 if (clz.equals (servlets[i].getServletClass ())) {
520                     servletNames.addElement (servlets[i].getServletName ());
521                     wa.removeServlet (servlets[i]);
522                 }
523             }
524             ServletMapping [] mappings = wa.getServletMapping ();
525             for (int i=0; i<mappings.length; i++) {
526                 if (servletNames.contains (mappings[i].getServletName ())) {
527                     wa.removeServletMapping (mappings[i]);
528                 }
529             }
530         }
531         else if (evt.getType () == DDChangeEvent.FILTER_CHANGED) {
532             String JavaDoc old = evt.getOldValue ();
533             if (old == null) {
534                 return;
535             }
536
537             Filter [] filters = getWebApp ().getFilter ();
538             for (int i=0; i<filters.length; i++) {
539                 if (old.equals (filters[i].getFilterClass ())) {
540                     filters[i].setFilterClass ((String JavaDoc)evt.getNewValue ());
541                 }
542             }
543         }
544         else if (evt.getType () == DDChangeEvent.FILTER_DELETED) {
545             String JavaDoc clz = evt.getNewValue ();
546             if (clz == null) {
547                 return;
548             }
549
550             WebApp wa = getWebApp ();
551             Filter [] filters = wa.getFilter ();
552             java.util.Vector JavaDoc filterNames = new java.util.Vector JavaDoc ();
553             for (int i=0; i<filters.length; i++) {
554                 if (clz.equals (filters[i].getFilterClass ())) {
555                     filterNames.addElement (filters[i].getFilterName ());
556                     wa.removeFilter (filters[i]);
557                 }
558             }
559             FilterMapping [] mappings = wa.getFilterMapping ();
560             for (int i=0; i<mappings.length; i++) {
561                 if (filterNames.contains (mappings[i].getFilterName ())) {
562                     wa.removeFilterMapping (mappings[i]);
563                 }
564             }
565         }
566         else if (evt.getType () == DDChangeEvent.LISTENER_CHANGED) {
567             String JavaDoc old = evt.getOldValue ();
568             if (old == null) {
569                 return;
570             }
571
572             Listener JavaDoc [] listeners = getWebApp ().getListener ();
573             for (int i=0; i<listeners.length; i++) {
574                 if (old.equals (listeners[i].getListenerClass ())) {
575                     listeners[i].setListenerClass ((String JavaDoc)evt.getNewValue ());
576                 }
577             }
578         }
579         else if (evt.getType () == DDChangeEvent.LISTENER_DELETED) {
580             String JavaDoc clz = evt.getNewValue ();
581             if (clz == null) {
582                 return;
583             }
584
585             WebApp wa = getWebApp ();
586             Listener JavaDoc [] listeners = wa.getListener ();
587             for (int i=0; i<listeners.length; i++) {
588                 if (clz.equals (listeners[i].getListenerClass ())) {
589                     wa.removeListener (listeners[i]);
590                     break;
591                 }
592             }
593         }
594         try {
595             writeModel(getWebApp());
596         } catch (IOException e) {
597             ErrorManager.getDefault().notify(e);
598         }
599     }
600
601     private OperationListener operationListener = new OperationAdapter() {
602         public void operationDelete(OperationEvent ev) {
603             FileObject fo = ev.getObject().getPrimaryFile();
604             String JavaDoc resourceName = getPackageName (fo);
605             if (resourceName != null && "java".equals(fo.getExt()) && getWebApp() != null) { //NOI18N
606
boolean foundElement=false;
607                 Servlet[] servlets = getWebApp().getServlet();
608                 for (int i=0;i<servlets.length;i++) {
609                     if (resourceName.equals(servlets[i].getServletClass())) {
610                         DDChangeEvent ddEvent = new DDChangeEvent(DDDataObject.this,DDDataObject.this,null,resourceName,DDChangeEvent.SERVLET_DELETED);
611                         deploymentChange (ddEvent);
612                         foundElement=true;
613                         break;
614                     }
615                 }
616                 if (foundElement) {
617                     return;
618                 }
619                 Filter[] filters = getWebApp().getFilter();
620                 for (int i=0;i<filters.length;i++) {
621                     if (resourceName.equals(filters[i].getFilterClass())) {
622                         DDChangeEvent ddEvent = new DDChangeEvent(DDDataObject.this,DDDataObject.this,null,resourceName,DDChangeEvent.FILTER_DELETED);
623                         deploymentChange (ddEvent);
624                         foundElement=true;
625                         break;
626                     }
627                 }
628                 if (foundElement) {
629                     return;
630                 }
631                 Listener JavaDoc[] listeners = getWebApp().getListener();
632                 for (int i=0;i<listeners.length;i++) {
633                     if (resourceName.equals(listeners[i].getListenerClass())) {
634                         DDChangeEvent ddEvent = new DDChangeEvent(DDDataObject.this,DDDataObject.this,null,resourceName,DDChangeEvent.LISTENER_DELETED);
635                         deploymentChange (ddEvent);
636                         break; // listener with that class should be only one
637
}
638                 }
639             }
640         }
641     };
642
643     public void stateChanged (javax.swing.event.ChangeEvent JavaDoc e) {
644         refreshSourceFolders ();
645     }
646
647     public HelpCtx getHelpCtx() {
648         return new HelpCtx(HELP_ID_PREFIX_OVERVIEW+"overviewNode"); //NOI18N
649
}
650
651     /** Used to detect if data model has already been created or not.
652      * Method is called before switching to the design view from XML view when the document isn't parseable.
653      */

654     protected boolean isModelCreated() {
655         return (webApp!=null && ((org.netbeans.modules.j2ee.dd.impl.web.WebAppProxy)webApp).getOriginal()!=null);
656     }
657
658     /** WeakListener for accepting external changes to web.xml
659     */

660     private class FileObjectObserver implements FileChangeListener {
661         FileObjectObserver (FileObject fo) {
662             fo.addFileChangeListener((FileChangeListener)org.openide.util.WeakListeners.create(
663                                         FileChangeListener.class, this, fo));
664         }
665
666         public void fileAttributeChanged(FileAttributeEvent fileAttributeEvent) {
667         }
668
669         public void fileChanged(FileEvent fileEvent) {
670             /*
671            WebAppProxy webApp = (WebAppProxy) DDDataObject.this.getWebApp();
672            boolean needRewriting = true;
673            if (webApp!= null && webApp.isWriting()) { // change from outside
674                webApp.setWriting(false);
675                needRewriting=false;
676            }
677            if (isSavingDocument()) {// document is being saved
678                setSavingDocument(false);
679                needRewriting=false;
680            }
681            if (needRewriting) getEditorSupport().restartTimer();
682             */

683         }
684
685         public void fileDataCreated(FileEvent fileEvent) {
686         }
687
688         public void fileDeleted(FileEvent fileEvent) {
689         }
690
691         public void fileFolderCreated(FileEvent fileEvent) {
692         }
693
694         public void fileRenamed(FileRenameEvent fileRenameEvent) {
695         }
696     }
697
698
699     public static final String JavaDoc DD_MULTIVIEW_PREFIX = "dd_multiview"; // NOI18N
700
public static final String JavaDoc MULTIVIEW_OVERVIEW = "Overview"; // NOI18N
701
public static final String JavaDoc MULTIVIEW_SERVLETS = "Servlets"; // NOI18N
702
public static final String JavaDoc MULTIVIEW_FILTERS = "Filters"; // NOI18N
703
public static final String JavaDoc MULTIVIEW_PAGES = "Pages"; // NOI18N
704
public static final String JavaDoc MULTIVIEW_REFERENCES = "References"; // NOI18N
705
public static final String JavaDoc MULTIVIEW_SECURITY = "Security"; //NOI18N
706

707     private ServletsMultiViewElement servletMVElement;
708
709     protected DesignMultiViewDesc[] getMultiViewDesc() {
710         return new DesignMultiViewDesc[] {
711             new DDView(this,MULTIVIEW_OVERVIEW),
712             new DDView(this,MULTIVIEW_SERVLETS),
713             new DDView(this,MULTIVIEW_FILTERS),
714             new DDView(this,MULTIVIEW_PAGES),
715             new DDView(this,MULTIVIEW_REFERENCES),
716             new DDView(this, MULTIVIEW_SECURITY)
717             //new DDView(this,"Security")
718
};
719     }
720
721     private static class DDView extends DesignMultiViewDesc implements Serializable {
722         private static final long serialVersionUID = -4814134594154669985L;
723         private String JavaDoc name;
724
725         DDView() {}
726
727         DDView(DDDataObject dObj,String JavaDoc name) {
728             super(dObj, name);
729             this.name=name;
730         }
731
732         public org.netbeans.core.spi.multiview.MultiViewElement createElement() {
733             DDDataObject dObj = (DDDataObject)getDataObject();
734             if (name.equals(MULTIVIEW_OVERVIEW)) {
735                 return new OverviewMultiViewElement(dObj,0);
736             } else if (name.equals(MULTIVIEW_SERVLETS)) {
737                 return new ServletsMultiViewElement(dObj,1);
738             } else if (name.equals(MULTIVIEW_FILTERS)) {
739                 return new FiltersMultiViewElement(dObj,2);
740             } else if(name.equals(MULTIVIEW_PAGES)) {
741                 return new PagesMultiViewElement(dObj,3);
742             } else if(name.equals(MULTIVIEW_REFERENCES)) {
743                 return new ReferencesMultiViewElement(dObj,4);
744             } else if (name.equals(MULTIVIEW_SECURITY)) {
745                 return new SecurityMultiViewElement(dObj, 5);
746             }
747             return null;
748         }
749
750         public HelpCtx getHelpCtx() {
751             if (name.equals(MULTIVIEW_OVERVIEW)) {
752                 return new HelpCtx(HELP_ID_PREFIX_OVERVIEW+"overviewNode"); //NOI18N
753
} else if (name.equals(MULTIVIEW_SERVLETS)) {
754                 return new HelpCtx(HELP_ID_PREFIX_SERVLETS+"servletsNode"); //NOI18N
755
} else if (name.equals(MULTIVIEW_FILTERS)) {
756                 return new HelpCtx(HELP_ID_PREFIX_FILTERS+"filtersNode"); //NOI18N
757
} else if(name.equals(MULTIVIEW_PAGES)) {
758                 return new HelpCtx(HELP_ID_PREFIX_OVERVIEW+"overviewNode"); //NOI18N
759
} else if(name.equals(MULTIVIEW_REFERENCES)) {
760                 return new HelpCtx(HELP_ID_PREFIX_REFERENCES+"references"); //NOI18N
761
}
762             return null;
763         }
764
765         public java.awt.Image JavaDoc getIcon() {
766             return org.openide.util.Utilities.loadImage("org/netbeans/modules/j2ee/ddloaders/web/resources/DDDataIcon.gif"); //NOI18N
767
}
768
769         public String JavaDoc preferredID() {
770             return DD_MULTIVIEW_PREFIX+name;
771         }
772
773         public String JavaDoc getDisplayName() {
774             return NbBundle.getMessage(DDDataObject.class,"TTL_"+name);
775         }
776     }
777
778     /** Enable to focus specific object in Multiview Editor
779      * The default implementation opens the XML View
780      */

781     public void showElement(Object JavaDoc element) {
782         Object JavaDoc target=null;
783         if (element instanceof Servlet) {
784             openView(1);
785             target=element;
786         } else if (element instanceof Filter) {
787             openView(2);
788             target=element;
789         } else if (element instanceof Listener JavaDoc) {
790             openView(0);
791             target="listeners"; //NOI18N
792
} else if (element instanceof InitParam) {
793             InitParam param = (InitParam)element;
794             InitParam[] params = getWebApp().getContextParam();
795             for (int i=0;i<params.length;i++) {
796                 if (params[i]==param) {
797                     openView(0);
798                     target="context_params"; //NOI18N
799
break;
800                 }
801             }
802         } else if (element instanceof ErrorPage) {
803             openView(3);
804             target="error_pages"; //NOI18N
805
}
806         if (target!=null) {
807             final Object JavaDoc key=target;
808             org.netbeans.modules.xml.multiview.Utils.runInAwtDispatchThread(new Runnable JavaDoc() {
809                 public void run() {
810                     ToolBarMultiViewElement mvEl = getActiveMVElement();
811                     if (mvEl!=null) mvEl.getSectionView().openPanel(key);
812                 }
813             });
814         }
815     }
816     /**
817      * Do not allow to remove web.xml except for version 2.5.
818      */

819     public boolean isDeleteAllowed() {
820         return WebApp.VERSION_2_5.equals(getWebApp().getVersion());
821     }
822     /** Enable to access Active element
823      */

824     public ToolBarMultiViewElement getActiveMVElement() {
825         return (ToolBarMultiViewElement)super.getActiveMultiViewElement();
826     }
827
828     public boolean isChangedFromUI() {
829         return changedFromUI;
830     }
831     
832     public void setChangedFromUI(boolean changedFromUI) {
833         this.changedFromUI=changedFromUI;
834     }
835 }
836
Popular Tags