KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > samples > FreeformPanelVisual


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.ant.freeform.samples;
21
22 import java.io.File JavaDoc;
23 import javax.swing.JFileChooser JavaDoc;
24 import javax.swing.JPanel JavaDoc;
25 import javax.swing.event.DocumentEvent JavaDoc;
26 import javax.swing.event.DocumentListener JavaDoc;
27 import javax.swing.text.Document JavaDoc;
28 import org.netbeans.spi.project.ui.support.ProjectChooser;
29 import org.openide.WizardDescriptor;
30 import org.openide.WizardValidationException;
31 import org.openide.filesystems.FileUtil;
32
33 public class FreeformPanelVisual extends JPanel JavaDoc implements DocumentListener JavaDoc {
34
35     public static final String JavaDoc PROP_PROJECT_NAME = "projectName";
36     
37     private FreeformWizardPanel panel;
38     private int type;
39     
40     public FreeformPanelVisual(FreeformWizardPanel panel) {
41         initComponents();
42         this.panel = panel;
43         this.type = type;
44         // Register listener on the textFields to make the automatic updates
45
projectNameTextField.getDocument().addDocumentListener(this);
46         projectLocationTextField.getDocument().addDocumentListener(this);
47     }
48     
49     
50     public String JavaDoc getProjectName() {
51         return this.projectNameTextField.getText();
52     }
53     
54     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
55
private void initComponents() {
56         projectNameLabel = new javax.swing.JLabel JavaDoc();
57         projectNameTextField = new javax.swing.JTextField JavaDoc();
58         projectLocationLabel = new javax.swing.JLabel JavaDoc();
59         projectLocationTextField = new javax.swing.JTextField JavaDoc();
60         browseButton = new javax.swing.JButton JavaDoc();
61         createdFolderLabel = new javax.swing.JLabel JavaDoc();
62         createdFolderTextField = new javax.swing.JTextField JavaDoc();
63
64         projectNameLabel.setLabelFor(projectNameTextField);
65         org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, "Project &Name:");
66
67         projectLocationLabel.setLabelFor(projectLocationTextField);
68         org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, "Project &Location:");
69
70         org.openide.awt.Mnemonics.setLocalizedText(browseButton, "Br&owse...");
71         browseButton.setActionCommand("BROWSE");
72         browseButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
73             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
74                 browseButtonActionPerformed(evt);
75             }
76         });
77
78         createdFolderLabel.setLabelFor(createdFolderTextField);
79         org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, "Project &Folder:");
80
81         createdFolderTextField.setEditable(false);
82
83         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
84         this.setLayout(layout);
85         layout.setHorizontalGroup(
86             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
87             .add(layout.createSequentialGroup()
88                 .addContainerGap()
89                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
90                     .add(projectNameLabel)
91                     .add(projectLocationLabel)
92                     .add(createdFolderLabel))
93                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
94                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
95                     .add(org.jdesktop.layout.GroupLayout.TRAILING, projectNameTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
96                     .add(org.jdesktop.layout.GroupLayout.TRAILING, projectLocationTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
97                     .add(org.jdesktop.layout.GroupLayout.TRAILING, createdFolderTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE))
98                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
99                 .add(browseButton)
100                 .addContainerGap())
101         );
102         layout.setVerticalGroup(
103             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
104             .add(layout.createSequentialGroup()
105                 .addContainerGap()
106                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
107                     .add(projectNameLabel)
108                     .add(projectNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
109                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
110                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
111                     .add(projectLocationLabel)
112                     .add(projectLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
113                     .add(browseButton))
114                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
115                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
116                     .add(createdFolderLabel)
117                     .add(createdFolderTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
118                 .addContainerGap(213, Short.MAX_VALUE))
119         );
120     }// </editor-fold>//GEN-END:initComponents
121

122     private void browseButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseButtonActionPerformed
123
String JavaDoc command = evt.getActionCommand();
124         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
125         FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
126         chooser.setDialogTitle("Select Project Location"); // XXX I18N
127
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
128         String JavaDoc path = this.projectLocationTextField.getText();
129         if (path.length() > 0) {
130             File JavaDoc f = new File JavaDoc(path);
131             if (f.exists()) {
132                 chooser.setSelectedFile(f);
133             }
134         }
135         if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
136             File JavaDoc projectDir = chooser.getSelectedFile();
137             projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath());
138         }
139         panel.fireChangeEvent();
140     }//GEN-LAST:event_browseButtonActionPerformed
141

142     
143     // Variables declaration - do not modify//GEN-BEGIN:variables
144
private javax.swing.JButton JavaDoc browseButton;
145     private javax.swing.JLabel JavaDoc createdFolderLabel;
146     private javax.swing.JTextField JavaDoc createdFolderTextField;
147     private javax.swing.JLabel JavaDoc projectLocationLabel;
148     private javax.swing.JTextField JavaDoc projectLocationTextField;
149     private javax.swing.JLabel JavaDoc projectNameLabel;
150     private javax.swing.JTextField JavaDoc projectNameTextField;
151     // End of variables declaration//GEN-END:variables
152

