KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > moveclass > WebservicesXmlMoveClassRefactoring


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.moveclass;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import javax.jmi.reflect.RefObject;
31 import org.netbeans.api.java.project.JavaProjectConstants;
32 import org.netbeans.api.project.FileOwnerQuery;
33 import org.netbeans.api.project.Project;
34 import org.netbeans.api.project.ProjectManager;
35 import org.netbeans.api.project.ProjectUtils;
36 import org.netbeans.api.project.SourceGroup;
37 import org.netbeans.api.project.Sources;
38 import org.netbeans.modules.j2ee.dd.api.web.Filter;
39 import org.netbeans.modules.j2ee.dd.api.web.Listener;
40 import org.netbeans.modules.j2ee.dd.api.web.Servlet;
41 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
42 import org.netbeans.jmi.javamodel.JavaClass;
43 import org.netbeans.jmi.javamodel.Resource;
44 import org.netbeans.modules.j2ee.dd.api.webservices.PortComponent;
45 import org.netbeans.modules.j2ee.dd.api.webservices.PortComponentHandler;
46 import org.netbeans.modules.j2ee.dd.api.webservices.WebserviceDescription;
47 import org.netbeans.modules.j2ee.dd.api.webservices.Webservices;
48 import org.netbeans.modules.j2ee.refactoring.Utility;
49 import org.netbeans.modules.javacore.api.JavaModel;
50 import org.netbeans.modules.javacore.internalapi.ExternalChange;
51 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
52 import org.netbeans.modules.refactoring.api.MoveClassRefactoring;
53 import org.netbeans.modules.refactoring.api.Problem;
54 import org.netbeans.modules.refactoring.api.RefactoringElement;
55 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
56 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
57 import org.netbeans.modules.web.api.webmodule.WebModule;
58 import org.netbeans.modules.websvc.api.webservices.WebServicesSupport;
59 import org.netbeans.modules.websvc.spi.webservices.WebServicesConstants;
60 import org.netbeans.modules.websvc.wsdl.WsCompileConfigDataObject;
61 import org.netbeans.modules.websvc.wsdl.config.api.Configuration;
62 import org.netbeans.modules.websvc.wsdl.xmlutils.XMLJ2eeDataObject;
63 import org.netbeans.spi.project.support.ant.AntProjectHelper;
64 import org.netbeans.spi.project.support.ant.EditableProperties;
65 import org.openide.ErrorManager;
66 import org.openide.filesystems.FileObject;
67 import org.openide.filesystems.FileUtil;
68 import org.openide.loaders.DataFolder;
69 import org.openide.loaders.DataObject;
70 import org.openide.loaders.DataObjectNotFoundException;
71 import org.openide.util.NbBundle;
72
73 /**
74  *
75  * @author Martin Grebac
76  */

