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.project;
043:
044: import java.awt.Color;
045: import java.util.Arrays;
046: import java.util.Collection;
047: import java.util.HashSet;
048: import javax.swing.JTextField;
049: import javax.swing.UIManager;
050: import javax.swing.event.DocumentEvent;
051: import javax.swing.event.DocumentListener;
052: import org.netbeans.api.project.ProjectUtils;
053: import org.netbeans.modules.apisupport.project.CreatedModifiedFiles;
054: import org.netbeans.modules.apisupport.project.NbModuleProject;
055: import org.netbeans.modules.apisupport.project.Util;
056: import org.netbeans.modules.apisupport.project.ui.UIUtil;
057: import org.netbeans.modules.apisupport.project.ui.wizard.BasicWizardIterator;
058: import org.netbeans.modules.apisupport.project.universe.ModuleEntry;
059: import org.netbeans.modules.apisupport.project.universe.NbPlatform;
060: import org.openide.WizardDescriptor;
061: import org.openide.util.HelpCtx;
062: import org.openide.util.NbBundle;
063: import org.openide.util.Utilities;
064:
065: /**
066: * The second panel in project template wizard.
067: *
068: * @author Milos Kleint
069: */
070: final class NameAndLocationPanel extends BasicWizardIterator.Panel {
071:
072: private static final String PROJECT_TEMPLATES_DIR = "Templates/Project"; // NOI18N
073: private static final String DEFAULT_CATEGORY_PATH = PROJECT_TEMPLATES_DIR
074: + "/Other"; // NOI18N
075:
076: private NewProjectIterator.DataModel data;
077:
078: /** Creates new NameAndLocationPanel */
079: NameAndLocationPanel(WizardDescriptor setting,
080: NewProjectIterator.DataModel data) {
081: super (setting);
082: this .data = data;
083: initComponents();
084: initAccessibility();
085: Color lblBgr = UIManager.getColor("Label.background"); // NOI18N
086: putClientProperty("NewFileWizard_Title",
087: getMessage("LBL_ProjectWizardTitle"));
088: modifiedFilesValue.setBackground(lblBgr);
089: createdFilesValue.setBackground(lblBgr);
090: modifiedFilesValue.setEditable(false);
091: createdFilesValue.setEditable(false);
092:
093: DocumentListener dListener = new UIUtil.DocumentAdapter() {
094: public void insertUpdate(DocumentEvent e) {
095: if (checkValidity()) {
096: updateData();
097: }
098: }
099: };
100: txtName.getDocument().addDocumentListener(dListener);
101: txtDisplayName.getDocument().addDocumentListener(dListener);
102: if (comCategory.getEditor().getEditorComponent() instanceof JTextField) {
103: JTextField txt = (JTextField) comCategory.getEditor()
104: .getEditorComponent();
105: txt.getDocument().addDocumentListener(dListener);
106: }
107: if (comPackageName.getEditor().getEditorComponent() instanceof JTextField) {
108: JTextField txt = (JTextField) comPackageName.getEditor()
109: .getEditorComponent();
110: txt.getDocument().addDocumentListener(dListener);
111: }
112: }
113:
114: protected void storeToDataModel() {
115: updateData();
116: }
117:
118: private String getCategoryPath() {
119: String path = UIUtil.getSFSPath(comCategory,
120: PROJECT_TEMPLATES_DIR);
121: return path == null ? DEFAULT_CATEGORY_PATH : path;
122: }
123:
124: private void updateData() {
125: data.setPackageName(comPackageName.getEditor().getItem()
126: .toString());
127: data.setName(txtName.getText().trim());
128: data.setDisplayName(txtDisplayName.getText().trim());
129: data.setCategory(getCategoryPath());
130: NewProjectIterator.generateFileChanges(data);
131: CreatedModifiedFiles fls = data.getCreatedModifiedFiles();
132: createdFilesValue.setText(generateText(fls.getCreatedPaths()));
133: modifiedFilesValue
134: .setText(generateText(fls.getModifiedPaths()));
135: //#68294 check if the paths for newly created files are valid or not..
136: String[] invalid = data.getCreatedModifiedFiles()
137: .getInvalidPaths();
138: if (invalid.length > 0) {
139: setError(NbBundle.getMessage(NameAndLocationPanel.class,
140: "ERR_ToBeCreateFileExists", invalid[0]));
141: }
142:
143: }
144:
145: protected void readFromDataModel() {
146: loadCombo();
147: if (data.getPackageName() != null) {
148: comPackageName.setSelectedItem(data.getPackageName());
149: }
150: checkValidity();
151: }
152:
153: protected String getPanelName() {
154: return getMessage("LBL_NameLocation_Title");
155: }
156:
157: private boolean checkValidity() {
158: if (!checkPlatformValidity()) {
159: return false;
160: }
161: if (txtName.getText().trim().length() == 0) {
162: setError(getMessage("ERR_Name_Prefix_Empty"));
163: return false;
164: }
165: if (!Utilities.isJavaIdentifier(txtName.getText().trim())) {
166: setError(getMessage("ERR_Name_Prefix_Invalid"));
167: return false;
168: }
169: String packageName = comPackageName.getEditor().getItem()
170: .toString().trim();
171: if (packageName.length() == 0
172: || !UIUtil.isValidPackageName(packageName)) {
173: setError(getMessage("ERR_Package_Invalid"));
174: return false;
175: }
176: if (!Util.isValidSFSPath(getCategoryPath())) {
177: setError(getMessage("ERR_Category_Invalid"));
178: return false;
179: }
180: markValid();
181: return true;
182: }
183:
184: private boolean checkPlatformValidity() {
185: NbModuleProject nbprj = data.getProject().getLookup().lookup(
186: NbModuleProject.class);
187: if (nbprj == null) {
188: //ignore this check for non default netbeans projects.
189: return true;
190: }
191: NbPlatform platform = nbprj.getPlatform(false);
192: if (platform == null) {
193: setError(getMessage("ERR_No_Platform"));
194: return false;
195: }
196: ModuleEntry[] entries = platform.getModules();
197: Collection<String> modules = new HashSet<String>(Arrays
198: .asList(NewProjectIterator.MODULES));
199:
200: for (int i = 0; i < entries.length; i++) {
201: modules.remove(entries[i].getCodeNameBase());
202: }
203: if (modules.size() > 0) {
204: setError(getMessage("ERR_Missing_Modules"));
205: return false;
206: }
207: return true;
208: }
209:
210: private void loadCombo() {
211: comCategory.setModel(UIUtil.createLayerPresenterComboModel(data
212: .getProject(), PROJECT_TEMPLATES_DIR));
213: }
214:
215: protected HelpCtx getHelp() {
216: return new HelpCtx(NameAndLocationPanel.class);
217: }
218:
219: private static String getMessage(String key) {
220: return NbBundle.getMessage(NameAndLocationPanel.class, key);
221: }
222:
223: /** This method is called from within the constructor to
224: * initialize the form.
225: * WARNING: Do NOT modify this code. The content of this method is
226: * always regenerated by the Form Editor.
227: */
228: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
229: private void initComponents() {
230: java.awt.GridBagConstraints gridBagConstraints;
231:
232: lblName = new javax.swing.JLabel();
233: txtName = new javax.swing.JTextField();
234: lblDisplayName = new javax.swing.JLabel();
235: txtDisplayName = new javax.swing.JTextField();
236: lblCategory = new javax.swing.JLabel();
237: comCategory = new javax.swing.JComboBox();
238: lblProjectName = new javax.swing.JLabel();
239: txtProjectName = new JTextField(ProjectUtils.getInformation(
240: this .data.getProject()).getDisplayName());
241: lblPackageName = new javax.swing.JLabel();
242: comPackageName = UIUtil.createPackageComboBox(this .data
243: .getSourceRootGroup());
244: createdFiles = new javax.swing.JLabel();
245: modifiedFiles = new javax.swing.JLabel();
246: filler = new javax.swing.JLabel();
247: createdFilesValue = new javax.swing.JTextArea();
248: modifiedFilesValue = new javax.swing.JTextArea();
249:
250: setLayout(new java.awt.GridBagLayout());
251:
252: lblName.setLabelFor(txtName);
253: org.openide.awt.Mnemonics.setLocalizedText(lblName,
254: org.openide.util.NbBundle.getMessage(
255: NameAndLocationPanel.class, "LBL_Name"));
256: gridBagConstraints = new java.awt.GridBagConstraints();
257: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
258: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12);
259: add(lblName, gridBagConstraints);
260:
261: gridBagConstraints = new java.awt.GridBagConstraints();
262: gridBagConstraints.gridwidth = 2;
263: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
264: add(txtName, gridBagConstraints);
265:
266: lblDisplayName.setLabelFor(txtDisplayName);
267: org.openide.awt.Mnemonics.setLocalizedText(lblDisplayName,
268: org.openide.util.NbBundle.getMessage(
269: NameAndLocationPanel.class, "LBL_DisplayName"));
270: gridBagConstraints = new java.awt.GridBagConstraints();
271: gridBagConstraints.gridx = 0;
272: gridBagConstraints.gridy = 1;
273: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
274: gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 12);
275: add(lblDisplayName, gridBagConstraints);
276:
277: gridBagConstraints = new java.awt.GridBagConstraints();
278: gridBagConstraints.gridx = 1;
279: gridBagConstraints.gridy = 1;
280: gridBagConstraints.gridwidth = 2;
281: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
282: gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
283: add(txtDisplayName, gridBagConstraints);
284:
285: lblCategory.setLabelFor(comCategory);
286: org.openide.awt.Mnemonics.setLocalizedText(lblCategory,
287: org.openide.util.NbBundle.getMessage(
288: NameAndLocationPanel.class, "LBL_Category"));
289: gridBagConstraints = new java.awt.GridBagConstraints();
290: gridBagConstraints.gridx = 0;
291: gridBagConstraints.gridy = 2;
292: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
293: gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 12);
294: add(lblCategory, gridBagConstraints);
295:
296: comCategory.setEditable(true);
297: gridBagConstraints = new java.awt.GridBagConstraints();
298: gridBagConstraints.gridx = 1;
299: gridBagConstraints.gridy = 2;
300: gridBagConstraints.gridwidth = 2;
301: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
302: gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
303: add(comCategory, gridBagConstraints);
304:
305: lblProjectName.setLabelFor(txtProjectName);
306: org.openide.awt.Mnemonics
307: .setLocalizedText(
308: lblProjectName,
309: java.util.ResourceBundle
310: .getBundle(
311: "org/netbeans/modules/apisupport/project/ui/wizard/librarydescriptor/Bundle")
312: .getString("LBL_ProjectName"));
313: gridBagConstraints = new java.awt.GridBagConstraints();
314: gridBagConstraints.gridx = 0;
315: gridBagConstraints.gridy = 3;
316: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
317: gridBagConstraints.insets = new java.awt.Insets(18, 0, 6, 12);
318: add(lblProjectName, gridBagConstraints);
319:
320: txtProjectName.setEditable(false);
321: gridBagConstraints = new java.awt.GridBagConstraints();
322: gridBagConstraints.gridx = 1;
323: gridBagConstraints.gridy = 3;
324: gridBagConstraints.gridwidth = 2;
325: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
326: gridBagConstraints.weightx = 1.0;
327: gridBagConstraints.insets = new java.awt.Insets(18, 0, 6, 0);
328: add(txtProjectName, gridBagConstraints);
329:
330: lblPackageName.setLabelFor(comPackageName);
331: org.openide.awt.Mnemonics
332: .setLocalizedText(
333: lblPackageName,
334: java.util.ResourceBundle
335: .getBundle(
336: "org/netbeans/modules/apisupport/project/ui/wizard/librarydescriptor/Bundle")
337: .getString("LBL_PackageName"));
338: gridBagConstraints = new java.awt.GridBagConstraints();
339: gridBagConstraints.gridx = 0;
340: gridBagConstraints.gridy = 4;
341: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
342: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12);
343: add(lblPackageName, gridBagConstraints);
344:
345: comPackageName.setEditable(true);
346: gridBagConstraints = new java.awt.GridBagConstraints();
347: gridBagConstraints.gridx = 1;
348: gridBagConstraints.gridy = 4;
349: gridBagConstraints.gridwidth = 2;
350: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
351: gridBagConstraints.weightx = 1.0;
352: add(comPackageName, gridBagConstraints);
353:
354: createdFiles.setLabelFor(createdFilesValue);
355: org.openide.awt.Mnemonics
356: .setLocalizedText(
357: createdFiles,
358: java.util.ResourceBundle
359: .getBundle(
360: "org/netbeans/modules/apisupport/project/ui/wizard/librarydescriptor/Bundle")
361: .getString("LBL_CreatedFiles"));
362: gridBagConstraints = new java.awt.GridBagConstraints();
363: gridBagConstraints.gridx = 0;
364: gridBagConstraints.gridy = 5;
365: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
366: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
367: gridBagConstraints.insets = new java.awt.Insets(36, 0, 6, 12);
368: add(createdFiles, gridBagConstraints);
369:
370: modifiedFiles.setLabelFor(modifiedFilesValue);
371: org.openide.awt.Mnemonics
372: .setLocalizedText(
373: modifiedFiles,
374: java.util.ResourceBundle
375: .getBundle(
376: "org/netbeans/modules/apisupport/project/ui/wizard/librarydescriptor/Bundle")
377: .getString("LBL_ModifiedFiles"));
378: gridBagConstraints = new java.awt.GridBagConstraints();
379: gridBagConstraints.gridx = 0;
380: gridBagConstraints.gridy = 6;
381: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
382: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
383: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12);
384: add(modifiedFiles, gridBagConstraints);
385:
386: gridBagConstraints = new java.awt.GridBagConstraints();
387: gridBagConstraints.gridx = 0;
388: gridBagConstraints.gridy = 7;
389: gridBagConstraints.gridwidth = 3;
390: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
391: gridBagConstraints.weightx = 1.0;
392: gridBagConstraints.weighty = 1.0;
393: add(filler, gridBagConstraints);
394:
395: createdFilesValue.setColumns(20);
396: createdFilesValue.setRows(5);
397: createdFilesValue.setBorder(null);
398: gridBagConstraints = new java.awt.GridBagConstraints();
399: gridBagConstraints.gridx = 1;
400: gridBagConstraints.gridy = 5;
401: gridBagConstraints.gridwidth = 2;
402: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
403: gridBagConstraints.weightx = 1.0;
404: gridBagConstraints.insets = new java.awt.Insets(36, 0, 6, 0);
405: add(createdFilesValue, gridBagConstraints);
406:
407: modifiedFilesValue.setColumns(20);
408: modifiedFilesValue.setRows(5);
409: modifiedFilesValue.setToolTipText("modifiedFilesValue");
410: modifiedFilesValue.setBorder(null);
411: gridBagConstraints = new java.awt.GridBagConstraints();
412: gridBagConstraints.gridx = 1;
413: gridBagConstraints.gridy = 6;
414: gridBagConstraints.gridwidth = 2;
415: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
416: gridBagConstraints.weightx = 1.0;
417: add(modifiedFilesValue, gridBagConstraints);
418:
419: }// </editor-fold>//GEN-END:initComponents
420:
421: private void initAccessibility() {
422: this .getAccessibleContext().setAccessibleDescription(
423: getMessage("ACS_NameAndLocationPanel"));
424: comPackageName.getAccessibleContext().setAccessibleDescription(
425: getMessage("ACS_CTL_PackageName"));
426: comCategory.getAccessibleContext().setAccessibleDescription(
427: getMessage("ACS_CTL_Category"));
428: txtDisplayName.getAccessibleContext().setAccessibleDescription(
429: getMessage("ACS_CTL_DisplayName"));
430: txtName.getAccessibleContext().setAccessibleDescription(
431: getMessage("ACS_CTL_Name"));
432: txtProjectName.getAccessibleContext().setAccessibleDescription(
433: getMessage("ACS_CTL_ProjectName"));
434: createdFilesValue.getAccessibleContext()
435: .setAccessibleDescription(
436: getMessage("ACS_CTL_CreatedFilesValue"));
437: modifiedFilesValue.getAccessibleContext()
438: .setAccessibleDescription(
439: getMessage("ACS_CTL_ModifiedFilesValue"));
440: }
441:
442: // Variables declaration - do not modify//GEN-BEGIN:variables
443: private javax.swing.JComboBox comCategory;
444: private javax.swing.JComboBox comPackageName;
445: private javax.swing.JLabel createdFiles;
446: private javax.swing.JTextArea createdFilesValue;
447: private javax.swing.JLabel filler;
448: private javax.swing.JLabel lblCategory;
449: private javax.swing.JLabel lblDisplayName;
450: private javax.swing.JLabel lblName;
451: private javax.swing.JLabel lblPackageName;
452: private javax.swing.JLabel lblProjectName;
453: private javax.swing.JLabel modifiedFiles;
454: private javax.swing.JTextArea modifiedFilesValue;
455: private javax.swing.JTextField txtDisplayName;
456: private javax.swing.JTextField txtName;
457: private javax.swing.JTextField txtProjectName;
458:
459: // End of variables declaration//GEN-END:variables
460:
461: private static String generateText(String[] relPaths) {
462: StringBuffer sb = new StringBuffer();
463: if (relPaths.length > 0) {
464: for (int i = 0; i < relPaths.length; i++) {
465: if (i > 0) {
466: sb.append('\n');
467: }
468: sb.append(relPaths[i]);
469: }
470: }
471: return sb.toString();
472: }
473:
474: }
|