KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsdl > 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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.wsdl.wizard;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import org.openide.ErrorManager;
26 import org.openide.util.NbBundle;
27 import org.xml.sax.*;
28
29 /** WsdlUIPanel.java - bottom panel for WSDL wizard
30  *
31  * @author mkuchtiak
32  */

33 public class WsdlUIPanel extends javax.swing.JPanel JavaDoc {
34
35     private static final String JavaDoc TARGET_URL_PREFIX = NbBundle.getMessage(WsdlUIPanel.class,"TXT_defaultTNS"); //NOI18N
36
private static final String JavaDoc DEFAULT_SERVICE_NAME = NbBundle.getMessage(WsdlUIPanel.class,"TXT_defaultServiceName"); //NOI18N
37
private WsdlPanel wizardPanel;
38     private javax.swing.JTextField JavaDoc fileNameTF;
39     
40     /** Creates new form WsdlUIPanel */
41     public WsdlUIPanel(WsdlPanel wizardPanel) {
42         initComponents();
43         this.wizardPanel=wizardPanel;
44         nsTF.setText(TARGET_URL_PREFIX+DEFAULT_SERVICE_NAME);
45     }
46     
47     void attachFileNameListener(javax.swing.JTextField JavaDoc fileNameTF) {
48         this.fileNameTF=fileNameTF;
49         if (fileNameTF!=null) {
50             nsTF.setText(TARGET_URL_PREFIX+fileNameTF.getText());
51             DocListener list = new DocListener();
52             javax.swing.text.Document JavaDoc doc = fileNameTF.getDocument();
53             doc.addDocumentListener(list);
54         } else {
55             nsTF.setText(TARGET_URL_PREFIX+DEFAULT_SERVICE_NAME);
56         }
57     }
58     
59     /** This method is called from within the constructor to
60      * initialize the form.
61      * WARNING: Do NOT modify this code. The content of this method is
62      * always regenerated by the Form Editor.
63      */

64     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
65
private void initComponents() {
66         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
67
68         cbImport = new javax.swing.JCheckBox JavaDoc();
69         schemaTF = new javax.swing.JTextField JavaDoc();
70         browseButton = new javax.swing.JButton JavaDoc();
71         schemaLB = new javax.swing.JLabel JavaDoc();
72         namespaceLB = new javax.swing.JLabel JavaDoc();
73         nsTF = new javax.swing.JTextField JavaDoc();
74         jPanel1 = new javax.swing.JPanel JavaDoc();
75
76         setLayout(new java.awt.GridBagLayout JavaDoc());
77
78         cbImport.setMnemonic(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "MNEM_importSchema").charAt(0));
79         cbImport.setText(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_importSchema"));
80         cbImport.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
81         cbImport.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
82         cbImport.addItemListener(new java.awt.event.ItemListener JavaDoc() {
83             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
84                 cbImportItemStateChanged(evt);
85             }
86         });
87
88         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
89         gridBagConstraints.gridx = 0;
90         gridBagConstraints.gridy = 2;
91         gridBagConstraints.gridwidth = 3;
92         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
93         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 6, 5);
94         add(cbImport, gridBagConstraints);
95         cbImport.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_importSchema"));
96
97         schemaTF.setEditable(false);
98         schemaTF.setToolTipText(java.util.ResourceBundle.getBundle("org/netbeans/modules/websvc/wsdl/wizard/Bundle").getString("HINT_schemaFiles"));
99         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
100         gridBagConstraints.gridx = 1;
101         gridBagConstraints.gridy = 3;
102         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
103         gridBagConstraints.weightx = 1.0;
104         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 2, 0);
105         add(schemaTF, gridBagConstraints);
106         schemaTF.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "A11Y_schemaTF"));
107
108         browseButton.setMnemonic(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "MNEM_browse").charAt(0));
109         browseButton.setText(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_browse"));
110         browseButton.setEnabled(false);
111         browseButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
112             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
113                 browseButtonActionPerformed(evt);
114             }
115         });
116
117         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
118         gridBagConstraints.gridx = 2;
119         gridBagConstraints.gridy = 3;
120         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
121         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 2, 0);
122         add(browseButton, gridBagConstraints);
123         browseButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "A11Y_browse"));
124
125         schemaLB.setDisplayedMnemonic(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "MNEM_schemaFiles").charAt(0));
126         schemaLB.setLabelFor(schemaTF);
127         schemaLB.setText(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_schemaFiles"));
128         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
129         gridBagConstraints.gridx = 0;
130         gridBagConstraints.gridy = 3;
131         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
132         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 2, 0);
133         add(schemaLB, gridBagConstraints);
134
135         namespaceLB.setDisplayedMnemonic(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "MNEM_targetNamespace").charAt(0));
136         namespaceLB.setLabelFor(nsTF);
137         namespaceLB.setText(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_targetNamespace"));
138         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
139         gridBagConstraints.gridx = 0;
140         gridBagConstraints.gridy = 0;
141         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
142         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 2, 0);
143         add(namespaceLB, gridBagConstraints);
144
145         nsTF.setToolTipText(java.util.ResourceBundle.getBundle("org/netbeans/modules/websvc/wsdl/wizard/Bundle").getString("HINT_targetNamespace"));
146         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
147         gridBagConstraints.gridx = 1;
148         gridBagConstraints.gridy = 0;
149         gridBagConstraints.gridwidth = 2;
150         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
151         gridBagConstraints.weightx = 1.0;
152         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 5, 2, 0);
153         add(nsTF, gridBagConstraints);
154         nsTF.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "HINT_targetNamespace"));
155
156         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
157         gridBagConstraints.gridx = 0;
158         gridBagConstraints.gridy = 5;
159         gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
160         gridBagConstraints.weighty = 1.0;
161         add(jPanel1, gridBagConstraints);
162
163     }// </editor-fold>//GEN-END:initComponents
164

