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.web.examples;
043:
044: import java.io.File;
045: import java.text.MessageFormat;
046:
047: import javax.swing.JFileChooser;
048: import javax.swing.event.DocumentEvent;
049: import javax.swing.event.DocumentListener;
050:
051: import org.netbeans.spi.project.ui.support.ProjectChooser;
052:
053: import org.openide.WizardDescriptor;
054: import org.openide.util.NbBundle;
055:
056: public class PanelProjectLocationVisual extends SettingsPanel implements
057: DocumentListener {
058:
059: private PanelConfigureProject panel;
060:
061: /** Creates new form PanelProjectLocationVisual */
062: public PanelProjectLocationVisual(PanelConfigureProject panel) {
063: initComponents();
064: this .panel = panel;
065: // projectNameTextField.setEditable(false);
066:
067: // Register listener on the textFields to make the automatic updates
068: projectNameTextField.getDocument().addDocumentListener(this );
069: projectLocationTextField.getDocument()
070: .addDocumentListener(this );
071: }
072:
073: /** This method is called from within the constructor to
074: * initialize the form.
075: * WARNING: Do NOT modify this code. The content of this method is
076: * always regenerated by the Form Editor.
077: */
078: private void initComponents() {//GEN-BEGIN:initComponents
079: java.awt.GridBagConstraints gridBagConstraints;
080:
081: projectNameLabel = new javax.swing.JLabel();
082: projectNameTextField = new javax.swing.JTextField();
083: projectLocationLabel = new javax.swing.JLabel();
084: projectLocationTextField = new javax.swing.JTextField();
085: Button = new javax.swing.JButton();
086: createdFolderLabel = new javax.swing.JLabel();
087: createdFolderTextField = new javax.swing.JTextField();
088:
089: setLayout(new java.awt.GridBagLayout());
090:
091: projectNameLabel
092: .setDisplayedMnemonic(org.openide.util.NbBundle
093: .getMessage(PanelProjectLocationVisual.class,
094: "LBL_NWP1_ProjectName_LabelMnemonic")
095: .charAt(0));
096: projectNameLabel.setLabelFor(projectNameTextField);
097: projectNameLabel.setText(org.openide.util.NbBundle.getMessage(
098: PanelProjectLocationVisual.class,
099: "LBL_NWP1_ProjectName_Label"));
100: gridBagConstraints = new java.awt.GridBagConstraints();
101: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
102: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
103: add(projectNameLabel, gridBagConstraints);
104:
105: gridBagConstraints = new java.awt.GridBagConstraints();
106: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
107: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
108: gridBagConstraints.weightx = 1.0;
109: gridBagConstraints.insets = new java.awt.Insets(0, 12, 12, 0);
110: add(projectNameTextField, gridBagConstraints);
111: projectNameTextField.getAccessibleContext()
112: .setAccessibleDescription(
113: org.openide.util.NbBundle.getMessage(
114: PanelProjectLocationVisual.class,
115: "ACS_LBL_NWP1_ProjectName_A11YDesc"));
116:
117: projectLocationLabel
118: .setDisplayedMnemonic(org.openide.util.NbBundle
119: .getMessage(PanelProjectLocationVisual.class,
120: "LBL_NWP1_ProjectLocation_LabelMnemonic")
121: .charAt(0));
122: projectLocationLabel.setLabelFor(projectLocationTextField);
123: projectLocationLabel.setText(org.openide.util.NbBundle
124: .getMessage(PanelProjectLocationVisual.class,
125: "LBL_NWP1_ProjectLocation_Label"));
126: gridBagConstraints = new java.awt.GridBagConstraints();
127: gridBagConstraints.gridy = 1;
128: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
129: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
130: add(projectLocationLabel, gridBagConstraints);
131:
132: gridBagConstraints = new java.awt.GridBagConstraints();
133: gridBagConstraints.gridy = 1;
134: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
135: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
136: gridBagConstraints.weightx = 1.0;
137: gridBagConstraints.insets = new java.awt.Insets(0, 12, 5, 0);
138: add(projectLocationTextField, gridBagConstraints);
139: projectLocationTextField
140: .getAccessibleContext()
141: .setAccessibleDescription(
142: org.openide.util.NbBundle
143: .getMessage(
144: PanelProjectLocationVisual.class,
145: "ACS_LBL_NPW1_ProjectLocation_A11YDesc"));
146:
147: Button.setText(org.openide.util.NbBundle.getMessage(
148: PanelProjectLocationVisual.class,
149: "LBL_NWP1_BrowseLocation_Button"));
150: Button.setActionCommand("BROWSE");
151: Button.addActionListener(new java.awt.event.ActionListener() {
152: public void actionPerformed(java.awt.event.ActionEvent evt) {
153: browseLocationAction(evt);
154: }
155: });
156:
157: gridBagConstraints = new java.awt.GridBagConstraints();
158: gridBagConstraints.gridy = 1;
159: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
160: gridBagConstraints.insets = new java.awt.Insets(0, 6, 5, 0);
161: add(Button, gridBagConstraints);
162: Button.getAccessibleContext().setAccessibleDescription(
163: org.openide.util.NbBundle.getMessage(
164: PanelProjectLocationVisual.class,
165: "ACS_LBL_NWP1_BrowseLocation_A11YDesc"));
166:
167: createdFolderLabel
168: .setDisplayedMnemonic(org.openide.util.NbBundle
169: .getMessage(PanelProjectLocationVisual.class,
170: "LBL_NWP1_CreatedProjectFolder_LablelMnemonic")
171: .charAt(0));
172: createdFolderLabel.setLabelFor(createdFolderTextField);
173: createdFolderLabel.setText(org.openide.util.NbBundle
174: .getMessage(PanelProjectLocationVisual.class,
175: "LBL_NWP1_CreatedProjectFolder_Lablel"));
176: gridBagConstraints = new java.awt.GridBagConstraints();
177: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
178: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
179: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
180: add(createdFolderLabel, gridBagConstraints);
181:
182: createdFolderTextField.setEditable(false);
183: gridBagConstraints = new java.awt.GridBagConstraints();
184: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
185: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
186: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
187: gridBagConstraints.weightx = 1.0;
188: gridBagConstraints.weighty = 1.0;
189: gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 0);
190: add(createdFolderTextField, gridBagConstraints);
191: createdFolderTextField
192: .getAccessibleContext()
193: .setAccessibleDescription(
194: org.openide.util.NbBundle
195: .getMessage(
196: PanelProjectLocationVisual.class,
197: "ACS_LBL_NWP1_CreatedProjectFolder_A11YDesc"));
198:
199: }//GEN-END:initComponents
200:
201: private void browseLocationAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseLocationAction
202: String command = evt.getActionCommand();
203:
204: if ("BROWSE".equals(command)) { //NOI18N
205: JFileChooser chooser = new JFileChooser();
206: chooser.setDialogTitle(NbBundle.getMessage(
207: PanelProjectLocationVisual.class,
208: "LBL_NWP1_SelectProjectLocation")); //NOI18N
209: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
210: String path = projectLocationTextField.getText();
211: if (path.length() > 0) {
212: File f = new File(path);
213: if (f.exists())
214: chooser.setSelectedFile(f);
215: }
216: if (JFileChooser.APPROVE_OPTION == chooser
217: .showOpenDialog(this )) {
218: File projectDir = chooser.getSelectedFile();
219: projectLocationTextField.setText(projectDir
220: .getAbsolutePath());
221: }
222: panel.fireChangeEvent();
223: }
224: }//GEN-LAST:event_browseLocationAction
225:
226: public void addNotify() {
227: super .addNotify();
228: //same problem as in 31086, initial focus on Cancel button
229: projectLocationTextField.requestFocus();
230: }
231:
232: boolean valid(WizardDescriptor wizardDescriptor) {
233: if (projectNameTextField.getText().length() == 0) {
234: wizardDescriptor.putProperty("WizardPanel_errorMessage",
235: NbBundle.getMessage(
236: PanelProjectLocationVisual.class,
237: "MSG_IllegalProjectName")); //NOI18N
238: return false; // Display name not specified
239: }
240:
241: File destFolder = new File(createdFolderTextField.getText());
242: File[] children = destFolder.listFiles();
243: if (destFolder.exists() && children != null
244: && children.length > 0) {
245: // Folder exists and is not empty
246: wizardDescriptor.putProperty("WizardPanel_errorMessage",
247: NbBundle.getMessage(
248: PanelProjectLocationVisual.class,
249: "MSG_ProjectFolderExists")); //NOI18N
250: return false;
251: }
252:
253: wizardDescriptor.putProperty("WizardPanel_errorMessage", ""); //NOI18N
254: return true;
255: }
256:
257: void store(WizardDescriptor d) {
258: String name = projectNameTextField.getText().trim();
259:
260: d.putProperty(WizardProperties.PROJECT_DIR, new File(
261: createdFolderTextField.getText().trim()));
262: d.putProperty(WizardProperties.NAME, name);
263:
264: File projectsDir = new File(this .projectLocationTextField
265: .getText());
266: if (projectsDir.isDirectory()) {
267: ProjectChooser.setProjectsFolder(projectsDir);
268: }
269: }
270:
271: void read(WizardDescriptor settings) {
272: File projectLocation = (File) settings
273: .getProperty(WizardProperties.PROJECT_DIR);
274: if (projectLocation == null)
275: projectLocation = ProjectChooser.getProjectsFolder();
276: else
277: projectLocation = projectLocation.getParentFile();
278:
279: projectLocationTextField.setText(projectLocation
280: .getAbsolutePath());
281: projectLocationTextField.selectAll();
282:
283: String formater = null;
284: String projectName = (String) settings
285: .getProperty(WizardProperties.NAME);
286:
287: if (projectName == null) {
288: formater = NbBundle.getMessage(
289: PanelProjectLocationVisual.class,
290: "LBL_NPW1_DefaultProjectName"); //NOI18N
291: } else {
292: formater = projectName + "{0}"; //NOI18N
293: }
294: if ((projectName == null)
295: || (validFreeProjectName(projectLocation, projectName) == null)) {
296: int baseCount = FoldersListSettings.getDefault()
297: .getNewProjectCount() + 1;
298: while ((projectName = validFreeProjectName(projectLocation,
299: formater, baseCount)) == null)
300: baseCount++;
301: // settings.putProperty(NewWebProjectWizardIterator.PROP_NAME_INDEX, new Integer(baseCount));
302: }
303: projectNameTextField.setText(projectName);
304: projectNameTextField.selectAll();
305: }
306:
307: // Variables declaration - do not modify//GEN-BEGIN:variables
308: private javax.swing.JButton Button;
309: private javax.swing.JLabel createdFolderLabel;
310: private javax.swing.JTextField createdFolderTextField;
311: private javax.swing.JLabel projectLocationLabel;
312: private javax.swing.JTextField projectLocationTextField;
313: private javax.swing.JLabel projectNameLabel;
314: protected javax.swing.JTextField projectNameTextField;
315:
316: // End of variables declaration//GEN-END:variables
317:
318: private static JFileChooser createChooser() {
319: JFileChooser chooser = new JFileChooser();
320: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
321: chooser.setAcceptAllFileFilterUsed(false);
322:
323: return chooser;
324: }
325:
326: private String validFreeProjectName(final File parentFolder,
327: final String formater, final int index) {
328: String name = MessageFormat.format(formater,
329: new Object[] { new Integer(index) });
330: File file = new File(parentFolder, name);
331: return file.exists() ? null : name;
332: }
333:
334: private String validFreeProjectName(final File parentFolder,
335: final String name) {
336: File file = new File(parentFolder, name);
337: return file.exists() ? null : name;
338: }
339:
340: // Implementation of DocumentListener --------------------------------------
341: public void changedUpdate(DocumentEvent e) {
342: updateTexts(e);
343: }
344:
345: public void insertUpdate(DocumentEvent e) {
346: updateTexts(e);
347: }
348:
349: public void removeUpdate(DocumentEvent e) {
350: updateTexts(e);
351: }
352:
353: // End if implementation of DocumentListener -------------------------------
354:
355: /** Handles changes in the project name and project directory
356: */
357: private void updateTexts(DocumentEvent e) {
358: createdFolderTextField.setText(getCreatedFolderPath());
359:
360: panel.fireChangeEvent(); // Notify that the panel changed
361: }
362:
363: private String getCreatedFolderPath() {
364: StringBuffer folder = new StringBuffer(projectLocationTextField
365: .getText().trim());
366: if (!projectLocationTextField.getText()
367: .endsWith(File.separator))
368: folder.append(File.separatorChar);
369: folder.append(projectNameTextField.getText().trim());
370:
371: return folder.toString();
372: }
373:
374: }
375:
376: //TODO implement check for project folder name and location
|