KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URISyntaxException JavaDoc;
26 import javax.swing.JTextField JavaDoc;
27 import org.netbeans.api.project.Project;
28 import org.netbeans.modules.xml.schema.model.Schema;
29 import org.netbeans.modules.xml.schema.model.SchemaModel;
30 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
31 import org.netbeans.modules.xml.wsdl.model.Definitions;
32 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
33 import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema;
34 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.UIUtilities;
35 import org.netbeans.modules.xml.xam.ModelSource;
36 import org.netbeans.modules.xml.xam.Model.State;
37 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
38 import org.openide.DialogDescriptor;
39 import org.openide.DialogDisplayer;
40 import org.openide.ErrorManager;
41 import org.openide.WizardValidationException;
42 import org.openide.filesystems.FileObject;
43 import org.openide.filesystems.FileUtil;
44 import org.openide.loaders.DataObject;
45 import org.openide.util.NbBundle;
46 import org.xml.sax.Attributes JavaDoc;
47 import org.xml.sax.InputSource JavaDoc;
48 import org.xml.sax.SAXException JavaDoc;
49 import org.xml.sax.XMLReader JavaDoc;
50
51 /** WsdlUIPanel.java - bottom panel for WSDL wizard
52  *
53  * @author mkuchtiak
54  */

55 public class WsdlUIPanel extends javax.swing.JPanel JavaDoc {
56     
57     private static final String JavaDoc TARGET_URL_PREFIX = NbBundle.getMessage(WsdlUIPanel.class,"TXT_defaultTNS"); //NOI18N
58

59     private WsdlPanel wizardPanel;
60     private javax.swing.JTextField JavaDoc fileNameTF;
61     
62     private boolean hasUserModifiedNamespace = false;
63     
64     private NamespaceDocListener mListener = new NamespaceDocListener();
65     
66     /** Creates new form WsdlUIPanel */
67     public WsdlUIPanel(WsdlPanel wizardPanel) {
68         initComponents();
69         this.wizardPanel=wizardPanel;
70         nsTF.setText(TARGET_URL_PREFIX);
71     }
72     
73     void attachFileNameListener(javax.swing.JTextField JavaDoc fileNameTF) {
74         this.fileNameTF=fileNameTF;
75         if (fileNameTF!=null) {
76             nsTF.setText(TARGET_URL_PREFIX+fileNameTF.getText());
77             DocListener list = new DocListener();
78             javax.swing.text.Document JavaDoc doc = fileNameTF.getDocument();
79             doc.addDocumentListener(list);
80         } else {
81             nsTF.setText(TARGET_URL_PREFIX);
82         }
83         
84         nsTF.getDocument().addDocumentListener(mListener);
85     }
86     
87     /** This method is called from within the constructor to
88      * initialize the form.
89      * WARNING: Do NOT modify this code. The content of this method is
90      * always regenerated by the Form Editor.
91      */

92     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
93
private void initComponents() {
94         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
95
96         cbImport = new javax.swing.JCheckBox JavaDoc();
97         schemaTF = new javax.swing.JTextField JavaDoc();
98         browseButton = new javax.swing.JButton JavaDoc();
99         schemaLB = new javax.swing.JLabel JavaDoc();
100         namespaceLB = new javax.swing.JLabel JavaDoc();
101         nsTF = new javax.swing.JTextField JavaDoc();
102         jPanel1 = new javax.swing.JPanel JavaDoc();
103
104         setLayout(new java.awt.GridBagLayout JavaDoc());
105
106         org.openide.awt.Mnemonics.setLocalizedText(cbImport, org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_importSchema")); // NOI18N
107
cbImport.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
108         cbImport.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
109         cbImport.addItemListener(new java.awt.event.ItemListener JavaDoc() {
110             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
111                 cbImportItemStateChanged(evt);
112             }
113         });
114
115         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
116         gridBagConstraints.gridx = 0;
117         gridBagConstraints.gridy = 2;
118         gridBagConstraints.gridwidth = 3;
119         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
120         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 6, 5);
121         add(cbImport, gridBagConstraints);
122         cbImport.getAccessibleContext().setAccessibleDescription(null); // NOI18N
123

124         schemaTF.setEditable(false);
125         schemaTF.setToolTipText(null); // NOI18N
126
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
127         gridBagConstraints.gridx = 1;
128         gridBagConstraints.gridy = 3;
129         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
130         gridBagConstraints.weightx = 1.0;
131         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 2, 0);
132         add(schemaTF, gridBagConstraints);
133         schemaTF.getAccessibleContext().setAccessibleDescription(null); // NOI18N
134

