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: package org.netbeans.modules.apisupport.feedreader;
042:
043: import java.io.File;
044: import javax.swing.JFileChooser;
045: import javax.swing.JPanel;
046: import javax.swing.event.DocumentEvent;
047: import javax.swing.event.DocumentListener;
048: import javax.swing.text.Document;
049: import org.netbeans.spi.project.ui.support.ProjectChooser;
050: import org.openide.WizardDescriptor;
051: import org.openide.WizardValidationException;
052: import org.openide.filesystems.FileUtil;
053: import org.openide.util.NbBundle;
054:
055: public class FeedReaderPanelVisual extends JPanel implements
056: DocumentListener {
057:
058: public static final String PROP_PROJECT_NAME = "projectName"; // NOI18N
059:
060: private FeedReaderWizardPanel panel;
061:
062: /** Creates new form PanelProjectLocationVisual */
063: public FeedReaderPanelVisual(FeedReaderWizardPanel panel) {
064: initComponents();
065: this .panel = panel;
066: // Register listener on the textFields to make the automatic updates
067: projectNameTextField.getDocument().addDocumentListener(this );
068: projectLocationTextField.getDocument()
069: .addDocumentListener(this );
070: }
071:
072: public String getProjectName() {
073: return this .projectNameTextField.getText();
074: }
075:
076: // <editor-fold defaultstate="collapsed" desc=" UI Code ">
077: private void initComponents() {
078: java.awt.GridBagConstraints gridBagConstraints;
079:
080: projectNameLabel = new javax.swing.JLabel();
081: projectNameTextField = new javax.swing.JTextField();
082: projectLocationLabel = new javax.swing.JLabel();
083: projectLocationTextField = new javax.swing.JTextField();
084: browseButton = new javax.swing.JButton();
085: createdFolderLabel = new javax.swing.JLabel();
086: createdFolderTextField = new javax.swing.JTextField();
087:
088: setLayout(new java.awt.GridBagLayout());
089:
090: projectNameLabel.setLabelFor(projectNameTextField);
091: gridBagConstraints = new java.awt.GridBagConstraints();
092: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
093: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
094: org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel,
095: NbBundle.getMessage(FeedReaderPanelVisual.class,
096: "LBL_ProjectName"));
097: add(projectNameLabel, gridBagConstraints);
098:
099: gridBagConstraints = new java.awt.GridBagConstraints();
100: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
101: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
102: gridBagConstraints.weightx = 1.0;
103: gridBagConstraints.insets = new java.awt.Insets(0, 12, 12, 0);
104: add(projectNameTextField, gridBagConstraints);
105:
106: projectLocationLabel.setLabelFor(projectLocationTextField);
107: gridBagConstraints = new java.awt.GridBagConstraints();
108: gridBagConstraints.gridy = 1;
109: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
110: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
111: org.openide.awt.Mnemonics.setLocalizedText(
112: projectLocationLabel, NbBundle.getMessage(
113: FeedReaderPanelVisual.class,
114: "LBL_ProjectLocation"));
115: add(projectLocationLabel, gridBagConstraints);
116:
117: gridBagConstraints = new java.awt.GridBagConstraints();
118: gridBagConstraints.gridy = 1;
119: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
120: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
121: gridBagConstraints.weightx = 1.0;
122: gridBagConstraints.insets = new java.awt.Insets(0, 12, 5, 0);
123: add(projectLocationTextField, gridBagConstraints);
124:
125: browseButton.setActionCommand("BROWSE");
126: browseButton
127: .addActionListener(new java.awt.event.ActionListener() {
128: public void actionPerformed(
129: java.awt.event.ActionEvent evt) {
130: browseLocationAction(evt);
131: }
132: });
133:
134: gridBagConstraints = new java.awt.GridBagConstraints();
135: gridBagConstraints.gridy = 1;
136: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
137: gridBagConstraints.insets = new java.awt.Insets(0, 6, 5, 0);
138: org.openide.awt.Mnemonics.setLocalizedText(browseButton,
139: NbBundle.getMessage(FeedReaderPanelVisual.class,
140: "LBL_Browse"));
141: add(browseButton, gridBagConstraints);
142:
143: createdFolderLabel.setLabelFor(createdFolderTextField);
144: gridBagConstraints = new java.awt.GridBagConstraints();
145: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
146: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
147: org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel,
148: NbBundle.getMessage(FeedReaderPanelVisual.class,
149: "LBL_ProjectFolder"));
150: add(createdFolderLabel, gridBagConstraints);
151:
152: createdFolderTextField.setEditable(false);
153: gridBagConstraints = new java.awt.GridBagConstraints();
154: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
155: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
156: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
157: gridBagConstraints.weightx = 1.0;
158: gridBagConstraints.weighty = 1.0;
159: gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 0);
160: add(createdFolderTextField, gridBagConstraints);
161: }
162:
163: // </editor-fold>
164:
165: private void browseLocationAction(java.awt.event.ActionEvent evt) {
166: String command = evt.getActionCommand();
167: if ("BROWSE".equals(command)) { // NOI18N
168: JFileChooser chooser = new JFileChooser();
169: FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
170: chooser.setDialogTitle(NbBundle.getMessage(
171: FeedReaderPanelVisual.class,
172: "LBL_SelectProjectLocation"));
173: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
174: String path = this .projectLocationTextField.getText();
175: if (path.length() > 0) {
176: File f = new File(path);
177: if (f.exists()) {
178: chooser.setSelectedFile(f);
179: }
180: }
181: if (JFileChooser.APPROVE_OPTION == chooser
182: .showOpenDialog(this )) { //NOI18N
183: File projectDir = chooser.getSelectedFile();
184: projectLocationTextField.setText(FileUtil
185: .normalizeFile(projectDir).getAbsolutePath());
186: }
187: panel.fireChangeEvent();
188: }
189: }
190:
191: public void addNotify() {
192: super .addNotify();
193: //same problem as in 31086, initial focus on Cancel button
194: projectNameTextField.requestFocus();
195: }
196:
197: boolean valid(WizardDescriptor wizardDescriptor) {
198: if (projectNameTextField.getText().length() == 0) {
199: wizardDescriptor.putProperty("WizardPanel_errorMessage", // NOI18N
200: NbBundle.getMessage(FeedReaderPanelVisual.class,
201: "MSG_InvalidProjectFolderName"));
202: return false; // Display name not specified
203: }
204: File f = FileUtil.normalizeFile(new File(
205: projectLocationTextField.getText()).getAbsoluteFile());
206: if (!f.isDirectory()) {
207: wizardDescriptor.putProperty("WizardPanel_errorMessage", // NOI18N
208: NbBundle.getMessage(FeedReaderPanelVisual.class,
209: "MSG_InvalidProjectFolderPath"));
210: return false;
211: }
212: final File destFolder = FileUtil.normalizeFile(new File(
213: createdFolderTextField.getText()).getAbsoluteFile());
214:
215: File projLoc = destFolder;
216: while (projLoc != null && !projLoc.exists()) {
217: projLoc = projLoc.getParentFile();
218: }
219: if (projLoc == null || !projLoc.canWrite()) {
220: wizardDescriptor.putProperty("WizardPanel_errorMessage", // NOI18N
221: NbBundle.getMessage(FeedReaderPanelVisual.class,
222: "MSG_CanNotCreateProjectFolder"));
223: return false;
224: }
225:
226: if (FileUtil.toFileObject(projLoc) == null) {
227: wizardDescriptor.putProperty("WizardPanel_errorMessage", // NOI18N
228: NbBundle.getMessage(FeedReaderPanelVisual.class,
229: "MSG_InvalidProjectFolderPath"));
230: return false;
231: }
232:
233: File[] kids = destFolder.listFiles();
234: if (destFolder.exists() && kids != null && kids.length > 0) {
235: // Folder exists and is not empty
236: wizardDescriptor.putProperty("WizardPanel_errorMessage", // NOI18N
237: NbBundle.getMessage(FeedReaderPanelVisual.class,
238: "MSG_ProjectFolderAlreadyExists"));
239: return false;
240: }
241: wizardDescriptor.putProperty("WizardPanel_errorMessage", ""); // NOI18N
242: return true;
243: }
244:
245: void store(WizardDescriptor d) {
246: String name = projectNameTextField.getText().trim();
247: String folder = createdFolderTextField.getText().trim();
248:
249: d.putProperty( /*XXX Define somewhere */"projdir", new File(
250: folder)); // NOI18N
251: d.putProperty( /*XXX Define somewhere */"name", name); // NOI18N
252: }
253:
254: void read(WizardDescriptor settings) {
255: File projectLocation = (File) settings.getProperty("projdir"); //NOI18N
256: if (projectLocation == null
257: || projectLocation.getParentFile() == null
258: || !projectLocation.getParentFile().isDirectory()) {
259: projectLocation = ProjectChooser.getProjectsFolder();
260: } else {
261: projectLocation = projectLocation.getParentFile();
262: }
263: this .projectLocationTextField.setText(projectLocation
264: .getAbsolutePath());
265:
266: String projectName = (String) settings.getProperty("name"); //NOI18N
267: if (projectName == null) {
268: projectName = "feedreader-suite";
269: }
270: this .projectNameTextField.setText(projectName);
271: this .projectNameTextField.selectAll();
272: }
273:
274: void validate(WizardDescriptor d) throws WizardValidationException {
275: // nothing to validate
276: }
277:
278: // UI Variables declaration -
279: private javax.swing.JButton browseButton;
280: private javax.swing.JLabel createdFolderLabel;
281: private javax.swing.JTextField createdFolderTextField;
282: private javax.swing.JLabel projectLocationLabel;
283: private javax.swing.JTextField projectLocationTextField;
284: private javax.swing.JLabel projectNameLabel;
285: private javax.swing.JTextField projectNameTextField;
286:
287: // End of variables declaration
288:
289: // Implementation of DocumentListener --------------------------------------
290:
291: public void changedUpdate(DocumentEvent e) {
292: updateTexts(e);
293: if (this .projectNameTextField.getDocument() == e.getDocument()) {
294: firePropertyChange(PROP_PROJECT_NAME, null,
295: this .projectNameTextField.getText());
296: }
297: }
298:
299: public void insertUpdate(DocumentEvent e) {
300: updateTexts(e);
301: if (this .projectNameTextField.getDocument() == e.getDocument()) {
302: firePropertyChange(PROP_PROJECT_NAME, null,
303: this .projectNameTextField.getText());
304: }
305: }
306:
307: public void removeUpdate(DocumentEvent e) {
308: updateTexts(e);
309: if (this .projectNameTextField.getDocument() == e.getDocument()) {
310: firePropertyChange(PROP_PROJECT_NAME, null,
311: this .projectNameTextField.getText());
312: }
313: }
314:
315: /** Handles changes in the Project name and project directory. */
316: private void updateTexts(DocumentEvent e) {
317: Document doc = e.getDocument();
318:
319: if (doc == projectNameTextField.getDocument()
320: || doc == projectLocationTextField.getDocument()) {
321: // Change in the project name
322:
323: String projectName = projectNameTextField.getText();
324: String projectFolder = projectLocationTextField.getText();
325:
326: //if ( projectFolder.trim().length() == 0 || projectFolder.equals( oldName ) ) {
327: createdFolderTextField.setText(projectFolder
328: + File.separatorChar + projectName);
329: //}
330:
331: }
332: panel.fireChangeEvent(); // Notify that the panel changed
333: }
334:
335: }
|