KickJava   Java API By Example, From Geeks To Geeks.

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


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.xml.core.wizard;
21
22 import java.awt.*;
23 import java.awt.event.*;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26
27 import java.util.Enumeration JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import javax.swing.ComboBoxModel JavaDoc;
33 import javax.swing.DefaultComboBoxModel JavaDoc;
34 import javax.swing.FocusManager JavaDoc;
35 import javax.swing.text.JTextComponent JavaDoc;
36
37 import org.openide.nodes.Children;
38 import org.openide.nodes.FilterNode;
39 import org.openide.nodes.Node;
40 import org.openide.nodes.NodeOperation;
41
42 import org.xml.sax.*;
43
44 /**
45  * Gathers data for DTD driven XML document instantionation.
46  *
47  * @author Petr Kuzel
48  * @version it does not work well, no PIDs no roots, ...
49  */

50 public class DTDPanel extends AbstractPanel {
51     /** Serial Version UID */
52     private static final long serialVersionUID = 5310047495162425192L;
53     
54     
55     /** Creates new form DTDPanel */
56     public DTDPanel() {
57         initComponents();
58         initAccessibility();
59         
60         // attach focus listener to editor directly
61
Component JavaDoc editor = pidComboBox.getEditor().getEditorComponent();
62         editor.addFocusListener(new FocusAdapter() {
63             public void focusLost(FocusEvent e) {
64                 if (existsPID()) updatePossibilities();
65             }
66         });
67         
68     }
69     
70     /** This method is called from within the constructor to
71      * initialize the form.
72      * WARNING: Do NOT modify this code. The content of this method is
73      * always regenerated by the Form Editor.
74      */

75     private void initComponents() {//GEN-BEGIN:initComponents
76
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
77
78         descTextArea = new javax.swing.JTextArea JavaDoc();
79         pidLabel = new javax.swing.JLabel JavaDoc();
80         pidComboBox = new javax.swing.JComboBox JavaDoc();
81         catalogButton = new javax.swing.JButton JavaDoc();
82         sidLabel = new javax.swing.JLabel JavaDoc();
83         sidComboBox = new javax.swing.JComboBox JavaDoc();
84         rootLabel = new javax.swing.JLabel JavaDoc();
85         rootComboBox = new javax.swing.JComboBox JavaDoc();
86         fillPanel = new javax.swing.JPanel JavaDoc();
87
88         setLayout(new java.awt.GridBagLayout JavaDoc());
89
90         setName(Util.THIS.getString("PROP_dtd_panel_name"));
91         descTextArea.setEditable(false);
92         descTextArea.setLineWrap(true);
93         descTextArea.setText(Util.THIS.getString("MSG_dtd_panel_desc"));
94         descTextArea.setWrapStyleWord(true);
95         descTextArea.setDisabledTextColor(descTextArea.getForeground());
96         descTextArea.setEnabled(false);
97         descTextArea.setOpaque(false);
98         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
99         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
100         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
101         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
102         add(descTextArea, gridBagConstraints);
103
104         pidLabel.setLabelFor(pidComboBox);
105         pidLabel.setText(Util.THIS.getString("PROP_dtd_pid_name"));
106         pidLabel.setToolTipText(Util.THIS.getString("PROP_dtd_pidLable_desc"));
107         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
108         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
109         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 12);
110         add(pidLabel, gridBagConstraints);
111
112         pidComboBox.setEditable(true);
113         pidComboBox.setToolTipText(Util.THIS.getString("PROP_dtd_pidComboBox_desc"));
114         pidComboBox.addActionListener(new java.awt.event.ActionListener JavaDoc() {
115             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
116                 pidComboBoxActionPerformed(evt);
117             }
118         });
119
120         pidComboBox.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
121             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
122                 pidComboBoxFocusLost(evt);
123             }
124         });
125
126         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
127         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
128         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
129         gridBagConstraints.weightx = 1.0;
130         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
131         add(pidComboBox, gridBagConstraints);
132
133         catalogButton.setText(Util.THIS.getString("PROP_dtd_catalog_name"));
134         catalogButton.setToolTipText(Util.THIS.getString("PROP_dtd_catalog_desc"));
135         catalogButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
136             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
137                 catalogButtonActionPerformed(evt);
138             }
139         });
140
141         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
142         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
143         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
144         add(catalogButton, gridBagConstraints);
145
146         sidLabel.setLabelFor(sidComboBox);
147         sidLabel.setText(Util.THIS.getString("PROP_dtd_sid_name"));
148         sidLabel.setToolTipText(Util.THIS.getString("PROP_dtd_sidLabel_desc"));
149         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
150         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
151         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 12);
152         add(sidLabel, gridBagConstraints);
153
154         sidComboBox.setEditable(true);
155         sidComboBox.setToolTipText(Util.THIS.getString("PROP_dtd_sidComboBox_desc"));
156         sidComboBox.addActionListener(new java.awt.event.ActionListener JavaDoc() {
157             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
158                 sidComboBoxActionPerformed(evt);
159             }
160         });
161
162         sidComboBox.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
163             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
164                 sidComboBoxFocusLost(evt);
165             }
166         });
167
168         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
169         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
170         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
171         gridBagConstraints.weightx = 1.0;
172         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
173         add(sidComboBox, gridBagConstraints);
174
175         rootLabel.setLabelFor(rootComboBox);
176         rootLabel.setText(Util.THIS.getString("PROP_dtd_root_name"));
177         rootLabel.setToolTipText(Util.THIS.getString("PROP_dtd_rootLabel_desc"));
178         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
179         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
180         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 12);
181         add(rootLabel, gridBagConstraints);
182
183         rootComboBox.setEditable(true);
184         rootComboBox.setToolTipText(Util.THIS.getString("PROP_dtd_rootComboBox_desc"));
185         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
186         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
187         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
188         gridBagConstraints.weightx = 1.0;
189         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
190         add(rootComboBox, gridBagConstraints);
191
192         fillPanel.setLayout(null);
193
194         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
195         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
196         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
197         gridBagConstraints.weightx = 1.0;
198         gridBagConstraints.weighty = 1.0;
199         add(fillPanel, gridBagConstraints);
200
201     }//GEN-END:initComponents
202

