KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsitconf > WSITEditor


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 2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.wsitconf;
21
22 import java.util.Collection JavaDoc;
23 import javax.swing.undo.UndoManager JavaDoc;
24 import org.netbeans.api.project.FileOwnerQuery;
25 import org.netbeans.api.project.Project;
26 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
27 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
28 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
29 import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport;
30 import org.netbeans.modules.websvc.api.jaxws.project.config.Client;
31 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel;
32 import org.netbeans.modules.websvc.api.jaxws.project.config.Service;
33 import org.netbeans.modules.websvc.jaxws.api.JAXWSSupport;
34 import org.netbeans.modules.websvc.wsitconf.spi.SecurityProfile;
35 import org.netbeans.modules.websvc.wsitconf.spi.SecurityProfileRegistry;
36 import org.netbeans.modules.websvc.wsitconf.ui.ErrorTopComponent;
37 import org.netbeans.modules.websvc.wsitconf.ui.service.ServiceTopComponent;
38 import org.netbeans.modules.websvc.wsitconf.ui.client.ClientTopComponent;
39 import org.netbeans.modules.websvc.wsitconf.util.UndoManagerHolder;
40 import org.netbeans.modules.websvc.wsitconf.wsdlmodelext.WSITModelSupport;
41 import org.netbeans.modules.xml.retriever.catalog.Utilities;
42 import org.netbeans.modules.xml.wsdl.model.*;
43 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
44 import org.openide.ErrorManager;
45 import org.openide.cookies.SaveCookie;
46 import org.openide.filesystems.FileObject;
47 import org.openide.loaders.DataObject;
48 import org.openide.nodes.Node;
49 import org.openide.util.Lookup;
50 import org.openide.util.NbBundle;
51 import java.io.IOException JavaDoc;
52 import java.net.MalformedURLException JavaDoc;
53 import java.util.LinkedList JavaDoc;
54 import javax.swing.JComponent JavaDoc;
55 import org.netbeans.api.java.project.JavaProjectConstants;
56 import org.netbeans.api.project.ProjectUtils;
57 import org.netbeans.api.project.SourceGroup;
58 import org.netbeans.api.project.Sources;
59 import org.netbeans.modules.websvc.core.wseditor.spi.WSEditor;
60 import org.netbeans.modules.websvc.wsitconf.util.Util;
61 import org.netbeans.modules.xml.xam.ModelSource;
62 import org.openide.loaders.DataObjectNotFoundException;
63
64 /**
65  *
66  * @author Martin Grebac
67  */

