KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > uiapi > ProjectCopyPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.project.uiapi;
20
21 import java.awt.CardLayout JavaDoc;
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.JFileChooser JavaDoc;
26 import javax.swing.UIManager JavaDoc;
27 import javax.swing.event.ChangeEvent JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29 import javax.swing.event.DocumentListener JavaDoc;
30 import javax.swing.event.DocumentEvent JavaDoc;
31 import org.netbeans.api.progress.ProgressHandle;
32 import org.netbeans.api.progress.ProgressHandleFactory;
33 import org.netbeans.api.project.Project;
34 import org.netbeans.spi.project.support.ProjectOperations;
35 import org.netbeans.api.project.ProjectUtils;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.openide.util.NbBundle;
39
40 /**
41  * @author Jan Lahoda
42  */

43 public class ProjectCopyPanel extends javax.swing.JPanel JavaDoc implements DocumentListener JavaDoc, DefaultProjectOperationsImplementation.InvalidablePanel {
44     
45     private Project project;
46     private boolean isMove;
47     private boolean invalid;
48     
49     private List JavaDoc<ChangeListener JavaDoc> listeners;
50     private ProgressHandle handle;
51     
52     /**
53      * Creates new form ProjectCopyPanel
54      */

55     public ProjectCopyPanel(ProgressHandle handle, Project project, boolean isMove) {
56         this.project = project;
57         this.isMove = isMove;
58         this.listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
59         this.handle = handle;
60         
61         
62         initComponents();
63         setProject();
64         projectName.getDocument().addDocumentListener(this);
65         projectLocation.getDocument().addDocumentListener(this);
66         
67         if (isMove) {
68             nameLabel.setVisible(false);
69             projectName.setVisible(false);
70         }
71         
72         if (Boolean.getBoolean("org.netbeans.modules.project.uiapi.DefaultProjectOperations.showProgress")) {
73             ((CardLayout JavaDoc) progress.getLayout()).show(progress, "progress");
74         }
75     }
76     
77     public synchronized void addChangeListener(ChangeListener JavaDoc l) {
78         listeners.add(l);
79     }
80     
81     public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
82         listeners.remove(l);
83     }
84     
85     /** This method is called from within the constructor to
86      * initialize the form.
87      * WARNING: Do NOT modify this code. The content of this method is
88      * always regenerated by the Form Editor.
89      */

