KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > wizard > WsdlPanel


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

19
20 package org.netbeans.modules.xml.wsdl.ui.wizard;
21
22 import java.awt.Component JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileWriter JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import javax.swing.JTextField JavaDoc;
32 import javax.swing.event.ChangeEvent JavaDoc;
33 import javax.swing.event.ChangeListener JavaDoc;
34 import javax.swing.event.DocumentEvent JavaDoc;
35 import javax.swing.event.DocumentListener JavaDoc;
36 import javax.swing.text.BadLocationException JavaDoc;
37 import javax.swing.text.Document JavaDoc;
38 import org.netbeans.api.project.Project;
39 import org.netbeans.api.project.SourceGroup;
40 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
41 import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
42 import org.netbeans.modules.xml.xam.ModelSource;
43 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
44 import org.netbeans.spi.project.ui.templates.support.Templates;
45 import org.openide.ErrorManager;
46 import org.openide.WizardDescriptor;
47 import org.openide.WizardValidationException;
48 import org.openide.cookies.EditorCookie;
49 import org.openide.filesystems.FileObject;
50 import org.openide.filesystems.FileUtil;
51 import org.openide.loaders.DataObject;
52 import org.openide.loaders.DataObjectNotFoundException;
53 import org.openide.loaders.TemplateWizard;
54 import org.openide.util.HelpCtx;
55
56 /**
57  *
58  * @author Milan Kuchtiak
59  */

