KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > ui > wizards > 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.j2ee.ejbjarproject.ui.wizards;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25
26 import javax.swing.JFileChooser JavaDoc;
27 import javax.swing.event.DocumentEvent JavaDoc;
28 import javax.swing.event.DocumentListener JavaDoc;
29 import org.netbeans.modules.j2ee.ejbjarproject.ui.FoldersListSettings;
30
31 import org.netbeans.spi.project.ui.support.ProjectChooser;
32
33 import org.openide.WizardDescriptor;
34 import org.openide.WizardValidationException;
35 import org.openide.util.NbBundle;
36
37 public class PanelProjectLocationVisual extends SettingsPanel implements DocumentListener JavaDoc {
38     
39     private PanelConfigureProject panel;
40     
41     /** Creates new form PanelProjectLocationVisual */
42     public PanelProjectLocationVisual(PanelConfigureProject panel) {
43         initComponents();
44         this.panel = panel;
45         
46         // Register listener on the textFields to make the automatic updates
47
projectNameTextField.getDocument().addDocumentListener(this);
48         projectLocationTextField.getDocument().addDocumentListener(this);
49     }
50     
51     /** This method is called from within the constructor to
52      * initialize the form.
53      * WARNING: Do NOT modify this code. The content of this method is
54      * always regenerated by the Form Editor.
55      */

56     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
57
private void initComponents() {
58         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
59
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         Button = new javax.swing.JButton JavaDoc();
65         createdFolderLabel = new javax.swing.JLabel JavaDoc();
66         createdFolderTextField = new javax.swing.JTextField JavaDoc();
67
68         setLayout(new java.awt.GridBagLayout JavaDoc());
69
70         projectNameLabel.setLabelFor(projectNameTextField);
71         org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, NbBundle.getBundle("org/netbeans/modules/j2ee/ejbjarproject/ui/wizards/Bundle").getString("LBL_NWP1_ProjectName_Label"));
72         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
73         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
74         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
75         add(projectNameLabel, gridBagConstraints);
76
77         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
78         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
79         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
80         gridBagConstraints.weightx = 1.0;
81         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 12, 0);
82         add(projectNameTextField, gridBagConstraints);
83         projectNameTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NWP1_ProjectName_A11YDesc"));
84
85         projectLocationLabel.setLabelFor(projectLocationTextField);
86         org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, NbBundle.getBundle("org/netbeans/modules/j2ee/ejbjarproject/ui/wizards/Bundle").getString("LBL_NWP1_ProjectLocation_Label"));
87         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
88         gridBagConstraints.gridy = 1;
89         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
90         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 0);
91         add(projectLocationLabel, gridBagConstraints);
92
93         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
94         gridBagConstraints.gridy = 1;
95         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
96         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
97         gridBagConstraints.weightx = 1.0;
98         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 5, 0);
99         add(projectLocationTextField, gridBagConstraints);
100         projectLocationTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NPW1_ProjectLocation_A11YDesc"));
101
102         org.openide.awt.Mnemonics.setLocalizedText(Button, NbBundle.getBundle("org/netbeans/modules/j2ee/ejbjarproject/ui/wizards/Bundle").getString("LBL_NWP1_BrowseLocation_Button"));
103         Button.setActionCommand("BROWSE");
104         Button.addActionListener(new java.awt.event.ActionListener JavaDoc() {
105             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
106                 browseLocationAction(evt);
107             }
108         });
109
110         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
111         gridBagConstraints.gridy = 1;
112         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
113         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 5, 0);
114         add(Button, gridBagConstraints);
115         Button.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NWP1_BrowseLocation_A11YDesc"));
116
117         createdFolderLabel.setLabelFor(createdFolderTextField);
118         org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, NbBundle.getBundle("org/netbeans/modules/j2ee/ejbjarproject/ui/wizards/Bundle").getString("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     }
137     // </editor-fold>//GEN-END:initComponents
138

139     private void browseLocationAction(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseLocationAction
140
String JavaDoc command = evt.getActionCommand();
141         
142         if ("BROWSE".equals(command)) { //NOI18N
143
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
144             chooser.setDialogTitle(NbBundle.getMessage(PanelProjectLocationVisual.class,"LBL_NWP1_SelectProjectLocation")); //NOI18N
145
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
146             String JavaDoc path = projectLocationTextField.getText();
147             if (path.length() > 0) {
148                 File JavaDoc f = new File JavaDoc(path);
149                 if (f.exists())
150                     chooser.setSelectedFile(f);
151             }
152             if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
153                 File JavaDoc projectDir = chooser.getSelectedFile();
154                 projectLocationTextField.setText(projectDir.getAbsolutePath());
155             }
156             panel.fireChangeEvent();
157         }
158     }//GEN-LAST:event_browseLocationAction
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     boolean valid(WizardDescriptor wizardDescriptor) {
167         if (projectNameTextField.getText().length() == 0) {
168             wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_IllegalProjectName")); //NOI18N
169
return false; // Display name not specified
170
}
171         
172         File JavaDoc destFolder = new File JavaDoc(createdFolderTextField.getText());
173         File JavaDoc[] children = destFolder.listFiles();
174         if (destFolder.exists() && children != null && children.length > 0) {
175             // Folder exists and is not empty
176
wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_ProjectFolderExists")); //NOI18N
177
return false;
178         }
179                 
180         wizardDescriptor.putProperty("WizardPanel_errorMessage", ""); //NOI18N
181
return true;
182     }
183     
184     void store(WizardDescriptor d) {
185         String JavaDoc name = projectNameTextField.getText().trim();
186         
187         d.putProperty(WizardProperties.PROJECT_DIR, new File JavaDoc(createdFolderTextField.getText().trim()));
188         d.putProperty(WizardProperties.NAME, name);
189         
190         File JavaDoc projectsDir = new File JavaDoc(this.projectLocationTextField.getText());
191         if (projectsDir.isDirectory()) {
192             ProjectChooser.setProjectsFolder (projectsDir);
193         }
194     }
195         
196     void validate (WizardDescriptor d) throws WizardValidationException {
197         // nothing to validate
198
}
199
200     void read (WizardDescriptor settings) {
201         File JavaDoc projectLocation = (File JavaDoc) settings.getProperty(WizardProperties.PROJECT_DIR);
202         if (projectLocation == null)
203             projectLocation = ProjectChooser.getProjectsFolder();
204         else
205             projectLocation = projectLocation.getParentFile();
206         
207         projectLocationTextField.setText(projectLocation.getAbsolutePath());
208         
209         String JavaDoc projectName = (String JavaDoc) settings.getProperty(WizardProperties.NAME);
210         if (projectName == null) {
211             int baseCount = FoldersListSettings.getDefault().getNewProjectCount() + 1;
212             String JavaDoc formater = NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NPW1_DefaultProjectName");
213             while ((projectName = validFreeProjectName(projectLocation, formater, baseCount)) == null)
214                 baseCount++;
215         }
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     // Implementation of DocumentListener --------------------------------------
246
public void changedUpdate(DocumentEvent JavaDoc e) {
247         updateTexts(e);
248     }
249     
250     public void insertUpdate(DocumentEvent JavaDoc e) {
251         updateTexts(e);
252     }
253     
254     public void removeUpdate(DocumentEvent JavaDoc e) {
255         updateTexts(e);
256     }
257     // End if implementation of DocumentListener -------------------------------
258

259     
260     /** Handles changes in the project name and project directory
261      */

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