77 public final class WebservicesXmlMoveClassRefactoring {
78     
79     private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.j2ee.refactoring.rename"); // NOI18N
80

81     public WebservicesXmlMoveClassRefactoring() { }
82     
83     /** Find usages in web deployment descriptor: web.xml
84      */

85     public Problem prepare(MoveClassRefactoring moveClassRefactor, Collection JavaDoc resources, RefactoringElementsBag refactoringElements) {
86         
87         Problem problem = null;
88         
89         for (Iterator JavaDoc i = resources.iterator(); i.hasNext();) {
90             
91             boolean isSEIMove = false;
92             
93             Resource resource = (Resource) i.next();
94             FileObject fo = JavaModel.getFileObject(resource);
95             
96             err.log("resource package name: " + resource.getPackageName());
97             err.log("resource: name " + resource.getName());
98             WebServicesSupport wsm = WebServicesSupport.getWebServicesSupport(fo);
99             if (wsm != null) { // the class is in a module with web services support
100

101                 FileObject webservicesXmlFO = wsm.getWebservicesDD();
102                 Webservices webservicesXmlDD = null;
103                 try {
104                     webservicesXmlDD = org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getDDRoot(webservicesXmlFO);
105                 } catch (IOException JavaDoc ioe) {
106                     // ignore
107
}
108                 
109                 if ((webservicesXmlDD != null) && (webservicesXmlDD.getStatus()!=Webservices.STATE_INVALID_UNPARSABLE)) {
110                     List JavaDoc classes = resource.getClassifiers();
111                     err.log("Classes in resource: " + classes);
112                     
113                     for (int j=0; j < classes.size(); j++ ) {
114                         JavaClass jClass = (JavaClass)classes.get(j);
115                         err.log("javaclass: "+ jClass);
116                         
117                         String JavaDoc name = jClass.getName();
118                         err.log("name: " + name);
119                         
120                         String JavaDoc oldPackage = name.substring(0, name.lastIndexOf('.'));
121                         err.log("oldPackage: " + oldPackage);
122                         
123                         String JavaDoc targetPackage = moveClassRefactor.getTargetPackageName(resource);
124                         err.log("targetPackage: " + targetPackage);
125                         
126                         String JavaDoc newName = targetPackage + "." + jClass.getSimpleName();
127                         err.log("newname: " + newName);
128                         
129                         WebserviceDescription[] wservices = webservicesXmlDD.getWebserviceDescription();
130                         err.log("Web Services: " + wservices);
131                         if ((wservices != null) && (wservices.length > 0)) {
132                             for (int w=0; w < wservices.length; w++) {
133                                 WebserviceDescription webService = (WebserviceDescription)wservices[w];
134                                 err.log("Web Service: " + webService);
135                                 
136                                 /* And update the actual web service reference direct in webservices.xml */
137                                 PortComponent[] ports = webService.getPortComponent();
138                                 err.log("Ports: " + ports);
139                                 if ((ports != null) && (ports.length > 0)) {
140                                     for (int p=0; p < ports.length; p++) {
141                                         
142                                         /* First check for presence in webservices.xml port */
143                                         PortComponent portComponent = (PortComponent)ports[p];
144                                         err.log("Port Component: " + portComponent);
145                                         String JavaDoc serviceEndpointInterface = portComponent.getServiceEndpointInterface();
146                                         err.log("serviceEndpointInterface: " + serviceEndpointInterface);
147                                         
148                                         PortComponentHandler[] handlers = portComponent.getHandler();
149                                         for (int k = 0; k < handlers.length; k++) {
150                                             if (name.equals(handlers[k].getHandlerClass())){
151                                                 RefactoringElementImplementation elem = new WebservicesHandlerMoveClassRefactoringElement(webservicesXmlDD, name, newName, handlers[k], webservicesXmlFO);
152                                                 refactoringElements.add(moveClassRefactor, elem);
153                                             }
154                                         }
155                                         
156                                         if ((serviceEndpointInterface != null) && (serviceEndpointInterface.equals(name))) {
157                                             isSEIMove = true;
158                                             RefactoringElementImplementation elem = new WebservicesSEIMoveClassRefactoringElement(webservicesXmlDD, name, newName, portComponent, webservicesXmlFO);
159                                             refactoringElements.add(moveClassRefactor, elem);
160                                         }
161                                         
162                                         /* try to locate the local wscompile config file:
163                                          * get project for webservices.xml, get name of the webservice and try to find properties of the type
164                                          * $propertyname-config.name - if found, get the property value and the path and locate the descriptor,
165                                          * then get the dd api descriptor object
166                                          */

167                                         String JavaDoc portName = portComponent.getPortComponentName();
168                                         Project project = FileOwnerQuery.getOwner(webservicesXmlFO);
169                                         err.log("Project: " + project);
170                                         Map JavaDoc properties = wsm.getAntProjectHelper().getStandardPropertyEvaluator().getProperties();
171                                         if (err.isLoggable(ErrorManager.INFORMATIONAL)) {
172                                             err.log("Properties: " + properties);
173                                         }
174                                         String JavaDoc propertyName = portName + WebServicesConstants.CONFIG_PROP_SUFFIX;
175                                         err.log("Property name: " + propertyName);
176                                         String JavaDoc propertyValue = (String JavaDoc)properties.get(propertyName);
177                                         err.log("Property value: " + propertyValue);
178                                         if (propertyValue != null) { // there's such property in the project
179
FileObject wsConfigXmlFO = null;
180                                             
181                                             File JavaDoc f = new File JavaDoc(propertyValue);
182                                             if (f.isAbsolute()) {
183                                                 wsConfigXmlFO = FileUtil.toFileObject(f);
184                                             } else {
185                                                 wsConfigXmlFO = project.getProjectDirectory().getFileObject(propertyValue);
186                                             }
187                                             if (wsConfigXmlFO != null) {
188                                                 Configuration wsConfigXmlDD = null;
189                                                 try {
190                                                     wsConfigXmlDD = org.netbeans.modules.websvc.wsdl.config.api.DDProvider.getDefault().getDDRoot(wsConfigXmlFO);
191                                                 } catch (IOException JavaDoc ioe) {
192                                                     //ignore
193
}
194                                                 DataObject data = null;
195                                                 try {
196                                                     data = DataObject.find(wsConfigXmlFO);
197                                                 } catch (DataObjectNotFoundException notfoundexc) {
198                                                     //ignore
199
}
200                                                 err.log("DataObject: " + data);
201                                                 if ((data != null) && (wsConfigXmlDD != null)) /*&& (wsConfigXmlDD.getStatus()!=Webservices.STATE_INVALID_UNPARSABLE)*/ {
202                                                     org.netbeans.modules.websvc.wsdl.config.api.Service service = wsConfigXmlDD.getService();
203                                                     if (service != null) {
204                                                         String JavaDoc packageName = service.getPackageName();
205                                                         err.log("Package name: " + packageName);
206                                                         if ((packageName != null) && (packageName.equals(oldPackage))) {
207                                                             if (isSEIMove) {
208                                                                 if (!isConfigXmlAlreadyMoved(refactoringElements, wsConfigXmlFO)) {
209                                                                     RefactoringElementImplementation elemFile = new WebservicesConfigXmlMoveClassRefactoringElement(data, moveClassRefactor.getTargetClassPathRoot(), moveClassRefactor.getTargetPackageName(fo));
210                                                                     refactoringElements.add(moveClassRefactor, elemFile);
211                                                                 }
212                                                                 RefactoringElementImplementation elemPkg = new WebservicesConfigXmlPackageMoveClassRefactoringElement(data, jClass.getResource().getPackageName(), targetPackage);
213                                                                 refactoringElements.add(moveClassRefactor, elemPkg);
214                                                                 targetPackage = targetPackage.replace('.', '/');
215                                                                 
216                                                                 Sources srcs = ProjectUtils.getSources(project);
217                                                                 if (srcs != null) {
218                                                                     SourceGroup[] sourceGroups = srcs.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
219                                                                     SourceGroup srcGroup = null;
220                                                                     boolean found = false;
221                                                                     for (int k=0; k < sourceGroups.length; k++) {
222                                                                         srcGroup = sourceGroups[k];
223                                                                         if (FileUtil.isParentOf(srcGroup.getRootFolder(), wsConfigXmlFO)) {
224                                                                             found = true;
225                                                                             break;
226                                                                         }
227                                                                     }
228                                                                     if (found) {
229                                                                         String JavaDoc relativePath = FileUtil.getRelativePath(srcGroup.getRootFolder(), wsConfigXmlFO);
230                                                                         int index = propertyValue.indexOf(relativePath);
231                                                                         String JavaDoc newValue = propertyValue.substring(0, 9) +
232                                                                                 propertyValue.substring(index, propertyValue.length()).replaceFirst(packageName, targetPackage);
233                                                                         
234                                                                         err.log("newvalue: " + newValue);
235                                                                         err.log("moving SEI, so move the config xml file with it, too");
236                                                                         RefactoringElementImplementation elem2 = new WebservicesProjectMoveClassRefactoringElement(data, wsm.getAntProjectHelper(), propertyName, propertyValue, newValue, targetPackage);
237                                                                         refactoringElements.add(moveClassRefactor, elem2);
238                                                                     }
239                                                                 }
240                                                             }
241                                                         }
242                                                         org.netbeans.modules.websvc.wsdl.config.api.Interface[] interfaces = service.getInterface();
243                                                         if ((interfaces != null) && (interfaces.length > 0)) {
244                                                             for (int ii=0; ii < interfaces.length; ii++) {
245                                                                 org.netbeans.modules.websvc.wsdl.config.api.Interface iface = interfaces[ii];
246                                                                 String JavaDoc servant = iface.getServantName();
247                                                                 String JavaDoc iname = iface.getName();
248                                                                 err.log("Servant: " + servant);
249                                                                 err.log("Interface: " + iname);
250                                                                 if ((servant != null) && (servant.equals(name))) {
251                                                                     RefactoringElementImplementation elem = new WebservicesConfigXmlServantMoveClassRefactoringElement(data, name, newName);
252                                                                     refactoringElements.add(moveClassRefactor, elem);
253                                                                 }
254                                                                 if ((iname != null) && (iname.equals(name))) {
255                                                                     RefactoringElementImplementation elem = new WebservicesConfigXmlInterfaceMoveClassRefactoringElement(data, name, newName);
256                                                                     refactoringElements.add(moveClassRefactor, elem);
257                                                                 }
258                                                             }
259                                                         }
260                                                     }
261                                                 } else {
262                                                     Object JavaDoc[] args = new Object JavaDoc [] {wsConfigXmlFO.getPath()};
263                                                     String JavaDoc msg = MessageFormat.format(NbBundle.getMessage(WebservicesXmlMoveClassRefactoring.class, "TXT_WebservicesConfigXmlMoveClassInvalidProblem"), args);
264                                                     Problem newProblem = new Problem(false, msg);
265                                                     problem = Utility.addProblemsToEnd(problem, newProblem);
266                                                 }
267                                             }
268                                         }
269                                     }
270                                 }
271                             }
272                         }
273                     }
274                 } else if (webservicesXmlDD != null) {
275                     Problem newProblem = new Problem(false, NbBundle.getMessage(WebservicesXmlMoveClassRefactoring.class, "TXT_WebservicesXmlMoveClassInvalidProblem"));
276                     if (problem == null) {
277                         problem = newProblem;
278                     } else {
279                         if (newProblem != null) problem.setNext(newProblem);
280                     }
281                 }
282             }
283         } // refactor webservices.xml
284
return problem;
285     }
286     
287     private boolean isConfigXmlAlreadyMoved(RefactoringElementsBag refactoringElements, FileObject file) {
288         Collection JavaDoc c = refactoringElements.getSession().getRefactoringElements();
289         Iterator JavaDoc refElems = c.iterator();
290         while (refElems.hasNext()) {
291             RefactoringElement refElem = (RefactoringElement)refElems.next();
292             FileObject f = refElem.getParentFile();
293             if (f != null) {
294                 DataObject d=null;
295                 try {
296                     d = DataObject.find(f);
297                 } catch (Exception JavaDoc e) {
298                     
299                 }
300                 if ( (d!=null) && (d instanceof WsCompileConfigDataObject)) {
301                     return file.equals(((WsCompileConfigDataObject)d).getPrimaryFile());
302                 }
303             }
304         }
305         return false;
306     }
307     
308     public final class WebservicesSEIMoveClassRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
309         
310         protected Webservices webservicesXmlDD;
311         private PortComponent pc;
312         
313         /** Creates a new instance of WebservicesSEIMoveClassRefactoringElement */
314         public WebservicesSEIMoveClassRefactoringElement(Webservices webservicesXmlDD, String JavaDoc oldName, String JavaDoc newName, PortComponent pc, FileObject parentFile) {
315             this.webservicesXmlDD = webservicesXmlDD;
316             this.pc = pc;
317             this.oldName = oldName;
318             this.newName = newName;
319             this.parentFile = parentFile;
320         }
321         
322         /** Returns text describing the refactoring formatted for display (using HTML tags).
323          * @return Formatted text.
324          */

325         public String JavaDoc getDisplayText() {
326             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), oldName, newName};
327             return MessageFormat.format(NbBundle.getMessage(WebservicesXmlMoveClassRefactoring.class, "TXT_WebservicesXmlSEIMoveClass"), args);
328         }
329         
330         /** Performs the change represented by this refactoring element.
331          */

