KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > feedreader > FeedReaderPanelVisual


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.feedreader;
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 import org.openide.util.NbBundle;
32
33 public class FeedReaderPanelVisual extends JPanel JavaDoc implements DocumentListener JavaDoc {
34
35     public static final String JavaDoc PROP_PROJECT_NAME = "projectName"; // NOI18N
36

37     private FeedReaderWizardPanel panel;
38     
39     /** Creates new form PanelProjectLocationVisual */
40     public FeedReaderPanelVisual(FeedReaderWizardPanel panel) {
41         initComponents();
42         this.panel = panel;
43         // Register listener on the textFields to make the automatic updates
44
projectNameTextField.getDocument().addDocumentListener( this );
45         projectLocationTextField.getDocument().addDocumentListener( this );
46     }
47     
48     public String JavaDoc getProjectName() {
49         return this.projectNameTextField.getText();
50     }
51     
52     // <editor-fold defaultstate="collapsed" desc=" UI Code ">
53
private void 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         browseButton = 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.setLabelFor(projectNameTextField);
67         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
68         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
69         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
70         org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, NbBundle.getMessage(FeedReaderPanelVisual.class, "LBL_ProjectName"));
71         add(projectNameLabel, gridBagConstraints);
72         
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         
81         projectLocationLabel.setLabelFor(projectLocationTextField);
82         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
83         gridBagConstraints.gridy = 1;
84         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
85         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 0);
86         org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, NbBundle.getMessage(FeedReaderPanelVisual.class, "LBL_ProjectLocation"));
87         add(projectLocationLabel, gridBagConstraints);
88         
89         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
90         gridBagConstraints.gridy = 1;
91         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
92         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
93         gridBagConstraints.weightx = 1.0;
94         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 5, 0);
95         add(projectLocationTextField, gridBagConstraints);
96         
97         browseButton.setActionCommand("BROWSE");
98         browseButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
99             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
100                 browseLocationAction(evt);
101             }
102         });
103         
104         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
105         gridBagConstraints.gridy = 1;
106         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
107         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 5, 0);
108         org.openide.awt.Mnemonics.setLocalizedText(browseButton, NbBundle.getMessage(FeedReaderPanelVisual.class, "LBL_Browse"));
109         add(browseButton, gridBagConstraints);
110         
111         createdFolderLabel.setLabelFor(createdFolderTextField);
112         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
113         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
114         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
115         org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, NbBundle.getMessage(FeedReaderPanelVisual.class, "LBL_ProjectFolder"));
116         add(createdFolderLabel, gridBagConstraints);
117         
118         createdFolderTextField.setEditable(false);
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         gridBagConstraints.weightx = 1.0;
124         gridBagConstraints.weighty = 1.0;
125         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 0);
126         add(createdFolderTextField, gridBagConstraints);
127     }
128     // </editor-fold>
129