203     private void catalogButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_catalogButtonActionPerformed
204
Node catalog = getCatalogNode();
205         if (catalog == null) return;
206         NodeOperation.getDefault().explore(catalog);
207     }//GEN-LAST:event_catalogButtonActionPerformed
208

209     private void initAccessibility() {
210         
211         Util util = Util.THIS;
212         rootLabel.setDisplayedMnemonic(util.getChar("PROP_dtd_rootLabel_mne"));
213         pidLabel.setDisplayedMnemonic(util.getChar("PROP_dtd_pidLabel_mne"));
214         sidLabel.setDisplayedMnemonic(util.getChar("PROP_dtd_sidLabel_mne"));
215         catalogButton.setMnemonic(util.getChar("PROP_dtd_catalog_mne"));
216         getAccessibleContext().setAccessibleDescription(descTextArea.getText());
217     }
218     
219     private void sidComboBoxActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_sidComboBoxActionPerformed
220
// ignore listing
221
if (sidComboBox.isPopupVisible()) return;
222         if (existsPID() == false) updatePossibilities();
223     }//GEN-LAST:event_sidComboBoxActionPerformed
224

225     private void pidComboBoxActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_pidComboBoxActionPerformed
226
// ignore listing
227
if (pidComboBox.isPopupVisible()) return;
228         
229         if (existsPID()) updatePossibilities();
230     }//GEN-LAST:event_pidComboBoxActionPerformed
231

232     private void pidComboBoxFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_pidComboBoxFocusLost
233
if (existsPID()) updatePossibilities();
234     }//GEN-LAST:event_pidComboBoxFocusLost
235

236     private void sidComboBoxFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_sidComboBoxFocusLost
237
if (existsPID() == false) updatePossibilities();
238     }//GEN-LAST:event_sidComboBoxFocusLost
239

240     /**
241      * Try to access catalog management node or <code>null</code>.
242      */