332         public void performChange() {
333             JavaMetamodel.getManager().registerExtChange(this);
334         }
335         
336         public void performExternalChange() {
337             if (pc != null) {
338                 pc.setServiceEndpointInterface(newName);
339                 try {
340                     webservicesXmlDD.write(parentFile);
341                 } catch (IOException JavaDoc ioe) {
342                     //TODO
343
}
344             }
345         }
346         
347         public void undoExternalChange() {
348             if (pc != null) {
349                 pc.setServiceEndpointInterface(oldName);
350                 try {
351                     webservicesXmlDD.write(parentFile);
352                 } catch (IOException JavaDoc ioe) {
353                     //TODO
354
}
355             }
356         }
357     }
358     
359     /**
360      * Refactoring element for message handler.
361      */

362     public final class WebservicesHandlerMoveClassRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
363         
364         protected Webservices webservicesXmlDD;
365         private PortComponentHandler handler;
366         
367         public WebservicesHandlerMoveClassRefactoringElement(Webservices webservicesXmlDD, String JavaDoc oldName, String JavaDoc newName, PortComponentHandler handler, FileObject parentFile) {
368             this.webservicesXmlDD = webservicesXmlDD;
369             this.handler = handler;
370             this.oldName = oldName;
371             this.newName = newName;
372             this.parentFile = parentFile;
373         }
374         
375         public String JavaDoc getDisplayText() {
376             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), oldName, newName};
377             return MessageFormat.format(NbBundle.getMessage(WebservicesXmlMoveClassRefactoring.class, "TXT_WebservicesXmlHandlerMoveClass"), args);
378         }
379         
380         public void performChange() {
381             JavaMetamodel.getManager().registerExtChange(this);
382         }
383         
384         public void performExternalChange() {
385             handler.setHandlerClass(newName);
386             write();
387         }
388         
389         public void undoExternalChange() {
390             handler.setHandlerClass(oldName);
391             write();
392         }
393         
394         private void write(){
395             try {
396                 webservicesXmlDD.write(parentFile);
397             } catch (IOException JavaDoc ioe) {
398                 ErrorManager.getDefault().notify(ioe);
399             }
400         }
401     }
402     
403     public final class WebservicesConfigXmlPackageMoveClassRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
404         
405         private DataObject data = null;
406         
407         /** Creates a new instance of WebservicesConfigXmlPackageMoveClassRefactoringElement */
408         public WebservicesConfigXmlPackageMoveClassRefactoringElement(DataObject data, String JavaDoc oldName, String JavaDoc newName) {
409             this.data = data;
410             this.oldName = oldName;
411             this.newName = newName;
412         }
413         
414         /** Returns text describing the refactoring formatted for display (using HTML tags).
415          * @return Formatted text.
416          */