135         org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_browse")); // NOI18N
136
browseButton.setEnabled(false);
137         browseButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
138             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
139                 browseButtonActionPerformed(evt);
140             }
141         });
142
143         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
144         gridBagConstraints.gridx = 2;
145         gridBagConstraints.gridy = 3;
146         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
147         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 2, 0);
148         add(browseButton, gridBagConstraints);
149         browseButton.getAccessibleContext().setAccessibleDescription(null); // NOI18N
150

151         schemaLB.setLabelFor(schemaTF);
152         org.openide.awt.Mnemonics.setLocalizedText(schemaLB, org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_schemaFiles")); // NOI18N
153
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
154         gridBagConstraints.gridx = 0;
155         gridBagConstraints.gridy = 3;
156         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
157         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 2, 0);
158         add(schemaLB, gridBagConstraints);
159
160         namespaceLB.setLabelFor(nsTF);
161         org.openide.awt.Mnemonics.setLocalizedText(namespaceLB, org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_targetNamespace")); // NOI18N
162
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
163         gridBagConstraints.gridx = 0;
164         gridBagConstraints.gridy = 0;
165         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
166         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 2, 0);
167         add(namespaceLB, gridBagConstraints);
168
169         nsTF.setToolTipText(null); // NOI18N
170
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
171         gridBagConstraints.gridx = 1;
172         gridBagConstraints.gridy = 0;
173         gridBagConstraints.gridwidth = 2;
174         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
175         gridBagConstraints.weightx = 1.0;
176         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 5, 2, 0);
177         add(nsTF, gridBagConstraints);
178         nsTF.getAccessibleContext().setAccessibleDescription(null); // NOI18N
179

180         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
181         gridBagConstraints.gridx = 0;
182         gridBagConstraints.gridy = 5;
183         gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
184         gridBagConstraints.weighty = 1.0;
185         add(jPanel1, gridBagConstraints);
186
187     }// </editor-fold>//GEN-END:initComponents
188

189     private void browseButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseButtonActionPerformed
190
// Create a temporary file and model for the import creator to use.
191
Project project = wizardPanel.getProject();
192         FileObject prjdir = project.getProjectDirectory();
193         // HACK: hard-coded NB project directory name
194
FileObject privdir = prjdir.getFileObject("nbproject/private");
195         // We prefer to use the private directory, but at the very
196
// least our file needs to be inside the project.
197
File JavaDoc directory = FileUtil.toFile(privdir != null ? privdir : prjdir);
198         String JavaDoc fname = fileNameTF.getText();
199         if (fname == null || fname.length() == 0) {
200             fname = "wizard";
201         }
202         File JavaDoc file = null;
203         try {
204             file = File.createTempFile(fname, ".wsdl", directory);
205             wizardPanel.populateFileFromTemplate(file);
206         } catch (Exception JavaDoc e) {
207             // This is quite unexpected.
208
ErrorManager.getDefault().notify(e);
209             if (file != null) {
210                 file.delete();
211             }
212             return;
213         }
214         WSDLModel model = wizardPanel.prepareModelFromFile(file);
215         model.startTransaction();
216         WSDLSchema wsdlSchema = model.getFactory().createWSDLSchema();
217         Definitions defs = model.getDefinitions();
218         defs.getTypes().addExtensibilityElement(wsdlSchema);
219         SchemaModel schemaModel = wsdlSchema.getSchemaModel();
220         Schema schema = schemaModel.getSchema();
221         // Must set namespace on embedded schema for import dialog to work.
222
schema.setTargetNamespace(defs.getTargetNamespace());
223         model.endTransaction();
224
225         // Use a specialized import creator for selecting files.
226
String JavaDoc original = schemaTF.getText().trim();
227         ImportSchemaCreator creator = new ImportSchemaCreator(schema, model, original);
228         DialogDescriptor descriptor = UIUtilities.getCreatorDialog(
229                 creator, NbBundle.getMessage(WsdlUIPanel.class,
230                 "TITLE_selectSchema"), true);
231         descriptor.setValid(false);
232         Object JavaDoc result = DialogDisplayer.getDefault().notify(descriptor);
233         if (result == DialogDescriptor.OK_OPTION) {
234             String JavaDoc selections = creator.getSelectedFiles();
235             schemaTF.setText(selections);
236             schemaTF.firePropertyChange("VALUE_SET", false, true);
237         }
238
239         // Must use DataObject to delete the temporary file.
240
file = FileUtil.normalizeFile(file);
241         FileObject fobj = FileUtil.toFileObject(file);
242         try {
243             DataObject.find(fobj).delete();
244         } catch (IOException JavaDoc ex) {
245             // Ignore, either the file isn't there or we can't delete it.
246
}
247     }//GEN-LAST:event_browseButtonActionPerformed
248

