Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* The Original Software is NetBeans. The Initial Developer of the Original |
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2010 Sun |
28 |
* Microsystems, Inc. All Rights Reserved. |
29 |
* |
30 |
* If you wish your version of this file to be governed by only the CDDL |
31 |
* or only the GPL Version 2, indicate your decision by adding |
32 |
* "[Contributor] elects to include this software in this distribution |
33 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
34 |
* single choice of license, a recipient has the option to distribute |
35 |
* your version of this file under either the CDDL, the GPL Version 2 or |
36 |
* to extend the choice of license to its licensees as provided above. |
37 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
38 |
* Version 2 license, then the option applies only if the new code is |
39 |
* made subject to such option by the copyright holder. |
40 |
*/ |
41 |
package org.netbeans.modules.apisupport.equinoxdemo; |
42 |
|
43 |
import java.io.File; |
44 |
import javax.swing.JFileChooser; |
45 |
import javax.swing.JPanel; |
46 |
import javax.swing.event.DocumentEvent; |
47 |
import javax.swing.event.DocumentListener; |
48 |
import javax.swing.text.Document; |
49 |
import org.netbeans.spi.project.ui.support.ProjectChooser; |
50 |
import org.openide.WizardDescriptor; |
51 |
import org.openide.WizardValidationException; |
52 |
import org.openide.filesystems.FileUtil; |
53 |
|
54 |
public class SampleAppPanelVisual extends JPanel implements DocumentListener { |
55 |
|
56 |
private static final String WIZARD_PANEL_ERROR_MESSAGE = WizardDescriptor.PROP_ERROR_MESSAGE; // NOI18N |
57 |
|
58 |
public static final String PROP_PROJECT_NAME = "projectName"; // NOI18N |
59 |
|
60 |
private SampleAppWizardPanel panel; |
61 |
|
62 |
/** Creates new form PanelProjectLocationVisual */ |
63 |
public SampleAppPanelVisual(SampleAppWizardPanel panel) { |
64 |
initComponents(); |
65 |
this.panel = panel; |
66 |
// Register listener on the textFields to make the automatic updates |
67 |
projectNameTextField.getDocument().addDocumentListener(this); |
68 |
projectLocationTextField.getDocument().addDocumentListener(this); |
69 |
} |
70 |
|
71 |
|
72 |
public String getProjectName() { |
73 |
return this.projectNameTextField.getText(); |
74 |
} |
75 |
|
76 |
/** This method is called from within the constructor to |
77 |
* initialize the form. |
78 |
* WARNING: Do NOT modify this code. The content of this method is |
79 |
* always regenerated by the Form Editor. |
80 |
*/ |
81 |
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
82 |
private void initComponents() { |
83 |
|
84 |
projectNameLabel = new javax.swing.JLabel(); |
85 |
projectNameTextField = new javax.swing.JTextField(); |
86 |
projectLocationLabel = new javax.swing.JLabel(); |
87 |
projectLocationTextField = new javax.swing.JTextField(); |
88 |
browseButton = new javax.swing.JButton(); |
89 |
createdFolderLabel = new javax.swing.JLabel(); |
90 |
createdFolderTextField = new javax.swing.JTextField(); |
91 |
|
92 |
projectNameLabel.setLabelFor(projectNameTextField); |
93 |
org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, org.openide.util.NbBundle.getMessage(SampleAppPanelVisual.class, "SampleAppPanelVisual.project_name")); // NOI18N |
94 |
|
95 |
projectLocationLabel.setLabelFor(projectLocationTextField); |
96 |
org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, org.openide.util.NbBundle.getMessage(SampleAppPanelVisual.class, "SampleAppPanelVisual.project_location")); // NOI18N |
97 |
|
98 |
org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(SampleAppPanelVisual.class, "SampleAppPanelVisual.browse")); // NOI18N |
99 |
browseButton.addActionListener(new java.awt.event.ActionListener() { |
100 |
public void actionPerformed(java.awt.event.ActionEvent evt) { |
101 |
browseButtonActionPerformed(evt); |
102 |
} |
103 |
}); |
104 |
|
105 |
createdFolderLabel.setLabelFor(createdFolderTextField); |
106 |
org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, org.openide.util.NbBundle.getMessage(SampleAppPanelVisual.class, "SampleAppPanelVisual.project_folder")); // NOI18N |
107 |
|
108 |
createdFolderTextField.setEditable(false); |
109 |
|
110 |
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); |
111 |
this.setLayout(layout); |
112 |
layout.setHorizontalGroup( |
113 |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
114 |
.addGroup(layout.createSequentialGroup() |
115 |
.addContainerGap() |
116 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
117 |
.addComponent(projectNameLabel) |
118 |
.addComponent(projectLocationLabel) |
119 |
.addComponent(createdFolderLabel)) |
120 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
121 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
122 |
.addComponent(projectNameTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE) |
123 |
.addComponent(projectLocationTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE) |
124 |
.addComponent(createdFolderTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)) |
125 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
126 |
.addComponent(browseButton) |
127 |
.addContainerGap()) |
128 |
); |
129 |
layout.setVerticalGroup( |
130 |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
131 |
.addGroup(layout.createSequentialGroup() |
132 |
.addContainerGap() |
133 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) |
134 |
.addComponent(projectNameLabel) |
135 |
.addComponent(projectNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) |
136 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
137 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) |
138 |
.addComponent(projectLocationLabel) |
139 |
.addComponent(projectLocationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) |
140 |
.addComponent(browseButton)) |
141 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
142 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) |
143 |
.addComponent(createdFolderLabel) |
144 |
.addComponent(createdFolderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) |
145 |
.addContainerGap(213, Short.MAX_VALUE)) |
146 |
); |
147 |
}// </editor-fold>//GEN-END:initComponents |
148 |
|
149 |
private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed |
150 |
JFileChooser chooser = new JFileChooser(); |
151 |
FileUtil.preventFileChooserSymlinkTraversal(chooser, null); |
152 |
chooser.setDialogTitle(org.openide.util.NbBundle.getMessage(SampleAppPanelVisual.class, "SampleAppPanelVisual.select_project_location")); |
153 |
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
154 |
String path = this.projectLocationTextField.getText(); |
155 |
if (path.length() > 0) { |
156 |
File f = new File(path); |
157 |
if (f.exists()) { |
158 |
chooser.setSelectedFile(f); |
159 |
} |
160 |
} |
161 |
if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { |
162 |
File projectDir = chooser.getSelectedFile(); |
163 |
projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath()); |
164 |
} |
165 |
panel.fireChangeEvent(); |
166 |
|
167 |
}//GEN-LAST:event_browseButtonActionPerformed |
168 |
|
169 |
|
170 |
// Variables declaration - do not modify//GEN-BEGIN:variables |
171 |
private javax.swing.JButton browseButton; |
172 |
private javax.swing.JLabel createdFolderLabel; |
173 |
private javax.swing.JTextField createdFolderTextField; |
174 |
private javax.swing.JLabel projectLocationLabel; |
175 |
private javax.swing.JTextField projectLocationTextField; |
176 |
private javax.swing.JLabel projectNameLabel; |
177 |
private javax.swing.JTextField projectNameTextField; |
178 |
// End of variables declaration//GEN-END:variables |
179 |
|
180 |
@Override |
181 |
public void addNotify() { |
182 |
super.addNotify(); |
183 |
//same problem as in 31086, initial focus on Cancel button |
184 |
projectNameTextField.requestFocus(); |
185 |
} |
186 |
|
187 |
boolean valid(WizardDescriptor wizardDescriptor) { |
188 |
|
189 |
if (projectNameTextField.getText().length() == 0) { |
190 |
wizardDescriptor.putProperty(WIZARD_PANEL_ERROR_MESSAGE, |
191 |
org.openide.util.NbBundle.getMessage(SampleAppPanelVisual.class, "SampleAppPanelVisual.invalid_folder_name")); |
192 |
return false; // Display name not specified |
193 |
} |
194 |
File f = FileUtil.normalizeFile(new File(projectLocationTextField.getText()).getAbsoluteFile()); |
195 |
if (!f.isDirectory()) { |
196 |
String message = org.openide.util.NbBundle.getMessage(SampleAppPanelVisual.class, "SampleAppPanelVisual.invalid_path"); |
197 |
wizardDescriptor.putProperty(WIZARD_PANEL_ERROR_MESSAGE, message); |
198 |
return false; |
199 |
} |
200 |
final File destFolder = FileUtil.normalizeFile(new File(createdFolderTextField.getText()).getAbsoluteFile()); |
201 |
|
202 |
File projLoc = destFolder; |
203 |
while (projLoc != null && !projLoc.exists()) { |
204 |
projLoc = projLoc.getParentFile(); |
205 |
} |
206 |
if (projLoc == null || !projLoc.canWrite()) { |
207 |
wizardDescriptor.putProperty(WIZARD_PANEL_ERROR_MESSAGE, |
208 |
org.openide.util.NbBundle.getMessage(SampleAppPanelVisual.class, "SampleAppPanelVisual.folder_creation_error")); |
209 |
return false; |
210 |
} |
211 |
|
212 |
if (FileUtil.toFileObject(projLoc) == null) { |
213 |
String message = org.openide.util.NbBundle.getMessage(SampleAppPanelVisual.class, "SampleAppPanelVisual.invalid_path"); |
214 |
wizardDescriptor.putProperty(WIZARD_PANEL_ERROR_MESSAGE, message); |
215 |
return false; |
216 |
} |
217 |
|
218 |
File[] kids = destFolder.listFiles(); |
219 |
if (destFolder.exists() && kids != null && kids.length > 0) { |
220 |
// Folder exists and is not empty |
221 |
wizardDescriptor.putProperty(WIZARD_PANEL_ERROR_MESSAGE, |
222 |
org.openide.util.NbBundle.getMessage(SampleAppPanelVisual.class, "SampleAppPanelVisual.folder_exists")); |
223 |
return false; |
224 |
} |
225 |
wizardDescriptor.putProperty(WIZARD_PANEL_ERROR_MESSAGE, ""); |
226 |
return true; |
227 |
} |
228 |
|
229 |
void store(WizardDescriptor d) { |
230 |
String name = projectNameTextField.getText().trim(); |
231 |
String folder = createdFolderTextField.getText().trim(); |
232 |
|
233 |
d.putProperty("projdir", new File(folder)); // NOI18N |
234 |
d.putProperty("name", name); // NOI18N |
235 |
} |
236 |
|
237 |
void read(WizardDescriptor settings) { |
238 |
File projectLocation = (File) settings.getProperty("projdir"); // NOI18N |
239 |
if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) { |
240 |
projectLocation = ProjectChooser.getProjectsFolder(); |
241 |
} else { |
242 |
projectLocation = projectLocation.getParentFile(); |
243 |
} |
244 |
this.projectLocationTextField.setText(projectLocation.getAbsolutePath()); |
245 |
|
246 |
String projectName = (String) settings.getProperty("name"); // NOI18N |
247 |
if(projectName == null) { |
248 |
projectName = org.openide.util.NbBundle.getMessage(SampleAppPanelVisual.class, "SampleAppPanelVisual.default_prj_name"); |
249 |
} |
250 |
this.projectNameTextField.setText(projectName); |
251 |
this.projectNameTextField.selectAll(); |
252 |
} |
253 |
|
254 |
void validate(WizardDescriptor d) throws WizardValidationException { |
255 |
// nothing to validate |
256 |
} |
257 |
|
258 |
// Implementation of DocumentListener -------------------------------------- |
259 |
|
260 |
public void changedUpdate(DocumentEvent e) { |
261 |
updateTexts(e); |
262 |
if (this.projectNameTextField.getDocument() == e.getDocument()) { |
263 |
firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); |
264 |
} |
265 |
} |
266 |
|
267 |
public void insertUpdate(DocumentEvent e) { |
268 |
updateTexts(e); |
269 |
if (this.projectNameTextField.getDocument() == e.getDocument()) { |
270 |
firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); |
271 |
} |
272 |
} |
273 |
|
274 |
public void removeUpdate(DocumentEvent e) { |
275 |
updateTexts(e); |
276 |
if (this.projectNameTextField.getDocument() == e.getDocument()) { |
277 |
firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); |
278 |
} |
279 |
} |
280 |
|
281 |
/** Handles changes in the Project name and project directory, */ |
282 |
private void updateTexts(DocumentEvent e) { |
283 |
|
284 |
Document doc = e.getDocument(); |
285 |
|
286 |
if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) { |
287 |
// Change in the project name |
288 |
|
289 |
String projectName = projectNameTextField.getText(); |
290 |
String projectFolder = projectLocationTextField.getText(); |
291 |
|
292 |
//if (projectFolder.trim().length() == 0 || projectFolder.equals(oldName)) { |
293 |
createdFolderTextField.setText(projectFolder + File.separatorChar + projectName); |
294 |
//} |
295 |
|
296 |
} |
297 |
panel.fireChangeEvent(); // Notify that the panel changed |
298 |
} |
299 |
|
300 |
} |