417         public String JavaDoc getDisplayText() {
418             Object JavaDoc[] args = new Object JavaDoc [] {data.getPrimaryFile(), oldName, newName};
419             return MessageFormat.format(NbBundle.getMessage(WebservicesXmlMoveClassRefactoring.class, "TXT_WebservicesConfigXmlPackageMoveClass"), args);
420         }
421         
422         /** Returns file that the element affects (relates to)
423          * @return File
424          */

425         public FileObject getParentFile() {
426             if (data != null) {
427                 return data.getPrimaryFile();
428             }
429             return null;
430         }
431         
432         /** Performs the change represented by this refactoring element.
433          */

434         public void performChange() {
435             JavaMetamodel.getManager().registerExtChange(this);
436         }
437         
438         public void performExternalChange() {
439             if (data != null) {
440                 FileObject wsConfigXmlFO = data.getPrimaryFile();
441                 Configuration wsConfigXmlDD = null;
442                 try {
443                     wsConfigXmlDD = org.netbeans.modules.websvc.wsdl.config.api.DDProvider.getDefault().getDDRoot(wsConfigXmlFO);
444                 } catch (IOException JavaDoc ioe) {
445                     //ignore
446
}
447                 if (wsConfigXmlDD != null) /* TODO check validity */ {
448                     org.netbeans.modules.websvc.wsdl.config.api.Service service = wsConfigXmlDD.getService();
449                     if (service != null) {
450                         service.setPackageName(newName);
451                         try {
452                             wsConfigXmlDD.write(wsConfigXmlFO);
453                         } catch (IOException JavaDoc ioe) {
454                             //TODO
455
}
456                     }
457                 }
458             }
459         }
460         
461         public void undoExternalChange() {
462             if (data != null) {
463                 FileObject wsConfigXmlFO = data.getPrimaryFile();
464                 Configuration wsConfigXmlDD = null;
465                 try {
466                     wsConfigXmlDD = org.netbeans.modules.websvc.wsdl.config.api.DDProvider.getDefault().getDDRoot(wsConfigXmlFO);
467                 } catch (IOException JavaDoc ioe) {
468                     //ignore
469
}
470                 if (wsConfigXmlDD != null) /* TODO check validity */ {
471                     org.netbeans.modules.websvc.wsdl.config.api.Service service = wsConfigXmlDD.getService();
472                     if (service != null) {
473                         service.setPackageName(oldName);
474                         try {
475                             wsConfigXmlDD.write(wsConfigXmlFO);
476                         } catch (IOException JavaDoc ioe) {
477                             //TODO
478
}
479                     }
480                 }
481             }
482         }
483     }
484     
485     public final class WebservicesConfigXmlMoveClassRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
486         
487         private DataObject data = null;
488         private FileObject target = null;
489         private FileObject originalFolder = null;
490         private String JavaDoc targetPackageName;
491         
492         /** Creates a new instance of WebservicesConfigXmlPackageMoveClassRefactoringElement */
493         public WebservicesConfigXmlMoveClassRefactoringElement(DataObject data, FileObject targetClassPathRoot, String JavaDoc targetPackageName) {
494             this.data = data;
495             this.target = targetClassPathRoot;
496             this.originalFolder = data.getPrimaryFile().getParent();
497             this.targetPackageName = targetPackageName;
498         }
499         
500         /** Returns text describing the refactoring formatted for display (using HTML tags).
501          * @return Formatted text.
502          */

