KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > core > wizard > SchemaPanel


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 package org.netbeans.modules.xml.core.wizard;
20
21 import java.awt.Component JavaDoc;
22 import java.net.*;
23 import java.io.*;
24 import java.util.Iterator JavaDoc;
25 import javax.swing.DefaultComboBoxModel JavaDoc;
26 import javax.swing.text.JTextComponent JavaDoc;
27
28 /**
29  * This panel gathers data that are necessary for instantiting of XML
30  * document conforming to given XML Schema.
31  * <p>
32  * Data allows to create a document that respect restrictions of current parser
33  * implementations (they use schemaLocation hint specifically).
34  *
35  * @author Petr Kuzel
36  */

37 public class SchemaPanel extends AbstractPanel {
38     /** Serial Version UID */
39     private static final long serialVersionUID = -7568909683682244030L;
40     
41     
42     /** Creates new form SchemaPanel */
43     public SchemaPanel() {
44         initComponents();
45         initAccessibility();
46     }
47     
48     /** This method is called from within the constructor to
49      * initialize the form.
50      * WARNING: Do NOT modify this code. The content of this method is
51      * always regenerated by the Form Editor.
52      */

53     private void initComponents() {//GEN-BEGIN:initComponents
54
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
55
56         descTextArea = new javax.swing.JTextArea JavaDoc();
57         locationLabel = new javax.swing.JLabel JavaDoc();
58         locationTextField = new javax.swing.JTextField JavaDoc();
59         locationButton = new javax.swing.JButton JavaDoc();
60         nsLabel = new javax.swing.JLabel JavaDoc();
61         nsComboBox = new javax.swing.JComboBox JavaDoc();
62         rootLabel = new javax.swing.JLabel JavaDoc();
63         rootComboBox = new javax.swing.JComboBox JavaDoc();
64         fillPanel = new javax.swing.JPanel JavaDoc();
65
66         setLayout(new java.awt.GridBagLayout JavaDoc());
67
68         setName(Util.THIS.getString("PROP_schema_panel_name"));
69         descTextArea.setColumns(20);
70         descTextArea.setEditable(false);
71         descTextArea.setLineWrap(true);
72         descTextArea.setText(Util.THIS.getString("MSG_schema_wizard_desc"));
73         descTextArea.setWrapStyleWord(true);
74         descTextArea.setDisabledTextColor(descTextArea.getForeground());
75         descTextArea.setEnabled(false);
76         descTextArea.setOpaque(false);
77         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
78         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
79         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
80         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
81         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
82         add(descTextArea, gridBagConstraints);
83
84         locationLabel.setLabelFor(locationTextField);
85         locationLabel.setText(Util.THIS.getString("LBL_schema_location"));
86         locationLabel.setToolTipText(Util.THIS.getString("PROP_schema_locationLabel_desc"));
87         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
88         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
89         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 12);
90         add(locationLabel, gridBagConstraints);
91
92         locationTextField.setToolTipText(Util.THIS.getString("PROP_schema_locationTextField_desc"));
93         locationTextField.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
94             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
95                 locationTextFieldFocusLost(evt);
96             }
97         });
98
99         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
100         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
101         gridBagConstraints.weightx = 1.0;
102         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 12);
103         add(locationTextField, gridBagConstraints);
104
105         locationButton.setText(Util.THIS.getString("LBL_browse"));
106         locationButton.setToolTipText(Util.THIS.getString("PROP_schema_locationButton_desc"));
107         locationButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
108             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
109                 locationButtonActionPerformed(evt);
110             }
111         });
112
113         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
114         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
115         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
116         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
117         add(locationButton, gridBagConstraints);
118
119         nsLabel.setLabelFor(nsComboBox);
120         nsLabel.setText(Util.THIS.getString("LBL_root_namespace"));
121         nsLabel.setToolTipText(Util.THIS.getString("PROP_schema_nsLabel_desc"));
122         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
123         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
124         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 12);
125         add(nsLabel, gridBagConstraints);
126
127         nsComboBox.setEditable(true);
128         nsComboBox.setToolTipText(Util.THIS.getString("PROP_schema_nsComboBox_desc"));
129         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
130         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
131         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
132         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
133         gridBagConstraints.weightx = 1.0;
134         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
135         add(nsComboBox, gridBagConstraints);
136
137         rootLabel.setLabelFor(rootComboBox);
138         rootLabel.setText(Util.THIS.getString("PROP_schema_root_name"));
139         rootLabel.setToolTipText(Util.THIS.getString("PROP_schema_rootLabel_desc"));
140         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
141         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
142         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
143         add(rootLabel, gridBagConstraints);
144
145         rootComboBox.setEditable(true);
146         rootComboBox.setToolTipText(Util.THIS.getString("PROP_schema_rootComboBox_desc"));
147         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
148         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
149         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
150         gridBagConstraints.weightx = 1.0;
151         add(rootComboBox, gridBagConstraints);
152
153         fillPanel.setLayout(null);
154
155         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
156         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
157         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
158         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
159         gridBagConstraints.weightx = 1.0;
160         gridBagConstraints.weighty = 1.0;
161         add(fillPanel, gridBagConstraints);
162
163     }//GEN-END:initComponents
164

