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.identity.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 Vitaly Bychkov
061: * @version 22 January 2006
062: */
063: public class SampleWizardPanelVisual extends JPanel implements
064: DocumentListener {
065: public static final String PROP_PROJECT_NAME = "projectName"; // NOI18N
066:
067: private SampleWizardPanel myPanel;
068: private int myType;
069:
070: /** Creates new form PanelProjectLocationVisual */
071: public SampleWizardPanelVisual(SampleWizardPanel panel) {
072: initComponents();
073: myPanel = panel;
074: // Register listener on the textFields to make the automatic updates
075: projectNameTextField.getDocument().addDocumentListener(this );
076: projectLocationTextField.getDocument()
077: .addDocumentListener(this );
078: }
079:
080: protected String getDefaultProjectName() {
081: return "IdentitySampleProject"; // NOI18N
082: }
083:
084: public String getProjectName() {
085: return this .projectNameTextField.getText();
086: }
087:
088: // <editor-fold defaultstate="collapsed" desc=" UI Code ">
089: private void initComponents() {
090: java.awt.GridBagConstraints gridBagConstraints;
091:
092: projectNameLabel = new javax.swing.JLabel();
093: projectNameTextField = new javax.swing.JTextField();
094: projectLocationLabel = new javax.swing.JLabel();
095: projectLocationTextField = new javax.swing.JTextField();
096: browseButton = new javax.swing.JButton();
097: createdFolderLabel = new javax.swing.JLabel();
098: createdFolderTextField = new javax.swing.JTextField();
099:
100: setLayout(new java.awt.GridBagLayout());
101:
102: projectNameLabel.setLabelFor(projectNameTextField);
103: gridBagConstraints = new java.awt.GridBagConstraints();
104: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
105: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
106: org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel,
107: NbBundle.getMessage(SampleWizardPanelVisual.class,
108: "LBL_ProjectName_Label"));
109: add(projectNameLabel, gridBagConstraints);
110:
111: gridBagConstraints = new java.awt.GridBagConstraints();
112: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
113: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
114: gridBagConstraints.weightx = 1.0;
115: gridBagConstraints.insets = new java.awt.Insets(0, 12, 12, 0);
116: add(projectNameTextField, gridBagConstraints);
117:
118: projectLocationLabel.setLabelFor(projectLocationTextField);
119: gridBagConstraints = new java.awt.GridBagConstraints();
120: gridBagConstraints.gridy = 1;
121: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
122: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
123: org.openide.awt.Mnemonics.setLocalizedText(
124: projectLocationLabel, NbBundle.getMessage(
125: SampleWizardPanelVisual.class,
126: "LBL_ProjectLocation_Label"));
127: add(projectLocationLabel, gridBagConstraints);
128:
129: gridBagConstraints = new java.awt.GridBagConstraints();
130: gridBagConstraints.gridy = 1;
131: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
132: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
133: gridBagConstraints.weightx = 1.0;
134: gridBagConstraints.insets = new java.awt.Insets(0, 12, 5, 0);
135: add(projectLocationTextField, gridBagConstraints);
136:
137: browseButton.setActionCommand("BROWSE"); //NOI18N
138: browseButton
139: .addActionListener(new java.awt.event.ActionListener() {
140: public void actionPerformed(
141: java.awt.event.ActionEvent evt) {
142: browseLocationAction(evt);
143: }
144: });
145:
146: gridBagConstraints = new java.awt.GridBagConstraints();
147: gridBagConstraints.gridy = 1;
148: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
149: gridBagConstraints.insets = new java.awt.Insets(0, 6, 5, 0);
150: org.openide.awt.Mnemonics.setLocalizedText(browseButton,
151: NbBundle.getMessage(SampleWizardPanelVisual.class,
152: "LBL_BrowseLocation_Button"));
153: add(browseButton, gridBagConstraints);
154:
155: createdFolderLabel.setLabelFor(createdFolderTextField);
156: gridBagConstraints = new java.awt.GridBagConstraints();
157: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
158: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
159: org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel,
160: NbBundle.getMessage(SampleWizardPanelVisual.class,
161: "LBL_CreatedProjectFolder_Lablel"));
162: add(createdFolderLabel, gridBagConstraints);
163:
164: createdFolderTextField.setEditable(false);
165: gridBagConstraints = new java.awt.GridBagConstraints();
166: gridBagConstraints.gridy = 2;
167: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
168: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
169: gridBagConstraints.insets = new java.awt.Insets(0, 12, 12, 0);
170: add(createdFolderTextField, gridBagConstraints);
171:
172: initAdditionalComponents();
173: }
174:
175: // </editor-fold>
176:
177: protected void initAdditionalComponents() {
178: JLabel specialLabel = new javax.swing.JLabel();
179: specialLabel.setFocusable(false);
180:
181: GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
182: gridBagConstraints.gridx = 0;
183: gridBagConstraints.gridy = 3;
184: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
185: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
186: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
187: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
188: gridBagConstraints.weightx = 1.0;
189: gridBagConstraints.weighty = 1.0;
190: gridBagConstraints.insets = new java.awt.Insets(0, 6, 0, 0);
191: add(specialLabel, gridBagConstraints);
192: }
193:
194: private void browseLocationAction(java.awt.event.ActionEvent evt) {
195: String command = evt.getActionCommand();
196: if ("BROWSE".equals(command)) { // NOI18N
197: JFileChooser chooser = new JFileChooser();
198: FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
199: chooser.setDialogTitle(NbBundle.getMessage(
200: SampleWizardPanelVisual.class,
201: "LBL_SelectProjectLocation"));
202: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
203: String path = this .projectLocationTextField.getText();
204: if (path.length() > 0) {
205: File f = new File(path);
206: if (f.exists()) {
207: chooser.setSelectedFile(f);
208: }
209: }
210: if (JFileChooser.APPROVE_OPTION == chooser
211: .showOpenDialog(this )) {
212: File projectDir = chooser.getSelectedFile();
213: projectLocationTextField.setText(FileUtil
214: .normalizeFile(projectDir).getAbsolutePath());
215: }
216: myPanel.fireChangeEvent();
217: }
218: }
219:
220: public void addNotify() {
221: super .addNotify();
222: //same problem as in 31086, initial focus on Cancel button
223: projectNameTextField.requestFocus();
224: }
225:
226: protected boolean valid(WizardDescriptor wizardDescriptor) {
227:
228: if (projectNameTextField.getText().length() == 0) {
229: wizardDescriptor.putProperty("WizardPanel_errorMessage", //NOI18N
230: NbBundle.getMessage(SampleWizardPanelVisual.class,
231: "MSG_IllegalProjectName"));
232: return false; // Display name not specified
233: }
234: File f = FileUtil.normalizeFile(new File(
235: projectLocationTextField.getText()).getAbsoluteFile());
236: if (!f.isDirectory()) {
237: String message = NbBundle.getMessage(
238: SampleWizardPanelVisual.class,
239: "MSG_IllegalProjectLocation");
240: wizardDescriptor.putProperty("WizardPanel_errorMessage",
241: message); // NOI18N
242: return false;
243: }
244: final File destFolder = FileUtil.normalizeFile(new File(
245: createdFolderTextField.getText()).getAbsoluteFile());
246:
247: File projLoc = destFolder;
248: while (projLoc != null && !projLoc.exists()) {
249: projLoc = projLoc.getParentFile();
250: }
251: if (projLoc == null || !projLoc.canWrite()) {
252: wizardDescriptor.putProperty("WizardPanel_errorMessage", // NOI18N
253: NbBundle.getMessage(SampleWizardPanelVisual.class,
254: "MSG_ProjectFolderReadOnly"));
255: return false;
256: }
257:
258: if (FileUtil.toFileObject(projLoc) == null) {
259: String message = NbBundle.getMessage(
260: SampleWizardPanelVisual.class,
261: "MSG_IllegalProjectLocation");
262: wizardDescriptor.putProperty("WizardPanel_errorMessage",
263: message); // NOI18N
264: return false;
265: }
266:
267: // File locFolder = FileUtil.normalizeFile(new File(projectLocationTextField.getText()).getAbsoluteFile());
268: // if(locFolder != null && locFolder.listFiles()!=null && locFolder.listFiles().length != 0) {
269: // String message = NbBundle.getMessage(SampleWizardPanelVisual.class, "MSG_ProjectLocationNotEmpty");
270: // wizardDescriptor.putProperty("WizardPanel_errorMessage", message); // NOI18N
271: // return false;
272: // }
273:
274: File[] kids = destFolder.listFiles();
275: if (destFolder.exists() && kids != null && kids.length > 0) {
276: // Folder exists and is not empty
277: wizardDescriptor.putProperty("WizardPanel_errorMessage", // NOI18N
278: NbBundle.getMessage(SampleWizardPanelVisual.class,
279: "MSG_ProjectFolderExists"));
280: return false;
281: }
282: wizardDescriptor.putProperty("WizardPanel_errorMessage", ""); // NOI18N
283: return true;
284: }
285:
286: protected void store(WizardDescriptor d) {
287:
288: String name = projectNameTextField.getText().trim();
289: String location = projectLocationTextField.getText().trim();
290: String folder = createdFolderTextField.getText().trim();
291:
292: d.putProperty(SampleWizardIterator.PROJDIR, new File(folder));
293: d.putProperty(SampleWizardIterator.NAME, name);
294: }
295:
296: protected void read(WizardDescriptor settings) {
297: File projectLocation = (File) settings
298: .getProperty(SampleWizardIterator.PROJDIR);
299: if (projectLocation == null
300: || projectLocation.getParentFile() == null
301: || !projectLocation.getParentFile().isDirectory()) {
302: projectLocation = ProjectChooser.getProjectsFolder();
303: } else {
304: projectLocation = projectLocation.getParentFile();
305: }
306: this .projectLocationTextField.setText(projectLocation
307: .getAbsolutePath());
308:
309: String projectName = (String) settings
310: .getProperty(SampleWizardIterator.NAME);
311: if (projectName == null) {
312: projectName = getDefaultProjectName();
313:
314: File file = new File(projectLocation, projectName);
315: int index1 = 1;
316: if (file.exists()) {
317: while (file.exists()) {
318: file = new File(projectLocation, projectName
319: + String.valueOf(index1));
320: index1++;
321: }
322: projectName = projectName + String.valueOf(index1 - 1);
323: }
324:
325: }
326: this .projectNameTextField.setText(projectName);
327: this .projectNameTextField.selectAll();
328: }
329:
330: protected void validate(WizardDescriptor d)
331: throws WizardValidationException {
332: // nothing to validate
333: }
334:
335: // UI Variables declaration -
336: private javax.swing.JButton browseButton;
337: private javax.swing.JLabel createdFolderLabel;
338: private javax.swing.JTextField createdFolderTextField;
339: private javax.swing.JLabel projectLocationLabel;
340: private javax.swing.JTextField projectLocationTextField;
341: private javax.swing.JLabel projectNameLabel;
342: private javax.swing.JTextField projectNameTextField;
343:
344: // private javax.swing.JTextArea projectNoteArea;
345: // private javax.swing.JTextPane projectNotePane;
346: // End of variables declaration
347:
348: // Private methods ---------------------------------------------------------
349:
350: private static JFileChooser createChooser() {
351: JFileChooser chooser = new JFileChooser();
352: FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
353: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
354: chooser.setAcceptAllFileFilterUsed(false);
355: chooser.setName(NbBundle.getMessage(
356: SampleWizardPanelVisual.class,
357: "LBL_SelectProjectDirectory"));
358: return chooser;
359: }
360:
361: private String validFreeProjectName(final File parentFolder,
362: final String formater, final int index) {
363: String name = MessageFormat.format(formater,
364: new Object[] { new Integer(index) });
365: File file = new File(parentFolder, name);
366: return file.exists() ? null : name;
367: }
368:
369: // Implementation of DocumentListener --------------------------------------
370:
371: public void changedUpdate(DocumentEvent e) {
372: updateTexts(e);
373: if (this .projectNameTextField.getDocument() == e.getDocument()) {
374: firePropertyChange(PROP_PROJECT_NAME, null,
375: this .projectNameTextField.getText());
376: }
377: }
378:
379: public void insertUpdate(DocumentEvent e) {
380: updateTexts(e);
381: if (this .projectNameTextField.getDocument() == e.getDocument()) {
382: firePropertyChange(PROP_PROJECT_NAME, null,
383: this .projectNameTextField.getText());
384: }
385: }
386:
387: public void removeUpdate(DocumentEvent e) {
388: updateTexts(e);
389: if (this .projectNameTextField.getDocument() == e.getDocument()) {
390: firePropertyChange(PROP_PROJECT_NAME, null,
391: this .projectNameTextField.getText());
392: }
393: }
394:
395: /** Handles changes in the Project name and project directory
396: */
397: private void updateTexts(DocumentEvent e) {
398:
399: Document doc = e.getDocument();
400:
401: if (doc == projectNameTextField.getDocument()
402: || doc == projectLocationTextField.getDocument()) {
403: // Change in the project name
404:
405: String projectName = projectNameTextField.getText();
406: String projectFolder = projectLocationTextField.getText();
407:
408: //if ( projectFolder.trim().length() == 0 || projectFolder.equals( oldName ) ) {
409: createdFolderTextField.setText(projectFolder
410: + File.separatorChar + projectName);
411: //}
412:
413: }
414: myPanel.fireChangeEvent(); // Notify that the panel changed
415: }
416: }
|