KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > basic > wizard > SchemaAdditionalInfoGUI


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.schema.ui.basic.wizard;
21
22 //java imports
23
import java.awt.Component JavaDoc;
24 import java.awt.Container JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.swing.JTextField JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import javax.swing.event.DocumentEvent JavaDoc;
31 import javax.swing.event.DocumentListener JavaDoc;
32 import javax.swing.text.BadLocationException JavaDoc;
33 import org.netbeans.spi.project.ui.templates.support.Templates;
34 import org.openide.WizardDescriptor;
35 import org.openide.awt.Mnemonics;
36 import org.openide.loaders.TemplateWizard;
37
38 //netbeans imports
39
import org.openide.util.NbBundle;
40
41 /**
42  * This class represents the schema wizard, that is the GUI.
43  * Read http://performance.netbeans.org/howto/dialogs/wizard-panels.html.
44  *
45  * @author Samaresh (Samaresh.Panda@Sun.Com)
46  */

47 final class SchemaAdditionalInfoGUI extends javax.swing.JPanel JavaDoc {
48     
49     public static final String JavaDoc DEFAULT_TARGET_NAMESPACE = NbBundle.getMessage(SchemaAdditionalInfoGUI.class,"TXT_defaultTNS"); //NOI18N
50

51     private static final long serialVersionUID = 1L;
52     private final List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
53     private boolean namespaceChangedByUser;
54     private JTextField JavaDoc fileField;
55     WizardDescriptor.Panel parentPanel;
56     
57     /**
58      * Creates new form SimpleTargetChooserGUI
59      */

60     public SchemaAdditionalInfoGUI() {
61     initComponents();
62     targetNamespaceTextField.setText(DEFAULT_TARGET_NAMESPACE);
63     namespaceChangedByUser = false;
64     targetNamespaceTextField.getDocument().addDocumentListener(
65         new DocumentListener JavaDoc(){
66         public void removeUpdate(DocumentEvent JavaDoc e) {
67         namespaceChangedByUser = true;
68         }
69         
70         public void insertUpdate(DocumentEvent JavaDoc e) {
71         namespaceChangedByUser = true;
72         }
73         
74         public void changedUpdate(DocumentEvent JavaDoc e) {
75         // attribute change
76
}
77         
78     });
79     }
80     
81     /**
82      * Returns the target namespace as entered by the user.
83      */

84     public String JavaDoc getTargetNamespace() {
85     return targetNamespaceTextField.getText();
86     }
87     
88     
89     private JTextField JavaDoc findFileNameField(Component JavaDoc panel, String JavaDoc text) {
90     Collection JavaDoc<Component JavaDoc> allComponents = new ArrayList JavaDoc<Component JavaDoc>();
91     getAllComponents(new Component JavaDoc[] {panel}, allComponents);
92     for (Component JavaDoc c : allComponents) {
93         // we assume that the first text field is the file text field
94
if (c instanceof JTextField JavaDoc) {
95         JTextField JavaDoc tf = (JTextField JavaDoc) c;
96         //if (text.equals(tf.getText())) {
97
return tf;
98         //}
99
}
100     }
101     return null;
102     }
103     
104     /*
105      * Recursively gets all components in the components array and puts it in allComponents
106      */

107     public static void getAllComponents( Component JavaDoc[] components, Collection JavaDoc<Component JavaDoc> allComponents ) {
108     for( int i = 0; i < components.length; i++ ) {
109         if( components[i] != null ) {
110         allComponents.add( components[i] );
111         if( ( ( Container JavaDoc )components[i] ).getComponentCount() != 0 ) {
112             getAllComponents( ( ( Container JavaDoc )components[i] ).getComponents(), allComponents );
113         }
114         }
115     }
116     }
117     
118     void setParentPanel(WizardDescriptor.Panel panel) {
119         this.parentPanel = panel;
120     }
121     
122     public void attachListenerToFileName(TemplateWizard wizard) {
123     if (fileField != null) {
124         return;
125     }
126         
127     fileField = findFileNameField(
128         parentPanel.getComponent(),
129         Templates.getTemplate(wizard).getName());
130     if (fileField != null) {
131         fileField.getDocument().addDocumentListener(
132         new DocumentListener JavaDoc() {
133         public void removeUpdate(DocumentEvent JavaDoc e) {
134             getText(e);
135         }
136         
137         public void insertUpdate(DocumentEvent JavaDoc e) {
138             getText(e);
139         }
140         
141         public void changedUpdate(DocumentEvent JavaDoc e) {
142             
143         }
144         
145         private void getText(DocumentEvent JavaDoc e) {
146             try {
147             String JavaDoc t =
148                 e.getDocument().getText(0, e.getDocument().getLength());
149             changeDefaultURL(t);
150             } catch (BadLocationException JavaDoc ex) {
151             // ignore the event
152
}
153             
154         }
155         }
156         );
157     }
158     }
159     
160     private void changeDefaultURL(String JavaDoc fileName) {
161     if (!namespaceChangedByUser && (fileName != null)) {
162         String JavaDoc currentNamespace = targetNamespaceTextField.getText();
163         String JavaDoc generatedNamespace =
164         currentNamespace.substring(0, currentNamespace.lastIndexOf("/")+1); //NOI18N
165
generatedNamespace += fileName;
166         targetNamespaceTextField.setText(generatedNamespace);
167         namespaceChangedByUser = false;
168     }
169     }
170     
171     /**
172      * Allows addition of listeners.
173      */

174     public void addChangeListener(ChangeListener JavaDoc l) {
175     listeners.add(l);
176     }
177     
178     /**
179      * Allows deletion of listeners.
180      */

181     public void removeChangeListener(ChangeListener JavaDoc l) {
182     listeners.remove(l);
183     }
184     
185 // /**
186
// * Fires state change event.
187
// */
188
// private void fireChange() {
189
// ChangeEvent e = new ChangeEvent(this);
190
// List templist;
191
// synchronized (this) {
192
// templist = new ArrayList (listeners);
193
// }
194
// Iterator it = templist.iterator();
195
// while (it.hasNext()) {
196
// ((ChangeListener)it.next()).stateChanged(e);
197
// }
198
// }
199

200     /**
201      * This method is called from within the constructor to
202      * initialize the form.
203      * WARNING: Do NOT modify this code. The content of this method is
204      * always regenerated by the Form Editor.
205      */

206     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
207
private void initComponents() {
208         targetNamespaceLabel = new javax.swing.JLabel JavaDoc();
209         Mnemonics.setLocalizedText(targetNamespaceLabel, NbBundle.getMessage(SchemaAdditionalInfoGUI.class, "LBL_TargetNamespace"));
210         targetNamespaceTextField = new javax.swing.JTextField JavaDoc();
211
212         targetNamespaceLabel.setLabelFor(targetNamespaceTextField);
213
214         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
215         this.setLayout(layout);
216         layout.setHorizontalGroup(
217             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
218             .add(layout.createSequentialGroup()
219                 .add(targetNamespaceLabel)
220                 .add(10, 10, 10)
221                 .add(targetNamespaceTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE))
222         );
223         layout.setVerticalGroup(
224             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
225             .add(layout.createSequentialGroup()
226                 .addContainerGap()
227                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
228                     .add(targetNamespaceLabel)
229                     .add(targetNamespaceTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
230                 .addContainerGap(21, Short.MAX_VALUE))
231         );
232     }// </editor-fold>//GEN-END:initComponents
233

234     
235     // Variables declaration - do not modify//GEN-BEGIN:variables
236
private javax.swing.JLabel JavaDoc targetNamespaceLabel;
237     private javax.swing.JTextField JavaDoc targetNamespaceTextField;
238     // End of variables declaration//GEN-END:variables
239

240 }
241
Popular Tags