249     private void cbImportItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_cbImportItemStateChanged
250
if (cbImport.isSelected()) {
251             schemaTF.setEditable(true);
252             browseButton.setEnabled(true);
253         } else {
254             schemaTF.setEditable(false);
255             browseButton.setEnabled(false);
256         }
257     }//GEN-LAST:event_cbImportItemStateChanged
258

259     
260     // Variables declaration - do not modify//GEN-BEGIN:variables
261
private javax.swing.JButton JavaDoc browseButton;
262     private javax.swing.JCheckBox JavaDoc cbImport;
263     private javax.swing.JPanel JavaDoc jPanel1;
264     private javax.swing.JLabel JavaDoc namespaceLB;
265     private javax.swing.JTextField JavaDoc nsTF;
266     private javax.swing.JLabel JavaDoc schemaLB;
267     private javax.swing.JTextField JavaDoc schemaTF;
268     // End of variables declaration//GEN-END:variables
269

270     private class NamespaceDocListener implements javax.swing.event.DocumentListener JavaDoc {
271         
272         public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
273             documentChanged(e);
274         }
275         
276         public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
277             documentChanged(e);
278         }
279         
280         public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
281             documentChanged(e);
282         }
283         
284         private void documentChanged(javax.swing.event.DocumentEvent JavaDoc e) {
285             hasUserModifiedNamespace = true;
286         }
287     }
288     
289     private class DocListener implements javax.swing.event.DocumentListener JavaDoc {
290         
291         public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
292             documentChanged(e);
293         }
294         
295         public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
296             documentChanged(e);
297         }
298         
299         public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
300             documentChanged(e);
301         }
302         
303         private void documentChanged(javax.swing.event.DocumentEvent JavaDoc e) {
304             if(!hasUserModifiedNamespace) {
305                 nsTF.getDocument().removeDocumentListener(mListener);
306                 nsTF.setText(TARGET_URL_PREFIX+fileNameTF.getText());
307                 nsTF.getDocument().addDocumentListener(mListener);
308             }
309             
310         }
311     }
312     
313     /** Class than provides basic informationn about schema file
314      */