165     private void browseButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseButtonActionPerformed
166
// TODO add your handling code here:
167
String JavaDoc dialogTitle = NbBundle.getMessage(WsdlUIPanel.class,"TITLE_selectSchema");
168         String JavaDoc maskTitle = NbBundle.getMessage(WsdlUIPanel.class,"TXT_schemaFiles");
169         java.io.File JavaDoc[] files = Utilities.selectFiles("xsd XSD", dialogTitle, maskTitle, wizardPanel.getProject());
170         if(files == null || files.length ==0) return;
171         String JavaDoc original = schemaTF.getText().trim();
172         StringBuilder JavaDoc fileString = new StringBuilder JavaDoc(original);
173         for(int i = 0; i < files.length; i++) {
174             java.io.File JavaDoc f = files[i];
175             String JavaDoc location = f.toURI().normalize().toString();
176             if (fileString.indexOf(location)>=0) continue;
177             if(fileString.length() > 0){
178                 fileString.append(",");
179             }
180             fileString.append(location);
181         }
182         schemaTF.setText(fileString.toString());
183     }//GEN-LAST:event_browseButtonActionPerformed
184

185     private void cbImportItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_cbImportItemStateChanged
186
// TODO add your handling code here:
187
if (cbImport.isSelected()) {
188             schemaTF.setEditable(true);
189             browseButton.setEnabled(true);
190         } else {
191             schemaTF.setEditable(false);
192             browseButton.setEnabled(false);
193         }
194     }//GEN-LAST:event_cbImportItemStateChanged
195

196     
197     // Variables declaration - do not modify//GEN-BEGIN:variables
198
private javax.swing.JButton JavaDoc browseButton;
199     private javax.swing.JCheckBox JavaDoc cbImport;
200     private javax.swing.JPanel JavaDoc jPanel1;
201     private javax.swing.JLabel JavaDoc namespaceLB;
202     private javax.swing.JTextField JavaDoc nsTF;
203     private javax.swing.JLabel JavaDoc schemaLB;
204     private javax.swing.JTextField JavaDoc schemaTF;
205     // End of variables declaration//GEN-END:variables
206

