KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > safedelete > WebservicesXmlSafeDeleteRefactoring


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.refactoring.safedelete;
21
22 import java.io.IOException JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24 import org.netbeans.jmi.javamodel.Element;
25 import org.netbeans.jmi.javamodel.JavaClass;
26 import org.netbeans.modules.j2ee.dd.api.webservices.DDProvider;
27 import org.netbeans.modules.j2ee.dd.api.webservices.PortComponent;
28 import org.netbeans.modules.j2ee.dd.api.webservices.PortComponentHandler;
29 import org.netbeans.modules.j2ee.dd.api.webservices.WebserviceDescription;
30 import org.netbeans.modules.j2ee.dd.api.webservices.Webservices;
31 import org.netbeans.modules.j2ee.refactoring.DefaultPositionBoundsResolver;
32 import org.netbeans.modules.j2ee.refactoring.J2EERefactoring;
33 import org.netbeans.modules.javacore.api.JavaModel;
34 import org.netbeans.modules.javacore.internalapi.ExternalChange;
35 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
36 import org.netbeans.modules.refactoring.api.Problem;
37 import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring;
38 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
39 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
40 import org.netbeans.modules.websvc.api.webservices.WebServicesSupport;
41 import org.openide.ErrorManager;
42 import org.openide.filesystems.FileObject;
43 import org.openide.loaders.DataObject;
44 import org.openide.loaders.DataObjectNotFoundException;
45 import org.openide.text.PositionBounds;
46 import org.openide.util.NbBundle;
47
48 /**
49  * Safe delelete refactoring for web service message handlers. Takes care
50  * of propagating changes to webservices.xml.
51  *
52  * @author Erno Mononen
53  */

54 public class WebservicesXmlSafeDeleteRefactoring implements J2EERefactoring {
55     
56     private Element element;
57     private SafeDeleteRefactoring safeDeleteRefactoring;
58     private Webservices webservicesXmlDD;
59     private FileObject webservicesXmlFO;
60     
61     public WebservicesXmlSafeDeleteRefactoring(Element element, SafeDeleteRefactoring safeDeleteRefactoring) {
62         this.element = element;
63         this.safeDeleteRefactoring = safeDeleteRefactoring;
64         FileObject fo = JavaModel.getFileObject(element.getResource());
65         WebServicesSupport wsm = WebServicesSupport.getWebServicesSupport(fo);
66         if (wsm != null){
67             this.webservicesXmlFO = wsm.getWebservicesDD();
68             try {
69                 this.webservicesXmlDD = DDProvider.getDefault().getDDRoot(webservicesXmlFO);
70             } catch (IOException JavaDoc ioe) {
71                 ErrorManager.getDefault().notify(ioe);
72             }
73         }
74     }
75     
76     public Problem preCheck() {
77         return null;
78     }
79     
80     public Problem checkParameters() {
81         return null;
82     }
83     
84     public Problem fastCheckParameters() {
85         return null;
86     }
87     
88     public Problem prepare(RefactoringElementsBag refactoringElementsBag) {
89         
90         if (webservicesXmlDD == null){
91             return null;
92         } else if (webservicesXmlDD.getStatus() == Webservices.STATE_INVALID_UNPARSABLE) {
93             return new Problem(false, NbBundle.getMessage(WebservicesXmlSafeDeleteRefactoring.class, "TXT_WebservicesXmlInvalid", webservicesXmlFO.getPath()));
94         }
95         
96         for (Object JavaDoc elem : element.getResource().getClassifiers()) {
97             String JavaDoc name = ((JavaClass) elem).getName();
98             
99             for (WebserviceDescription webService : webservicesXmlDD.getWebserviceDescription()) {
100                 
101                 for (PortComponent portComponent : webService.getPortComponent()){
102                     
103                     for(PortComponentHandler handler : portComponent.getHandler()){
104                         
105                         if (name.equals(handler.getHandlerClass())){
106                             RefactoringElementImplementation refactoringElem =
107                                     new WebserviceXmlSafeDeleteRefactoringElement(webservicesXmlDD, portComponent, handler, webservicesXmlFO);
108                             refactoringElementsBag.add(safeDeleteRefactoring, refactoringElem);
109                         }
110                     }
111                 }
112             }
113         }
114         return null;
115         
116     }
117     
118     static class WebserviceXmlSafeDeleteRefactoringElement extends AbstractSafeDeleteRefactoringElement implements ExternalChange {
119         
120         protected Webservices webservicesXmlDD;
121         private PortComponentHandler handler;
122         private PortComponent portComponent;
123         
124         public WebserviceXmlSafeDeleteRefactoringElement(Webservices webservicesXmlDD, PortComponent portComponent, PortComponentHandler handler, FileObject parentFile) {
125             this.webservicesXmlDD = webservicesXmlDD;
126             this.handler = handler;
127             this.portComponent = portComponent;
128             this.parentFile = parentFile;
129         }
130         
131         public String JavaDoc getDisplayText() {
132             return MessageFormat.format(NbBundle.getMessage(WebservicesXmlSafeDeleteRefactoring.class, "TXT_WebservicesXmlHandlerSafeDelete"), handler.getHandlerName());
133         }
134         
135         public void performChange() {
136             JavaMetamodel.getManager().registerExtChange(this);
137         }
138         
139         public void performExternalChange() {
140             portComponent.removeHandler(handler);
141             write();
142         }
143         
144         public void undoExternalChange() {
145             portComponent.addHandler(handler);
146             write();
147         }
148         
149         private void write(){
150             try {
151                 webservicesXmlDD.write(parentFile);
152             } catch (IOException JavaDoc ioe) {
153                 ErrorManager.getDefault().notify(ioe);
154             }
155         }
156         
157         public PositionBounds getPosition() {
158             try {
159                 return new DefaultPositionBoundsResolver(DataObject.find(getParentFile()), handler.getHandlerClass()).getPositionBounds();
160             } catch (DataObjectNotFoundException ex) {
161                 ErrorManager.getDefault().notify(ex);
162             }
163             return null;
164         }
165         
166     }
167     
168 }
169
Popular Tags