60 final class WsdlPanel implements WizardDescriptor.FinishablePanel {
61     
62     public static final String JavaDoc FILE_NAME = "FILE_NAME";
63     
64     public static final String JavaDoc WSDL_TARGETNAMESPACE = "WSDL_TARGETNAMESPACE";
65     
66     public static final String JavaDoc WSDL_DEFINITION_NAME = "WSDL_DEFINITION_NAME";
67     
68     
69     private static final String JavaDoc DEFAULT_TARGET_NAMESPACE = "urn:WS/wsdl"; //NOI18N
70

71     private final List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc();
72     private WsdlUIPanel gui;
73
74     private Project project;
75     private TemplateWizard templateWizard;
76     
77     private JTextField JavaDoc fileNameTextField;
78     
79     private File JavaDoc tempWSDLFile = null;
80     
81     private WSDLModel mTempWSDLModel = null;
82     
83     private String JavaDoc mErrorMessage;
84     
85     private TextChangeListener mListener = new TextChangeListener();
86     
87     WsdlPanel(Project project, SourceGroup[] folders) {
88         this.project = project;
89     }
90     
91     TemplateWizard getTemplateWizard() {
92         return templateWizard;
93     }
94     
95     void setNameTF(JTextField JavaDoc nameTF) {
96         gui.attachFileNameListener(nameTF);
97         if(nameTF != null) {
98             nameTF.getDocument().removeDocumentListener(mListener);//remove existing one
99
nameTF.getDocument().addDocumentListener(mListener);
100             fileNameTextField = nameTF;
101         }
102     }
103
104     public Component JavaDoc getComponent() {
105         if (gui == null) {
106             gui = new WsdlUIPanel(this);
107             gui.getSchemaFileTextField().addPropertyChangeListener(new SchemaImportTextChangeListener());
108             gui.getSchemaFileTextField().getDocument().addDocumentListener(new SchemaImportTextChangeListener());
109 // gui.setPreferredSize(new Dimension(450, 400));
110
}
111         return gui;
112     }
113  
114     public Project getProject(){
115         return project;
116     }
117     
118     public HelpCtx getHelp() {
119         return new HelpCtx(WsdlPanel.class);
120     }
121
122     
123     
124     public boolean isValid() {
125         if(templateWizard != null) {
126             templateWizard.putProperty ("WizardPanel_errorMessage", this.mErrorMessage); // NOI18N
127
}
128         return this.mErrorMessage == null;
129         
130     }
131     
132     
133     public void addChangeListener(ChangeListener JavaDoc l) {
134         listeners.add(l);
135     }
136
137     public void removeChangeListener(ChangeListener JavaDoc l) {
138         listeners.remove(l);
139     }
140
141     protected void fireChange() {
142         ChangeEvent JavaDoc e = new ChangeEvent JavaDoc(this);
143         Iterator JavaDoc it = listeners.iterator();
144         while (it.hasNext()) {
145             ((ChangeListener JavaDoc)it.next()).stateChanged(e);
146         }
147     }
148
149     public void readSettings( Object JavaDoc settings ) {
150         templateWizard = (TemplateWizard)settings;
151         
152         
153         //if user come to first panel we need to discard out temp wsdl model
154
templateWizard.putProperty(WizardPortTypeConfigurationStep.TEMP_WSDLMODEL, null);
155         templateWizard.putProperty(WizardPortTypeConfigurationStep.TEMP_WSDLFILE, null);
156         
157     }
158
159     public void storeSettings(Object JavaDoc settings) {
160         TemplateWizard wiz = (TemplateWizard) settings;
161         
162         if (WizardDescriptor.PREVIOUS_OPTION.equals(((WizardDescriptor) settings).getValue())) {
163             return;
164         }
165         if (WizardDescriptor.CANCEL_OPTION.equals(((WizardDescriptor) settings).getValue())) {
166             return;
167         }
168         
169         String JavaDoc fileName = Templates.getTargetName(wiz);
170         wiz.putProperty(FILE_NAME, fileName);
171         String JavaDoc targetNamespace = getNS();
172         wiz.putProperty(WSDL_TARGETNAMESPACE, targetNamespace);
173         String JavaDoc definitionName = fileName;
174         wiz.putProperty(WSDL_DEFINITION_NAME, definitionName);
175         try {
176             if (tempWSDLFile == null) {
177                 // Create a temporary file for storing our settings.
178
tempWSDLFile = File.createTempFile(fileName + "RIT", ".wsdl"); // NOI18N
179
tempWSDLFile.deleteOnExit();
180                 populateFileFromTemplate(tempWSDLFile);
181                 templateWizard.putProperty(
182                         WizardPortTypeConfigurationStep.TEMP_WSDLFILE, tempWSDLFile);
183                 mTempWSDLModel = prepareModelFromFile(tempWSDLFile);
184                 templateWizard.putProperty(
185                         WizardPortTypeConfigurationStep.TEMP_WSDLMODEL, mTempWSDLModel);
186             } else {
187                 templateWizard.putProperty(
188                         WizardPortTypeConfigurationStep.TEMP_WSDLMODEL, mTempWSDLModel);
189                 templateWizard.putProperty(
190                         WizardPortTypeConfigurationStep.TEMP_WSDLFILE, tempWSDLFile);
191                 mTempWSDLModel.startTransaction();
192                 mTempWSDLModel.getDefinitions().setTargetNamespace(targetNamespace);
193                 mTempWSDLModel.getDefinitions().setName(definitionName);
194                 if (mTempWSDLModel.getDefinitions().getTypes() == null) {
195                     mTempWSDLModel.getDefinitions().setTypes(
196                             mTempWSDLModel.getFactory().createTypes());
197                 }
198                 mTempWSDLModel.endTransaction();
199             }
200         } catch (Exception JavaDoc e) {
201             ErrorManager.getDefault().notify(e);
202         }
203     }
204
205     /**
206      * Create a temporary WSDL file that is based on the wizard template.
207      *
208      * @param wizard the template wizard.
209      * @param file the WSDL file to be written.
210      * @throws DataObjectNotFoundException
211      * if the wizard template is missing.
212      * @throws IOException
213      * if unable to create the temporary file.
214      */

215     void populateFileFromTemplate(File JavaDoc file) throws
216             DataObjectNotFoundException, IOException JavaDoc {
217         if (templateWizard == null) {
218             throw new IOException JavaDoc("templateWizard not defined");
219         }
220         FileObject template = Templates.getTemplate(templateWizard);
221         DataObject dTemplate = DataObject.find(template);
222         if (dTemplate != null) {
223             EditorCookie editorCookie = (EditorCookie) DataObject.find(
224                     dTemplate.getPrimaryFile()).getCookie(EditorCookie.class);
225             editorCookie.openDocument();
226             Document JavaDoc doc = editorCookie.getDocument();
227             FileWriter JavaDoc writer = new FileWriter JavaDoc(file);
228             try {
229                 writer.write(doc.getText(0, doc.getLength()));
230             } catch (BadLocationException JavaDoc ble) {
231                 // This basically cannot happen.
232
ErrorManager.getDefault().notify(ble);
233             }
234             writer.close();
235         }
236     }
237
238     /**
239      * Load and initialize the WSDL model from the given file, which should
240      * already have a minimal WSDL definition. The preparation includes
241      * setting the definition name, adding a namespace and prefix, and
242      * adding the types component.
243      *
244      * @param file the file with a minimal WSDL definition.
245      * @return the model.
246      */

247     WSDLModel prepareModelFromFile(File JavaDoc file) {
248         file = FileUtil.normalizeFile(file);
249         FileObject fobj = FileUtil.toFileObject(file);
250         String JavaDoc definitionName = fobj.getName();
251         ModelSource modelSource = org.netbeans.modules.xml.retriever.
252                 catalog.Utilities.getModelSource(fobj, fobj.canWrite());
253         WSDLModel model = WSDLModelFactory.getDefault().getModel(modelSource);
254         model.startTransaction();
255         model.getDefinitions().setName(definitionName);
256         String JavaDoc ns = getNS();
257         model.getDefinitions().setTargetNamespace(ns);
258         ((AbstractDocumentComponent) model.getDefinitions()).addPrefix("tns", ns);
259         if (model.getDefinitions().getTypes() == null) {
260             model.getDefinitions().setTypes(model.getFactory().createTypes());
261         }
262         model.endTransaction();
263         return model;
264     }
265
266     String JavaDoc getNS() {
267         String JavaDoc targetNamespace = gui.getNS();
268         if (targetNamespace.length() == 0) {
269             targetNamespace = DEFAULT_TARGET_NAMESPACE;
270         }
271         return targetNamespace;
272     }
273     
274     WsdlUIPanel.SchemaInfo[] getSchemas() {
275         return gui.getSchemas();
276     }
277     
278     boolean isImport() {
279         return gui.isImport();
280     }
281
282     public boolean isFinishPanel() {
283         return isValid();
284     }
285
286     private void fireChangeEvent() {
287         Iterator JavaDoc<ChangeListener JavaDoc> it = this.listeners.iterator();
288         ChangeEvent JavaDoc e = new ChangeEvent JavaDoc(this);
289         while(it.hasNext()) {
290             ChangeListener JavaDoc l = it.next();
291             l.stateChanged(e);
292         }
293     }
294     
295     private boolean isValidName(Document JavaDoc doc) {
296         try {
297             String JavaDoc text = doc.getText(0, doc.getLength());
298             boolean isValid = org.netbeans.modules.xml.xam.dom.Utils.isValidNCName(text);
299             if(!isValid) {
300                 mErrorMessage = "Name \"" + text + "\" is not a valid NCName";
301             } else {
302                 mErrorMessage = null;
303             }
304             
305         } catch(Exception JavaDoc ex) {
306             ex.printStackTrace();
307         }
308         
309         return mErrorMessage == null;
310     }
311     
312     
313     
314     private void validateFileName() {
315         boolean validFileName = isValidName(this.fileNameTextField.getDocument());
316         if(!validFileName) {
317             fireChangeEvent();
318             return;
319         }
320     }
321
322     
323     class TextChangeListener implements DocumentListener JavaDoc {
324          
325          public void changedUpdate(DocumentEvent JavaDoc e) {
326             validateFileName();
327          }
328          
329          public void insertUpdate(DocumentEvent JavaDoc e) {
330              validateFileName();
331          }
332
333          public void removeUpdate(DocumentEvent JavaDoc e) {
334              validateFileName();
335          }
336  
337     }
338     
339     class SchemaImportTextChangeListener implements PropertyChangeListener JavaDoc, DocumentListener JavaDoc {
340         
341         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
342             if (evt.getPropertyName().equals("VALUE_SET")) {
343                 validateSchemas();
344             }
345         }
346
347         public void insertUpdate(DocumentEvent JavaDoc e) {
348             validateSchemas();
349         }
350
351         public void removeUpdate(DocumentEvent JavaDoc e) {
352             validateSchemas();
353         }
354
355         public void changedUpdate(DocumentEvent JavaDoc e) {
356             validateSchemas();
357         }
358
359    }
360     
361     public void validateSchemas() {
362         try {
363             gui.validateSchemas();
364         } catch (WizardValidationException e) {
365             mErrorMessage = e.getLocalizedMessage();
366             fireChangeEvent();
367             return;
368         }
369         mErrorMessage = null;
370         fireChangeEvent();
371     }
372     
373     
374     
375     
376 }
377
Popular Tags