503         public String JavaDoc getDisplayText() {
504             Object JavaDoc[] args = new Object JavaDoc [] {data.getPrimaryFile().getNameExt(), target.getPath() + "/" + targetPackageName.replace('.','/')}; //NOI18N
505
return MessageFormat.format(NbBundle.getMessage(WebservicesXmlMoveClassRefactoring.class, "TXT_WebservicesConfigXmlMoveClass"), args);
506         }
507         
508         /** Returns file that the element affects (relates to)
509          * @return File
510          */

511         public FileObject getParentFile() {
512             if (data != null) {
513                 return data.getPrimaryFile();
514             }
515             return null;
516         }
517         
518         /** Performs the change represented by this refactoring element.
519          */

520         public void performChange() {
521             JavaMetamodel.getManager().registerExtChange(this);
522         }
523         
524         public void performExternalChange() {
525             if (data != null) {
526                 try {
527                     DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(target, targetPackageName.replace('.','/')));
528                     data.move(folder);
529                 } catch (IOException JavaDoc ioe) {
530                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Could not move file: " + ioe);
531                 }
532             }
533         }
534         
535         public void undoExternalChange() {
536             if (data != null) {
537                 DataFolder folder = DataFolder.findFolder(originalFolder);
538                 try {
539                     data.move(folder);
540                 } catch (IOException JavaDoc ioe) {
541                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Could not undo move file: " + ioe);
542                 }
543             }
544         }
545     }
546     
547     public final class WebservicesConfigXmlServantMoveClassRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
548         
549         private DataObject data = null;
550         
551         /** Creates a new instance of WebservicesConfigXmlServantMoveClassRefactoringElement */
552         public WebservicesConfigXmlServantMoveClassRefactoringElement(DataObject data, String JavaDoc oldName, String JavaDoc newName) {
553             this.oldName = oldName;
554             this.newName = newName;
555             this.data = data;
556         }
557         
558         /** Returns text describing the refactoring formatted for display (using HTML tags).
559          * @return Formatted text.
560          */

