KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > scripting > php > webproject > wizard > PHPWebProjectPanelVisual


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.scripting.php.webproject.wizard;
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.modules.scripting.php.webproject.PhpProjectProperties;
29 import org.netbeans.spi.project.ui.support.ProjectChooser;
30 import org.openide.WizardDescriptor;
31 import org.openide.WizardValidationException;
32 import org.openide.filesystems.FileUtil;
33
34 public class PHPWebProjectPanelVisual extends JPanel JavaDoc implements DocumentListener JavaDoc {
35     
36     public static final String JavaDoc PROP_PROJECT_NAME = "projectName"; // NOI18N
37

38     private static final String JavaDoc DEFAULT_HTTP_PREFIX = "http://localhost/"; // NOI18N
39

40     private PHPWebProjectWizardPanel panel;
41     private String JavaDoc oldProjectName;
42     
43     public PHPWebProjectPanelVisual(PHPWebProjectWizardPanel panel) {
44         initComponents();
45         this.panel = panel;
46         // Register listener on the textFields to make the automatic updates
47
projectNameTextField.getDocument().addDocumentListener(this);
48         projectLocationTextField.getDocument().addDocumentListener(this);
49         serverUsesSourceCheckBox.setSelected(true);
50         targetDirTextField.setEnabled(!serverUsesSourceCheckBox.isSelected());
51         welcomePageTextField.setText(PhpProjectProperties.DEFAULT_START_PAGE);
52      }
53     
54     public String JavaDoc getProjectName() {
55         return this.projectNameTextField.getText();
56     }
57     
58     /** This method is called from within the constructor to
59      * initialize the form.
60      * WARNING: Do NOT modify this code. The content of this method is
61      * always regenerated by the Form Editor.
62      */

63     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
64
private void initComponents() {
65
66         projectNameLabel = new javax.swing.JLabel JavaDoc();
67         projectNameTextField = new javax.swing.JTextField JavaDoc();
68         projectLocationLabel = new javax.swing.JLabel JavaDoc();
69         projectLocationTextField = new javax.swing.JTextField JavaDoc();
70         browseButton = new javax.swing.JButton JavaDoc();
71         createdFolderLabel = new javax.swing.JLabel JavaDoc();
72         createdFolderTextField = new javax.swing.JTextField JavaDoc();
73         welcomePageLabel = new javax.swing.JLabel JavaDoc();
74         welcomePageTextField = new javax.swing.JTextField JavaDoc();
75         serverUsesSourceCheckBox = new javax.swing.JCheckBox JavaDoc();
76         targetDirLabel = new javax.swing.JLabel JavaDoc();
77         targetDirTextField = new javax.swing.JTextField JavaDoc();
78         urlLabel = new javax.swing.JLabel JavaDoc();
79         urlTextField = new javax.swing.JTextField JavaDoc();
80
81         projectNameLabel.setLabelFor(projectNameTextField);
82         org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_ProjectName")); // NOI18N
83

84         projectNameTextField.setToolTipText(org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "TT_ProjectName")); // NOI18N
85

86         projectLocationLabel.setLabelFor(projectLocationTextField);
87         org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_ProjectLocation")); // NOI18N
88

89         projectLocationTextField.setToolTipText(org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "TT_ProjectLocation")); // NOI18N
90

91         org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_ProjectLocationBrowse")); // NOI18N
92
browseButton.setActionCommand("BROWSE");
93         browseButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
94             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
95                 browseButtonActionPerformed(evt);
96             }
97         });
98
99         createdFolderLabel.setLabelFor(createdFolderTextField);
100         org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_ProjectSrcFolder")); // NOI18N
101

102         createdFolderTextField.setEditable(false);
103
104         welcomePageLabel.setLabelFor(welcomePageTextField);
105         org.openide.awt.Mnemonics.setLocalizedText(welcomePageLabel, org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_welcomePage")); // NOI18N
106

107         welcomePageTextField.setToolTipText(org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "TT_welcomePage")); // NOI18N
108

109         org.openide.awt.Mnemonics.setLocalizedText(serverUsesSourceCheckBox, org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_serverUsesSourceDir")); // NOI18N
110
serverUsesSourceCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
111         serverUsesSourceCheckBox.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
112         serverUsesSourceCheckBox.addActionListener(new java.awt.event.ActionListener JavaDoc() {
113             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
114                 serverUsesSourceCheckBoxActionPerformed(evt);
115             }
116         });
117
118         targetDirLabel.setLabelFor(targetDirTextField);
119         org.openide.awt.Mnemonics.setLocalizedText(targetDirLabel, org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_targetDir")); // NOI18N
120

121         targetDirTextField.setToolTipText(org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "TT_targetDir")); // NOI18N
122

