KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > 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.java.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         
43         // Register listener on the textFields to make the automatic updates
44
projectNameTextField.getDocument().addDocumentListener(this);
45         projectLocationTextField.getDocument().addDocumentListener(this);
46     }
47     
48     /** This method is called from within the constructor to
49      * initialize the form.
50      * WARNING: Do NOT modify this code. The content of this method is
51      * always regenerated by the Form Editor.
52      */

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

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

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

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

264     
265     /** Handles changes in the project name and project directory
266      */

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