KickJava   Java API By Example, From Geeks To Geeks.

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


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.UIManager JavaDoc;
26 import javax.swing.event.ChangeEvent JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
28 import javax.swing.event.DocumentEvent JavaDoc;
29 import javax.swing.event.DocumentListener JavaDoc;
30 import org.netbeans.api.progress.ProgressHandle;
31 import org.netbeans.api.progress.ProgressHandleFactory;
32 import org.netbeans.api.project.Project;
33 import org.netbeans.api.project.ProjectUtils;
34 import org.netbeans.modules.project.uiapi.DefaultProjectOperationsImplementation.InvalidablePanel;
35 import org.openide.filesystems.FileUtil;
36
37 /**
38  * @author Jan Lahoda
39  */

40 public class DefaultProjectRenamePanel extends javax.swing.JPanel JavaDoc implements DocumentListener JavaDoc, InvalidablePanel {
41     
42     private Project project;
43     private List JavaDoc<ChangeListener JavaDoc> listeners;
44     private ProgressHandle handle;
45     
46     /**
47      * Creates new form DefaultProjectRenamePanel
48      */

49     public DefaultProjectRenamePanel(ProgressHandle handle, Project project, String JavaDoc name) {
50         this.project = project;
51         this.handle = handle;
52         
53         if (name == null) {
54             name = ProjectUtils.getInformation(project).getDisplayName();
55         }
56         
57         this.listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
58         
59         initComponents();
60         
61         projectName.setText(name);
62         projectName.getDocument().addDocumentListener(this);
63         updateProjectFolder();
64         validateDialog();
65         
66         if (Boolean.getBoolean("org.netbeans.modules.project.uiapi.DefaultProjectOperations.showProgress")) {
67             ((CardLayout JavaDoc) progress.getLayout()).show(progress, "progress");
68         }
69     }
70     
71     public synchronized void addChangeListener(ChangeListener JavaDoc l) {
72         listeners.add(l);
73     }
74     
75     public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
76         listeners.remove(l);
77     }
78     
79     /** This method is called from within the constructor to
80      * initialize the form.
81      * WARNING: Do NOT modify this code. The content of this method is
82      * always regenerated by the Form Editor.
83      */

84     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
85
private void initComponents() {
86         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
87
88         jLabel1 = new javax.swing.JLabel JavaDoc();
89         projectFolderLabel = new javax.swing.JLabel JavaDoc();
90         projectName = new javax.swing.JTextField JavaDoc();
91         projectFolder = new javax.swing.JTextField JavaDoc();
92         alsoRenameFolder = new javax.swing.JCheckBox JavaDoc();
93         jLabel3 = new javax.swing.JLabel JavaDoc();
94         errorMessage = new javax.swing.JLabel JavaDoc();
95         progress = new javax.swing.JPanel JavaDoc();
96         jPanel4 = new javax.swing.JPanel JavaDoc();
97         progressImpl = new javax.swing.JPanel JavaDoc();
98         jLabel5 = new javax.swing.JLabel JavaDoc();
99         jPanel3 = new javax.swing.JPanel JavaDoc();
100
101         setLayout(new java.awt.GridBagLayout JavaDoc());
102
103         getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "ACSD_Project_Rename", new Object JavaDoc[] {}));
104         jLabel1.setLabelFor(projectName);
105         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "LBL_Project_Name"));
106         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
107         gridBagConstraints.gridx = 0;
108         gridBagConstraints.gridy = 1;
109         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
110         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 12);
111         add(jLabel1, gridBagConstraints);
112
113         projectFolderLabel.setLabelFor(projectFolder);
114         org.openide.awt.Mnemonics.setLocalizedText(projectFolderLabel, org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "LBL_Project_Folder"));
115         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
116         gridBagConstraints.gridx = 0;
117         gridBagConstraints.gridy = 3;
118         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
119         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 18, 0, 12);
120         add(projectFolderLabel, gridBagConstraints);
121
122         projectName.setColumns(30);
123         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
124         gridBagConstraints.gridx = 1;
125         gridBagConstraints.gridy = 1;
126         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
127         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
128         add(projectName, gridBagConstraints);
129         projectName.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "ACSN_Project_Name", new Object JavaDoc[] {}));
130         projectName.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "ACSD_Project_Name", new Object JavaDoc[] {}));
131
132         projectFolder.setColumns(30);
133         projectFolder.setEditable(false);
134         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
135         gridBagConstraints.gridx = 1;
136         gridBagConstraints.gridy = 3;
137         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
138         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 0, 0);
139         add(projectFolder, gridBagConstraints);
140         projectFolder.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "ACSN_Project_Folder", new Object JavaDoc[] {}));
141         projectFolder.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "ACSD_Project_Folder", new Object JavaDoc[] {}));
142
143         org.openide.awt.Mnemonics.setLocalizedText(alsoRenameFolder, org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "LBL_Also_Rename_Project_Folder"));
144         alsoRenameFolder.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
145         alsoRenameFolder.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
146         alsoRenameFolder.addActionListener(new java.awt.event.ActionListener JavaDoc() {
147             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
148                 alsoRenameFolderActionPerformed(evt);
149             }
150         });
151
152         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
153         gridBagConstraints.gridx = 0;
154         gridBagConstraints.gridy = 2;
155         gridBagConstraints.gridwidth = 2;
156         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
157         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
158         add(alsoRenameFolder, gridBagConstraints);
159         alsoRenameFolder.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "ACSN_Also_Rename_Project_Folder", new Object JavaDoc[] {}));
160         alsoRenameFolder.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "ACSD_Also_Rename_Project_Folder", new Object JavaDoc[] {}));
161
162         org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "LBL_Rename_Dialog_Text", new Object JavaDoc[] {ProjectUtils.getInformation(project).getDisplayName()}));
163         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
164         gridBagConstraints.gridwidth = 2;
165         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
166         add(jLabel3, gridBagConstraints);
167
168         errorMessage.setForeground(UIManager.getColor("nb.errorForeground"));
169         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
170         gridBagConstraints.gridx = 0;
171         gridBagConstraints.gridy = 4;
172         gridBagConstraints.gridwidth = 2;
173         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
174         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
175         gridBagConstraints.weightx = 1.0;
176         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
177         add(errorMessage, gridBagConstraints);
178
179         progress.setLayout(new java.awt.CardLayout JavaDoc());
180
181         progress.add(jPanel4, "not-progress");
182
183         progressImpl.setLayout(new java.awt.GridBagLayout JavaDoc());
184
185         org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "LBL_Renaming_Project", new Object JavaDoc[] {}));
186         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
187         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
188         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
189         gridBagConstraints.weightx = 1.0;
190         progressImpl.add(jLabel5, gridBagConstraints);
191
192         jPanel3.setLayout(new java.awt.BorderLayout JavaDoc());
193
194         jPanel3.add(ProgressHandleFactory.createProgressComponent(handle));
195         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
196         gridBagConstraints.gridx = 0;
197         gridBagConstraints.gridy = 1;
198         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
199         gridBagConstraints.weightx = 1.0;
200         progressImpl.add(jPanel3, gridBagConstraints);
201
202         progress.add(progressImpl, "progress");
203
204         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
205         gridBagConstraints.gridx = 0;
206         gridBagConstraints.gridy = 5;
207         gridBagConstraints.gridwidth = 2;
208         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
209         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
210         gridBagConstraints.weightx = 1.0;
211         gridBagConstraints.weighty = 1.0;
212         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
213         add(progress, gridBagConstraints);
214
215     }
216     // </editor-fold>//GEN-END:initComponents
217

