KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > bluej > wizard > EmptyBluejPanelVisual


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 package org.netbeans.bluej.wizard;
20
21 import java.io.File JavaDoc;
22 import javax.swing.JFileChooser JavaDoc;
23 import javax.swing.JPanel JavaDoc;
24 import javax.swing.event.DocumentEvent JavaDoc;
25 import javax.swing.event.DocumentListener JavaDoc;
26 import javax.swing.text.Document JavaDoc;
27 import org.netbeans.spi.project.ui.support.ProjectChooser;
28 import org.openide.WizardDescriptor;
29 import org.openide.WizardValidationException;
30 import org.openide.filesystems.FileUtil;
31 import org.openide.util.NbBundle;
32
33 public class EmptyBluejPanelVisual extends JPanel JavaDoc implements DocumentListener JavaDoc {
34
35     public static final String JavaDoc PROP_PROJECT_NAME = "projectName"; // NOI18N
36
private static final String JavaDoc WIZ_PANEL_ERROR = "WizardPanel_errorMessage"; // NOI18N
37
private EmptyBluejWizardPanel panel;
38     
39     /** Creates new form PanelProjectLocationVisual */
40     public EmptyBluejPanelVisual(EmptyBluejWizardPanel panel) {
41         initComponents();
42         this.panel = panel;
43         // Register listener on the textFields to make the automatic updates
44
projectNameTextField.getDocument().addDocumentListener(this);
45         projectLocationTextField.getDocument().addDocumentListener(this);
46     }
47     
48     
49     public String JavaDoc getProjectName() {
50         return this.projectNameTextField.getText();
51     }
52     
53     /** This method is called from within the constructor to
54      * initialize the form.
55      * WARNING: Do NOT modify this code. The content of this method is
56      * always regenerated by the Form Editor.
57      */

58     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
59
private void initComponents() {
60         projectNameLabel = new javax.swing.JLabel JavaDoc();
61         projectNameTextField = new javax.swing.JTextField JavaDoc();
62         projectLocationLabel = new javax.swing.JLabel JavaDoc();
63         projectLocationTextField = new javax.swing.JTextField JavaDoc();
64         browseButton = new javax.swing.JButton JavaDoc();
65         createdFolderLabel = new javax.swing.JLabel JavaDoc();
66         createdFolderTextField = new javax.swing.JTextField JavaDoc();
67
68         projectNameLabel.setLabelFor(projectNameTextField);
69         org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, org.openide.util.NbBundle.getMessage(EmptyBluejPanelVisual.class, "projectNameLabel"));
70
71         projectLocationLabel.setLabelFor(projectLocationTextField);
72         org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, org.openide.util.NbBundle.getMessage(EmptyBluejPanelVisual.class, "projectLocationLabel"));
73
74         org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(EmptyBluejPanelVisual.class, "browseButton"));
75         browseButton.setActionCommand("BROWSE");
76         browseButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
77             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
78                 browseButtonActionPerformed(evt);
79             }
80         });
81
82         createdFolderLabel.setLabelFor(createdFolderTextField);
83         org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, org.openide.util.NbBundle.getMessage(EmptyBluejPanelVisual.class, "createdFolderLabel"));
84
85         createdFolderTextField.setEditable(false);
86
87         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
88         this.setLayout(layout);
89         layout.setHorizontalGroup(
90             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
91             .add(layout.createSequentialGroup()
92                 .addContainerGap()
93                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
94                     .add(projectNameLabel)
95                     .add(projectLocationLabel)
96                     .add(createdFolderLabel))
97                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
98                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
99                     .add(org.jdesktop.layout.GroupLayout.TRAILING, projectNameTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
100                     .add(org.jdesktop.layout.GroupLayout.TRAILING, projectLocationTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
101                     .add(org.jdesktop.layout.GroupLayout.TRAILING, createdFolderTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE))
102                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
103                 .add(browseButton)
104                 .addContainerGap())
105         );
106         layout.setVerticalGroup(
107             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
108             .add(layout.createSequentialGroup()
109                 .addContainerGap()
110                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
111                     .add(projectNameLabel)
112                     .add(projectNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
113                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
114                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
115                     .add(projectLocationLabel)
116                     .add(projectLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
117                     .add(browseButton))
118                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
119                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
120                     .add(createdFolderLabel)
121                     .add(createdFolderTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
122                 .addContainerGap(213, Short.MAX_VALUE))
123         );
124     }// </editor-fold>//GEN-END:initComponents
125

126     private void browseButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseButtonActionPerformed
127
String JavaDoc command = evt.getActionCommand();
128         if ("BROWSE".equals(command)) { // NOI18N
129
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
130             FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
131             chooser.setDialogTitle(NbBundle.getMessage(EmptyBluejPanelVisual.class, "browse_dialog_title"));
132             chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
133             String JavaDoc path = this.projectLocationTextField.getText();
134             if (path.length() > 0) {
135                 File JavaDoc f = new File JavaDoc(path);
136                 if (f.exists()) {
137                     chooser.setSelectedFile(f);
138                 }
139             }
140             if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
141                 File JavaDoc projectDir = chooser.getSelectedFile();
142                 projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath());
143             }
144             panel.fireChangeEvent();
145         }
146         
147     }//GEN-LAST:event_browseButtonActionPerformed
148