561         public String JavaDoc getDisplayText() {
562             Object JavaDoc[] args = new Object JavaDoc [] {data.getPrimaryFile(), oldName, newName};
563             return MessageFormat.format(NbBundle.getMessage(WebservicesXmlMoveClassRefactoring.class, "TXT_WebservicesConfigXmlServantMoveClass"), args);
564         }
565         
566         /** Performs the change represented by this refactoring element.
567          */

568         public void performChange() {
569             JavaMetamodel.getManager().registerExtChange(this);
570         }
571         
572         /** Returns file that the element affects (relates to)
573          * @return File
574          */

575         public FileObject getParentFile() {
576             if (data != null) {
577                 return data.getPrimaryFile();
578             }
579             return null;
580         }
581         
582         public void performExternalChange() {
583             if (data != null) {
584                 FileObject wsConfigXmlFO = data.getPrimaryFile();
585                 Configuration wsConfigXmlDD = null;
586                 try {
587                     wsConfigXmlDD = org.netbeans.modules.websvc.wsdl.config.api.DDProvider.getDefault().getDDRoot(wsConfigXmlFO);
588                 } catch (IOException JavaDoc ioe) {
589                     //ignore
590
}
591                 if (wsConfigXmlDD != null) /* TODO check validity */ {
592                     err.log("perform external change: WebservicesConfigXmlServantMoveClassRefactoringElement");
593                     org.netbeans.modules.websvc.wsdl.config.api.Service service = wsConfigXmlDD.getService();
594                     org.netbeans.modules.websvc.wsdl.config.api.Interface[] interfaces = service.getInterface();
595                     err.log("Interfaces: " + interfaces);
596                     if ((interfaces != null) && (interfaces.length > 0)) {
597                         for (int ii=0; ii < interfaces.length; ii++) {
598                             org.netbeans.modules.websvc.wsdl.config.api.Interface iface = interfaces[ii];
599                             String JavaDoc servant = iface.getServantName();
600                             err.log("Servant: " + servant);
601                             if ((servant != null) && (servant.equals(oldName))) {
602                                 iface.setServantName(newName);
603                             }
604                         }
605                         try {
606                             wsConfigXmlDD.write(wsConfigXmlFO);
607                         } catch (IOException JavaDoc ioe) {
608                             //TODO
609
}
610                     }
611                 }
612             }
613         }
614         
615         public void undoExternalChange() {
616             if (data != null) {
617                 FileObject wsConfigXmlFO = data.getPrimaryFile();
618                 Configuration wsConfigXmlDD = null;
619                 try {
620                     wsConfigXmlDD = org.netbeans.modules.websvc.wsdl.config.api.DDProvider.getDefault().getDDRoot(wsConfigXmlFO);
621                 } catch (IOException JavaDoc ioe) {
622                     //ignore
623
}
624                 if (wsConfigXmlDD != null) /* TODO check validity */ {
625                     err.log("undo external change: WebservicesConfigXmlServantMoveClassRefactoringElement");
626                     org.netbeans.modules.websvc.wsdl.config.api.Service service = wsConfigXmlDD.getService();
627                     org.netbeans.modules.websvc.wsdl.config.api.Interface[] interfaces = service.getInterface();
628                     err.log("Interfaces: " + interfaces);
629                     if ((interfaces != null) && (interfaces.length > 0)) {
630                         for (int ii=0; ii < interfaces.length; ii++) {
631                             org.netbeans.modules.websvc.wsdl.config.api.Interface iface = interfaces[ii];
632                             String JavaDoc servant = iface.getServantName();
633                             err.log("Servant: " + servant);
634                             if ((servant != null) && (servant.equals(newName))) {
635                                 iface.setServantName(oldName);
636                             }
637                         }
638                         try {
639                             wsConfigXmlDD.write(wsConfigXmlFO);
640                         } catch (IOException JavaDoc ioe) {
641                             //TODO
642
}
643                     }
644                 }
645             }
646         }
647     }
648     
649     public final class WebservicesConfigXmlInterfaceMoveClassRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
650         
651         private DataObject data = null;
652         
653         /** Creates a new instance of WebservicesConfigXmlInterfaceMoveClassRefactoringElement */
654         public WebservicesConfigXmlInterfaceMoveClassRefactoringElement(DataObject data, String JavaDoc oldName, String JavaDoc newName) {
655             this.oldName = oldName;
656             this.newName = newName;
657             this.data = data;
658         }
659         
660         /** Returns text describing the refactoring formatted for display (using HTML tags).
661          * @return Formatted text.
662          */

