001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.websvc.rest.samples.ui;
043:
044: import java.awt.GridBagConstraints;
045: import java.io.File;
046: import java.text.MessageFormat;
047: import javax.swing.JFileChooser;
048: import javax.swing.JLabel;
049: import javax.swing.JPanel;
050: import javax.swing.event.DocumentEvent;
051: import javax.swing.event.DocumentListener;
052: import javax.swing.text.Document;
053: import org.netbeans.spi.project.ui.support.ProjectChooser;
054: import org.openide.WizardDescriptor;
055: import org.openide.WizardValidationException;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.util.NbBundle;
058:
059: /**
060: * @author Peter Liu
061: */
062: public class SampleWizardPanelVisual extends JPanel implements
063: DocumentListener {
064: public static final String PROP_PROJECT_NAME = "projectName"; // NOI18N
065:
066: private SampleWizardPanel myPanel;
067: private int myType;
068:
069: /** Creates new form PanelProjectLocationVisual */
070: public SampleWizardPanelVisual(SampleWizardPanel panel) {
071: initComponents();
072: myPanel = panel;
073: // Register listener on the textFields to make the automatic updates
074: projectNameTextField.getDocument().addDocumentListener(this );
075: projectLocationTextField.getDocument()
076: .addDocumentListener(this );
077: }
078:
079: protected String getDefaultProjectName() {
080: return "RestSampleProject"; // NOI18N
081: }
082:
083: public String getProjectName() {
084: return this .projectNameTextField.getText();
085: }
086:
087: // <editor-fold defaultstate="collapsed" desc=" UI Code ">
088: private void initComponents() {
089: java.awt.GridBagConstraints gridBagConstraints;
090:
091: projectNameLabel = new javax.swing.JLabel();
092: projectNameTextField = new javax.swing.JTextField();
093: projectLocationLabel = new javax.swing.JLabel();
094: projectLocationTextField = new javax.swing.JTextField();
095: browseButton = new javax.swing.JButton();
096: createdFolderLabel = new javax.swing.JLabel();
097: createdFolderTextField = new javax.swing.JTextField();
098:
099: setLayout(new java.awt.GridBagLayout());
100:
101: projectNameLabel.setLabelFor(projectNameTextField);
102: gridBagConstraints = new java.awt.GridBagConstraints();
103: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
104: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
105: org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel,
106: NbBundle.getMessage(SampleWizardPanelVisual.class,
107: "LBL_ProjectName_Label"));
108: add(projectNameLabel, gridBagConstraints);
109:
110: gridBagConstraints = new java.awt.GridBagConstraints();
111: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
112: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
113: gridBagConstraints.weightx = 1.0;
114: gridBagConstraints.insets = new java.awt.Insets(0, 12, 12, 0);
115: add(projectNameTextField, gridBagConstraints);
116:
117: projectLocationLabel.setLabelFor(projectLocationTextField);
118: gridBagConstraints = new java.awt.GridBagConstraints();
119: gridBagConstraints.gridy = 1;
120: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
121: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
122: org.openide.awt.Mnemonics.setLocalizedText(
123: projectLocationLabel, NbBundle.getMessage(
124: SampleWizardPanelVisual.class,
125: "LBL_ProjectLocation_Label"));
126: add(projectLocationLabel, gridBagConstraints);
127:
128: gridBagConstraints = new java.awt.GridBagConstraints();
129: gridBagConstraints.gridy = 1;
130: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
131: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
132: gridBagConstraints.weightx = 1.0;
133: gridBagConstraints.insets = new java.awt.Insets(0, 12, 5, 0);
134: add(projectLocationTextField, gridBagConstraints);
135:
136: browseButton.setActionCommand("BROWSE"); //NOI18N
137: browseButton
138: .addActionListener(new java.awt.event.ActionListener() {
139: public void actionPerformed(
140: java.awt.event.ActionEvent evt) {
141: browseLocationAction(evt);
142: }
143: });
144:
145: gridBagConstraints = new java.awt.GridBagConstraints();
146: gridBagConstraints.gridy = 1;
147: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
148: gridBagConstraints.insets = new java.awt.Insets(0, 6, 5, 0);
149: org.openide.awt.Mnemonics.setLocalizedText(browseButton,
150: NbBundle.getMessage(SampleWizardPanelVisual.class,
151: "LBL_BrowseLocation_Button"));
152: add(browseButton, gridBagConstraints);
153:
154: createdFolderLabel.setLabelFor(createdFolderTextField);
155: gridBagConstraints = new java.awt.GridBagConstraints();
156: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
157: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
158: org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel,
159: NbBundle.getMessage(SampleWizardPanelVisual.class,
160: "LBL_CreatedProjectFolder_Lablel"));
161: add(createdFolderLabel, gridBagConstraints);
162:
163: createdFolderTextField.setEditable(false);
164: gridBagConstraints = new java.awt.GridBagConstraints();
165: gridBagConstraints.gridy = 2;
166: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
167: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
168: gridBagConstraints.insets = new java.awt.Insets(0, 12, 12, 0);
169: add(createdFolderTextField, gridBagConstraints);
170:
171: initAdditionalComponents();
172: }
173:
174: // </editor-fold>
175:
176: protected void initAdditionalComponents() {
177: JLabel specialLabel = new javax.swing.JLabel();
178: specialLabel.setFocusable(false);
179:
180: GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
181: gridBagConstraints.gridx = 0;
182: gridBagConstraints.gridy = 3;
183: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
184: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
185: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
186: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
187: gridBagConstraints.weightx = 1.0;
188: gridBagConstraints.weighty = 1.0;
189: gridBagConstraints.insets = new java.awt.Insets(0, 6, 0, 0);
190: add(specialLabel, gridBagConstraints);
191: }
192:
193: private void browseLocationAction(java.awt.event.ActionEvent evt) {
194: String command = evt.getActionCommand();
195: if ("BROWSE".equals(command)) { // NOI18N
196: JFileChooser chooser = new JFileChooser();
197: FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
198: chooser.setDialogTitle(NbBundle.getMessage(
199: SampleWizardPanelVisual.class,
200: "LBL_SelectProjectLocation"));
201: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
202: String path = this .projectLocationTextField.getText();
203: if (path.length() > 0) {
204: File f = new File(path);
205: if (f.exists()) {
206: chooser.setSelectedFile(f);
207: }
208: }
209: if (JFileChooser.APPROVE_OPTION == chooser
210: .showOpenDialog(this )) {
211: File projectDir = chooser.getSelectedFile();
212: projectLocationTextField.setText(FileUtil
213: .normalizeFile(projectDir).getAbsolutePath());
214: }
215: myPanel.fireChangeEvent();
216: }
217: }
218:
219: public void addNotify() {
220: super .addNotify();
221: //same problem as in 31086, initial focus on Cancel button
222: projectNameTextField.requestFocus();
223: }
224:
225: protected boolean valid(WizardDescriptor wizardDescriptor) {
226:
227: if (projectNameTextField.getText().length() == 0) {
228: wizardDescriptor.putProperty("WizardPanel_errorMessage", //NOI18N
229: NbBundle.getMessage(SampleWizardPanelVisual.class,
230: "MSG_IllegalProjectName"));
231: return false; // Display name not specified
232: }
233: File f = FileUtil.normalizeFile(new File(
234: projectLocationTextField.getText()).getAbsoluteFile());
235: if (!f.isDirectory()) {
236: String message = NbBundle.getMessage(
237: SampleWizardPanelVisual.class,
238: "MSG_IllegalProjectLocation");
239: wizardDescriptor.putProperty("WizardPanel_errorMessage",
240: message); // NOI18N
241: return false;
242: }
243: final File destFolder = FileUtil.normalizeFile(new File(
244: createdFolderTextField.getText()).getAbsoluteFile());
245:
246: File projLoc = destFolder;
247: while (projLoc != null && !projLoc.exists()) {
248: projLoc = projLoc.getParentFile();
249: }
250: if (projLoc == null || !projLoc.canWrite()) {
251: wizardDescriptor.putProperty("WizardPanel_errorMessage", // NOI18N
252: NbBundle.getMessage(SampleWizardPanelVisual.class,
253: "MSG_ProjectFolderReadOnly"));
254: return false;
255: }
256:
257: if (FileUtil.toFileObject(projLoc) == null) {
258: String message = NbBundle.getMessage(
259: SampleWizardPanelVisual.class,
260: "MSG_IllegalProjectLocation");
261: wizardDescriptor.putProperty("WizardPanel_errorMessage",
262: message); // NOI18N
263: return false;
264: }
265:
266: // File locFolder = FileUtil.normalizeFile(new File(projectLocationTextField.getText()).getAbsoluteFile());
267: // if(locFolder != null && locFolder.listFiles()!=null && locFolder.listFiles().length != 0) {
268: // String message = NbBundle.getMessage(SampleWizardPanelVisual.class, "MSG_ProjectLocationNotEmpty");
269: // wizardDescriptor.putProperty("WizardPanel_errorMessage", message); // NOI18N
270: // return false;
271: // }
272:
273: File[] kids = destFolder.listFiles();
274: if (destFolder.exists() && kids != null && kids.length > 0) {
275: // Folder exists and is not empty
276: wizardDescriptor.putProperty("WizardPanel_errorMessage", // NOI18N
277: NbBundle.getMessage(SampleWizardPanelVisual.class,
278: "MSG_ProjectFolderExists"));
279: return false;
280: }
281: wizardDescriptor.putProperty("WizardPanel_errorMessage", ""); // NOI18N
282: return true;
283: }
284:
285: protected void store(WizardDescriptor d) {
286:
287: String name = projectNameTextField.getText().trim();
288: String location = projectLocationTextField.getText().trim();
289: String folder = createdFolderTextField.getText().trim();
290:
291: d.putProperty(SampleWizardIterator.PROJDIR, new File(folder));
292: d.putProperty(SampleWizardIterator.NAME, name);
293: }
294:
295: protected void read(WizardDescriptor settings) {
296: File projectLocation = (File) settings
297: .getProperty(SampleWizardIterator.PROJDIR);
298: if (projectLocation == null
299: || projectLocation.getParentFile() == null
300: || !projectLocation.getParentFile().isDirectory()) {
301: projectLocation = ProjectChooser.getProjectsFolder();
302: } else {
303: projectLocation = projectLocation.getParentFile();
304: }
305: this .projectLocationTextField.setText(projectLocation
306: .getAbsolutePath());
307:
308: String projectName = (String) settings
309: .getProperty(SampleWizardIterator.NAME);
310: if (projectName == null) {
311: projectName = getDefaultProjectName();
312:
313: File file = new File(projectLocation, projectName);
314: int index1 = 1;
315: if (file.exists()) {
316: while (file.exists()) {
317: file = new File(projectLocation, projectName
318: + String.valueOf(index1));
319: index1++;
320: }
321: projectName = projectName + String.valueOf(index1 - 1);
322: }
323:
324: }
325: this .projectNameTextField.setText(projectName);
326: this .projectNameTextField.selectAll();
327: }
328:
329: protected void validate(WizardDescriptor d)
330: throws WizardValidationException {
331: // nothing to validate
332: }
333:
334: // UI Variables declaration -
335: private javax.swing.JButton browseButton;
336: private javax.swing.JLabel createdFolderLabel;
337: private javax.swing.JTextField createdFolderTextField;
338: private javax.swing.JLabel projectLocationLabel;
339: private javax.swing.JTextField projectLocationTextField;
340: private javax.swing.JLabel projectNameLabel;
341: private javax.swing.JTextField projectNameTextField;
342:
343: // private javax.swing.JTextArea projectNoteArea;
344: // private javax.swing.JTextPane projectNotePane;
345: // End of variables declaration
346:
347: // Private methods ---------------------------------------------------------
348:
349: private static JFileChooser createChooser() {
350: JFileChooser chooser = new JFileChooser();
351: FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
352: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
353: chooser.setAcceptAllFileFilterUsed(false);
354: chooser.setName(NbBundle.getMessage(
355: SampleWizardPanelVisual.class,
356: "LBL_SelectProjectDirectory"));
357: return chooser;
358: }
359:
360: private String validFreeProjectName(final File parentFolder,
361: final String formater, final int index) {
362: String name = MessageFormat.format(formater,
363: new Object[] { new Integer(index) });
364: File file = new File(parentFolder, name);
365: return file.exists() ? null : name;
366: }
367:
368: // Implementation of DocumentListener --------------------------------------
369:
370: public void changedUpdate(DocumentEvent e) {
371: updateTexts(e);
372: if (this .projectNameTextField.getDocument() == e.getDocument()) {
373: firePropertyChange(PROP_PROJECT_NAME, null,
374: this .projectNameTextField.getText());
375: }
376: }
377:
378: public void insertUpdate(DocumentEvent e) {
379: updateTexts(e);
380: if (this .projectNameTextField.getDocument() == e.getDocument()) {
381: firePropertyChange(PROP_PROJECT_NAME, null,
382: this .projectNameTextField.getText());
383: }
384: }
385:
386: public void removeUpdate(DocumentEvent e) {
387: updateTexts(e);
388: if (this .projectNameTextField.getDocument() == e.getDocument()) {
389: firePropertyChange(PROP_PROJECT_NAME, null,
390: this .projectNameTextField.getText());
391: }
392: }
393:
394: /** Handles changes in the Project name and project directory
395: */
396: private void updateTexts(DocumentEvent e) {
397:
398: Document doc = e.getDocument();
399:
400: if (doc == projectNameTextField.getDocument()
401: || doc == projectLocationTextField.getDocument()) {
402: // Change in the project name
403:
404: String projectName = projectNameTextField.getText();
405: String projectFolder = projectLocationTextField.getText();
406:
407: //if ( projectFolder.trim().length() == 0 || projectFolder.equals( oldName ) ) {
408: createdFolderTextField.setText(projectFolder
409: + File.separatorChar + projectName);
410: //}
411:
412: }
413: myPanel.fireChangeEvent(); // Notify that the panel changed
414: }
415: }
|