315     static class SchemaInfo {
316         private java.net.URL JavaDoc url;
317         SchemaInfo(java.net.URL JavaDoc url) {
318             this.url=url;
319         }
320         
321         java.net.URL JavaDoc getURL() {
322             return url;
323         }
324         
325         String JavaDoc getNamespace() {
326             InputSource JavaDoc is = new InputSource JavaDoc(url.toExternalForm());
327             try {
328                 return parse(is);
329             } catch (java.io.IOException JavaDoc ex){
330             } catch (SAXException JavaDoc ex){}
331             return "";
332         }
333         
334         String JavaDoc getSchemaName() {
335             return url.toExternalForm();
336         }
337         
338         /** Parses XML document and creates the list of tags
339          */

340         private String JavaDoc parse(InputSource JavaDoc is) throws java.io.IOException JavaDoc, SAXException JavaDoc {
341             XMLReader JavaDoc xmlReader = org.openide.xml.XMLUtil.createXMLReader();
342             NsHandler handler = new NsHandler();
343             xmlReader.setContentHandler(handler);
344             //xmlReader.setFeature("http://xml.org/sax/features/use-locator2",true);
345
try {
346                 xmlReader.parse(is);
347             } catch (SAXException JavaDoc ex) {
348                 if (!"EXIT".equals(ex.getMessage())) throw ex; //NOI18N
349
}
350             String JavaDoc ns = handler.getNs();
351             if (ns==null) return "";
352             else return ns;
353         }
354         
355         private static class NsHandler extends org.xml.sax.helpers.DefaultHandler JavaDoc {
356             private String JavaDoc ns;
357             
358             public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
359                 if (qName.endsWith("schema")) { //NOI18N
360
ns=attributes.getValue("targetNamespace"); //NOI18N
361
throw new SAXException JavaDoc("EXIT"); //NOI18N
362
}
363             }
364             
365             String JavaDoc getNs() {
366                 return ns;
367             }
368         }
369         
370     }
371     
372     String JavaDoc getNS() {
373         return nsTF.getText();
374     }
375     
376     boolean isImport() {
377         return cbImport.isSelected();
378     }
379     
380     SchemaInfo[] getSchemas() {
381         if (cbImport.isSelected()) {
382             String JavaDoc schemas = schemaTF.getText();
383             String JavaDoc[] urls = schemas.split(",");
384             java.util.List JavaDoc infos = new java.util.ArrayList JavaDoc();
385             for (int i=0;i<urls.length;i++) {
386                 String JavaDoc urlString=urls[i].trim();
387                 if (urlString.length()==0) continue;
388                 try {
389                     java.net.URL JavaDoc url = new java.net.URL JavaDoc(urlString);
390                     infos.add(new SchemaInfo(url));
391                 } catch (java.net.MalformedURLException JavaDoc ex) {
392                     // testing if target folder contains XML Schema
393
try {
394                         org.openide.loaders.DataFolder folder = wizardPanel.getTemplateWizard().getTargetFolder();
395                         org.openide.filesystems.FileObject fo = folder.getPrimaryFile();
396                         if ((fo.getFileObject(urlString))!=null) {
397                             String JavaDoc parentURL = fo.getURL().toExternalForm();
398                             infos.add(new SchemaInfo(new java.net.URL JavaDoc(parentURL+urlString)));
399                         }
400                     } catch (java.io.IOException JavaDoc ex1) {}
401                 }
402             }
403             SchemaInfo[] result = new SchemaInfo[infos.size()];
404             infos.toArray(result);
405             return result;
406         }
407         return new SchemaInfo[]{};
408     }
409
410     public void validateSchemas() throws WizardValidationException {
411         if (cbImport.isSelected()) {
412             String JavaDoc schemas = schemaTF.getText();
413             String JavaDoc[] urls = schemas.split(",");
414             for (int i=0;i<urls.length;i++) {
415                 String JavaDoc urlString=urls[i].trim();
416                 if (urlString.length()==0) continue;
417                 createSchemaModel(urlString);
418             }
419         }
420         
421     }
422     
423     private void createSchemaModel(String JavaDoc urlString) throws WizardValidationException {
424         java.net.URL JavaDoc url = null;
425         try {
426             url = new java.net.URL JavaDoc(urlString);
427         } catch (MalformedURLException JavaDoc e) {
428             org.openide.loaders.DataFolder folder;
429                 try {
430                     folder = wizardPanel.getTemplateWizard().getTargetFolder();
431                     org.openide.filesystems.FileObject fo = folder.getPrimaryFile();
432                     if ((fo.getFileObject(urlString))!=null) {
433                         String JavaDoc parentURL = fo.getURL().toExternalForm();
434                         try {
435                             url = new java.net.URL JavaDoc(parentURL+urlString);
436                         } catch (MalformedURLException JavaDoc e1) {
437                             throw new WizardValidationException(schemaTF, e1.getMessage(), e1.getLocalizedMessage());
438                         }
439                     }
440                 } catch (IOException JavaDoc e1) {
441                     throw new WizardValidationException(schemaTF, e1.getMessage(), e1.getLocalizedMessage());
442                 }
443                 if (url == null) {
444                     String JavaDoc errorString = NbBundle.getMessage(WsdlUIPanel.class, "INVALID_SCHEMA_FILE", urlString);
445                     throw new WizardValidationException(schemaTF, errorString, errorString);
446                 }
447         }
448         ModelSource source;
449         try {
450             FileObject fo = FileUtil.toFileObject(new File JavaDoc(url.toURI()));
451             if (fo == null) {
452                 String JavaDoc errorMessage = NbBundle.getMessage(WsdlUIPanel.class, "INVALID_SCHEMA_FILE", urlString);
453                 throw new WizardValidationException(schemaTF, errorMessage, errorMessage);
454             }
455             source = org.netbeans.modules.xml.retriever.catalog.Utilities.
456                     createModelSource(fo, false);
457         } catch (WizardValidationException e) {
458             throw e;
459         } catch (CatalogModelException e) {
460             throw new WizardValidationException(schemaTF, e.getMessage(), e.getLocalizedMessage());
461         } catch (URISyntaxException JavaDoc e) {
462             throw new WizardValidationException(schemaTF, e.getMessage(), e.getLocalizedMessage());
463         } catch (Throwable JavaDoc e) {
464             String JavaDoc errorMessage = NbBundle.getMessage(WsdlUIPanel.class, "INVALID_SCHEMA_FILE", urlString);
465             throw new WizardValidationException(schemaTF, errorMessage, errorMessage);
466         }
467         SchemaModel model = null;
468         try {
469             model = SchemaModelFactory.getDefault().getModel(source);
470         } catch (Throwable JavaDoc e) {
471             String JavaDoc errorMessage = NbBundle.getMessage(WsdlUIPanel.class, "INVALID_SCHEMA_FILE", urlString);
472             throw new WizardValidationException(schemaTF, errorMessage, errorMessage);
473         }
474         
475         if (model == null || model.getState().equals(State.NOT_WELL_FORMED)) {
476             String JavaDoc errorMessage = NbBundle.getMessage(WsdlUIPanel.class, "INVALID_SCHEMA_FILE", urlString);
477             throw new WizardValidationException(schemaTF, errorMessage, errorMessage);
478         }
479     }
480     
481     public JTextField JavaDoc getSchemaFileTextField() {
482         return schemaTF;
483     }
484 }
485
Popular Tags