663         public String JavaDoc getDisplayText() {
664             Object JavaDoc[] args = new Object JavaDoc [] {data.getPrimaryFile(), oldName, newName};
665             return MessageFormat.format(NbBundle.getMessage(WebservicesXmlMoveClassRefactoring.class, "TXT_WebservicesConfigXmlInterfaceMoveClass"), args);
666         }
667         
668         /** Returns file that the element affects (relates to)
669          * @return File
670          */

671         public FileObject getParentFile() {
672             if (data != null) {
673                 return data.getPrimaryFile();
674             }
675             return null;
676         }
677         
678         /** Performs the change represented by this refactoring element.
679          */

680         public void performChange() {
681             JavaMetamodel.getManager().registerExtChange(this);
682         }
683         
684         public void performExternalChange() {
685             if (data != null) {
686                 FileObject wsConfigXmlFO = data.getPrimaryFile();
687                 Configuration wsConfigXmlDD = null;
688                 try {
689                     wsConfigXmlDD = org.netbeans.modules.websvc.wsdl.config.api.DDProvider.getDefault().getDDRoot(wsConfigXmlFO);
690                 } catch (IOException JavaDoc ioe) {
691                     //ignore
692
}
693                 if (wsConfigXmlDD != null) /* TODO check validity */ {
694                     err.log("perform external change: WebservicesConfigXmlInterfaceMoveClassRefactoringElement");
695                     org.netbeans.modules.websvc.wsdl.config.api.Service service = wsConfigXmlDD.getService();
696                     org.netbeans.modules.websvc.wsdl.config.api.Interface[] interfaces = service.getInterface();
697                     err.log("Interfaces: " + interfaces);
698                     if ((interfaces != null) && (interfaces.length > 0)) {
699                         for (int ii=0; ii < interfaces.length; ii++) {
700                             org.netbeans.modules.websvc.wsdl.config.api.Interface iface = interfaces[ii];
701                             String JavaDoc interf = iface.getName();
702                             err.log("Interface: " + interf);
703                             if ((interf != null) && (interf.equals(oldName))) {
704                                 iface.setName(newName);
705                             }
706                         }
707                         try {
708                             wsConfigXmlDD.write(wsConfigXmlFO);
709                         } catch (IOException JavaDoc ioe) {
710                             //TODO
711
}
712                     }
713                 }
714             }
715         }
716         
717         public void undoExternalChange() {
718             if (data != null) {
719                 FileObject wsConfigXmlFO = data.getPrimaryFile();
720                 Configuration wsConfigXmlDD = null;
721                 try {
722                     wsConfigXmlDD = org.netbeans.modules.websvc.wsdl.config.api.DDProvider.getDefault().getDDRoot(wsConfigXmlFO);
723                 } catch (IOException JavaDoc ioe) {
724                     //ignore
725
}
726                 if (wsConfigXmlDD != null) /* TODO check validity */ {
727                     err.log("undo external change: WebservicesConfigXmlInterfaceMoveClassRefactoringElement");
728                     org.netbeans.modules.websvc.wsdl.config.api.Service service = wsConfigXmlDD.getService();
729                     org.netbeans.modules.websvc.wsdl.config.api.Interface[] interfaces = service.getInterface();
730                     err.log("Interfaces: " + interfaces);
731                     if ((interfaces != null) && (interfaces.length > 0)) {
732                         for (int ii=0; ii < interfaces.length; ii++) {
733                             org.netbeans.modules.websvc.wsdl.config.api.Interface iface = interfaces[ii];
734                             String JavaDoc interf = iface.getName();
735                             err.log("Interface: " + interf);
736                             if ((interf != null) && (interf.equals(newName))) {
737                                 iface.setName(oldName);
738                             }
739                         }
740                         try {
741                             wsConfigXmlDD.write(wsConfigXmlFO);
742                         } catch (IOException JavaDoc ioe) {
743                             //TODO
744
}
745                     }
746                 }
747             }
748         }
749     }
750     
751     public final class WebservicesProjectMoveClassRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
752         
753         private DataObject data = null;
754         private AntProjectHelper helper;
755         private String JavaDoc propertyName;
756         private String JavaDoc propertyValue;
757         private String JavaDoc targetPackage;
758         private String JavaDoc newValue;
759         
760         /** Creates a new instance of WebservicesProjectMoveClassRefactoringElement */
761         public WebservicesProjectMoveClassRefactoringElement(DataObject data, AntProjectHelper helper, String JavaDoc propertyName, String JavaDoc propertyValue, String JavaDoc newValue, String JavaDoc targetPackage) {
762             this.propertyName = propertyName;
763             this.propertyValue = propertyValue;
764             this.targetPackage = targetPackage;
765             this.data = data;
766             this.newValue = newValue;
767             this.helper = helper;
768         }
769         
770         /** Returns file that the element affects (relates to)
771          * @return File
772          */