207     private class DocListener implements javax.swing.event.DocumentListener JavaDoc {
208         
209         public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
210             documentChanged(e);
211         }
212         
213         public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
214             documentChanged(e);
215         }
216         
217         public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
218             documentChanged(e);
219         }
220         
221         private void documentChanged(javax.swing.event.DocumentEvent JavaDoc e) {
222             String JavaDoc ns = nsTF.getText();
223             int index = ns.lastIndexOf('/');
224             if (index>0) {
225                 boolean hasExt = ns.endsWith(".wsdl"); //NOI18N
226
nsTF.setText(ns.substring(0,index+1)+fileNameTF.getText().trim()+(hasExt?".wsdl":"")); //NOI18N
227
}
228         }
229     }
230     
231     /** Class than provides basic informationn about schema file
232      */

233     static class SchemaInfo {
234         private java.net.URL JavaDoc url;
235         SchemaInfo(java.net.URL JavaDoc url) {
236             this.url=url;
237         }
238         
239         java.net.URL JavaDoc getURL() {
240             return url;
241         }
242         
243         String JavaDoc getNamespace() {
244             InputSource is = new InputSource(url.toExternalForm());
245             try {
246                 return parse(is);
247             } catch (java.io.IOException JavaDoc ex){
248             } catch (SAXException ex){}
249             return "";
250         }
251         
252         String JavaDoc getSchemaName() {
253             return url.toExternalForm();
254         }
255         
256         /** Parses XML document and creates the list of tags
257          */

258         private String JavaDoc parse(InputSource is) throws java.io.IOException JavaDoc, SAXException {
259             XMLReader xmlReader = org.openide.xml.XMLUtil.createXMLReader();
260             NsHandler handler = new NsHandler();
261             xmlReader.setContentHandler(handler);
262             //xmlReader.setFeature("http://xml.org/sax/features/use-locator2",true);
263
try {
264                 xmlReader.parse(is);
265             } catch (SAXException ex) {
266                 if (!"EXIT".equals(ex.getMessage())) throw ex; //NOI18N
267
}
268             String JavaDoc ns = handler.getNs();
269             if (ns==null) return "";
270             else return ns;
271         }
272         
273         private static class NsHandler extends org.xml.sax.helpers.DefaultHandler JavaDoc {
274             private String JavaDoc ns;
275             
276             public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes attributes) throws SAXException {
277                 if (qName.endsWith("schema")) { //NOI18N
278
ns=attributes.getValue("targetNamespace"); //NOI18N
279
throw new SAXException("EXIT"); //NOI18N
280
}
281             }
282             
283             String JavaDoc getNs() {
284                 return ns;
285             }
286         }
287         
288     }
289     
290     String JavaDoc getNS() {
291         return nsTF.getText();
292     }
293     
294     String JavaDoc getWsName() {
295         if (fileNameTF!=null)
296             return fileNameTF.getText();
297         else
298             return DEFAULT_SERVICE_NAME;
299     }
300     
301     boolean isImport() {
302         return cbImport.isSelected();
303     }
304     
305     SchemaInfo[] getSchemas() {
306         if (cbImport.isSelected()) {
307             String JavaDoc schemas = schemaTF.getText();
308             String JavaDoc[] urls = schemas.split(",");
309             List JavaDoc<SchemaInfo> infos = new ArrayList JavaDoc<SchemaInfo>();
310             for (int i=0;i<urls.length;i++) {
311                 String JavaDoc urlString=urls[i].trim();
312                 if (urlString.length()==0) continue;
313                 try {
314                     java.net.URL JavaDoc url = new java.net.URL JavaDoc(urlString);
315                     infos.add(new SchemaInfo(url));
316                 } catch (java.net.MalformedURLException JavaDoc ex) {
317                     // testing if target folder contains XML Schema
318
try {
319                         org.openide.loaders.DataFolder folder = wizardPanel.getTemplateWizard().getTargetFolder();
320                         org.openide.filesystems.FileObject fo = folder.getPrimaryFile();
321                         if ((fo.getFileObject(urlString))!=null) {
322                             String JavaDoc parentURL = fo.getURL().toExternalForm();
323                             infos.add(new SchemaInfo(new java.net.URL JavaDoc(parentURL+urlString)));
324                         }
325                     } catch (java.io.IOException JavaDoc ex1) {}
326                 }
327             }
328             SchemaInfo[] result = new SchemaInfo[infos.size()];
329             infos.<SchemaInfo>toArray(result);
330             return result;
331         }
332         return new SchemaInfo[]{};
333     }
334 }
335
Popular Tags