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.project.uiapi;
042:
043: import java.awt.CardLayout;
044: import java.io.File;
045: import javax.swing.JFileChooser;
046: import javax.swing.UIManager;
047: import javax.swing.event.ChangeListener;
048: import javax.swing.event.DocumentListener;
049: import javax.swing.event.DocumentEvent;
050: import org.netbeans.api.progress.ProgressHandle;
051: import org.netbeans.api.progress.ProgressHandleFactory;
052: import org.netbeans.api.project.Project;
053: import org.netbeans.spi.project.support.ProjectOperations;
054: import org.netbeans.api.project.ProjectUtils;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.util.ChangeSupport;
058: import org.openide.util.NbBundle;
059:
060: /**
061: * @author Jan Lahoda
062: */
063: public class ProjectCopyPanel extends javax.swing.JPanel implements
064: DocumentListener,
065: DefaultProjectOperationsImplementation.InvalidablePanel {
066:
067: private Project project;
068: private boolean isMove;
069: private boolean invalid;
070:
071: private final ChangeSupport changeSupport = new ChangeSupport(this );
072: private ProgressHandle handle;
073:
074: /**
075: * Creates new form ProjectCopyPanel
076: */
077: public ProjectCopyPanel(ProgressHandle handle, Project project,
078: boolean isMove) {
079: this .project = project;
080: this .isMove = isMove;
081: this .handle = handle;
082:
083: initComponents();
084: setProject();
085: projectName.getDocument().addDocumentListener(this );
086: projectLocation.getDocument().addDocumentListener(this );
087:
088: if (isMove) {
089: nameLabel.setVisible(false);
090: projectName.setVisible(false);
091: }
092:
093: if (Boolean
094: .getBoolean("org.netbeans.modules.project.uiapi.DefaultProjectOperations.showProgress")) {
095: ((CardLayout) progress.getLayout()).show(progress,
096: "progress");
097: }
098: }
099:
100: public void addChangeListener(ChangeListener l) {
101: changeSupport.addChangeListener(l);
102: }
103:
104: public void removeChangeListener(ChangeListener l) {
105: changeSupport.removeChangeListener(l);
106: }
107:
108: /** This method is called from within the constructor to
109: * initialize the form.
110: * WARNING: Do NOT modify this code. The content of this method is
111: * always regenerated by the Form Editor.
112: */
113: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
114: private void initComponents() {
115: java.awt.GridBagConstraints gridBagConstraints;
116:
117: jLabel1 = new javax.swing.JLabel();
118: jLabel2 = new javax.swing.JLabel();
119: projectLocation = new javax.swing.JTextField();
120: nameLabel = new javax.swing.JLabel();
121: projectName = new javax.swing.JTextField();
122: browse = new javax.swing.JButton();
123: jLabel4 = new javax.swing.JLabel();
124: projectFolder = new javax.swing.JTextField();
125: extSourcesWarning = new javax.swing.JLabel();
126: errorMessage = new javax.swing.JLabel();
127: progress = new javax.swing.JPanel();
128: jPanel4 = new javax.swing.JPanel();
129: progressImpl = new javax.swing.JPanel();
130: jLabel5 = new javax.swing.JLabel();
131: jPanel3 = new javax.swing.JPanel();
132:
133: setLayout(new java.awt.GridBagLayout());
134:
135: getAccessibleContext().setAccessibleDescription(
136: org.openide.util.NbBundle.getMessage(
137: ProjectCopyPanel.class, "ACSD_Copy_Move_Panel",
138: new Object[] { new Integer(isMove ? 1 : 0) }));
139: org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
140: org.openide.util.NbBundle.getMessage(
141: ProjectCopyPanel.class,
142: "LBL_Copy_Move_Dialog_Text", new Object[] {
143: new Integer(isMove ? 1 : 0),
144: ProjectUtils.getInformation(project)
145: .getDisplayName() }));
146: gridBagConstraints = new java.awt.GridBagConstraints();
147: gridBagConstraints.gridwidth = 2;
148: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
149: gridBagConstraints.weightx = 1.0;
150: add(jLabel1, gridBagConstraints);
151:
152: jLabel2.setLabelFor(projectLocation);
153: org.openide.awt.Mnemonics
154: .setLocalizedText(jLabel2, org.openide.util.NbBundle
155: .getMessage(ProjectCopyPanel.class,
156: "LBL_Project_Location"));
157: gridBagConstraints = new java.awt.GridBagConstraints();
158: gridBagConstraints.gridx = 0;
159: gridBagConstraints.gridy = 2;
160: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
161: gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 12);
162: add(jLabel2, gridBagConstraints);
163:
164: projectLocation.setColumns(30);
165: gridBagConstraints = new java.awt.GridBagConstraints();
166: gridBagConstraints.gridx = 1;
167: gridBagConstraints.gridy = 2;
168: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
169: gridBagConstraints.weightx = 1.0;
170: gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 5);
171: add(projectLocation, gridBagConstraints);
172: projectLocation.getAccessibleContext().setAccessibleName(
173: org.openide.util.NbBundle.getMessage(
174: ProjectCopyPanel.class,
175: "ACSN_Project_Location", new Object[] {}));
176: projectLocation.getAccessibleContext()
177: .setAccessibleDescription(
178: org.openide.util.NbBundle.getMessage(
179: ProjectCopyPanel.class,
180: "ACSD_Project_Location",
181: new Object[] {}));
182:
183: nameLabel.setLabelFor(projectName);
184: org.openide.awt.Mnemonics.setLocalizedText(nameLabel,
185: org.openide.util.NbBundle.getMessage(
186: ProjectCopyPanel.class, "LBL_Project_Name"));
187: gridBagConstraints = new java.awt.GridBagConstraints();
188: gridBagConstraints.gridx = 0;
189: gridBagConstraints.gridy = 1;
190: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
191: gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 12);
192: add(nameLabel, gridBagConstraints);
193:
194: projectName.setColumns(30);
195: gridBagConstraints = new java.awt.GridBagConstraints();
196: gridBagConstraints.gridx = 1;
197: gridBagConstraints.gridy = 1;
198: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
199: gridBagConstraints.weightx = 1.0;
200: gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 5);
201: add(projectName, gridBagConstraints);
202: projectName.getAccessibleContext().setAccessibleName(
203: org.openide.util.NbBundle.getMessage(
204: ProjectCopyPanel.class, "ACSN_Project_Name",
205: new Object[] {}));
206: projectName.getAccessibleContext().setAccessibleDescription(
207: org.openide.util.NbBundle.getMessage(
208: ProjectCopyPanel.class, "ACSD_Project_Name",
209: new Object[] {}));
210:
211: org.openide.awt.Mnemonics.setLocalizedText(browse,
212: org.openide.util.NbBundle.getMessage(
213: ProjectCopyPanel.class, "LBL_Browse",
214: new Object[] {}));
215: browse.addActionListener(new java.awt.event.ActionListener() {
216: public void actionPerformed(java.awt.event.ActionEvent evt) {
217: browseActionPerformed(evt);
218: }
219: });
220:
221: gridBagConstraints = new java.awt.GridBagConstraints();
222: gridBagConstraints.gridx = 2;
223: gridBagConstraints.gridy = 2;
224: gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
225: add(browse, gridBagConstraints);
226: browse.getAccessibleContext().setAccessibleDescription(
227: org.openide.util.NbBundle.getMessage(
228: ProjectCopyPanel.class, "ACSD_Browse",
229: new Object[] {}));
230:
231: jLabel4.setLabelFor(projectFolder);
232: org.openide.awt.Mnemonics.setLocalizedText(jLabel4,
233: org.openide.util.NbBundle.getMessage(
234: ProjectCopyPanel.class, "LBL_Project_Folder"));
235: gridBagConstraints = new java.awt.GridBagConstraints();
236: gridBagConstraints.gridx = 0;
237: gridBagConstraints.gridy = 3;
238: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
239: gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 12);
240: add(jLabel4, gridBagConstraints);
241:
242: projectFolder.setColumns(30);
243: projectFolder.setEditable(false);
244: gridBagConstraints = new java.awt.GridBagConstraints();
245: gridBagConstraints.gridx = 1;
246: gridBagConstraints.gridy = 3;
247: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
248: gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 5);
249: add(projectFolder, gridBagConstraints);
250: projectFolder.getAccessibleContext().setAccessibleName(
251: org.openide.util.NbBundle.getMessage(
252: ProjectCopyPanel.class, "ACSN_Project_Folder",
253: new Object[] {}));
254: projectFolder.getAccessibleContext().setAccessibleDescription(
255: org.openide.util.NbBundle.getMessage(
256: ProjectCopyPanel.class, "ACSD_Project_Folder",
257: new Object[] {}));
258:
259: extSourcesWarning.setForeground(UIManager
260: .getColor("nb.errorForeground"));
261: gridBagConstraints = new java.awt.GridBagConstraints();
262: gridBagConstraints.gridx = 0;
263: gridBagConstraints.gridy = 4;
264: gridBagConstraints.gridwidth = 3;
265: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
266: gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 0);
267: add(extSourcesWarning, gridBagConstraints);
268:
269: errorMessage.setForeground(UIManager
270: .getColor("nb.errorForeground"));
271: gridBagConstraints = new java.awt.GridBagConstraints();
272: gridBagConstraints.gridx = 0;
273: gridBagConstraints.gridy = 5;
274: gridBagConstraints.gridwidth = 3;
275: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
276: gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 0);
277: add(errorMessage, gridBagConstraints);
278:
279: progress.setLayout(new java.awt.CardLayout());
280:
281: progress.add(jPanel4, "not-progress");
282:
283: progressImpl.setLayout(new java.awt.GridBagLayout());
284:
285: org.openide.awt.Mnemonics.setLocalizedText(jLabel5,
286: org.openide.util.NbBundle.getMessage(
287: ProjectCopyPanel.class, "LBL_Copying_Moving",
288: new Object[] { isMove ? new Integer(1)
289: : new Integer(0) }));
290: gridBagConstraints = new java.awt.GridBagConstraints();
291: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
292: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
293: gridBagConstraints.weightx = 1.0;
294: progressImpl.add(jLabel5, gridBagConstraints);
295:
296: jPanel3.setLayout(new java.awt.BorderLayout());
297:
298: jPanel3.add(ProgressHandleFactory
299: .createProgressComponent(handle));
300: gridBagConstraints = new java.awt.GridBagConstraints();
301: gridBagConstraints.gridx = 0;
302: gridBagConstraints.gridy = 1;
303: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
304: gridBagConstraints.weightx = 1.0;
305: progressImpl.add(jPanel3, gridBagConstraints);
306:
307: progress.add(progressImpl, "progress");
308:
309: gridBagConstraints = new java.awt.GridBagConstraints();
310: gridBagConstraints.gridx = 0;
311: gridBagConstraints.gridy = 6;
312: gridBagConstraints.gridwidth = 3;
313: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
314: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
315: gridBagConstraints.weightx = 1.0;
316: gridBagConstraints.weighty = 0.5;
317: add(progress, gridBagConstraints);
318:
319: }
320:
321: // </editor-fold>//GEN-END:initComponents
322:
323: private void browseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseActionPerformed
324: // TODO add your handling code here:
325: File current = new File(projectLocation.getText());
326: JFileChooser chooser = new JFileChooser(current);
327:
328: chooser.setMultiSelectionEnabled(false);
329: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
330:
331: if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
332: projectLocation.setText(chooser.getSelectedFile()
333: .getAbsolutePath());
334: }
335: }//GEN-LAST:event_browseActionPerformed
336:
337: // Variables declaration - do not modify//GEN-BEGIN:variables
338: private javax.swing.JButton browse;
339: private javax.swing.JLabel errorMessage;
340: private javax.swing.JLabel extSourcesWarning;
341: private javax.swing.JLabel jLabel1;
342: private javax.swing.JLabel jLabel2;
343: private javax.swing.JLabel jLabel4;
344: private javax.swing.JLabel jLabel5;
345: private javax.swing.JPanel jPanel3;
346: private javax.swing.JPanel jPanel4;
347: private javax.swing.JLabel nameLabel;
348: private javax.swing.JPanel progress;
349: private javax.swing.JPanel progressImpl;
350: private javax.swing.JTextField projectFolder;
351: private javax.swing.JTextField projectLocation;
352: private javax.swing.JTextField projectName;
353: // End of variables declaration//GEN-END:variables
354:
355: private String lastComputedName;
356:
357: private String computeValidProjectName(String projectLocation,
358: String projectNamePrefix) {
359: File location = new File(projectLocation);
360:
361: if (!location.exists()) {
362: lastComputedName = projectNamePrefix;
363: return projectNamePrefix;
364: }
365:
366: int num = 1;
367: String projectName = projectNamePrefix;
368: if (new File(location, projectName).exists()) {
369: while (new File(location, projectName = projectNamePrefix
370: + "_" + num).exists()) {
371: num++;
372: }
373: }
374: lastComputedName = projectName;
375: return projectName;
376: }
377:
378: private void setProject() {
379: FileObject parent = project.getProjectDirectory().getParent();
380: File parentFile = FileUtil.toFile(parent);
381:
382: projectLocation.setText(parentFile.getAbsolutePath());
383:
384: if (isMove) {
385: projectName.setText(ProjectUtils.getInformation(project)
386: .getName());
387: } else {
388: projectName.setText(computeValidProjectName(parentFile
389: .getAbsolutePath(), ProjectUtils.getInformation(
390: project).getName()));
391: }
392:
393: updateProjectFolder();
394: validateDialog();
395:
396: if (hasExternalSources() && !isMove) {
397: extSourcesWarning.setText(NbBundle.getMessage(
398: ProjectCopyPanel.class, "WRN_External_Sources"));
399: invalid = true;
400: }
401: }
402:
403: private boolean hasExternalSources() {
404: for (FileObject file : ProjectOperations.getDataFiles(project)) {
405: if (!FileUtil.isParentOf(project.getProjectDirectory(),
406: file)) {
407: return true;
408: }
409: }
410: return false;
411: }
412:
413: public String getNewName() {
414: return projectName.getText();
415: }
416:
417: public String getProjectFolderName() {
418: return project.getProjectDirectory().getNameExt();
419: }
420:
421: public File getNewDirectory() {
422: return new File(projectLocation.getText());
423: }
424:
425: public void changedUpdate(DocumentEvent e) {
426: //ignored
427: }
428:
429: public void insertUpdate(DocumentEvent e) {
430: if (e.getDocument().equals(projectLocation.getDocument())) {
431: if (lastComputedName != null
432: && lastComputedName.equals(projectName.getText())) {
433: projectName
434: .setText(computeValidProjectName(new File(
435: projectLocation.getText())
436: .getAbsolutePath(), ProjectUtils
437: .getInformation(project).getName()));
438: }
439: }
440: updateProjectFolder();
441: validateDialog();
442: }
443:
444: public void removeUpdate(DocumentEvent e) {
445: if (e.getDocument().equals(projectLocation.getDocument())) {
446: if (lastComputedName != null
447: && lastComputedName.equals(projectName.getText())) {
448: projectName
449: .setText(computeValidProjectName(new File(
450: projectLocation.getText())
451: .getAbsolutePath(), ProjectUtils
452: .getInformation(project).getName()));
453: }
454: }
455: updateProjectFolder();
456: validateDialog();
457: }
458:
459: private void updateProjectFolder() {
460: final File location = new File(projectLocation.getText());
461: File projectFolderFile = location;
462: if (isMove) {
463: projectFolderFile = new File(location, project
464: .getProjectDirectory().getNameExt());
465: } else {
466: projectFolderFile = new File(location, projectName
467: .getText());
468: }
469: projectFolder.setText(projectFolderFile.getAbsolutePath());
470: }
471:
472: public boolean isPanelValid() {
473: return " ".equals(errorMessage.getText()) && !invalid;
474: }
475:
476: private void validateDialog() {
477: if (invalid) {
478: //no reason to do anything:
479: return;
480: }
481:
482: String newError = computeError();
483: boolean changed = false;
484: String currentError = errorMessage.getText();
485:
486: newError = newError != null ? newError : " ";
487: changed = !currentError.equals(newError);
488:
489: errorMessage.setText(newError);
490:
491: if (changed) {
492: changeSupport.fireChange();
493: }
494: }
495:
496: private String computeError() {
497: File location = new File(projectLocation.getText());
498: return DefaultProjectOperationsImplementation.computeError(
499: location, projectName.getText(), projectFolder
500: .getText(), false);
501: }
502:
503: public void showProgress() {
504: projectFolder.setEnabled(false);
505: projectLocation.setEnabled(false);
506: projectName.setEnabled(false);
507: browse.setEnabled(false);
508:
509: ((CardLayout) progress.getLayout()).show(progress, "progress");
510: }
511: }
|