001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.apisupport.project.ui.wizard;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import javax.swing.event.DocumentEvent;
047: import javax.swing.event.DocumentListener;
048: import org.netbeans.api.project.Project;
049: import org.netbeans.api.project.ProjectManager;
050: import org.netbeans.api.project.ProjectUtils;
051: import org.netbeans.modules.apisupport.project.Util;
052: import org.netbeans.modules.apisupport.project.ui.UIUtil;
053: import org.netbeans.modules.apisupport.project.ui.customizer.SuiteUtils;
054: import org.openide.ErrorManager;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.util.NbBundle;
058:
059: /**
060: * Second UI panel of <code>NewNbModuleWizardIterator</code> for
061: * <em>standalone</em> module creating mode. Allow user to enter basic
062: * configuration:
063: *
064: * <ul>
065: * <li>Code Name Base</li>
066: * <li>Module Display Name</li>
067: * <li>Localizing Bundle</li>
068: * <li>XML Layer</li>
069: * </ul>
070: *
071: * @author Martin Krauskopf
072: */
073: final class BasicConfVisualPanel extends
074: BasicVisualPanel.NewTemplatePanel {
075:
076: static final String EXAMPLE_BASE_NAME = "org.yourorghere."; // NOI18N
077:
078: private boolean wasBundleUpdated;
079:
080: private boolean listenersAttached;
081: private final DocumentListener cnbDL;
082: private final DocumentListener layerDL;
083: private final DocumentListener bundleDL;
084:
085: public BasicConfVisualPanel(final NewModuleProjectData data) {
086: super (data);
087: initComponents();
088: initAccessibility();
089: cnbDL = new UIUtil.DocumentAdapter() {
090: public void insertUpdate(DocumentEvent e) {
091: checkCodeNameBase();
092: }
093: };
094: if (isLibraryWizard()) {
095: // for library modules, don't generate any layer.
096: layer.setVisible(false);
097: layerValue.setVisible(false);
098: layerDL = null;
099: } else {
100: layerDL = new UIUtil.DocumentAdapter() {
101: public void insertUpdate(DocumentEvent e) {
102: checkLayer();
103: }
104: };
105: }
106: bundleDL = new UIUtil.DocumentAdapter() {
107: public void insertUpdate(DocumentEvent e) {
108: wasBundleUpdated = true;
109: checkBundle();
110: }
111: };
112: }
113:
114: private void initAccessibility() {
115: this .getAccessibleContext().setAccessibleDescription(
116: getMessage("ACS_BasicConfVisualPanel"));
117: bundleValue.getAccessibleContext().setAccessibleDescription(
118: getMessage("ACS_CTL_BundleValue"));
119: codeNameBaseValue.getAccessibleContext()
120: .setAccessibleDescription(
121: getMessage("ACS_CTL_CodeNameBaseValue"));
122: displayNameValue.getAccessibleContext()
123: .setAccessibleDescription(
124: getMessage("ACS_CTL_DisplayNameValue"));
125: layerValue.getAccessibleContext().setAccessibleDescription(
126: getMessage("ACS_CTL_LayerValue"));
127: }
128:
129: private void checkCodeNameBase() {
130: String dotName = getCodeNameBaseValue();
131: if (!Util.isValidJavaFQN(dotName)) {
132: setError(getMessage("MSG_InvalidCNB"));
133: } else if (getData().isSuiteComponent()
134: && cnbIsAlreadyInSuite(getData().getSuiteRoot(),
135: dotName)) {
136: setError(NbBundle.getMessage(BasicConfVisualPanel.class,
137: "MSG_ComponentWithSuchCNBAlreadyInSuite", dotName));
138: } else {
139: markValid();
140: // update layer and bundle from the cnb
141: String slashName = dotName.replace('.', '/');
142: if (!wasBundleUpdated) {
143: bundleValue.setText(slashName + "/Bundle.properties"); // NOI18N
144: wasBundleUpdated = false;
145: }
146: if (getData().isNetBeansOrg()) {
147: // Ensure that official naming conventions are respected.
148: String cnbShort = abbreviate(dotName);
149: String name = getData().getProjectName();
150: if (!name.equals(cnbShort)) {
151: setError(NbBundle
152: .getMessage(
153: BasicConfVisualPanel.class,
154: "BasicConfVisualPanel_err_wrong_nborg_name",
155: cnbShort));
156: }
157: }
158: }
159: }
160:
161: private static String abbreviate(String cnb) {
162: return cnb.replaceFirst("^org\\.netbeans\\.modules\\.", ""). // NOI18N
163: replaceFirst(
164: "^org\\.netbeans\\.(libs|lib|api|spi|core)\\.",
165: "$1."). // NOI18N
166: replaceFirst("^org\\.netbeans\\.", "o.n."). // NOI18N
167: replaceFirst("^org\\.openide\\.", "openide."). // NOI18N
168: replaceFirst("^org\\.", "o."). // NOI18N
169: replaceFirst("^com\\.sun\\.", "c.s."). // NOI18N
170: replaceFirst("^com\\.", "c."); // NOI18N
171: }
172:
173: private void checkBundle() {
174: checkEntry(getBundleValue(), "bundle", ".properties"); // NOI18N
175: }
176:
177: private void checkLayer() {
178: String layerPath = getLayerValue();
179: if (layerPath != null) {
180: checkEntry(layerPath, "layer", ".xml"); // NOI18N
181: }
182: }
183:
184: /** Used for Layer and Bundle entries. */
185: private void checkEntry(String path, String resName,
186: String extension) {
187: if (path.length() == 0) {
188: setError(NbBundle.getMessage(BasicConfVisualPanel.class,
189: "BasicConfVisualPanel_err_" + resName + "_empty"));
190: return;
191: }
192: if (path.indexOf('/') == -1) {
193: setError(NbBundle.getMessage(BasicConfVisualPanel.class,
194: "BasicConfVisualPanel_err_" + resName + "_def_pkg"));
195: return;
196: }
197: if (!path.endsWith(extension)) {
198: setError(NbBundle.getMessage(BasicConfVisualPanel.class,
199: "BasicConfVisualPanel_err_" + resName + "_ext",
200: extension));
201: return;
202: }
203: markValid();
204: }
205:
206: void refreshData() {
207: String cnb = getData().getCodeNameBase();
208: codeNameBaseValue.setText(cnb);
209: if (cnb.startsWith(EXAMPLE_BASE_NAME)) {
210: codeNameBaseValue.select(0, EXAMPLE_BASE_NAME.length() - 1);
211: }
212: String dn = getData().getProjectDisplayName();
213: displayNameValue.setText(dn);
214: checkCodeNameBase();
215: }
216:
217: /** Stores collected data into model. */
218: void storeData() {
219: // change will be fired -> update data
220: getData().setCodeNameBase(getCodeNameBaseValue());
221: getData().setProjectDisplayName(displayNameValue.getText());
222: getData().setBundle(getBundleValue());
223: getData().setLayer(getLayerValue());
224: }
225:
226: private String getCodeNameBaseValue() {
227: return codeNameBaseValue.getText().trim();
228: }
229:
230: private String getBundleValue() {
231: return bundleValue.getText().trim();
232: }
233:
234: private String getLayerValue() {
235: String v = layerValue.getText().trim();
236: if (v.length() == 0) {
237: return null;
238: } else {
239: return v;
240: }
241: }
242:
243: private boolean cnbIsAlreadyInSuite(String suiteDir, String cnb) {
244: boolean result = false;
245: FileObject suiteDirFO = FileUtil
246: .toFileObject(new File(suiteDir));
247: try {
248: Project suite = ProjectManager.getDefault().findProject(
249: suiteDirFO);
250: for (Project p : SuiteUtils.getSubProjects(suite)) {
251: if (ProjectUtils.getInformation(p).getName()
252: .equals(cnb)) {
253: result = true;
254: break;
255: }
256: }
257: } catch (IOException e) {
258: Util.err.notify(ErrorManager.INFORMATIONAL, e);
259: }
260: return result;
261: }
262:
263: public @Override
264: void addNotify() {
265: super .addNotify();
266: attachDocumentListeners();
267: }
268:
269: public @Override
270: void removeNotify() {
271: // prevent checking when the panel is not "active"
272: removeDocumentListeners();
273: super .removeNotify();
274: }
275:
276: private void attachDocumentListeners() {
277: if (!listenersAttached) {
278: codeNameBaseValue.getDocument().addDocumentListener(cnbDL);
279: bundleValue.getDocument().addDocumentListener(bundleDL);
280: if (!isLibraryWizard()) {
281: layerValue.getDocument().addDocumentListener(layerDL);
282: }
283: listenersAttached = true;
284: }
285: }
286:
287: private void removeDocumentListeners() {
288: if (listenersAttached) {
289: codeNameBaseValue.getDocument().removeDocumentListener(
290: cnbDL);
291: bundleValue.getDocument().removeDocumentListener(bundleDL);
292: if (!isLibraryWizard()) {
293: layerValue.getDocument()
294: .removeDocumentListener(layerDL);
295: }
296: listenersAttached = false;
297: }
298: }
299:
300: private static String getMessage(String key) {
301: return NbBundle.getMessage(BasicConfVisualPanel.class, key);
302: }
303:
304: /** This method is called from within the constructor to
305: * initialize the form.
306: * WARNING: Do NOT modify this code. The content of this method is
307: * always regenerated by the Form Editor.
308: */
309: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
310: private void initComponents() {
311: java.awt.GridBagConstraints gridBagConstraints;
312:
313: confPanel = new javax.swing.JPanel();
314: codeNameBase = new javax.swing.JLabel();
315: displayName = new javax.swing.JLabel();
316: bundle = new javax.swing.JLabel();
317: layer = new javax.swing.JLabel();
318: codeNameBaseValue = new javax.swing.JTextField();
319: displayNameValue = new javax.swing.JTextField();
320: bundleValue = new javax.swing.JTextField();
321: layerValue = new javax.swing.JTextField();
322: filler = new javax.swing.JLabel();
323:
324: setLayout(new java.awt.GridBagLayout());
325:
326: confPanel.setLayout(new java.awt.GridBagLayout());
327:
328: codeNameBase.setLabelFor(codeNameBaseValue);
329: org.openide.awt.Mnemonics
330: .setLocalizedText(codeNameBase,
331: org.openide.util.NbBundle.getMessage(
332: BasicConfVisualPanel.class,
333: "LBL_CodeNameBase"));
334: gridBagConstraints = new java.awt.GridBagConstraints();
335: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
336: gridBagConstraints.insets = new java.awt.Insets(1, 0, 6, 12);
337: confPanel.add(codeNameBase, gridBagConstraints);
338:
339: displayName.setLabelFor(displayNameValue);
340: org.openide.awt.Mnemonics.setLocalizedText(displayName,
341: org.openide.util.NbBundle.getMessage(
342: BasicConfVisualPanel.class,
343: "LBL_ModuleDisplayName"));
344: gridBagConstraints = new java.awt.GridBagConstraints();
345: gridBagConstraints.gridx = 0;
346: gridBagConstraints.gridy = 1;
347: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
348: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12);
349: confPanel.add(displayName, gridBagConstraints);
350:
351: bundle.setLabelFor(bundleValue);
352: org.openide.awt.Mnemonics.setLocalizedText(bundle,
353: org.openide.util.NbBundle.getMessage(
354: BasicConfVisualPanel.class,
355: "LBL_LocalizingBundle"));
356: gridBagConstraints = new java.awt.GridBagConstraints();
357: gridBagConstraints.gridx = 0;
358: gridBagConstraints.gridy = 2;
359: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
360: gridBagConstraints.insets = new java.awt.Insets(18, 0, 0, 12);
361: confPanel.add(bundle, gridBagConstraints);
362:
363: layer.setLabelFor(layerValue);
364: org.openide.awt.Mnemonics.setLocalizedText(layer,
365: org.openide.util.NbBundle.getMessage(
366: BasicConfVisualPanel.class, "LBL_XMLLayer"));
367: gridBagConstraints = new java.awt.GridBagConstraints();
368: gridBagConstraints.gridx = 0;
369: gridBagConstraints.gridy = 3;
370: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
371: gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 12);
372: confPanel.add(layer, gridBagConstraints);
373:
374: gridBagConstraints = new java.awt.GridBagConstraints();
375: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
376: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
377: gridBagConstraints.weightx = 1.0;
378: gridBagConstraints.insets = new java.awt.Insets(1, 0, 6, 0);
379: confPanel.add(codeNameBaseValue, gridBagConstraints);
380:
381: gridBagConstraints = new java.awt.GridBagConstraints();
382: gridBagConstraints.gridx = 1;
383: gridBagConstraints.gridy = 1;
384: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
385: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
386: confPanel.add(displayNameValue, gridBagConstraints);
387:
388: gridBagConstraints = new java.awt.GridBagConstraints();
389: gridBagConstraints.gridx = 1;
390: gridBagConstraints.gridy = 2;
391: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
392: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
393: gridBagConstraints.insets = new java.awt.Insets(18, 0, 0, 0);
394: confPanel.add(bundleValue, gridBagConstraints);
395:
396: gridBagConstraints = new java.awt.GridBagConstraints();
397: gridBagConstraints.gridx = 1;
398: gridBagConstraints.gridy = 3;
399: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
400: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
401: gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
402: confPanel.add(layerValue, gridBagConstraints);
403:
404: gridBagConstraints = new java.awt.GridBagConstraints();
405: gridBagConstraints.gridx = 0;
406: gridBagConstraints.gridy = 4;
407: gridBagConstraints.gridwidth = 2;
408: gridBagConstraints.weighty = 1.0;
409: confPanel.add(filler, gridBagConstraints);
410:
411: gridBagConstraints = new java.awt.GridBagConstraints();
412: gridBagConstraints.gridx = 0;
413: gridBagConstraints.gridy = 0;
414: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
415: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
416: gridBagConstraints.weightx = 1.0;
417: gridBagConstraints.weighty = 1.0;
418: gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 0);
419: add(confPanel, gridBagConstraints);
420:
421: }
422:
423: // </editor-fold>//GEN-END:initComponents
424:
425: // Variables declaration - do not modify//GEN-BEGIN:variables
426: private javax.swing.JLabel bundle;
427: private javax.swing.JTextField bundleValue;
428: private javax.swing.JLabel codeNameBase;
429: private javax.swing.JTextField codeNameBaseValue;
430: private javax.swing.JPanel confPanel;
431: private javax.swing.JLabel displayName;
432: private javax.swing.JTextField displayNameValue;
433: private javax.swing.JLabel filler;
434: private javax.swing.JLabel layer;
435: private javax.swing.JTextField layerValue;
436: // End of variables declaration//GEN-END:variables
437:
438: }
|