123         urlLabel.setLabelFor(urlTextField);
124         org.openide.awt.Mnemonics.setLocalizedText(urlLabel, org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_url")); // NOI18N
125

126         urlTextField.setToolTipText(org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "TT_url")); // NOI18N
127

128         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
129         this.setLayout(layout);
130         layout.setHorizontalGroup(
131             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
132             .add(layout.createSequentialGroup()
133                 .addContainerGap()
134                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
135                     .add(layout.createSequentialGroup()
136                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
137                             .add(projectNameLabel)
138                             .add(projectLocationLabel)
139                             .add(createdFolderLabel)
140                             .add(welcomePageLabel)
141                             .add(targetDirLabel)
142                             .add(urlLabel))
143                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
144                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
145                             .add(targetDirTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
146                             .add(org.jdesktop.layout.GroupLayout.LEADING, welcomePageTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
147                             .add(projectNameTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
148                             .add(projectLocationTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
149                             .add(createdFolderTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
150                             .add(urlTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE))
151                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
152                         .add(browseButton))
153                     .add(serverUsesSourceCheckBox))
154                 .addContainerGap())
155         );
156         layout.setVerticalGroup(
157             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
158             .add(layout.createSequentialGroup()
159                 .addContainerGap()
160                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
161                     .add(projectNameLabel)
162                     .add(projectNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
163                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
164                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
165                     .add(projectLocationLabel)
166                     .add(projectLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
167                     .add(browseButton))
168                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
169                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
170                     .add(createdFolderLabel)
171                     .add(createdFolderTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
172                 .add(10, 10, 10)
173                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
174                     .add(welcomePageLabel)
175                     .add(welcomePageTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
176                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
177                 .add(serverUsesSourceCheckBox)
178                 .add(15, 15, 15)
179                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
180                     .add(targetDirLabel)
181                     .add(targetDirTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
182                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
183                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
184                     .add(urlLabel)
185                     .add(urlTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
186                 .addContainerGap(104, Short.MAX_VALUE))
187         );
188
189         browseButton.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_ProjectLocationBrowse")); // NOI18N
190
}// </editor-fold>//GEN-END:initComponents
191

192     private void serverUsesSourceCheckBoxActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_serverUsesSourceCheckBoxActionPerformed
193
targetDirTextField.setEnabled(!serverUsesSourceCheckBox.isSelected());
194 }//GEN-LAST:event_serverUsesSourceCheckBoxActionPerformed
195

196     private void browseButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseButtonActionPerformed
197
String JavaDoc command = evt.getActionCommand();
198         if ("BROWSE".equals(command)) {
199             JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
200             FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
201             chooser.setDialogTitle("Select Project Location");
202             chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
203             String JavaDoc path = this.projectLocationTextField.getText();
204             if (path.length() > 0) {
205                 File JavaDoc f = new File JavaDoc(path);
206                 if (f.exists()) {
207                     chooser.setSelectedFile(f);
208                 }
209             }
210             if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
211                 File JavaDoc projectDir = chooser.getSelectedFile();
212                 projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath());
213             }
214             panel.fireChangeEvent();
215         }
216         
217     }//GEN-LAST:event_browseButtonActionPerformed
218

219     
220     // Variables declaration - do not modify//GEN-BEGIN:variables
221
private javax.swing.JButton JavaDoc browseButton;
222     private javax.swing.JLabel JavaDoc createdFolderLabel;
223     private javax.swing.JTextField JavaDoc createdFolderTextField;
224     private javax.swing.JLabel JavaDoc projectLocationLabel;
225     private javax.swing.JTextField JavaDoc projectLocationTextField;
226     private javax.swing.JLabel JavaDoc projectNameLabel;
227     private javax.swing.JTextField JavaDoc projectNameTextField;
228     private javax.swing.JCheckBox JavaDoc serverUsesSourceCheckBox;
229     private javax.swing.JLabel JavaDoc targetDirLabel;
230     private javax.swing.JTextField JavaDoc targetDirTextField;
231     private javax.swing.JLabel JavaDoc urlLabel;
232     private javax.swing.JTextField JavaDoc urlTextField;
233     private javax.swing.JLabel JavaDoc welcomePageLabel;
234     private javax.swing.JTextField JavaDoc welcomePageTextField;
235     // End of variables declaration//GEN-END:variables
236

237     public void addNotify() {
238         super.addNotify();
239         //same problem as in 31086, initial focus on Cancel button
240
projectNameTextField.requestFocus();
241     }
242     
243     boolean valid(WizardDescriptor d) {
244         
245         if (projectNameTextField.getText().length() == 0) {
246             d.putProperty("WizardPanel_errorMessage",
247                     "Project Name is not a valid folder name.");
248             return false; // Display name not specified
249
}
250         File JavaDoc f = FileUtil.normalizeFile(new File JavaDoc(projectLocationTextField.getText()).getAbsoluteFile());
251         if (!f.isDirectory()) {
252             String JavaDoc message = "Project Folder is not a valid path.";
253             d.putProperty("WizardPanel_errorMessage", message);
254             return false;
255         }
256         final File JavaDoc destFolder = FileUtil.normalizeFile(new File JavaDoc(createdFolderTextField.getText()).getAbsoluteFile());
257         
258         File JavaDoc projLoc = destFolder;
259         while (projLoc != null && !projLoc.exists()) {
260             projLoc = projLoc.getParentFile();
261         }
262         if (projLoc == null || !projLoc.canWrite()) {
263             d.putProperty("WizardPanel_errorMessage",
264                     "Project Folder cannot be created.");
265             return false;
266         }
267         
268         if (FileUtil.toFileObject(projLoc) == null) {
269             String JavaDoc message = "Project Folder is not a valid path.";
270             d.putProperty("WizardPanel_errorMessage", message);
271             return false;
272         }
273         
274         File JavaDoc[] kids = destFolder.listFiles();
275         if (destFolder.exists() && kids != null && kids.length > 0) {
276             // Folder exists and is not empty
277
d.putProperty("WizardPanel_errorMessage",
278                     "Project Folder already exists and is not empty.");
279             return false;
280         }
281         d.putProperty("WizardPanel_errorMessage", "");
282         
283         return true;
284     }
285     
286     void store(WizardDescriptor d) {
287         String JavaDoc name = projectNameTextField.getText().trim();
288         String JavaDoc folder = createdFolderTextField.getText().trim();
289         
290         d.putProperty(PhpProjectProperties.PROJECT_DIR, new File JavaDoc(folder));
291         d.putProperty(PhpProjectProperties.NAME, name);
292         d.putProperty(PhpProjectProperties.WELCOME_PAGE, welcomePageTextField.getText().trim());
293         d.putProperty(PhpProjectProperties.USE_SOURCE_DIR, Boolean.valueOf(serverUsesSourceCheckBox.isSelected()));
294         d.putProperty(PhpProjectProperties.TARGET_DIR, targetDirTextField.getText().trim());
295         d.putProperty(PhpProjectProperties.URL, urlTextField.getText().trim());
296     }
297     
298     void read(WizardDescriptor settings) {
299         File JavaDoc projectLocation = (File JavaDoc) settings.getProperty(PhpProjectProperties.PROJECT_DIR);
300         if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) {
301             projectLocation = ProjectChooser.getProjectsFolder();
302         } else {
303             projectLocation = projectLocation.getParentFile();
304         }
305         this.projectLocationTextField.setText(projectLocation.getAbsolutePath());
306         
307         String JavaDoc projectName = (String JavaDoc) settings.getProperty(PhpProjectProperties.NAME);
308         if(projectName == null) {
309             projectName = "PHPWebProject";
310         }
311         this.projectNameTextField.setText(projectName);
312         this.projectNameTextField.selectAll();
313     }
314     
315     void validate(WizardDescriptor d) throws WizardValidationException {
316         // nothing to validate
317
}
318     
319     // Implementation of DocumentListener --------------------------------------
320

321     public void changedUpdate(DocumentEvent JavaDoc e) {
322         updateTexts(e);
323         if (this.projectNameTextField.getDocument() == e.getDocument()) {
324             firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
325         }
326     }
327     
328     public void insertUpdate(DocumentEvent JavaDoc e) {
329         updateTexts(e);
330         if (this.projectNameTextField.getDocument() == e.getDocument()) {
331             firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
332         }
333     }
334     
335     public void removeUpdate(DocumentEvent JavaDoc e) {
336         updateTexts(e);
337         if (this.projectNameTextField.getDocument() == e.getDocument()) {
338             firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
339         }
340     }
341     
342     /** Handles changes in the Project name and project directory, */
343     private void updateTexts(DocumentEvent JavaDoc e) {
344         
345         Document JavaDoc doc = e.getDocument();
346         
347         if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) {
348             // Change in the project name
349

350             String JavaDoc projectName = projectNameTextField.getText();
351             String JavaDoc projectFolder = projectLocationTextField.getText();
352             
353             createdFolderTextField.setText(projectFolder + File.separatorChar + projectName);
354             
355             if (oldProjectName == null || oldProjectName.length() == 0) {
356                 urlTextField.setText(DEFAULT_HTTP_PREFIX + projectName);
357                 targetDirTextField.setText(File.separator + projectName);
358             }
359             else if (!oldProjectName.equals(projectName)) {
360                 urlTextField.setText(urlTextField.getText().replaceAll(oldProjectName, projectName));
361                 targetDirTextField.setText(targetDirTextField.getText().replaceAll(oldProjectName, projectName));
362             }
363             
364             oldProjectName = projectName;
365         }
366         panel.fireChangeEvent(); // Notify that the panel changed
367
}
368     
369 }
370
Popular Tags