KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > paintapp > PaintAppPanelVisual


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.modules.apisupport.paintapp;
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
32 public class PaintAppPanelVisual extends JPanel JavaDoc implements DocumentListener JavaDoc {
33     
34     private static final String JavaDoc WIZARD_PANEL_ERROR_MESSAGE = "WizardPanel_errorMessage"; // NOI18N
35

36     public static final String JavaDoc PROP_PROJECT_NAME = "projectName"; // NOI18N
37

38     private PaintAppWizardPanel panel;
39     
40     /** Creates new form PanelProjectLocationVisual */
41     public PaintAppPanelVisual(PaintAppWizardPanel panel) {
42         initComponents();
43         this.panel = panel;
44         // Register listener on the textFields to make the automatic updates
45
projectNameTextField.getDocument().addDocumentListener(this);
46         projectLocationTextField.getDocument().addDocumentListener(this);
47     }
48     
49     
50     public String JavaDoc getProjectName() {
51         return this.projectNameTextField.getText();
52     }
53     
54     /** This method is called from within the constructor to
55      * initialize the form.
56      * WARNING: Do NOT modify this code. The content of this method is
57      * always regenerated by the Form Editor.
58      */

59     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
60
private void initComponents() {
61         projectNameLabel = new javax.swing.JLabel JavaDoc();
62         projectNameTextField = new javax.swing.JTextField JavaDoc();
63         projectLocationLabel = new javax.swing.JLabel JavaDoc();
64         projectLocationTextField = new javax.swing.JTextField JavaDoc();
65         browseButton = new javax.swing.JButton JavaDoc();
66         createdFolderLabel = new javax.swing.JLabel JavaDoc();
67         createdFolderTextField = new javax.swing.JTextField JavaDoc();
68
69         projectNameLabel.setLabelFor(projectNameTextField);
70         org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, org.openide.util.NbBundle.getMessage(PaintAppPanelVisual.class, "PaintAppPanelVisual.project_name")); // NOI18N
71

72         projectLocationLabel.setLabelFor(projectLocationTextField);
73         org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, org.openide.util.NbBundle.getMessage(PaintAppPanelVisual.class, "PaintAppPanelVisual.project_location")); // NOI18N
74

75         org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(PaintAppPanelVisual.class, "PaintAppPanelVisual.browse")); // NOI18N
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(PaintAppPanelVisual.class, "PaintAppPanelVisual.project_folder")); // NOI18N
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
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
128             FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
129             chooser.setDialogTitle(org.openide.util.NbBundle.getMessage(PaintAppPanelVisual.class, "PaintAppPanelVisual.select_project_location"));
130             chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
131             String JavaDoc path = this.projectLocationTextField.getText();
132             if (path.length() > 0) {
133                 File JavaDoc f = new File JavaDoc(path);
134                 if (f.exists()) {
135                     chooser.setSelectedFile(f);
136                 }
137             }
138             if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
139                 File JavaDoc projectDir = chooser.getSelectedFile();
140                 projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath());
141             }
142             panel.fireChangeEvent();
143         
144     }//GEN-LAST:event_browseButtonActionPerformed
145

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

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

236     public void changedUpdate(DocumentEvent JavaDoc e) {
237         updateTexts(e);
238         if (this.projectNameTextField.getDocument() == e.getDocument()) {
239             firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
240         }
241     }
242     
243     public void insertUpdate(DocumentEvent JavaDoc e) {
244         updateTexts(e);
245         if (this.projectNameTextField.getDocument() == e.getDocument()) {
246             firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
247         }
248     }
249     
250     public void removeUpdate(DocumentEvent JavaDoc e) {
251         updateTexts(e);
252         if (this.projectNameTextField.getDocument() == e.getDocument()) {
253             firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
254         }
255     }
256     
257     /** Handles changes in the Project name and project directory, */
258     private void updateTexts(DocumentEvent JavaDoc e) {
259         
260         Document JavaDoc doc = e.getDocument();
261         
262         if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) {
263             // Change in the project name
264

265             String JavaDoc projectName = projectNameTextField.getText();
266             String JavaDoc projectFolder = projectLocationTextField.getText();
267             
268             //if (projectFolder.trim().length() == 0 || projectFolder.equals(oldName)) {
269
createdFolderTextField.setText(projectFolder + File.separatorChar + projectName);
270             //}
271

272         }
273         panel.fireChangeEvent(); // Notify that the panel changed
274
}
275     
276 }
277
Popular Tags