243     private Node getCatalogNode() {
244
245 // TopManager top = TopManager.getDefault();
246
// Node runtime = top.getPlaces().nodes().environment();
247
// Children children = runtime.getChildren();
248
// Enumeration en = children.nodes();
249
//
250
// while (en.hasMoreElements()) {
251
// Node next = (Node) en.nextElement();
252
// //!!! it is undeclared dependency on catalog module
253
// if ("XML-CATALOG".equals(next.getName())) { // NOI18N
254
// return new FilterNode(next);
255
// }
256
// }
257

258         return null;
259     }
260
261     // does user entered a PID
262
private boolean existsPID() {
263         Object JavaDoc pid = pidModel.getSelectedItem();
264         return (pid != null) && (pid instanceof String JavaDoc)
265             && (((String JavaDoc)pid).trim().equals("") == false);
266     }
267
268     private void updatePossibilities() {
269         //??? we are in AWT parse in separate task
270

271         Util.THIS.debug("Updating possible roots (DTD)...");
272
273         InputSource in = new InputSource();
274
275         Object JavaDoc sid = sidModel.getSelectedItem();
276         String JavaDoc systemId = null;
277         try {
278             if (sid != null) {
279                 systemId = sid.toString();
280                 URL JavaDoc context = model.getTargetFolderURL();
281                 if (context != null) {
282                     systemId = new URL JavaDoc(context, systemId).toExternalForm();
283                 }
284             }
285         } catch (MalformedURLException JavaDoc ex) {
286             // ignore it use one passes by user
287
}
288         in.setSystemId(systemId);
289
290         Object JavaDoc pid = pidModel.getSelectedItem();
291         in.setPublicId( pid == null ? null : pid.toString() );
292
293         Util.THIS.debug("PID: " + pid + ", SID:" + sid);
294         Set JavaDoc roots = new DTDParser().parse(in);
295
296         if (roots.size() > 0) {
297             rootModel.removeAllElements();
298             Iterator JavaDoc it = roots.iterator();
299             while (it.hasNext()) {
300                 String JavaDoc next = (String JavaDoc) it.next();
301                 rootModel.addElement(next);
302             }
303         }
304
305         // select suggested text (do not focus it)
306
Component JavaDoc editor = rootComboBox.getEditor().getEditorComponent();
307         if (editor instanceof JTextComponent JavaDoc) {
308             ((JTextComponent JavaDoc)editor).selectAll();
309         }
310     }
311
312     /** User just entered the panel, init view by model values
313      */

314     protected void initView() {
315         
316         // fetch catalogs etc.
317
String JavaDoc[] pids = Util.getKnownDTDPublicIDs();
318         pidModel = new DefaultComboBoxModel JavaDoc(pids);
319         sidModel = new DefaultComboBoxModel JavaDoc(recentSIDs);
320         rootModel = new DefaultComboBoxModel JavaDoc();
321         
322         pidComboBox.setModel(pidModel);
323         pidComboBox.getEditor().selectAll();
324
325         if (true /* #23966 */ || getCatalogNode() == null) {
326             catalogButton.setVisible(false);
327             // !!! gridwith mus be set somehow to remaider!
328
}
329         
330         // set models
331
rootComboBox.setModel(rootModel);
332         
333         sidComboBox.setModel(sidModel);
334         
335     }
336     
337     /** User just leaved the panel, update model
338      */

339     protected void updateModel() {
340         Object JavaDoc pid = pidModel.getSelectedItem();
341         model.setPublicID( pid == null ? null : pid.toString() );
342         
343         Object JavaDoc sid = sidModel.getSelectedItem();
344         model.setSystemID(sid == null ? null : sid.toString());
345         
346         Object JavaDoc root = rootModel.getSelectedItem();
347         model.setRoot(root == null ? null : root.toString());
348
349         Util.THIS.debug("Model updated PID: " + pid + " SID: " + sid + " root: " + root);
350         
351         // update recent
352

353         if (recentSIDs.contains(sid) == false) {
354             recentSIDs.add(sid);
355         }
356     }
357     
358     /** User just reentered the panel.
359      */

360     protected void updateView() {
361     }
362
363     private DefaultComboBoxModel JavaDoc rootModel;
364     private DefaultComboBoxModel JavaDoc pidModel;
365     private DefaultComboBoxModel JavaDoc sidModel;
366     
367     private static Vector JavaDoc recentSIDs = new Vector JavaDoc();
368     
369     // Variables declaration - do not modify//GEN-BEGIN:variables
370
private javax.swing.JLabel JavaDoc rootLabel;
371     private javax.swing.JButton JavaDoc catalogButton;
372     private javax.swing.JLabel JavaDoc pidLabel;
373     private javax.swing.JComboBox JavaDoc rootComboBox;
374     private javax.swing.JComboBox JavaDoc sidComboBox;
375     private javax.swing.JTextArea JavaDoc descTextArea;
376     private javax.swing.JComboBox JavaDoc pidComboBox;
377     private javax.swing.JLabel JavaDoc sidLabel;
378     private javax.swing.JPanel JavaDoc fillPanel;
379     // End of variables declaration//GEN-END:variables
380

381 }
382
Popular Tags