153     public void addNotify() {
154         super.addNotify();
155         //same problem as in 31086, initial focus on Cancel button
156
projectNameTextField.requestFocus();
157     }
158     
159     boolean valid(WizardDescriptor wizardDescriptor) {
160         // XXX I18N
161

162         if (projectNameTextField.getText().length() == 0) {
163             wizardDescriptor.putProperty("WizardPanel_errorMessage",
164                     "Project Name is not a valid folder name.");
165             return false; // Display name not specified
166
}
167         File JavaDoc f = FileUtil.normalizeFile(new File JavaDoc(projectLocationTextField.getText()).getAbsoluteFile());
168         if (!f.isDirectory()) {
169             String JavaDoc message = "Project Folder is not a valid path.";
170             wizardDescriptor.putProperty("WizardPanel_errorMessage", message);
171             return false;
172         }
173         final File JavaDoc destFolder = FileUtil.normalizeFile(new File JavaDoc(createdFolderTextField.getText()).getAbsoluteFile());
174         
175         File JavaDoc projLoc = destFolder;
176         while (projLoc != null && !projLoc.exists()) {
177             projLoc = projLoc.getParentFile();
178         }
179         if (projLoc == null || !projLoc.canWrite()) {
180             wizardDescriptor.putProperty("WizardPanel_errorMessage",
181                     "Project Folder cannot be created.");
182             return false;
183         }
184         
185         if (FileUtil.toFileObject(projLoc) == null) {
186             String JavaDoc message = "Project Folder is not a valid path.";
187             wizardDescriptor.putProperty("WizardPanel_errorMessage", message);
188             return false;
189         }
190         
191         File JavaDoc[] kids = destFolder.listFiles();
192         if (destFolder.exists() && kids != null && kids.length > 0) {
193             // Folder exists and is not empty
194
wizardDescriptor.putProperty("WizardPanel_errorMessage",
195                     "Project Folder already exists and is not empty.");
196             return false;
197         }
198         wizardDescriptor.putProperty("WizardPanel_errorMessage", "");
199         return true;
200     }
201     
202     void store(WizardDescriptor d) {
203         String JavaDoc name = projectNameTextField.getText().trim();
204         String JavaDoc folder = createdFolderTextField.getText().trim();
205         
206         d.putProperty("projdir", new File JavaDoc(folder));
207         d.putProperty("name", name);
208     }
209     
210     void read(WizardDescriptor settings) {
211         File JavaDoc projectLocation = (File JavaDoc) settings.getProperty("projdir");
212         if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) {
213             projectLocation = ProjectChooser.getProjectsFolder();
214         } else {
215             projectLocation = projectLocation.getParentFile();
216         }
217         this.projectLocationTextField.setText(projectLocation.getAbsolutePath());
218         
219         String JavaDoc projectName = (String JavaDoc) settings.getProperty("name");
220         if (projectName == null) {
221             projectName = "freeform";
222         }
223         this.projectNameTextField.setText(projectName);
224         this.projectNameTextField.selectAll();
225     }
226     
227     void validate(WizardDescriptor d) throws WizardValidationException {
228         // nothing to validate
229
}
230     
231     // Implementation of DocumentListener --------------------------------------
232

233     public void changedUpdate(DocumentEvent JavaDoc e) {
234         updateTexts(e);
235         if (this.projectNameTextField.getDocument() == e.getDocument()) {
236             firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
237         }
238     }
239     
240     public void insertUpdate(DocumentEvent JavaDoc e) {
241         updateTexts(e);
242         if (this.projectNameTextField.getDocument() == e.getDocument()) {
243             firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
244         }
245     }
246     
247     public void removeUpdate(DocumentEvent JavaDoc e) {
248         updateTexts(e);
249         if (this.projectNameTextField.getDocument() == e.getDocument()) {
250             firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
251         }
252     }
253     
254     /** Handles changes in the Project name and project directory, */
255     private void updateTexts(DocumentEvent JavaDoc e) {
256         
257         Document JavaDoc doc = e.getDocument();
258         
259         if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) {
260             // Change in the project name
261

262             String JavaDoc projectName = projectNameTextField.getText();
263             String JavaDoc projectFolder = projectLocationTextField.getText();
264             
265             //if (projectFolder.trim().length() == 0 || projectFolder.equals(oldName)) {
266
createdFolderTextField.setText(projectFolder + File.separatorChar + projectName);
267             //}
268

269         }
270         panel.fireChangeEvent(); // Notify that the panel changed
271
}
272     
273 }
274
Popular Tags