218     private void alsoRenameFolderActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_alsoRenameFolderActionPerformed
219
validateDialog();
220     }//GEN-LAST:event_alsoRenameFolderActionPerformed
221

222     
223     // Variables declaration - do not modify//GEN-BEGIN:variables
224
private javax.swing.JCheckBox JavaDoc alsoRenameFolder;
225     private javax.swing.JLabel JavaDoc errorMessage;
226     private javax.swing.JLabel JavaDoc jLabel1;
227     private javax.swing.JLabel JavaDoc jLabel3;
228     private javax.swing.JLabel JavaDoc jLabel5;
229     private javax.swing.JPanel JavaDoc jPanel3;
230     private javax.swing.JPanel JavaDoc jPanel4;
231     private javax.swing.JPanel JavaDoc progress;
232     private javax.swing.JPanel JavaDoc progressImpl;
233     private javax.swing.JTextField JavaDoc projectFolder;
234     private javax.swing.JLabel JavaDoc projectFolderLabel;
235     private javax.swing.JTextField JavaDoc projectName;
236     // End of variables declaration//GEN-END:variables
237

238     public String JavaDoc getNewName() {
239         return projectName.getText();
240     }
241     
242     public boolean getRenameProjectFolder() {
243         return alsoRenameFolder.isSelected();
244     }
245     
246     public void changedUpdate(DocumentEvent JavaDoc e) {
247         //ignored
248
}
249     
250     public void insertUpdate(DocumentEvent JavaDoc e) {
251         updateProjectFolder();
252         validateDialog();
253     }
254     
255     public void removeUpdate(DocumentEvent JavaDoc e) {
256         updateProjectFolder();
257         validateDialog();
258     }
259     
260     private void updateProjectFolder() {
261         File JavaDoc location = FileUtil.toFile(project.getProjectDirectory().getParent());
262         File JavaDoc projectFolderFile = new File JavaDoc(location, projectName.getText());
263         
264         projectFolder.setText(projectFolderFile.getAbsolutePath());
265     }
266     
267     public boolean isPanelValid() {
268         return " ".equals(errorMessage.getText());
269     }
270
271     private void validateDialog() {
272         String JavaDoc newError = computeError();
273         boolean changed = false;
274         String JavaDoc currentError = errorMessage.getText();
275         
276         newError = newError != null ? newError : " ";
277         changed = !currentError.equals(newError);
278         
279         errorMessage.setText(newError);
280         
281         if (changed) {
282             ChangeListener JavaDoc[] listenersCopy;
283                     
284             synchronized (this) {
285                 listenersCopy = listeners.toArray(new ChangeListener JavaDoc[0]);
286             }
287             ChangeEvent JavaDoc evt = new ChangeEvent JavaDoc(this);
288             
289             for (ChangeListener JavaDoc l : listenersCopy) {
290                 l.stateChanged(evt);
291             }
292         }
293     }
294     
295     private String JavaDoc computeError() {
296         File JavaDoc location = FileUtil.toFile(project.getProjectDirectory().getParent());
297         
298         return DefaultProjectOperationsImplementation.computeError(location, projectName.getText(), !getRenameProjectFolder());
299     }
300     
301     public void showProgress() {
302         projectFolder.setEnabled(false);
303         projectName.setEnabled(false);
304         alsoRenameFolder.setEnabled(false);
305         progress.setVisible(true);
306         
307         ((CardLayout JavaDoc) progress.getLayout()).last(progress);
308     }
309     
310 }
311
Popular Tags