773         public FileObject getParentFile() {
774             if (data != null) {
775                 return data.getPrimaryFile();
776             }
777             return null;
778         }
779         
780         /** Returns text describing the refactoring formatted for display (using HTML tags).
781          * @return Formatted text.
782          */

783         public String JavaDoc getDisplayText() {
784             Object JavaDoc[] args = new Object JavaDoc [] {propertyName, propertyValue, newValue};
785             return MessageFormat.format(NbBundle.getMessage(WebservicesXmlMoveClassRefactoring.class, "TXT_WebservicesProjectMoveClass"), args);
786         }
787         
788         /** Performs the change represented by this refactoring element.
789          */

790         public void performChange() {
791             JavaMetamodel.getManager().registerExtChange(this);
792         }
793         
794         public void performExternalChange() {
795             EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
796             if (data != null) {
797                 ep.setProperty(propertyName, newValue);
798             }
799             helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
800             Project project = null;
801             try {
802                 project = ProjectManager.getDefault().findProject(helper.getProjectDirectory());
803                 ProjectManager.getDefault().saveProject(project);
804             } catch (IOException JavaDoc ioe) { // do nothing
805
}
806         }
807         
808         public void undoExternalChange() {
809             if (data != null) {
810             }
811         }
812     }
813     
814 }
815
Popular Tags