KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > examples > PanelProjectLocationVisual


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.web.examples;
21
22 import java.io.File JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24
25 import javax.swing.JFileChooser JavaDoc;
26 import javax.swing.event.DocumentEvent JavaDoc;
27 import javax.swing.event.DocumentListener JavaDoc;
28
29 import org.netbeans.spi.project.ui.support.ProjectChooser;
30
31 import org.openide.WizardDescriptor;
32 import org.openide.util.NbBundle;
33
34 public class PanelProjectLocationVisual extends SettingsPanel implements DocumentListener JavaDoc {
35
36     private PanelConfigureProject panel;
37
38     /** Creates new form PanelProjectLocationVisual */
39     public PanelProjectLocationVisual(PanelConfigureProject panel) {
40         initComponents();
41         this.panel = panel;
42 // projectNameTextField.setEditable(false);
43

44         // Register listener on the textFields to make the automatic updates
45
projectNameTextField.getDocument().addDocumentListener(this);
46         projectLocationTextField.getDocument().addDocumentListener(this);
47     }
48     
49     /** This method is called from within the constructor to
50      * initialize the form.
51      * WARNING: Do NOT modify this code. The content of this method is
52      * always regenerated by the Form Editor.
53      */

54     private void initComponents() {//GEN-BEGIN:initComponents
55
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
56
57         projectNameLabel = new javax.swing.JLabel JavaDoc();
58         projectNameTextField = new javax.swing.JTextField JavaDoc();
59         projectLocationLabel = new javax.swing.JLabel JavaDoc();
60         projectLocationTextField = new javax.swing.JTextField JavaDoc();
61         Button = new javax.swing.JButton JavaDoc();
62         createdFolderLabel = new javax.swing.JLabel JavaDoc();
63         createdFolderTextField = new javax.swing.JTextField JavaDoc();
64
65         setLayout(new java.awt.GridBagLayout JavaDoc());
66
67         projectNameLabel.setDisplayedMnemonic(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectName_LabelMnemonic").charAt(0));
68         projectNameLabel.setLabelFor(projectNameTextField);
69         projectNameLabel.setText(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectName_Label"));
70         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
71         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
72         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
73         add(projectNameLabel, gridBagConstraints);
74
75         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
76         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
77         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
78         gridBagConstraints.weightx = 1.0;
79         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 12, 0);
80         add(projectNameTextField, gridBagConstraints);
81         projectNameTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NWP1_ProjectName_A11YDesc"));
82
83         projectLocationLabel.setDisplayedMnemonic(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectLocation_LabelMnemonic").charAt(0));
84         projectLocationLabel.setLabelFor(projectLocationTextField);
85         projectLocationLabel.setText(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectLocation_Label"));
86         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
87         gridBagConstraints.gridy = 1;
88         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
89         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 0);
90         add(projectLocationLabel, gridBagConstraints);
91
92         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
93         gridBagConstraints.gridy = 1;
94         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
95         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
96         gridBagConstraints.weightx = 1.0;
97         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 5, 0);
98         add(projectLocationTextField, gridBagConstraints);
99         projectLocationTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NPW1_ProjectLocation_A11YDesc"));
100
101         Button.setText(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_BrowseLocation_Button"));
102         Button.setActionCommand("BROWSE");
103         Button.addActionListener(new java.awt.event.ActionListener JavaDoc() {
104             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
105                 browseLocationAction(evt);
106             }
107         });
108
109         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
110         gridBagConstraints.gridy = 1;
111         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
112         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 5, 0);
113         add(Button, gridBagConstraints);
114         Button.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NWP1_BrowseLocation_A11YDesc"));
115
116         createdFolderLabel.setDisplayedMnemonic(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_CreatedProjectFolder_LablelMnemonic").charAt(0));
117         createdFolderLabel.setLabelFor(createdFolderTextField);
118         createdFolderLabel.setText(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_CreatedProjectFolder_Lablel"));
119         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
120         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
121         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
122         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
123         add(createdFolderLabel, gridBagConstraints);
124
125         createdFolderTextField.setEditable(false);
126         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
127         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
128         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
129         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
130         gridBagConstraints.weightx = 1.0;
131         gridBagConstraints.weighty = 1.0;
132         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 0);
133         add(createdFolderTextField, gridBagConstraints);
134         createdFolderTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NWP1_CreatedProjectFolder_A11YDesc"));
135
136     }//GEN-END:initComponents
137

138     private void browseLocationAction(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseLocationAction
139
String JavaDoc command = evt.getActionCommand();
140         
141         if ("BROWSE".equals(command)) { //NOI18N
142
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
143             chooser.setDialogTitle(NbBundle.getMessage(PanelProjectLocationVisual.class,"LBL_NWP1_SelectProjectLocation")); //NOI18N
144
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
145             String JavaDoc path = projectLocationTextField.getText();
146             if (path.length() > 0) {
147                 File JavaDoc f = new File JavaDoc(path);
148                 if (f.exists())
149                     chooser.setSelectedFile(f);
150             }
151             if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
152                 File JavaDoc projectDir = chooser.getSelectedFile();
153                 projectLocationTextField.setText(projectDir.getAbsolutePath());
154             }
155             panel.fireChangeEvent();
156         }
157     }//GEN-LAST:event_browseLocationAction
158

159     public void addNotify() {
160         super.addNotify();
161         //same problem as in 31086, initial focus on Cancel button
162
projectLocationTextField.requestFocus();
163     }
164     
165     boolean valid(WizardDescriptor wizardDescriptor) {
166         if (projectNameTextField.getText().length() == 0) {
167             wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_IllegalProjectName")); //NOI18N
168
return false; // Display name not specified
169
}
170         
171         File JavaDoc destFolder = new File JavaDoc(createdFolderTextField.getText());
172         File JavaDoc[] children = destFolder.listFiles();
173         if (destFolder.exists() && children != null && children.length > 0) {
174             // Folder exists and is not empty
175
wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_ProjectFolderExists")); //NOI18N
176
return false;
177         }
178                 
179         wizardDescriptor.putProperty("WizardPanel_errorMessage", ""); //NOI18N
180
return true;
181     }
182     
183     void store(WizardDescriptor d) {
184         String JavaDoc name = projectNameTextField.getText().trim();
185         
186         d.putProperty(WizardProperties.PROJECT_DIR, new File JavaDoc(createdFolderTextField.getText().trim()));
187         d.putProperty(WizardProperties.NAME, name);
188         
189         File JavaDoc projectsDir = new File JavaDoc(this.projectLocationTextField.getText());
190         if (projectsDir.isDirectory()) {
191             ProjectChooser.setProjectsFolder (projectsDir);
192         }
193     }
194         
195     void read (WizardDescriptor settings) {
196         File JavaDoc projectLocation = (File JavaDoc) settings.getProperty(WizardProperties.PROJECT_DIR);
197         if (projectLocation == null)
198             projectLocation = ProjectChooser.getProjectsFolder();
199         else
200             projectLocation = projectLocation.getParentFile();
201         
202         projectLocationTextField.setText(projectLocation.getAbsolutePath());
203         projectLocationTextField.selectAll();
204         
205         String JavaDoc formater = null;
206         String JavaDoc projectName = (String JavaDoc) settings.getProperty(WizardProperties.NAME);
207         
208         if (projectName == null) {
209             formater = NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NPW1_DefaultProjectName"); //NOI18N
210
} else {
211             formater = projectName + "{0}"; //NOI18N
212
}
213         if ((projectName == null) || (validFreeProjectName(projectLocation, projectName) == null)) {
214             int baseCount = FoldersListSettings.getDefault().getNewProjectCount() + 1;
215             while ((projectName = validFreeProjectName(projectLocation, formater, baseCount)) == null)
216                 baseCount++;
217 // settings.putProperty(NewWebProjectWizardIterator.PROP_NAME_INDEX, new Integer(baseCount));
218
}
219         projectNameTextField.setText(projectName);
220         projectNameTextField.selectAll();
221     }
222
223     // Variables declaration - do not modify//GEN-BEGIN:variables
224
private javax.swing.JButton JavaDoc Button;
225     private javax.swing.JLabel JavaDoc createdFolderLabel;
226     private javax.swing.JTextField JavaDoc createdFolderTextField;
227     private javax.swing.JLabel JavaDoc projectLocationLabel;
228     private javax.swing.JTextField JavaDoc projectLocationTextField;
229     private javax.swing.JLabel JavaDoc projectNameLabel;
230     protected javax.swing.JTextField JavaDoc projectNameTextField;
231     // End of variables declaration//GEN-END:variables
232

233     private static JFileChooser JavaDoc createChooser() {
234         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
235         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
236         chooser.setAcceptAllFileFilterUsed(false);
237         
238         return chooser;
239     }
240
241     private String JavaDoc validFreeProjectName(final File JavaDoc parentFolder, final String JavaDoc formater, final int index) {
242         String JavaDoc name = MessageFormat.format(formater, new Object JavaDoc[] {new Integer JavaDoc (index)});
243         File JavaDoc file = new File JavaDoc(parentFolder, name);
244         return file.exists() ? null : name;
245     }
246
247     private String JavaDoc validFreeProjectName(final File JavaDoc parentFolder, final String JavaDoc name) {
248         File JavaDoc file = new File JavaDoc(parentFolder, name);
249         return file.exists() ? null : name;
250     }
251     
252     // Implementation of DocumentListener --------------------------------------
253
public void changedUpdate(DocumentEvent JavaDoc e) {
254         updateTexts(e);
255     }
256     
257     public void insertUpdate(DocumentEvent JavaDoc e) {
258         updateTexts(e);
259     }
260     
261     public void removeUpdate(DocumentEvent JavaDoc e) {
262         updateTexts(e);
263     }
264     // End if implementation of DocumentListener -------------------------------
265

266     
267     /** Handles changes in the project name and project directory
268      */

269     private void updateTexts(DocumentEvent JavaDoc e) {
270         createdFolderTextField.setText(getCreatedFolderPath());
271
272         panel.fireChangeEvent(); // Notify that the panel changed
273
}
274     
275     private String JavaDoc getCreatedFolderPath() {
276         StringBuffer JavaDoc folder = new StringBuffer JavaDoc(projectLocationTextField.getText().trim());
277         if (!projectLocationTextField.getText().endsWith(File.separator))
278             folder.append(File.separatorChar);
279         folder.append(projectNameTextField.getText().trim());
280         
281         return folder.toString();
282     }
283     
284 }
285
286 //TODO implement check for project folder name and location
287
Popular Tags