149     
150     // Variables declaration - do not modify//GEN-BEGIN:variables
151
private javax.swing.JButton JavaDoc browseButton;
152     private javax.swing.JLabel JavaDoc createdFolderLabel;
153     private javax.swing.JTextField JavaDoc createdFolderTextField;
154     private javax.swing.JLabel JavaDoc projectLocationLabel;
155     private javax.swing.JTextField JavaDoc projectLocationTextField;
156     private javax.swing.JLabel JavaDoc projectNameLabel;
157     private javax.swing.JTextField JavaDoc projectNameTextField;
158     // End of variables declaration//GEN-END:variables
159

160     public void addNotify() {
161         super.addNotify();
162         //same problem as in 31086, initial focus on Cancel button
163
projectNameTextField.requestFocus();
164     }
165     
166     
167     void store(WizardDescriptor d) {
168         String JavaDoc name = projectNameTextField.getText().trim();
169         String JavaDoc folder = createdFolderTextField.getText().trim();
170         
171         d.putProperty("projdir", new File JavaDoc(folder)); // NOI18N
172
d.putProperty("name", name); // NOI18N
173
}
174     
175     void read(WizardDescriptor settings) {
176         File JavaDoc projectLocation = (File JavaDoc) settings.getProperty("projdir"); // NOI18N
177
if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) {
178             projectLocation = ProjectChooser.getProjectsFolder();
179         } else {
180             projectLocation = projectLocation.getParentFile();
181         }
182         this.projectLocationTextField.setText(projectLocation.getAbsolutePath());
183         
184         String JavaDoc projectName = (String JavaDoc) settings.getProperty("name"); // NOI18N
185
if(projectName == null) {
186             projectName = "EmptyBluej"; // NOI18N
187
}
188         this.projectNameTextField.setText(projectName);
189         this.projectNameTextField.selectAll();
190     }
191     
192     void validate(WizardDescriptor d) throws WizardValidationException {
193         // nothing to validate
194
}
195
196     public boolean valid(WizardDescriptor wizardDescriptor) {
197         
198         if (projectNameTextField.getText().length() == 0) {
199             wizardDescriptor.putProperty(WIZ_PANEL_ERROR,
200                     NbBundle.getMessage(EmptyBluejPanelVisual.class, "ERROR_wrongName"));
201             return false; // Display name not specified
202
}
203         File JavaDoc f = FileUtil.normalizeFile(new File JavaDoc(projectLocationTextField.getText()).getAbsoluteFile());
204         if (!f.isDirectory()) {
205             String JavaDoc message = NbBundle.getMessage(EmptyBluejPanelVisual.class, "ERROR_wrongFolder");
206             wizardDescriptor.putProperty(WIZ_PANEL_ERROR, message);
207             return false;
208         }
209         final File JavaDoc destFolder = FileUtil.normalizeFile(new File JavaDoc(createdFolderTextField.getText()).getAbsoluteFile());
210         
211         File JavaDoc projLoc = destFolder;
212         while (projLoc != null && !projLoc.exists()) {
213             projLoc = projLoc.getParentFile();
214         }
215         if (projLoc == null || !projLoc.canWrite()) {
216             wizardDescriptor.putProperty(WIZ_PANEL_ERROR,
217                     NbBundle.getMessage(EmptyBluejPanelVisual.class, "ERROR_cannot_create_folder"));
218             return false;
219         }
220         
221         if (FileUtil.toFileObject(projLoc) == null) {
222             String JavaDoc message = NbBundle.getMessage(EmptyBluejPanelVisual.class, "ERROR_wrong_path");
223             wizardDescriptor.putProperty(WIZ_PANEL_ERROR, message);
224             return false;
225         }
226         
227         File JavaDoc[] kids = destFolder.listFiles();
228         if (destFolder.exists() && kids != null && kids.length > 0) {
229             // Folder exists and is not empty
230
wizardDescriptor.putProperty(WIZ_PANEL_ERROR,
231                     NbBundle.getMessage(EmptyBluejPanelVisual.class, "ERROR_exists"));
232             return false;
233         }
234         wizardDescriptor.putProperty(WIZ_PANEL_ERROR, "");
235         return true;
236     }
237     
238     // Implementation of DocumentListener --------------------------------------
239

240     public void changedUpdate(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 insertUpdate(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     public void removeUpdate(DocumentEvent JavaDoc e) {
255         updateTexts(e);
256         if (this.projectNameTextField.getDocument() == e.getDocument()) {
257             firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
258         }
259     }
260     
261     /** Handles changes in the Project name and project directory, */
262     private void updateTexts(DocumentEvent JavaDoc e) {
263         
264         Document JavaDoc doc = e.getDocument();
265         
266         if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) {
267             // Change in the project name
268

269             String JavaDoc projectName = projectNameTextField.getText();
270             String JavaDoc projectFolder = projectLocationTextField.getText();
271             
272             //if (projectFolder.trim().length() == 0 || projectFolder.equals(oldName)) {
273
createdFolderTextField.setText(projectFolder + File.separatorChar + projectName);
274             //}
275

276         }
277         panel.fireChangeEvent(); // Notify that the panel changed
278
}
279     
280 }
281
Popular Tags