68 public class WSITEditor implements WSEditor, UndoManagerHolder {
69
70     private UndoManager JavaDoc undoManager;
71     private Collection JavaDoc<FileObject> createdFiles = new LinkedList JavaDoc();
72             
73     /**
74      * Creates a new instance of WSITEditor
75      */

76     public WSITEditor() {
77         populateProfileRegistry();
78     }
79
80     public String JavaDoc getTitle() {
81         return NbBundle.getMessage(WSITEditor.class, "EDITOR_TITLE"); //NOI18N
82
}
83
84     public JComponent JavaDoc createWSEditorComponent(Node node, JaxWsModel jaxWsModel) {
85
86         WSDLModel clientWsdlModel;
87         WSDLModel wsdlModel;
88
89         //is it a client node?
90
Client client = (Client)node.getLookup().lookup(Client.class);
91         //is it a service node?
92
Service service = (Service)node.getLookup().lookup(Service.class);
93         
94         Project p = null;
95         if (jaxWsModel != null) {
96             p = FileOwnerQuery.getOwner(jaxWsModel.getJaxWsFile());
97         }
98         
99         boolean wsitSupported = false;
100         if (client != null){ //its a client
101
if (p != null) {
102                 JAXWSClientSupport wscs = JAXWSClientSupport.getJaxWsClientSupport(p.getProjectDirectory());
103                 if (wscs != null) {
104                     wsitSupported = Util.isWsitSupported(p);
105                     if (wsitSupported) {
106                         try {
107                             clientWsdlModel = WSITModelSupport.getModel(node, jaxWsModel, this, true, createdFiles);
108                             wsdlModel = WSITModelSupport.getServiceModelForClient(wscs, client);
109                             return new ClientTopComponent(client, jaxWsModel, clientWsdlModel, wsdlModel, node);
110                         } catch(Exception JavaDoc e){
111                             ErrorManager.getDefault().notify(e);
112                         }
113                     } else {
114                         return new ErrorTopComponent(NbBundle.getMessage(WSITEditor.class, "TXT_WSIT_NotDetected", getServerName(p)));
115                     }
116                 }
117             }
118         } else {
119             if (p != null) {
120                 JAXWSSupport wss = JAXWSSupport.getJAXWSSupport(p.getProjectDirectory());
121                 if (wss != null) {
122                     wsitSupported = Util.isWsitSupported(p);
123                     if (wsitSupported) {
124                         try {
125                             wsdlModel = WSITModelSupport.getModel(node, jaxWsModel, this, true, createdFiles);
126                             return new ServiceTopComponent(service, jaxWsModel, wsdlModel, node, getUndoManager());
127                         } catch(Exception JavaDoc e){
128                             ErrorManager.getDefault().notify(e);
129                         }
130                     } else {
131                         return new ErrorTopComponent(NbBundle.getMessage(WSITEditor.class, "TXT_WSIT_NotDetected", getServerName(p)));
132                     }
133                 }
134             }
135         }
136         return new ErrorTopComponent(NbBundle.getMessage(WSITEditor.class, "TXT_WSIT_NotSupported"));
137     }
138
139     public void save(Node node, JaxWsModel jaxWsModel) {
140         if (node == null) return;
141         try {
142             WSDLModel model = WSITModelSupport.getModel(node, jaxWsModel, this, false, createdFiles);
143             if (model != null) {
144                 FileObject wsdlFO = Utilities.getFileObject(model.getModelSource());
145                 if (wsdlFO == null) {
146                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Cannot find fileobject in lookup for: " + model.getModelSource()); //NOI18N
147
}
148                 DataObject wsdlDO = DataObject.find(wsdlFO);
149                 if ((wsdlDO != null) && (wsdlDO.isModified())) {
150                     SaveCookie wsdlSaveCookie = (SaveCookie)wsdlDO.getCookie(SaveCookie.class);
151                     if(wsdlSaveCookie != null){
152                         wsdlSaveCookie.save();
153                     }
154                     wsdlDO.setModified(false);
155                 }
156             }
157         } catch (Exception JavaDoc e){
158             ErrorManager.getDefault().notify(e);
159         }
160     }
161
162     public void cancel(Node node, JaxWsModel jaxWsModel) {
163         if (node == null) return;
164         WSDLModel model = null;
165         
166         // remove imports from main config file if a new config file was imported
167

168         FileObject srcRoot = (FileObject) node.getLookup().lookup(FileObject.class);
169         if (srcRoot != null) {
170             Project p = FileOwnerQuery.getOwner(srcRoot);
171             if (p != null) {
172                 Sources sources = ProjectUtils.getSources(p);
173                 if (sources != null) {
174                     SourceGroup[] sourceGroups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
175                     if ((sourceGroups != null) || (sourceGroups.length > 0)) {
176                         FileObject srcFolder = sourceGroups[0].getRootFolder();
177                         WSDLModel mainModel = WSITModelSupport.getMainClientModel(srcFolder);
178                         if (mainModel != null) {
179                             Collection JavaDoc<Import> imports = mainModel.getDefinitions().getImports();
180                             for (Import i : imports) {
181                                 try {
182                                     WSDLModel importedModel = i.getImportedWSDLModel();
183                                     ModelSource importedms = importedModel.getModelSource();
184                                     FileObject importedfo = org.netbeans.modules.xml.retriever.catalog.Utilities.getFileObject(importedms);
185                                     mainModel.startTransaction();
186                                     if (createdFiles.contains(importedfo)) {
187                                         mainModel.getDefinitions().removeImport(i);
188                                     }
189                                     mainModel.endTransaction();
190                                     FileObject mainFO = Utilities.getFileObject(mainModel.getModelSource());
191                                     if (mainFO == null) {
192                                         ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL,
193                                                 "Cannot find fileobject in lookup for: " + model.getModelSource()); //NOI18N
194
}
195                                     try {
196                                         DataObject mainDO = DataObject.find(mainFO);
197                                         if ((mainDO != null) && (mainDO.isModified())) {
198                                             SaveCookie wsdlSaveCookie = (SaveCookie)mainDO.getCookie(SaveCookie.class);
199                                             if(wsdlSaveCookie != null){
200                                                 wsdlSaveCookie.save();
201                                             }
202                                             mainDO.setModified(false);
203                                         }
204                                     } catch (IOException JavaDoc ioe) {
205                                         // ignore - just don't do anything
206
}
207                                 } catch (CatalogModelException ex) {
208                                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage());
209                                 }
210                             }
211                         }
212                     }
213                 }
214             }
215         }
216         
217         // now first undo all edits in the files
218
try {
219             model = WSITModelSupport.getModel(node, jaxWsModel, this, false, createdFiles);
220         } catch (MalformedURLException JavaDoc ex) {
221             ex.printStackTrace();
222         } catch (Exception JavaDoc ex) {
223             ex.printStackTrace();
224         }
225
226         if (model != null) {
227             try {
228                 if (getUndoManager() != null) {
229                     while (getUndoManager().canUndo()) {
230                         getUndoManager().undo();
231                     }
232                 }
233             } catch (Exception JavaDoc e){
234                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, e.getMessage());
235             }
236             FileObject fo = org.netbeans.modules.xml.retriever.catalog.Utilities.getFileObject(model.getModelSource());
237             DataObject dO = null;
238             try {
239                 dO = DataObject.find(fo);
240             } catch (DataObjectNotFoundException ex) {
241                 ErrorManager.getDefault().notify(ex);
242             }
243             if (dO != null) {
244                 try {
245                     model.sync();
246                 } catch (IOException JavaDoc ex) {
247                     ErrorManager.getDefault().notify(ex);
248                 }
249                 dO.setModified(false);
250             }
251         }
252         
253         // and remove all created files during this run
254
if ((createdFiles != null) && (createdFiles.size() > 0)) {
255             for (FileObject fo : createdFiles) {
256                 if (fo != null) {
257                     try {
258                         DataObject dO = DataObject.find(fo);
259                         if (dO != null) {
260                             dO.delete();
261                         }
262                     } catch (IOException JavaDoc ex) {
263                         ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage()); //NOI18N
264
}
265                 }
266             }
267         }
268     }
269
270     private String JavaDoc getServerName(Project p) {
271         J2eeModuleProvider mp = (J2eeModuleProvider)p.getLookup().lookup(J2eeModuleProvider.class);
272         if (mp != null) {
273             String JavaDoc sID = mp.getServerInstanceID();
274             J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(sID);
275             return j2eePlatform.getDisplayName();
276         }
277         return null;
278     }
279     
280     public UndoManager JavaDoc getUndoManager() {
281         return undoManager;
282     }
283
284     public void setUndoManager(UndoManager JavaDoc undoManager) {
285         this.undoManager = undoManager;
286     }
287
288     private SecurityProfileRegistry populateProfileRegistry() {
289         SecurityProfileRegistry registry = SecurityProfileRegistry.getDefault();
290         if (registry.getSecurityProfiles().isEmpty()) {
291             Lookup.Result results = Lookup.getDefault().
292                     lookup(new Lookup.Template(SecurityProfile.class));
293             Collection JavaDoc<SecurityProfile> profiles = results.allInstances();
294             for (SecurityProfile p : profiles) {
295                 registry.register(p);
296             }
297         }
298         
299         return registry;
300     }
301
302 }
303
Popular Tags