165     private void initAccessibility() {
166
167         // memonics
168
Util util = Util.THIS;
169         locationLabel.setDisplayedMnemonic(util.getChar("PROP_schema_locationLabel_mne"));
170         rootLabel.setDisplayedMnemonic(util.getChar("PROP_schema_rootLabel_mne"));
171         nsLabel.setDisplayedMnemonic(util.getChar("PROP_schema_nsLabel_mne"));
172         locationButton.setMnemonic(util.getChar("PROP_schema_locationButton_mne"));
173         getAccessibleContext().setAccessibleDescription(descTextArea.getText());
174     }
175     
176     private void locationTextFieldFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_locationTextFieldFocusLost
177
updatePossibilities();
178     }//GEN-LAST:event_locationTextFieldFocusLost
179

180     private void locationButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_locationButtonActionPerformed
181
File file = Util.selectSchemaFile("xsd xml xsdl");
182         if (file == null) return;
183         try {
184             URL url = file.toURL();
185             locationTextField.setText(url.toExternalForm());
186             updatePossibilities();
187         } catch (MalformedURLException ex) {
188             // ignore it
189
}
190     }//GEN-LAST:event_locationButtonActionPerformed
191

192     /**
193      * Update namespace and root combo models.
194      */

195     private void updatePossibilities() {
196         
197         //??? we are in AWT parser in other Task and post results in AWT-queue
198

199         SchemaParser parser = new SchemaParser();
200         
201         String JavaDoc systemId = locationTextField.getText();
202         try {
203             URL context = model.getTargetFolderURL();
204             if (context != null) {
205                 systemId = new URL(context, systemId).toExternalForm();
206             }
207         } catch (MalformedURLException ex) {
208             // ignore it use one passes by user
209
}
210         SchemaParser.SchemaInfo info = parser.parse(systemId);
211         
212         if (info == null) return;
213         
214         nsModel.removeAllElements();
215         if (info.namespace != null) {
216             nsModel.addElement(info.namespace);
217         }
218         
219         if (info.roots.size() > 0) {
220             rootModel.removeAllElements();
221             Iterator JavaDoc it = info.roots.iterator();
222             while (it.hasNext()) {
223                 String JavaDoc next = (String JavaDoc) it.next();
224                 rootModel.addElement(next);
225             }
226         }
227         
228         // select suggested text (do not focus it)
229
Component JavaDoc editor = rootComboBox.getEditor().getEditorComponent();
230         if (editor instanceof JTextComponent JavaDoc) {
231             ((JTextComponent JavaDoc)editor).selectAll();
232         }
233     }
234     
235     /** User just entered the panel, init view by model values
236      */

237     protected void initView() {
238         
239         // we use well-defined mutable models
240

241         nsModel = new DefaultComboBoxModel JavaDoc();
242         rootModel = new DefaultComboBoxModel JavaDoc();
243         
244         nsComboBox.setModel(nsModel);
245         rootComboBox.setModel(rootModel);
246     }
247     
248     /** User just leaved the panel, update model
249      */

250     protected void updateModel() {
251         String JavaDoc uri = locationTextField.getText();
252         if (uri != null) {
253             // we need to escape spaces, URI does not like them
254
uri = uri.replaceAll(" ", "%20"); // NOI18N
255
try {
256                 // escape the non-ASCII characters
257
uri = new URI(uri).toASCIIString();
258             } catch (URISyntaxException e) {
259                 // the specified uri is not valid, it is too late to fix it now
260
}
261         }
262         model.setSystemID(uri == null || uri.length() == 0 ? null : uri);
263         
264         Object JavaDoc ns = nsComboBox.getSelectedItem();
265         model.setNamespace(ns == null ? null : ns.toString());
266         
267         Object JavaDoc root = rootComboBox.getSelectedItem();
268         model.setRoot(root == null ? null : root.toString());
269         
270     }
271     
272     /** User just reentered the panel.
273      */

274     protected void updateView() {
275     }
276     
277     private DefaultComboBoxModel JavaDoc nsModel;
278     private DefaultComboBoxModel JavaDoc rootModel;
279     
280     // Variables declaration - do not modify//GEN-BEGIN:variables
281
private javax.swing.JLabel JavaDoc locationLabel;
282     private javax.swing.JLabel JavaDoc rootLabel;
283     private javax.swing.JLabel JavaDoc nsLabel;
284     private javax.swing.JComboBox JavaDoc rootComboBox;
285     private javax.swing.JTextField JavaDoc locationTextField;
286     private javax.swing.JTextArea JavaDoc descTextArea;
287     private javax.swing.JButton JavaDoc locationButton;
288     private javax.swing.JComboBox JavaDoc nsComboBox;
289     private javax.swing.JPanel JavaDoc fillPanel;
290     // End of variables declaration//GEN-END:variables
291

292 }
293
Popular Tags