90     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
91
private void initComponents() {
92         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
93
94         jLabel1 = new javax.swing.JLabel JavaDoc();
95         jLabel2 = new javax.swing.JLabel JavaDoc();
96         projectLocation = new javax.swing.JTextField JavaDoc();
97         nameLabel = new javax.swing.JLabel JavaDoc();
98         projectName = new javax.swing.JTextField JavaDoc();
99         browse = new javax.swing.JButton JavaDoc();
100         jLabel4 = new javax.swing.JLabel JavaDoc();
101         projectFolder = new javax.swing.JTextField JavaDoc();
102         extSourcesWarning = new javax.swing.JLabel JavaDoc();
103         errorMessage = new javax.swing.JLabel JavaDoc();
104         progress = new javax.swing.JPanel JavaDoc();
105         jPanel4 = new javax.swing.JPanel JavaDoc();
106         progressImpl = new javax.swing.JPanel JavaDoc();
107         jLabel5 = new javax.swing.JLabel JavaDoc();
108         jPanel3 = new javax.swing.JPanel JavaDoc();
109
110         setLayout(new java.awt.GridBagLayout JavaDoc());
111
112         getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "ACSD_Copy_Move_Panel", new Object JavaDoc[] {new Integer JavaDoc(isMove ? 1 : 0)}));
113         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "LBL_Copy_Move_Dialog_Text", new Object JavaDoc[] {new Integer JavaDoc(isMove ? 1 : 0), ProjectUtils.getInformation(project).getDisplayName()}));
114         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
115         gridBagConstraints.gridwidth = 2;
116         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
117         gridBagConstraints.weightx = 1.0;
118         add(jLabel1, gridBagConstraints);
119
120         jLabel2.setLabelFor(projectLocation);
121         org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "LBL_Project_Location"));
122         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
123         gridBagConstraints.gridx = 0;
124         gridBagConstraints.gridy = 2;
125         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
126         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 0, 12);
127         add(jLabel2, gridBagConstraints);
128
129         projectLocation.setColumns(30);
130         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
131         gridBagConstraints.gridx = 1;
132         gridBagConstraints.gridy = 2;
133         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
134         gridBagConstraints.weightx = 1.0;
135         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 0, 5);
136         add(projectLocation, gridBagConstraints);
137         projectLocation.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "ACSN_Project_Location", new Object JavaDoc[] {}));
138         projectLocation.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "ACSD_Project_Location", new Object JavaDoc[] {}));
139
140         nameLabel.setLabelFor(projectName);
141         org.openide.awt.Mnemonics.setLocalizedText(nameLabel, org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "LBL_Project_Name"));
142         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
143         gridBagConstraints.gridx = 0;
144         gridBagConstraints.gridy = 1;
145         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
146         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 12);
147         add(nameLabel, gridBagConstraints);
148
149         projectName.setColumns(30);
150         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
151         gridBagConstraints.gridx = 1;
152         gridBagConstraints.gridy = 1;
153         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
154         gridBagConstraints.weightx = 1.0;
155         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 5);
156         add(projectName, gridBagConstraints);
157         projectName.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "ACSN_Project_Name", new Object JavaDoc[] {}));
158         projectName.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "ACSD_Project_Name", new Object JavaDoc[] {}));
159
160         org.openide.awt.Mnemonics.setLocalizedText(browse, org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "LBL_Browse", new Object JavaDoc[] {}));
161         browse.addActionListener(new java.awt.event.ActionListener JavaDoc() {
162             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
163                 browseActionPerformed(evt);
164             }
165         });
166
167         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
168         gridBagConstraints.gridx = 2;
169         gridBagConstraints.gridy = 2;
170         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 0, 0);
171         add(browse, gridBagConstraints);
172         browse.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "ACSD_Browse", new Object JavaDoc[] {}));
173
174         jLabel4.setLabelFor(projectFolder);
175         org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "LBL_Project_Folder"));
176         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
177         gridBagConstraints.gridx = 0;
178         gridBagConstraints.gridy = 3;
179         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
180         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 0, 12);
181         add(jLabel4, gridBagConstraints);
182
183         projectFolder.setColumns(30);
184         projectFolder.setEditable(false);
185         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
186         gridBagConstraints.gridx = 1;
187         gridBagConstraints.gridy = 3;
188         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
189         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 0, 5);
190         add(projectFolder, gridBagConstraints);
191         projectFolder.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "ACSN_Project_Folder", new Object JavaDoc[] {}));
192         projectFolder.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "ACSD_Project_Folder", new Object JavaDoc[] {}));
193
194         extSourcesWarning.setForeground(UIManager.getColor("nb.errorForeground"));
195         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
196         gridBagConstraints.gridx = 0;
197         gridBagConstraints.gridy = 4;
198         gridBagConstraints.gridwidth = 3;
199         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
200         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
201         add(extSourcesWarning, gridBagConstraints);
202
203         errorMessage.setForeground(UIManager.getColor("nb.errorForeground"));
204         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
205         gridBagConstraints.gridx = 0;
206         gridBagConstraints.gridy = 5;
207         gridBagConstraints.gridwidth = 3;
208         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
209         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
210         add(errorMessage, gridBagConstraints);
211
212         progress.setLayout(new java.awt.CardLayout JavaDoc());
213
214         progress.add(jPanel4, "not-progress");
215
216         progressImpl.setLayout(new java.awt.GridBagLayout JavaDoc());
217
218         org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "LBL_Copying_Moving", new Object JavaDoc[] {isMove ? new Integer JavaDoc(1) : new Integer JavaDoc(0)}));
219         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
220         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
221         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
222         gridBagConstraints.weightx = 1.0;
223         progressImpl.add(jLabel5, gridBagConstraints);
224
225         jPanel3.setLayout(new java.awt.BorderLayout JavaDoc());
226
227         jPanel3.add(ProgressHandleFactory.createProgressComponent(handle));
228         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
229         gridBagConstraints.gridx = 0;
230         gridBagConstraints.gridy = 1;
231         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
232         gridBagConstraints.weightx = 1.0;
233         progressImpl.add(jPanel3, gridBagConstraints);
234
235         progress.add(progressImpl, "progress");
236
237         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
238         gridBagConstraints.gridx = 0;
239         gridBagConstraints.gridy = 6;
240         gridBagConstraints.gridwidth = 3;
241         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
242         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
243         gridBagConstraints.weightx = 1.0;
244         gridBagConstraints.weighty = 0.5;
245         add(progress, gridBagConstraints);
246
247     }
248     // </editor-fold>//GEN-END:initComponents
249

