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: package org.netbeans.modules.apisupport.paintapp;
042:
043: import java.io.File;
044: import javax.swing.JFileChooser;
045: import javax.swing.JPanel;
046: import javax.swing.event.DocumentEvent;
047: import javax.swing.event.DocumentListener;
048: import javax.swing.text.Document;
049: import org.netbeans.spi.project.ui.support.ProjectChooser;
050: import org.openide.WizardDescriptor;
051: import org.openide.WizardValidationException;
052: import org.openide.filesystems.FileUtil;
053:
054: public class PaintAppPanelVisual extends JPanel implements
055: DocumentListener {
056:
057: private static final String WIZARD_PANEL_ERROR_MESSAGE = "WizardPanel_errorMessage"; // NOI18N
058:
059: public static final String PROP_PROJECT_NAME = "projectName"; // NOI18N
060:
061: private PaintAppWizardPanel panel;
062:
063: /** Creates new form PanelProjectLocationVisual */
064: public PaintAppPanelVisual(PaintAppWizardPanel panel) {
065: initComponents();
066: this .panel = panel;
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: public String getProjectName() {
074: return this .projectNameTextField.getText();
075: }
076:
077: /** This method is called from within the constructor to
078: * initialize the form.
079: * WARNING: Do NOT modify this code. The content of this method is
080: * always regenerated by the Form Editor.
081: */
082: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
083: private void initComponents() {
084: projectNameLabel = new javax.swing.JLabel();
085: projectNameTextField = new javax.swing.JTextField();
086: projectLocationLabel = new javax.swing.JLabel();
087: projectLocationTextField = new javax.swing.JTextField();
088: browseButton = new javax.swing.JButton();
089: createdFolderLabel = new javax.swing.JLabel();
090: createdFolderTextField = new javax.swing.JTextField();
091:
092: projectNameLabel.setLabelFor(projectNameTextField);
093: org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel,
094: org.openide.util.NbBundle.getMessage(
095: PaintAppPanelVisual.class,
096: "PaintAppPanelVisual.project_name")); // NOI18N
097:
098: projectLocationLabel.setLabelFor(projectLocationTextField);
099: org.openide.awt.Mnemonics
100: .setLocalizedText(projectLocationLabel,
101: org.openide.util.NbBundle.getMessage(
102: PaintAppPanelVisual.class,
103: "PaintAppPanelVisual.project_location")); // NOI18N
104:
105: org.openide.awt.Mnemonics.setLocalizedText(browseButton,
106: org.openide.util.NbBundle.getMessage(
107: PaintAppPanelVisual.class,
108: "PaintAppPanelVisual.browse")); // NOI18N
109: browseButton
110: .addActionListener(new java.awt.event.ActionListener() {
111: public void actionPerformed(
112: java.awt.event.ActionEvent evt) {
113: browseButtonActionPerformed(evt);
114: }
115: });
116:
117: createdFolderLabel.setLabelFor(createdFolderTextField);
118: org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel,
119: org.openide.util.NbBundle.getMessage(
120: PaintAppPanelVisual.class,
121: "PaintAppPanelVisual.project_folder")); // NOI18N
122:
123: createdFolderTextField.setEditable(false);
124:
125: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
126: this );
127: this .setLayout(layout);
128: layout
129: .setHorizontalGroup(layout
130: .createParallelGroup(
131: org.jdesktop.layout.GroupLayout.LEADING)
132: .add(
133: layout
134: .createSequentialGroup()
135: .addContainerGap()
136: .add(
137: layout
138: .createParallelGroup(
139: org.jdesktop.layout.GroupLayout.LEADING)
140: .add(
141: projectNameLabel)
142: .add(
143: projectLocationLabel)
144: .add(
145: createdFolderLabel))
146: .addPreferredGap(
147: org.jdesktop.layout.LayoutStyle.RELATED)
148: .add(
149: layout
150: .createParallelGroup(
151: org.jdesktop.layout.GroupLayout.LEADING)
152: .add(
153: org.jdesktop.layout.GroupLayout.TRAILING,
154: projectNameTextField,
155: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
156: 191,
157: Short.MAX_VALUE)
158: .add(
159: org.jdesktop.layout.GroupLayout.TRAILING,
160: projectLocationTextField,
161: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
162: 191,
163: Short.MAX_VALUE)
164: .add(
165: org.jdesktop.layout.GroupLayout.TRAILING,
166: createdFolderTextField,
167: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
168: 191,
169: Short.MAX_VALUE))
170: .addPreferredGap(
171: org.jdesktop.layout.LayoutStyle.RELATED)
172: .add(browseButton)
173: .addContainerGap()));
174: layout
175: .setVerticalGroup(layout
176: .createParallelGroup(
177: org.jdesktop.layout.GroupLayout.LEADING)
178: .add(
179: layout
180: .createSequentialGroup()
181: .addContainerGap()
182: .add(
183: layout
184: .createParallelGroup(
185: org.jdesktop.layout.GroupLayout.BASELINE)
186: .add(
187: projectNameLabel)
188: .add(
189: projectNameTextField,
190: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
191: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
192: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
193: .addPreferredGap(
194: org.jdesktop.layout.LayoutStyle.RELATED)
195: .add(
196: layout
197: .createParallelGroup(
198: org.jdesktop.layout.GroupLayout.BASELINE)
199: .add(
200: projectLocationLabel)
201: .add(
202: projectLocationTextField,
203: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
204: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
205: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
206: .add(
207: browseButton))
208: .addPreferredGap(
209: org.jdesktop.layout.LayoutStyle.RELATED)
210: .add(
211: layout
212: .createParallelGroup(
213: org.jdesktop.layout.GroupLayout.BASELINE)
214: .add(
215: createdFolderLabel)
216: .add(
217: createdFolderTextField,
218: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
219: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
220: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
221: .addContainerGap(213,
222: Short.MAX_VALUE)));
223: }// </editor-fold>//GEN-END:initComponents
224:
225: private void browseButtonActionPerformed(
226: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
227: JFileChooser chooser = new JFileChooser();
228: FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
229: chooser.setDialogTitle(org.openide.util.NbBundle.getMessage(
230: PaintAppPanelVisual.class,
231: "PaintAppPanelVisual.select_project_location"));
232: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
233: String path = this .projectLocationTextField.getText();
234: if (path.length() > 0) {
235: File f = new File(path);
236: if (f.exists()) {
237: chooser.setSelectedFile(f);
238: }
239: }
240: if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this )) {
241: File projectDir = chooser.getSelectedFile();
242: projectLocationTextField.setText(FileUtil.normalizeFile(
243: projectDir).getAbsolutePath());
244: }
245: panel.fireChangeEvent();
246:
247: }//GEN-LAST:event_browseButtonActionPerformed
248:
249: // Variables declaration - do not modify//GEN-BEGIN:variables
250: private javax.swing.JButton browseButton;
251: private javax.swing.JLabel createdFolderLabel;
252: private javax.swing.JTextField createdFolderTextField;
253: private javax.swing.JLabel projectLocationLabel;
254: private javax.swing.JTextField projectLocationTextField;
255: private javax.swing.JLabel projectNameLabel;
256: private javax.swing.JTextField projectNameTextField;
257:
258: // End of variables declaration//GEN-END:variables
259:
260: public void addNotify() {
261: super .addNotify();
262: //same problem as in 31086, initial focus on Cancel button
263: projectNameTextField.requestFocus();
264: }
265:
266: boolean valid(WizardDescriptor wizardDescriptor) {
267:
268: if (projectNameTextField.getText().length() == 0) {
269: wizardDescriptor.putProperty(WIZARD_PANEL_ERROR_MESSAGE,
270: org.openide.util.NbBundle.getMessage(
271: PaintAppPanelVisual.class,
272: "PaintAppPanelVisual.invalid_folder_name"));
273: return false; // Display name not specified
274: }
275: File f = FileUtil.normalizeFile(new File(
276: projectLocationTextField.getText()).getAbsoluteFile());
277: if (!f.isDirectory()) {
278: String message = org.openide.util.NbBundle.getMessage(
279: PaintAppPanelVisual.class,
280: "PaintAppPanelVisual.invalid_path");
281: wizardDescriptor.putProperty(WIZARD_PANEL_ERROR_MESSAGE,
282: message);
283: return false;
284: }
285: final File destFolder = FileUtil.normalizeFile(new File(
286: createdFolderTextField.getText()).getAbsoluteFile());
287:
288: File projLoc = destFolder;
289: while (projLoc != null && !projLoc.exists()) {
290: projLoc = projLoc.getParentFile();
291: }
292: if (projLoc == null || !projLoc.canWrite()) {
293: wizardDescriptor
294: .putProperty(
295: WIZARD_PANEL_ERROR_MESSAGE,
296: org.openide.util.NbBundle
297: .getMessage(
298: PaintAppPanelVisual.class,
299: "PaintAppPanelVisual.folder_creation_error"));
300: return false;
301: }
302:
303: if (FileUtil.toFileObject(projLoc) == null) {
304: String message = org.openide.util.NbBundle.getMessage(
305: PaintAppPanelVisual.class,
306: "PaintAppPanelVisual.invalid_path");
307: wizardDescriptor.putProperty(WIZARD_PANEL_ERROR_MESSAGE,
308: message);
309: return false;
310: }
311:
312: File[] kids = destFolder.listFiles();
313: if (destFolder.exists() && kids != null && kids.length > 0) {
314: // Folder exists and is not empty
315: wizardDescriptor.putProperty(WIZARD_PANEL_ERROR_MESSAGE,
316: org.openide.util.NbBundle.getMessage(
317: PaintAppPanelVisual.class,
318: "PaintAppPanelVisual.folder_exists"));
319: return false;
320: }
321: wizardDescriptor.putProperty(WIZARD_PANEL_ERROR_MESSAGE, "");
322: return true;
323: }
324:
325: void store(WizardDescriptor d) {
326: String name = projectNameTextField.getText().trim();
327: String folder = createdFolderTextField.getText().trim();
328:
329: d.putProperty("projdir", new File(folder)); // NOI18N
330: d.putProperty("name", name); // NOI18N
331: }
332:
333: void read(WizardDescriptor settings) {
334: File projectLocation = (File) settings.getProperty("projdir"); // NOI18N
335: if (projectLocation == null
336: || projectLocation.getParentFile() == null
337: || !projectLocation.getParentFile().isDirectory()) {
338: projectLocation = ProjectChooser.getProjectsFolder();
339: } else {
340: projectLocation = projectLocation.getParentFile();
341: }
342: this .projectLocationTextField.setText(projectLocation
343: .getAbsolutePath());
344:
345: String projectName = (String) settings.getProperty("name"); // NOI18N
346: if (projectName == null) {
347: projectName = org.openide.util.NbBundle.getMessage(
348: PaintAppPanelVisual.class,
349: "PaintAppPanelVisual.default_prj_name");
350: }
351: this .projectNameTextField.setText(projectName);
352: this .projectNameTextField.selectAll();
353: }
354:
355: void validate(WizardDescriptor d) throws WizardValidationException {
356: // nothing to validate
357: }
358:
359: // Implementation of DocumentListener --------------------------------------
360:
361: public void changedUpdate(DocumentEvent e) {
362: updateTexts(e);
363: if (this .projectNameTextField.getDocument() == e.getDocument()) {
364: firePropertyChange(PROP_PROJECT_NAME, null,
365: this .projectNameTextField.getText());
366: }
367: }
368:
369: public void insertUpdate(DocumentEvent e) {
370: updateTexts(e);
371: if (this .projectNameTextField.getDocument() == e.getDocument()) {
372: firePropertyChange(PROP_PROJECT_NAME, null,
373: this .projectNameTextField.getText());
374: }
375: }
376:
377: public void removeUpdate(DocumentEvent e) {
378: updateTexts(e);
379: if (this .projectNameTextField.getDocument() == e.getDocument()) {
380: firePropertyChange(PROP_PROJECT_NAME, null,
381: this .projectNameTextField.getText());
382: }
383: }
384:
385: /** Handles changes in the Project name and project directory, */
386: private void updateTexts(DocumentEvent e) {
387:
388: Document doc = e.getDocument();
389:
390: if (doc == projectNameTextField.getDocument()
391: || doc == projectLocationTextField.getDocument()) {
392: // Change in the project name
393:
394: String projectName = projectNameTextField.getText();
395: String projectFolder = projectLocationTextField.getText();
396:
397: //if (projectFolder.trim().length() == 0 || projectFolder.equals(oldName)) {
398: createdFolderTextField.setText(projectFolder
399: + File.separatorChar + projectName);
400: //}
401:
402: }
403: panel.fireChangeEvent(); // Notify that the panel changed
404: }
405:
406: }
|