KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > wizard > BasicConfVisualPanel


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.apisupport.project.ui.wizard;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import javax.swing.event.DocumentEvent JavaDoc;
26 import javax.swing.event.DocumentListener JavaDoc;
27 import org.netbeans.api.project.Project;
28 import org.netbeans.api.project.ProjectManager;
29 import org.netbeans.api.project.ProjectUtils;
30 import org.netbeans.modules.apisupport.project.Util;
31 import org.netbeans.modules.apisupport.project.ui.UIUtil;
32 import org.netbeans.modules.apisupport.project.ui.customizer.SuiteUtils;
33 import org.openide.ErrorManager;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.util.NbBundle;
37
38 /**
39  * Second UI panel of <code>NewNbModuleWizardIterator</code> for
40  * <em>standalone</em> module creating mode. Allow user to enter basic
41  * configuration:
42  *
43  * <ul>
44  * <li>Code Name Base</li>
45  * <li>Module Display Name</li>
46  * <li>Localizing Bundle</li>
47  * <li>XML Layer</li>
48  * </ul>
49  *
50  * @author Martin Krauskopf
51  */

52 final class BasicConfVisualPanel extends BasicVisualPanel.NewTemplatePanel {
53     
54     static final String JavaDoc EXAMPLE_BASE_NAME = "org.yourorghere."; // NOI18N
55

56     private boolean wasLayerUpdated;
57     private boolean wasBundleUpdated;
58     
59     private boolean listenersAttached;
60     private final DocumentListener JavaDoc cnbDL;
61     private final DocumentListener JavaDoc layerDL;
62     private final DocumentListener JavaDoc bundleDL;
63     
64     public BasicConfVisualPanel(final NewModuleProjectData data) {
65         super(data);
66         initComponents();
67         initAccessibility();
68         cnbDL = new UIUtil.DocumentAdapter() {
69             public void insertUpdate(DocumentEvent JavaDoc e) { checkCodeNameBase(); }
70         };
71         if (isLibraryWizard()) {
72             // for library modules, don't generate any layer.
73
layer.setVisible(false);
74             layerValue.setVisible(false);
75             layerDL = null;
76         } else {
77             layerDL = new UIUtil.DocumentAdapter() {
78                 public void insertUpdate(DocumentEvent JavaDoc e) { wasLayerUpdated = true; checkLayer(); }
79             };
80         }
81         bundleDL = new UIUtil.DocumentAdapter() {
82             public void insertUpdate(DocumentEvent JavaDoc e) { wasBundleUpdated = true; checkBundle(); }
83         };
84     }
85     
86     private void initAccessibility() {
87         this.getAccessibleContext().setAccessibleDescription(getMessage("ACS_BasicConfVisualPanel"));
88         bundleValue.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_BundleValue"));
89         codeNameBaseValue.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_CodeNameBaseValue"));
90         displayNameValue.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_DisplayNameValue"));
91         layerValue.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_LayerValue"));
92     }
93     
94     private void checkCodeNameBase() {
95         if (!Util.isValidJavaFQN(getCodeNameBaseValue())) {
96             setError(getMessage("MSG_InvalidCNB"));
97         } else if (getData().isSuiteComponent() && cnbIsAlreadyInSuite(getData().getSuiteRoot(), getCodeNameBaseValue())) {
98             setError(NbBundle.getMessage(BasicConfVisualPanel.class, "MSG_ComponentWithSuchCNBAlreadyInSuite",
99                     getCodeNameBaseValue()));
100         } else {
101             markValid();
102             // update layer and bundle from the cnb
103
String JavaDoc dotName = getCodeNameBaseValue();
104             String JavaDoc slashName = dotName.replace('.', '/');
105             if (!wasBundleUpdated) {
106                 bundleValue.setText(slashName + "/Bundle.properties"); // NOI18N
107
wasBundleUpdated = false;
108             }
109             if (!wasLayerUpdated && !isLibraryWizard()) {
110                 layerValue.setText(slashName + "/layer.xml"); // NOI18N
111
wasLayerUpdated = false;
112             }
113         }
114     }
115     
116     private void checkBundle() {
117         checkEntry(getBundleValue(), "bundle", ".properties"); // NOI18N
118
}
119     
120     private void checkLayer() {
121         checkEntry(getLayerValue(), "layer", ".xml"); // NOI18N
122
}
123     
124     /** Used for Layer and Bundle entries. */
125     private void checkEntry(String JavaDoc path, String JavaDoc resName, String JavaDoc extension) {
126         if (path.length() == 0) {
127             setError(NbBundle.getMessage(BasicConfVisualPanel.class, "BasicConfVisualPanel_err_" + resName + "_empty"));
128             return;
129         }
130         if (path.indexOf('/') == -1) {
131             setError(NbBundle.getMessage(BasicConfVisualPanel.class, "BasicConfVisualPanel_err_" + resName + "_def_pkg"));
132             return;
133         }
134         if (!path.endsWith(extension)) {
135             setError(NbBundle.getMessage(BasicConfVisualPanel.class, "BasicConfVisualPanel_err_" + resName + "_ext", extension));
136             return;
137         }
138         markValid();
139     }
140     
141     void refreshData() {
142         String JavaDoc cnb = getData().getCodeNameBase();
143         codeNameBaseValue.setText(cnb);
144         if (cnb.startsWith(EXAMPLE_BASE_NAME)) {
145             codeNameBaseValue.select(0, EXAMPLE_BASE_NAME.length() - 1);
146         }
147         String JavaDoc dn = getData().getProjectDisplayName();
148         displayNameValue.setText(dn);
149         checkCodeNameBase();
150     }
151     
152     /** Stores collected data into model. */
153     void storeData() {
154         // change will be fired -> update data
155
getData().setCodeNameBase(getCodeNameBaseValue());
156         getData().setProjectDisplayName(displayNameValue.getText());
157         getData().setBundle(getBundleValue());
158         if (!isLibraryWizard()) {
159             getData().setLayer(getLayerValue());
160         }
161     }
162     
163     private String JavaDoc getCodeNameBaseValue() {
164         return codeNameBaseValue.getText().trim();
165     }
166     
167     private String JavaDoc getBundleValue() {
168         return bundleValue.getText().trim();
169     }
170     
171     private String JavaDoc getLayerValue() {
172         return layerValue.getText().trim();
173     }
174     
175     private boolean cnbIsAlreadyInSuite(String JavaDoc suiteDir, String JavaDoc cnb) {
176         boolean result = false;
177         FileObject suiteDirFO = FileUtil.toFileObject(new File JavaDoc(suiteDir));
178         try {
179             Project suite = ProjectManager.getDefault().findProject(suiteDirFO);
180             for (Iterator JavaDoc it = SuiteUtils.getSubProjects(suite).iterator(); it.hasNext();) {
181                 Project p = (Project) it.next();
182                 if (ProjectUtils.getInformation(p).getName().equals(cnb)) {
183                     result = true;
184                     break;
185                 }
186             }
187         } catch (IOException JavaDoc e) {
188             Util.err.notify(ErrorManager.INFORMATIONAL, e);
189         }
190         return result;
191     }
192     
193     public void addNotify() {
194         super.addNotify();
195         attachDocumentListeners();
196     }
197     
198     public void removeNotify() {
199         // prevent checking when the panel is not "active"
200
removeDocumentListeners();
201         super.removeNotify();
202     }
203     
204     private void attachDocumentListeners() {
205         if (!listenersAttached) {
206             codeNameBaseValue.getDocument().addDocumentListener(cnbDL);
207             bundleValue.getDocument().addDocumentListener(bundleDL);
208             if (!isLibraryWizard()) {
209                 layerValue.getDocument().addDocumentListener(layerDL);
210             }
211             listenersAttached = true;
212         }
213     }
214     
215     private void removeDocumentListeners() {
216         if (listenersAttached) {
217             codeNameBaseValue.getDocument().removeDocumentListener(cnbDL);
218             bundleValue.getDocument().removeDocumentListener(bundleDL);
219             if (!isLibraryWizard()) {
220                 layerValue.getDocument().removeDocumentListener(layerDL);
221             }
222             listenersAttached = false;
223         }
224     }
225     
226     private static String JavaDoc getMessage(String JavaDoc key) {
227         return NbBundle.getMessage(BasicConfVisualPanel.class, key);
228     }
229     
230     /** This method is called from within the constructor to
231      * initialize the form.
232      * WARNING: Do NOT modify this code. The content of this method is
233      * always regenerated by the Form Editor.
234      */

235     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
236
private void initComponents() {
237         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
238
239         confPanel = new javax.swing.JPanel JavaDoc();
240         codeNameBase = new javax.swing.JLabel JavaDoc();
241         displayName = new javax.swing.JLabel JavaDoc();
242         bundle = new javax.swing.JLabel JavaDoc();
243         layer = new javax.swing.JLabel JavaDoc();
244         codeNameBaseValue = new javax.swing.JTextField JavaDoc();
245         displayNameValue = new javax.swing.JTextField JavaDoc();
246         bundleValue = new javax.swing.JTextField JavaDoc();
247         layerValue = new javax.swing.JTextField JavaDoc();
248         filler = new javax.swing.JLabel JavaDoc();
249
250         setLayout(new java.awt.GridBagLayout JavaDoc());
251
252         confPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
253
254         codeNameBase.setLabelFor(codeNameBaseValue);
255         org.openide.awt.Mnemonics.setLocalizedText(codeNameBase, org.openide.util.NbBundle.getMessage(BasicConfVisualPanel.class, "LBL_CodeNameBase"));
256         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
257         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
258         gridBagConstraints.insets = new java.awt.Insets JavaDoc(1, 0, 6, 12);
259         confPanel.add(codeNameBase, gridBagConstraints);
260
261         displayName.setLabelFor(displayNameValue);
262         org.openide.awt.Mnemonics.setLocalizedText(displayName, org.openide.util.NbBundle.getMessage(BasicConfVisualPanel.class, "LBL_ModuleDisplayName"));
263         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
264         gridBagConstraints.gridx = 0;
265         gridBagConstraints.gridy = 1;
266         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
267         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
268         confPanel.add(displayName, gridBagConstraints);
269
270         bundle.setLabelFor(bundleValue);
271         org.openide.awt.Mnemonics.setLocalizedText(bundle, org.openide.util.NbBundle.getMessage(BasicConfVisualPanel.class, "LBL_LocalizingBundle"));
272         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
273         gridBagConstraints.gridx = 0;
274         gridBagConstraints.gridy = 2;
275         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
276         gridBagConstraints.insets = new java.awt.Insets JavaDoc(18, 0, 0, 12);
277         confPanel.add(bundle, gridBagConstraints);
278
279         layer.setLabelFor(layerValue);
280         org.openide.awt.Mnemonics.setLocalizedText(layer, org.openide.util.NbBundle.getMessage(BasicConfVisualPanel.class, "LBL_XMLLayer"));
281         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
282         gridBagConstraints.gridx = 0;
283         gridBagConstraints.gridy = 3;
284         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
285         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 0, 12);
286         confPanel.add(layer, gridBagConstraints);
287
288         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
289         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
290         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
291         gridBagConstraints.weightx = 1.0;
292         gridBagConstraints.insets = new java.awt.Insets JavaDoc(1, 0, 6, 0);
293         confPanel.add(codeNameBaseValue, gridBagConstraints);
294
295         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
296         gridBagConstraints.gridx = 1;
297         gridBagConstraints.gridy = 1;
298         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
299         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
300         confPanel.add(displayNameValue, gridBagConstraints);
301
302         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
303         gridBagConstraints.gridx = 1;
304         gridBagConstraints.gridy = 2;
305         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
306         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
307         gridBagConstraints.insets = new java.awt.Insets JavaDoc(18, 0, 0, 0);
308         confPanel.add(bundleValue, gridBagConstraints);
309
310         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
311         gridBagConstraints.gridx = 1;
312         gridBagConstraints.gridy = 3;
313         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
314         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
315         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 0, 0);
316         confPanel.add(layerValue, gridBagConstraints);
317
318         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
319         gridBagConstraints.gridx = 0;
320         gridBagConstraints.gridy = 4;
321         gridBagConstraints.gridwidth = 2;
322         gridBagConstraints.weighty = 1.0;
323         confPanel.add(filler, gridBagConstraints);
324
325         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
326         gridBagConstraints.gridx = 0;
327         gridBagConstraints.gridy = 0;
328         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
329         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
330         gridBagConstraints.weightx = 1.0;
331         gridBagConstraints.weighty = 1.0;
332         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 0, 4, 0);
333         add(confPanel, gridBagConstraints);
334
335     }
336     // </editor-fold>//GEN-END:initComponents
337

338     // Variables declaration - do not modify//GEN-BEGIN:variables
339
private javax.swing.JLabel JavaDoc bundle;
340     private javax.swing.JTextField JavaDoc bundleValue;
341     private javax.swing.JLabel JavaDoc codeNameBase;
342     private javax.swing.JTextField JavaDoc codeNameBaseValue;
343     private javax.swing.JPanel JavaDoc confPanel;
344     private javax.swing.JLabel JavaDoc displayName;
345     private javax.swing.JTextField JavaDoc displayNameValue;
346     private javax.swing.JLabel JavaDoc filler;
347     private javax.swing.JLabel JavaDoc layer;
348     private javax.swing.JTextField JavaDoc layerValue;
349     // End of variables declaration//GEN-END:variables
350

351 }
352
Popular Tags