250     private void browseActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseActionPerformed
251
// TODO add your handling code here:
252
File JavaDoc current = new File JavaDoc(projectLocation.getText());
253         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc(current);
254         
255         chooser.setMultiSelectionEnabled(false);
256         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
257         
258         if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
259             projectLocation.setText(chooser.getSelectedFile().getAbsolutePath());
260         }
261     }//GEN-LAST:event_browseActionPerformed
262

263     
264     // Variables declaration - do not modify//GEN-BEGIN:variables
265
private javax.swing.JButton JavaDoc browse;
266     private javax.swing.JLabel JavaDoc errorMessage;
267     private javax.swing.JLabel JavaDoc extSourcesWarning;
268     private javax.swing.JLabel JavaDoc jLabel1;
269     private javax.swing.JLabel JavaDoc jLabel2;
270     private javax.swing.JLabel JavaDoc jLabel4;
271     private javax.swing.JLabel JavaDoc jLabel5;
272     private javax.swing.JPanel JavaDoc jPanel3;
273     private javax.swing.JPanel JavaDoc jPanel4;
274     private javax.swing.JLabel JavaDoc nameLabel;
275     private javax.swing.JPanel JavaDoc progress;
276     private javax.swing.JPanel JavaDoc progressImpl;
277     private javax.swing.JTextField JavaDoc projectFolder;
278     private javax.swing.JTextField JavaDoc projectLocation;
279     private javax.swing.JTextField JavaDoc projectName;
280     // End of variables declaration//GEN-END:variables
281

