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.jmx.examples;
043:
044: import java.io.File;
045: import java.text.MessageFormat;
046:
047: import javax.swing.JFileChooser;
048: import javax.swing.event.DocumentEvent;
049: import javax.swing.event.DocumentListener;
050:
051: import org.netbeans.spi.project.ui.support.ProjectChooser;
052:
053: import org.openide.WizardDescriptor;
054: import org.openide.util.NbBundle;
055: import org.openide.awt.Mnemonics;
056:
057: public class PanelProjectLocationVisual extends SettingsPanel implements
058: DocumentListener {
059:
060: private PanelConfigureProject panel;
061:
062: /** Creates new form PanelProjectLocationVisual */
063: public PanelProjectLocationVisual(PanelConfigureProject panel) {
064: initComponents();
065: this .panel = panel;
066:
067: // Register listener on the textFields to make the automatic updates
068: projectNameTextField.getDocument().addDocumentListener(this );
069: projectLocationTextField.getDocument()
070: .addDocumentListener(this );
071:
072: Mnemonics.setLocalizedText(projectNameLabel, NbBundle
073: .getMessage(PanelProjectLocationVisual.class,
074: "LBL_NWP1_ProjectName_Label")); // NOI18N
075: projectNameLabel.setLabelFor(projectNameTextField);
076: projectNameTextField.getAccessibleContext()
077: .setAccessibleDescription(
078: NbBundle.getMessage(
079: PanelProjectLocationVisual.class,
080: "ACS_LBL_NWP1_ProjectName_A11YDesc"));// NOI18N
081:
082: Mnemonics.setLocalizedText(projectLocationLabel, NbBundle
083: .getMessage(PanelProjectLocationVisual.class,
084: "LBL_NWP1_ProjectLocation_Label"));// NOI18N
085: projectLocationLabel.setLabelFor(projectLocationTextField);
086: projectLocationTextField
087: .getAccessibleContext()
088: .setAccessibleDescription(
089: NbBundle
090: .getMessage(
091: PanelProjectLocationVisual.class,
092: "ACS_LBL_NPW1_ProjectLocation_A11YDesc"));// NOI18N
093:
094: Mnemonics.setLocalizedText(Button, NbBundle.getMessage(
095: PanelProjectLocationVisual.class,
096: "LBL_NWP1_BrowseLocation_Button"));// NOI18N
097: Button.getAccessibleContext().setAccessibleDescription(
098: NbBundle.getMessage(PanelProjectLocationVisual.class,
099: "ACS_LBL_NWP1_BrowseLocation_A11YDesc"));// NOI18N
100:
101: Mnemonics.setLocalizedText(createdFolderLabel, NbBundle
102: .getMessage(PanelProjectLocationVisual.class,
103: "LBL_NWP1_CreatedProjectFolder_Lablel"));// NOI18N
104: createdFolderLabel.setLabelFor(createdFolderTextField);
105: createdFolderTextField
106: .getAccessibleContext()
107: .setAccessibleDescription(
108: NbBundle
109: .getMessage(
110: PanelProjectLocationVisual.class,
111: "ACS_LBL_NWP1_CreatedProjectFolder_A11YDesc"));// NOI18N
112:
113: }
114:
115: /** This method is called from within the constructor to
116: * initialize the form.
117: * WARNING: Do NOT modify this code. The content of this method is
118: * always regenerated by the Form Editor.
119: */
120: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
121: private void initComponents() {
122: java.awt.GridBagConstraints gridBagConstraints;
123:
124: projectNameLabel = new javax.swing.JLabel();
125: projectNameTextField = new javax.swing.JTextField();
126: projectLocationLabel = new javax.swing.JLabel();
127: projectLocationTextField = new javax.swing.JTextField();
128: Button = new javax.swing.JButton();
129: createdFolderLabel = new javax.swing.JLabel();
130: createdFolderTextField = new javax.swing.JTextField();
131:
132: setLayout(new java.awt.GridBagLayout());
133:
134: projectNameLabel.setLabelFor(projectNameTextField);
135: gridBagConstraints = new java.awt.GridBagConstraints();
136: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
137: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
138: add(projectNameLabel, gridBagConstraints);
139: projectNameLabel.getAccessibleContext().setAccessibleName("");
140: projectNameLabel.getAccessibleContext()
141: .setAccessibleDescription("");
142:
143: gridBagConstraints = new java.awt.GridBagConstraints();
144: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
145: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
146: gridBagConstraints.weightx = 1.0;
147: gridBagConstraints.insets = new java.awt.Insets(0, 12, 12, 0);
148: add(projectNameTextField, gridBagConstraints);
149: projectNameTextField.getAccessibleContext().setAccessibleName(
150: "");
151: projectNameTextField.getAccessibleContext()
152: .setAccessibleDescription("");
153:
154: projectLocationLabel.setLabelFor(projectLocationTextField);
155: gridBagConstraints = new java.awt.GridBagConstraints();
156: gridBagConstraints.gridy = 1;
157: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
158: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
159: add(projectLocationLabel, gridBagConstraints);
160: projectLocationLabel.getAccessibleContext().setAccessibleName(
161: "");
162: projectLocationLabel.getAccessibleContext()
163: .setAccessibleDescription("");
164:
165: gridBagConstraints = new java.awt.GridBagConstraints();
166: gridBagConstraints.gridy = 1;
167: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
168: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
169: gridBagConstraints.weightx = 1.0;
170: gridBagConstraints.insets = new java.awt.Insets(0, 12, 5, 0);
171: add(projectLocationTextField, gridBagConstraints);
172: projectLocationTextField.getAccessibleContext()
173: .setAccessibleName("");
174: projectLocationTextField.getAccessibleContext()
175: .setAccessibleDescription("");
176:
177: Button.setActionCommand("BROWSE");
178: Button.addActionListener(new java.awt.event.ActionListener() {
179: public void actionPerformed(java.awt.event.ActionEvent evt) {
180: browseLocationAction(evt);
181: }
182: });
183:
184: gridBagConstraints = new java.awt.GridBagConstraints();
185: gridBagConstraints.gridy = 1;
186: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
187: gridBagConstraints.insets = new java.awt.Insets(0, 6, 5, 0);
188: add(Button, gridBagConstraints);
189: Button.getAccessibleContext().setAccessibleName("");
190: Button.getAccessibleContext().setAccessibleDescription("");
191:
192: createdFolderLabel.setLabelFor(createdFolderTextField);
193: gridBagConstraints = new java.awt.GridBagConstraints();
194: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
195: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
196: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
197: add(createdFolderLabel, gridBagConstraints);
198: createdFolderLabel.getAccessibleContext().setAccessibleName("");
199: createdFolderLabel.getAccessibleContext()
200: .setAccessibleDescription("");
201:
202: createdFolderTextField.setEditable(false);
203: gridBagConstraints = new java.awt.GridBagConstraints();
204: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
205: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
206: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
207: gridBagConstraints.weightx = 1.0;
208: gridBagConstraints.weighty = 1.0;
209: gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 0);
210: add(createdFolderTextField, gridBagConstraints);
211: createdFolderTextField.getAccessibleContext()
212: .setAccessibleName("");
213: createdFolderTextField.getAccessibleContext()
214: .setAccessibleDescription("");
215:
216: }
217:
218: // </editor-fold>//GEN-END:initComponents
219:
220: private void browseLocationAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseLocationAction
221: String command = evt.getActionCommand();
222:
223: if ("BROWSE".equals(command)) { //NOI18N
224: JFileChooser chooser = new JFileChooser();
225: chooser.setDialogTitle(NbBundle.getMessage(
226: PanelProjectLocationVisual.class,
227: "LBL_NWP1_SelectProjectLocation")); //NOI18N
228: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
229: String path = projectLocationTextField.getText();
230: if (path.length() > 0) {
231: File f = new File(path);
232: if (f.exists())
233: chooser.setSelectedFile(f);
234: }
235: if (JFileChooser.APPROVE_OPTION == chooser
236: .showOpenDialog(this )) {
237: File projectDir = chooser.getSelectedFile();
238: projectLocationTextField.setText(projectDir
239: .getAbsolutePath());
240: }
241: panel.fireChangeEvent();
242: }
243: }//GEN-LAST:event_browseLocationAction
244:
245: public void addNotify() {
246: super .addNotify();
247: //same problem as in 31086, initial focus on Cancel button
248: projectLocationTextField.requestFocus();
249: }
250:
251: boolean valid(WizardDescriptor wizardDescriptor) {
252: if (projectNameTextField.getText().length() == 0) {
253: wizardDescriptor.putProperty("WizardPanel_errorMessage",
254: NbBundle.getMessage(
255: PanelProjectLocationVisual.class,
256: "MSG_IllegalProjectName")); //NOI18N
257: return false; // Display name not specified
258: }
259:
260: File destFolder = new File(createdFolderTextField.getText());
261: File[] children = destFolder.listFiles();
262: if (destFolder.exists() && children != null
263: && children.length > 0) {
264: // Folder exists and is not empty
265: wizardDescriptor.putProperty("WizardPanel_errorMessage",
266: NbBundle.getMessage(
267: PanelProjectLocationVisual.class,
268: "MSG_ProjectFolderExists")); //NOI18N
269: return false;
270: }
271:
272: wizardDescriptor.putProperty("WizardPanel_errorMessage", ""); //NOI18N
273: return true;
274: }
275:
276: void store(WizardDescriptor d) {
277: String name = projectNameTextField.getText().trim();
278:
279: d.putProperty(WizardProperties.PROJECT_DIR, new File(
280: createdFolderTextField.getText().trim()));
281: d.putProperty(WizardProperties.NAME, name);
282:
283: File projectsDir = new File(this .projectLocationTextField
284: .getText());
285: if (projectsDir.isDirectory()) {
286: ProjectChooser.setProjectsFolder(projectsDir);
287: }
288: }
289:
290: void read(WizardDescriptor settings) {
291: File projectLocation = (File) settings
292: .getProperty(WizardProperties.PROJECT_DIR);
293: if (projectLocation == null)
294: projectLocation = ProjectChooser.getProjectsFolder();
295: else
296: projectLocation = projectLocation.getParentFile();
297:
298: projectLocationTextField.setText(projectLocation
299: .getAbsolutePath());
300:
301: String formater = null;
302: String projectName = (String) settings
303: .getProperty(WizardProperties.NAME);
304:
305: if (projectName == null) {
306: formater = NbBundle.getMessage(
307: PanelProjectLocationVisual.class,
308: "LBL_NPW1_DefaultProjectName"); //NOI18N
309: } else {
310: formater = projectName + "{0}"; //NOI18N
311: }
312: if ((projectName == null)
313: || (validFreeProjectName(projectLocation, projectName) == null)) {
314: int baseCount = FoldersListSettings.getDefault()
315: .getNewProjectCount() + 1;
316: while ((projectName = validFreeProjectName(projectLocation,
317: formater, baseCount)) == null)
318: baseCount++;
319: // settings.putProperty(NewWebProjectWizardIterator.PROP_NAME_INDEX, new Integer(baseCount));
320: }
321: projectNameTextField.setText(projectName);
322: projectNameTextField.selectAll();
323: }
324:
325: // Variables declaration - do not modify//GEN-BEGIN:variables
326: private javax.swing.JButton Button;
327: private javax.swing.JLabel createdFolderLabel;
328: private javax.swing.JTextField createdFolderTextField;
329: private javax.swing.JLabel projectLocationLabel;
330: private javax.swing.JTextField projectLocationTextField;
331: private javax.swing.JLabel projectNameLabel;
332: protected javax.swing.JTextField projectNameTextField;
333:
334: // End of variables declaration//GEN-END:variables
335:
336: private static JFileChooser createChooser() {
337: JFileChooser chooser = new JFileChooser();
338: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
339: chooser.setAcceptAllFileFilterUsed(false);
340:
341: return chooser;
342: }
343:
344: private String validFreeProjectName(final File parentFolder,
345: final String formater, final int index) {
346: String name = MessageFormat.format(formater,
347: new Object[] { new Integer(index) });
348: File file = new File(parentFolder, name);
349: return file.exists() ? null : name;
350: }
351:
352: private String validFreeProjectName(final File parentFolder,
353: final String name) {
354: File file = new File(parentFolder, name);
355: return file.exists() ? null : name;
356: }
357:
358: // Implementation of DocumentListener --------------------------------------
359: public void changedUpdate(DocumentEvent e) {
360: updateTexts(e);
361: }
362:
363: public void insertUpdate(DocumentEvent e) {
364: updateTexts(e);
365: }
366:
367: public void removeUpdate(DocumentEvent e) {
368: updateTexts(e);
369: }
370:
371: // End if implementation of DocumentListener -------------------------------
372:
373: /** Handles changes in the project name and project directory
374: */
375: private void updateTexts(DocumentEvent e) {
376: createdFolderTextField.setText(getCreatedFolderPath());
377:
378: panel.fireChangeEvent(); // Notify that the panel changed
379: }
380:
381: private String getCreatedFolderPath() {
382: StringBuffer folder = new StringBuffer(projectLocationTextField
383: .getText().trim());
384: if (!projectLocationTextField.getText()
385: .endsWith(File.separator))
386: folder.append(File.separatorChar);
387: folder.append(projectNameTextField.getText().trim());
388:
389: return folder.toString();
390: }
391:
392: }
393:
394: //TODO implement check for project folder name and location
|