130     private void browseLocationAction(java.awt.event.ActionEvent JavaDoc evt) {
131         String JavaDoc command = evt.getActionCommand();
132         if ("BROWSE".equals(command)) { // NOI18N
133
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
134             FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
135             chooser.setDialogTitle(NbBundle.getMessage(FeedReaderPanelVisual.class, "LBL_SelectProjectLocation"));
136             chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
137             String JavaDoc path = this.projectLocationTextField.getText();
138             if (path.length() > 0) {
139                 File JavaDoc f = new File JavaDoc(path);
140                 if (f.exists()) {
141                     chooser.setSelectedFile(f);
142                 }
143             }
144             if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { //NOI18N
145
File JavaDoc projectDir = chooser.getSelectedFile();
146                 projectLocationTextField.setText( FileUtil.normalizeFile(projectDir).getAbsolutePath() );
147             }
148             panel.fireChangeEvent();
149         }
150     }
151     
152     public void addNotify() {
153         super.addNotify();
154         //same problem as in 31086, initial focus on Cancel button
155
projectNameTextField.requestFocus();
156     }
157     
158     boolean valid( WizardDescriptor wizardDescriptor ) {
159         if ( projectNameTextField.getText().length() == 0 ) {
160             wizardDescriptor.putProperty( "WizardPanel_errorMessage", // NOI18N
161
NbBundle.getMessage(FeedReaderPanelVisual.class, "MSG_InvalidProjectFolderName"));
162             return false; // Display name not specified
163
}
164         File JavaDoc f = FileUtil.normalizeFile(new File JavaDoc(projectLocationTextField.getText()).getAbsoluteFile());
165         if (!f.isDirectory()) {
166             wizardDescriptor.putProperty("WizardPanel_errorMessage", // NOI18N
167
NbBundle.getMessage(FeedReaderPanelVisual.class, "MSG_InvalidProjectFolderPath"));
168             return false;
169         }
170         final File JavaDoc destFolder = FileUtil.normalizeFile(new File JavaDoc(createdFolderTextField.getText()).getAbsoluteFile());
171         
172         File JavaDoc projLoc = destFolder;
173         while (projLoc != null && !projLoc.exists()) {
174             projLoc = projLoc.getParentFile();
175         }
176         if (projLoc == null || !projLoc.canWrite()) {
177             wizardDescriptor.putProperty( "WizardPanel_errorMessage", // NOI18N
178
NbBundle.getMessage(FeedReaderPanelVisual.class, "MSG_CanNotCreateProjectFolder"));
179             return false;
180         }
181         
182         if (FileUtil.toFileObject(projLoc) == null) {
183             wizardDescriptor.putProperty("WizardPanel_errorMessage", // NOI18N
184
NbBundle.getMessage(FeedReaderPanelVisual.class, "MSG_InvalidProjectFolderPath"));
185             return false;
186         }
187         
188         File JavaDoc[] kids = destFolder.listFiles();
189         if ( destFolder.exists() && kids != null && kids.length > 0) {
190             // Folder exists and is not empty
191
wizardDescriptor.putProperty( "WizardPanel_errorMessage", // NOI18N
192
NbBundle.getMessage(FeedReaderPanelVisual.class, "MSG_ProjectFolderAlreadyExists"));
193             return false;
194         }
195         wizardDescriptor.putProperty("WizardPanel_errorMessage", ""); // NOI18N
196
return true;
197     }
198     
199     void store( WizardDescriptor d ) {
200         String JavaDoc name = projectNameTextField.getText().trim();
201         String JavaDoc folder = createdFolderTextField.getText().trim();
202         
203         d.putProperty( /*XXX Define somewhere */ "projdir", new File JavaDoc( folder )); // NOI18N
204
d.putProperty( /*XXX Define somewhere */ "name", name ); // NOI18N
205
}
206     
207     void read(WizardDescriptor settings) {
208         File JavaDoc projectLocation = (File JavaDoc) settings.getProperty("projdir"); //NOI18N
209
if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) {
210             projectLocation = ProjectChooser.getProjectsFolder();
211         } else {
212             projectLocation = projectLocation.getParentFile();
213         }
214         this.projectLocationTextField.setText(projectLocation.getAbsolutePath());
215         
216         String JavaDoc projectName = (String JavaDoc) settings.getProperty("name"); //NOI18N
217
if (projectName == null) {
218             projectName = "feedreader-suite";
219         }
220         this.projectNameTextField.setText(projectName);
221         this.projectNameTextField.selectAll();
222     }
223     
224     void validate(WizardDescriptor d) throws WizardValidationException {
225         // nothing to validate
226
}
227     
228     // UI Variables declaration -
229
private javax.swing.JButton JavaDoc browseButton;
230     private javax.swing.JLabel JavaDoc createdFolderLabel;
231     private javax.swing.JTextField JavaDoc createdFolderTextField;
232     private javax.swing.JLabel JavaDoc projectLocationLabel;
233     private javax.swing.JTextField JavaDoc projectLocationTextField;
234     private javax.swing.JLabel JavaDoc projectNameLabel;
235     private javax.swing.JTextField JavaDoc projectNameTextField;
236     // End of variables declaration
237

238     // Implementation of DocumentListener --------------------------------------
239

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

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

275         }
276         panel.fireChangeEvent(); // Notify that the panel changed
277
}
278     
279 }
280
Popular Tags