282     private String JavaDoc lastComputedName;
283     
284     private String JavaDoc computeValidProjectName(String JavaDoc projectLocation, String JavaDoc projectNamePrefix) {
285         File JavaDoc location = new File JavaDoc(projectLocation);
286         
287         if (!location.exists()) {
288             lastComputedName = projectNamePrefix;
289             return projectNamePrefix;
290         }
291         
292         int num = 1;
293         String JavaDoc projectName = projectNamePrefix;
294         if (new File JavaDoc(location, projectName).exists()) {
295             while (new File JavaDoc(location, projectName = projectNamePrefix + "_" + num).exists()) {
296                 num++;
297             }
298         }
299         lastComputedName = projectName;
300         return projectName;
301     }
302     
303     private void setProject() {
304         FileObject parent = project.getProjectDirectory().getParent();
305         File JavaDoc parentFile = FileUtil.toFile(parent);
306         
307         projectLocation.setText(parentFile.getAbsolutePath());
308         
309         if (isMove) {
310             projectName.setText(ProjectUtils.getInformation(project).getName());
311         } else {
312             projectName.setText(computeValidProjectName(parentFile.getAbsolutePath(), ProjectUtils.getInformation(project).getName()));
313         }
314         
315         updateProjectFolder();
316         validateDialog();
317         
318         if (hasExternalSources() && !isMove) {
319             extSourcesWarning.setText(NbBundle.getMessage(ProjectCopyPanel.class, "WRN_External_Sources"));
320             invalid = true;
321         }
322     }
323     
324     private boolean hasExternalSources() {
325         for (FileObject file : ProjectOperations.getDataFiles(project)) {
326             if (!FileUtil.isParentOf(project.getProjectDirectory(), file)) {
327                 return true;
328             }
329         }
330         return false;
331     }
332     
333     public String JavaDoc getNewName() {
334         return projectName.getText();
335     }
336     
337     public String JavaDoc getProjectFolderName() {
338         return project.getProjectDirectory().getNameExt();
339     }
340     
341     public File JavaDoc getNewDirectory() {
342         return new File JavaDoc(projectLocation.getText());
343     }
344     
345     public void changedUpdate(DocumentEvent JavaDoc e) {
346         //ignored
347
}
348     
349     public void insertUpdate(DocumentEvent JavaDoc e) {
350         if (e.getDocument().equals(projectLocation.getDocument())) {
351             if (lastComputedName != null && lastComputedName.equals(projectName.getText())) {
352                 projectName.setText(computeValidProjectName(new File JavaDoc(projectLocation.getText()).getAbsolutePath(),
353                         ProjectUtils.getInformation(project).getName()));
354             }
355         }
356         updateProjectFolder();
357         validateDialog();
358     }
359     
360     public void removeUpdate(DocumentEvent JavaDoc e) {
361         if (e.getDocument().equals(projectLocation.getDocument())) {
362             if (lastComputedName != null && lastComputedName.equals(projectName.getText())) {
363                 projectName.setText(computeValidProjectName(new File JavaDoc(projectLocation.getText()).getAbsolutePath(),
364                         ProjectUtils.getInformation(project).getName()));
365             }
366         }
367         updateProjectFolder();
368         validateDialog();
369     }
370     
371     private void updateProjectFolder() {
372         final File JavaDoc location = new File JavaDoc(projectLocation.getText());
373         File JavaDoc projectFolderFile = location;
374         if (isMove) {
375             projectFolderFile = new File JavaDoc(location, project.getProjectDirectory().getNameExt());
376         } else {
377             projectFolderFile = new File JavaDoc(location, projectName.getText());
378         }
379         projectFolder.setText(projectFolderFile.getAbsolutePath());
380     }
381     
382     public boolean isPanelValid() {
383         return " ".equals(errorMessage.getText()) && !invalid;
384     }
385
386     private void validateDialog() {
387         if (invalid) {
388             //no reason to do anything:
389
return ;
390         }
391         
392         String JavaDoc newError = computeError();
393         boolean changed = false;
394         String JavaDoc currentError = errorMessage.getText();
395         
396         newError = newError != null ? newError : " ";
397         changed = !currentError.equals(newError);
398         
399         errorMessage.setText(newError);
400         
401         if (changed) {
402             ChangeListener JavaDoc[] listenersCopy;
403                     
404             synchronized (this) {
405                 listenersCopy = listeners.toArray(new ChangeListener JavaDoc[0]);
406             }
407             ChangeEvent JavaDoc evt = new ChangeEvent JavaDoc(this);
408             
409             for (ChangeListener JavaDoc l : listenersCopy) {
410                 l.stateChanged(evt);
411             }
412         }
413     }
414     
415     private String JavaDoc computeError() {
416         File JavaDoc location = new File JavaDoc(projectLocation.getText());
417         return DefaultProjectOperationsImplementation.computeError(location, projectName.getText(), projectFolder.getText(), false);
418     }
419     
420     public void showProgress() {
421         projectFolder.setEnabled(false);
422         projectLocation.setEnabled(false);
423         projectName.setEnabled(false);
424         browse.setEnabled(false);
425         
426         ((CardLayout JavaDoc) progress.getLayout()).show(progress, "progress");
427     }
428 }
429
Popular Tags