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-2006 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.j2me.cdc.project.ui.wizards;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.text.MessageFormat;
047:
048: import javax.swing.JFileChooser;
049: import javax.swing.event.DocumentEvent;
050: import javax.swing.event.DocumentListener;
051: import javax.swing.text.Document;
052:
053: import org.netbeans.modules.j2me.cdc.project.ui.CDCFoldersListSettings;
054: import org.netbeans.spi.project.ui.support.ProjectChooser;
055: import org.openide.WizardDescriptor;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.util.NbBundle;
058:
059: /**
060: *
061: * @author Petr Hrebejk
062: */
063:
064: public class PanelProjectLocationVisual extends SettingsPanel implements
065: DocumentListener {
066:
067: public static final String PROP_PROJECT_NAME = "projectName"; //NOI18N
068:
069: private PanelConfigureProject panel;
070: private int type;
071: private String preferredName;
072:
073: /** Creates new form PanelProjectLocationVisual */
074: public PanelProjectLocationVisual(PanelConfigureProject panel,
075: int type, String preferredName) {
076: initComponents();
077: this .panel = panel;
078: this .type = type;
079: this .preferredName = preferredName;
080: // Register listener on the textFields to make the automatic updates
081: projectNameTextField.getDocument().addDocumentListener(this );
082: projectLocationTextField.getDocument()
083: .addDocumentListener(this );
084: }
085:
086: public String getProjectName() {
087: return this .projectNameTextField.getText();
088: }
089:
090: /** This method is called from within the constructor to
091: * initialize the form.
092: * WARNING: Do NOT modify this code. The content of this method is
093: * always regenerated by the Form Editor.
094: */
095: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
096: private void initComponents() {
097: java.awt.GridBagConstraints gridBagConstraints;
098:
099: projectNameLabel = new javax.swing.JLabel();
100: projectNameTextField = new javax.swing.JTextField();
101: projectLocationLabel = new javax.swing.JLabel();
102: projectLocationTextField = new javax.swing.JTextField();
103: browseButton = new javax.swing.JButton();
104: createdFolderLabel = new javax.swing.JLabel();
105: createdFolderTextField = new javax.swing.JTextField();
106:
107: setLayout(new java.awt.GridBagLayout());
108:
109: getAccessibleContext().setAccessibleName(
110: org.openide.util.NbBundle.getMessage(
111: PanelProjectLocationVisual.class,
112: "ACSN_PanelProjectLocationVisual"));
113: getAccessibleContext().setAccessibleDescription(
114: org.openide.util.NbBundle.getMessage(
115: PanelProjectLocationVisual.class,
116: "ACSD_PanelProjectLocationVisual"));
117: projectNameLabel.setLabelFor(projectNameTextField);
118: org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel,
119: org.openide.util.NbBundle.getMessage(
120: PanelProjectLocationVisual.class,
121: "LBL_NWP1_ProjectName_Label"));
122: gridBagConstraints = new java.awt.GridBagConstraints();
123: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
124: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
125: add(projectNameLabel, gridBagConstraints);
126: projectNameLabel.getAccessibleContext().setAccessibleName(
127: org.openide.util.NbBundle.getBundle(
128: PanelProjectLocationVisual.class).getString(
129: "ACSN_projectNameLabel"));
130: projectNameLabel.getAccessibleContext()
131: .setAccessibleDescription(
132: org.openide.util.NbBundle.getBundle(
133: PanelProjectLocationVisual.class)
134: .getString("ACSD_projectNameLabel"));
135:
136: gridBagConstraints = new java.awt.GridBagConstraints();
137: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
138: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
139: gridBagConstraints.weightx = 1.0;
140: gridBagConstraints.insets = new java.awt.Insets(0, 12, 12, 0);
141: add(projectNameTextField, gridBagConstraints);
142:
143: projectLocationLabel.setLabelFor(projectLocationTextField);
144: org.openide.awt.Mnemonics.setLocalizedText(
145: projectLocationLabel, org.openide.util.NbBundle
146: .getMessage(PanelProjectLocationVisual.class,
147: "LBL_NWP1_ProjectLocation_Label"));
148: gridBagConstraints = new java.awt.GridBagConstraints();
149: gridBagConstraints.gridy = 1;
150: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
151: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
152: add(projectLocationLabel, gridBagConstraints);
153: projectLocationLabel.getAccessibleContext().setAccessibleName(
154: org.openide.util.NbBundle.getBundle(
155: PanelProjectLocationVisual.class).getString(
156: "ACSN_projectLocationLabel"));
157: projectLocationLabel
158: .getAccessibleContext()
159: .setAccessibleDescription(
160: org.openide.util.NbBundle.getBundle(
161: PanelProjectLocationVisual.class)
162: .getString("ACSD_projectLocationLabel"));
163:
164: gridBagConstraints = new java.awt.GridBagConstraints();
165: gridBagConstraints.gridy = 1;
166: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
167: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
168: gridBagConstraints.weightx = 1.0;
169: gridBagConstraints.insets = new java.awt.Insets(0, 12, 5, 0);
170: add(projectLocationTextField, gridBagConstraints);
171:
172: org.openide.awt.Mnemonics.setLocalizedText(browseButton,
173: org.openide.util.NbBundle.getMessage(
174: PanelProjectLocationVisual.class,
175: "LBL_NWP1_BrowseLocation_Button"));
176: browseButton.setActionCommand("BROWSE");
177: browseButton
178: .addActionListener(new java.awt.event.ActionListener() {
179: public void actionPerformed(
180: java.awt.event.ActionEvent evt) {
181: browseLocationAction(evt);
182: }
183: });
184:
185: gridBagConstraints = new java.awt.GridBagConstraints();
186: gridBagConstraints.gridy = 1;
187: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
188: gridBagConstraints.insets = new java.awt.Insets(0, 6, 5, 0);
189: add(browseButton, gridBagConstraints);
190: browseButton.getAccessibleContext().setAccessibleName(
191: org.openide.util.NbBundle.getBundle(
192: PanelProjectLocationVisual.class).getString(
193: "ACSN_browseButton"));
194: browseButton.getAccessibleContext().setAccessibleDescription(
195: org.openide.util.NbBundle.getBundle(
196: PanelProjectLocationVisual.class).getString(
197: "ACSD_browseButton"));
198:
199: createdFolderLabel.setLabelFor(createdFolderTextField);
200: org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel,
201: org.openide.util.NbBundle.getMessage(
202: PanelProjectLocationVisual.class,
203: "LBL_NWP1_CreatedProjectFolder_Lablel"));
204: gridBagConstraints = new java.awt.GridBagConstraints();
205: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
206: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
207: add(createdFolderLabel, gridBagConstraints);
208: createdFolderLabel.getAccessibleContext().setAccessibleName(
209: org.openide.util.NbBundle.getBundle(
210: PanelProjectLocationVisual.class).getString(
211: "ACSN_createdFolderLabel"));
212: createdFolderLabel.getAccessibleContext()
213: .setAccessibleDescription(
214: org.openide.util.NbBundle.getBundle(
215: PanelProjectLocationVisual.class)
216: .getString("ACSD_createdFolderLabel"));
217:
218: createdFolderTextField.setEditable(false);
219: gridBagConstraints = new java.awt.GridBagConstraints();
220: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
221: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
222: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
223: gridBagConstraints.weightx = 1.0;
224: gridBagConstraints.weighty = 1.0;
225: gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 0);
226: add(createdFolderTextField, gridBagConstraints);
227:
228: }
229:
230: // </editor-fold>//GEN-END:initComponents
231:
232: private void browseLocationAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseLocationAction
233: String command = evt.getActionCommand();
234: if ("BROWSE".equals(command)) { // NOI18N
235: JFileChooser chooser = new JFileChooser();
236: FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
237: chooser.setDialogTitle(NbBundle.getMessage(
238: PanelProjectLocationVisual.class,
239: "LBL_NWP1_SelectProjectLocation"));
240: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
241: String path = this .projectLocationTextField.getText();
242: if (path.length() > 0) {
243: File f = new File(path);
244: if (f.exists()) {
245: chooser.setSelectedFile(f);
246: }
247: }
248: if (JFileChooser.APPROVE_OPTION == chooser
249: .showOpenDialog(this )) { //NOI18N
250: File projectDir = chooser.getSelectedFile();
251: projectLocationTextField.setText(projectDir
252: .getAbsolutePath());
253: }
254: panel.fireChangeEvent();
255: }
256: }//GEN-LAST:event_browseLocationAction
257:
258: public void addNotify() {
259: super .addNotify();
260: //same problem as in 31086, initial focus on Cancel button
261: projectNameTextField.requestFocus();
262: }
263:
264: boolean valid(WizardDescriptor wizardDescriptor) {
265:
266: if (projectNameTextField.getText().length() == 0) {
267: wizardDescriptor.putProperty("WizardPanel_errorMessage", // NOI18N
268: NbBundle.getMessage(
269: PanelProjectLocationVisual.class,
270: "MSG_IllegalProjectName"));
271: return false; // Display name not specified
272: }
273: File f = new File(projectLocationTextField.getText())
274: .getAbsoluteFile();
275: if (getCanonicalFile(f) == null) {
276: String message = NbBundle.getMessage(
277: PanelProjectLocationVisual.class,
278: "MSG_IllegalProjectLocation");
279: wizardDescriptor.putProperty("WizardPanel_errorMessage",
280: message);
281: return false;
282: }
283: final File destFolder = getCanonicalFile(new File(
284: createdFolderTextField.getText()).getAbsoluteFile());
285: if (destFolder == null) {
286: String message = NbBundle.getMessage(
287: PanelProjectLocationVisual.class,
288: "MSG_IllegalProjectName");
289: wizardDescriptor.putProperty("WizardPanel_errorMessage",
290: message);
291: return false;
292: }
293:
294: File projLoc = destFolder;
295: while (projLoc != null && !projLoc.exists()) {
296: projLoc = projLoc.getParentFile();
297: }
298: if (projLoc == null || !projLoc.canWrite()) {
299: wizardDescriptor.putProperty("WizardPanel_errorMessage", // NOI18N
300: NbBundle.getMessage(
301: PanelProjectLocationVisual.class,
302: "MSG_ProjectFolderReadOnly"));
303: return false;
304: }
305:
306: if (FileUtil.toFileObject(projLoc) == null) {
307: String message = NbBundle.getMessage(
308: PanelProjectLocationVisual.class,
309: "MSG_IllegalProjectLocation");
310: wizardDescriptor.putProperty("WizardPanel_errorMessage",
311: message);
312: return false;
313: }
314:
315: File[] kids = destFolder.listFiles();
316: if (destFolder.exists() && kids != null && kids.length > 0) {
317: // Folder exists and is not empty
318: wizardDescriptor.putProperty("WizardPanel_errorMessage", // NOI18N
319: NbBundle.getMessage(
320: PanelProjectLocationVisual.class,
321: "MSG_ProjectFolderExists"));
322: return false;
323: }
324: return true;
325: }
326:
327: void store(WizardDescriptor d) {
328:
329: String name = projectNameTextField.getText().trim();
330: String folder = createdFolderTextField.getText().trim();
331:
332: d.putProperty( /*XXX Define somewhere */"projdir", new File(
333: folder)); // NOI18N
334: d.putProperty( /*XXX Define somewhere */"name", name); // NOI18N
335: }
336:
337: void read(WizardDescriptor settings) {
338: File projectLocation = (File) settings.getProperty("projdir"); //NOI18N
339: if (projectLocation == null
340: || projectLocation.getParentFile() == null
341: || !projectLocation.getParentFile().isDirectory()) {
342: projectLocation = ProjectChooser.getProjectsFolder();
343: } else {
344: projectLocation = projectLocation.getParentFile();
345: }
346: this .projectLocationTextField.setText(projectLocation
347: .getAbsolutePath());
348:
349: String projectName = (String) settings.getProperty("name"); //NOI18N
350: if (projectName == null) {
351: if (preferredName == null) {
352: if (this .type == NewCDCProjectWizardIterator.TYPE_APP) {
353: int baseCount = CDCFoldersListSettings
354: .getNewApplicationCount() + 1;
355: String formater = NbBundle.getMessage(
356: PanelProjectLocationVisual.class,
357: "TXT_JavaApplication");
358: while ((projectName = validFreeProjectName(
359: projectLocation, formater, baseCount)) == null)
360: baseCount++;
361: settings
362: .putProperty(
363: NewCDCProjectWizardIterator.PROP_NAME_INDEX,
364: new Integer(baseCount));
365: } else {
366: int baseCount = CDCFoldersListSettings
367: .getNewLibraryCount() + 1;
368: String formater = NbBundle.getMessage(
369: PanelProjectLocationVisual.class,
370: "TXT_JavaLibrary");
371: while ((projectName = validFreeProjectName(
372: projectLocation, formater, baseCount)) == null)
373: baseCount++;
374: settings
375: .putProperty(
376: NewCDCProjectWizardIterator.PROP_NAME_INDEX,
377: new Integer(baseCount));
378: }
379: } else {
380: projectName = preferredName;
381: }
382: }
383: this .projectNameTextField.setText(projectName);
384: this .projectNameTextField.selectAll();
385: }
386:
387: void validate(WizardDescriptor d) {
388: // nothing to validate
389: }
390:
391: // Variables declaration - do not modify//GEN-BEGIN:variables
392: private javax.swing.JButton browseButton;
393: private javax.swing.JLabel createdFolderLabel;
394: private javax.swing.JTextField createdFolderTextField;
395: private javax.swing.JLabel projectLocationLabel;
396: private javax.swing.JTextField projectLocationTextField;
397: private javax.swing.JLabel projectNameLabel;
398: private javax.swing.JTextField projectNameTextField;
399:
400: // End of variables declaration//GEN-END:variables
401:
402: // Private methods ---------------------------------------------------------
403:
404: private String validFreeProjectName(final File parentFolder,
405: final String formater, final int index) {
406: String name = MessageFormat.format(formater,
407: new Object[] { new Integer(index) });
408: File file = new File(parentFolder, name);
409: return file.exists() ? null : name;
410: }
411:
412: // Implementation of DocumentListener --------------------------------------
413:
414: public void changedUpdate(DocumentEvent e) {
415: updateTexts(e);
416: if (this .projectNameTextField.getDocument() == e.getDocument()) {
417: firePropertyChange(PROP_PROJECT_NAME, null,
418: this .projectNameTextField.getText());
419: }
420: }
421:
422: public void insertUpdate(DocumentEvent e) {
423: updateTexts(e);
424: if (this .projectNameTextField.getDocument() == e.getDocument()) {
425: firePropertyChange(PROP_PROJECT_NAME, null,
426: this .projectNameTextField.getText());
427: }
428: }
429:
430: public void removeUpdate(DocumentEvent e) {
431: updateTexts(e);
432: if (this .projectNameTextField.getDocument() == e.getDocument()) {
433: firePropertyChange(PROP_PROJECT_NAME, null,
434: this .projectNameTextField.getText());
435: }
436: }
437:
438: /** Handles changes in the Project name and project directory
439: */
440: private void updateTexts(DocumentEvent e) {
441:
442: Document doc = e.getDocument();
443:
444: if (doc == projectNameTextField.getDocument()
445: || doc == projectLocationTextField.getDocument()) {
446: // Change in the project name
447:
448: String projectName = projectNameTextField.getText();
449: String projectFolder = projectLocationTextField.getText();
450:
451: //if ( projectFolder.trim().length() == 0 || projectFolder.equals( oldName ) ) {
452: createdFolderTextField.setText(projectFolder
453: + File.separatorChar + projectName);
454: //}
455:
456: }
457: panel.fireChangeEvent(); // Notify that the panel changed
458: }
459:
460: static File getCanonicalFile(File file) {
461: try {
462: return file.getCanonicalFile();
463: } catch (IOException e) {
464: return null;
465: }
466: }
467:
468: }
|