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 java.util.ArrayList;
046: import javax.swing.UIManager;
047: import javax.swing.event.ChangeEvent;
048: import javax.swing.event.ChangeListener;
049: import javax.swing.event.DocumentEvent;
050: import javax.swing.event.DocumentListener;
051: import org.netbeans.api.progress.ProgressHandle;
052: import org.netbeans.api.progress.ProgressHandleFactory;
053: import org.netbeans.api.project.Project;
054: import org.netbeans.api.project.ProjectUtils;
055: import org.netbeans.modules.project.uiapi.DefaultProjectOperationsImplementation.InvalidablePanel;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.util.ChangeSupport;
058:
059: /**
060: * @author Jan Lahoda
061: */
062: public class DefaultProjectRenamePanel extends javax.swing.JPanel
063: implements DocumentListener, InvalidablePanel {
064:
065: private Project project;
066: private final ChangeSupport changeSupport = new ChangeSupport(this );
067: private ProgressHandle handle;
068:
069: /**
070: * Creates new form DefaultProjectRenamePanel
071: */
072: public DefaultProjectRenamePanel(ProgressHandle handle,
073: Project project, String name) {
074: this .project = project;
075: this .handle = handle;
076:
077: if (name == null) {
078: name = ProjectUtils.getInformation(project)
079: .getDisplayName();
080: }
081:
082: initComponents();
083:
084: projectName.setText(name);
085: projectName.getDocument().addDocumentListener(this );
086: updateProjectFolder();
087: validateDialog();
088:
089: if (Boolean
090: .getBoolean("org.netbeans.modules.project.uiapi.DefaultProjectOperations.showProgress")) {
091: ((CardLayout) progress.getLayout()).show(progress,
092: "progress");
093: }
094: }
095:
096: public void addChangeListener(ChangeListener l) {
097: changeSupport.addChangeListener(l);
098: }
099:
100: public void removeChangeListener(ChangeListener l) {
101: changeSupport.removeChangeListener(l);
102: }
103:
104: /** This method is called from within the constructor to
105: * initialize the form.
106: * WARNING: Do NOT modify this code. The content of this method is
107: * always regenerated by the Form Editor.
108: */
109: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
110: private void initComponents() {
111: java.awt.GridBagConstraints gridBagConstraints;
112:
113: jLabel1 = new javax.swing.JLabel();
114: projectFolderLabel = new javax.swing.JLabel();
115: projectName = new javax.swing.JTextField();
116: projectFolder = new javax.swing.JTextField();
117: alsoRenameFolder = new javax.swing.JCheckBox();
118: jLabel3 = new javax.swing.JLabel();
119: errorMessage = new javax.swing.JLabel();
120: progress = new javax.swing.JPanel();
121: jPanel4 = new javax.swing.JPanel();
122: progressImpl = new javax.swing.JPanel();
123: jLabel5 = new javax.swing.JLabel();
124: jPanel3 = new javax.swing.JPanel();
125:
126: setLayout(new java.awt.GridBagLayout());
127:
128: getAccessibleContext().setAccessibleDescription(
129: org.openide.util.NbBundle.getMessage(
130: DefaultProjectRenamePanel.class,
131: "ACSD_Project_Rename", new Object[] {}));
132: jLabel1.setLabelFor(projectName);
133: org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
134: org.openide.util.NbBundle.getMessage(
135: DefaultProjectRenamePanel.class,
136: "LBL_Project_Name"));
137: gridBagConstraints = new java.awt.GridBagConstraints();
138: gridBagConstraints.gridx = 0;
139: gridBagConstraints.gridy = 1;
140: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
141: gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 12);
142: add(jLabel1, gridBagConstraints);
143:
144: projectFolderLabel.setLabelFor(projectFolder);
145: org.openide.awt.Mnemonics.setLocalizedText(projectFolderLabel,
146: org.openide.util.NbBundle.getMessage(
147: DefaultProjectRenamePanel.class,
148: "LBL_Project_Folder"));
149: gridBagConstraints = new java.awt.GridBagConstraints();
150: gridBagConstraints.gridx = 0;
151: gridBagConstraints.gridy = 3;
152: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
153: gridBagConstraints.insets = new java.awt.Insets(6, 18, 0, 12);
154: add(projectFolderLabel, gridBagConstraints);
155:
156: projectName.setColumns(30);
157: gridBagConstraints = new java.awt.GridBagConstraints();
158: gridBagConstraints.gridx = 1;
159: gridBagConstraints.gridy = 1;
160: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
161: gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 0);
162: add(projectName, gridBagConstraints);
163: projectName.getAccessibleContext().setAccessibleName(
164: org.openide.util.NbBundle.getMessage(
165: DefaultProjectRenamePanel.class,
166: "ACSN_Project_Name", new Object[] {}));
167: projectName.getAccessibleContext().setAccessibleDescription(
168: org.openide.util.NbBundle.getMessage(
169: DefaultProjectRenamePanel.class,
170: "ACSD_Project_Name", new Object[] {}));
171:
172: projectFolder.setColumns(30);
173: projectFolder.setEditable(false);
174: gridBagConstraints = new java.awt.GridBagConstraints();
175: gridBagConstraints.gridx = 1;
176: gridBagConstraints.gridy = 3;
177: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
178: gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
179: add(projectFolder, gridBagConstraints);
180: projectFolder.getAccessibleContext().setAccessibleName(
181: org.openide.util.NbBundle.getMessage(
182: DefaultProjectRenamePanel.class,
183: "ACSN_Project_Folder", new Object[] {}));
184: projectFolder.getAccessibleContext().setAccessibleDescription(
185: org.openide.util.NbBundle.getMessage(
186: DefaultProjectRenamePanel.class,
187: "ACSD_Project_Folder", new Object[] {}));
188:
189: org.openide.awt.Mnemonics.setLocalizedText(alsoRenameFolder,
190: org.openide.util.NbBundle.getMessage(
191: DefaultProjectRenamePanel.class,
192: "LBL_Also_Rename_Project_Folder"));
193: alsoRenameFolder.setBorder(javax.swing.BorderFactory
194: .createEmptyBorder(0, 0, 0, 0));
195: alsoRenameFolder.setMargin(new java.awt.Insets(0, 0, 0, 0));
196: alsoRenameFolder
197: .addActionListener(new java.awt.event.ActionListener() {
198: public void actionPerformed(
199: java.awt.event.ActionEvent evt) {
200: alsoRenameFolderActionPerformed(evt);
201: }
202: });
203:
204: gridBagConstraints = new java.awt.GridBagConstraints();
205: gridBagConstraints.gridx = 0;
206: gridBagConstraints.gridy = 2;
207: gridBagConstraints.gridwidth = 2;
208: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
209: gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 0);
210: add(alsoRenameFolder, gridBagConstraints);
211: alsoRenameFolder.getAccessibleContext().setAccessibleName(
212: org.openide.util.NbBundle.getMessage(
213: DefaultProjectRenamePanel.class,
214: "ACSN_Also_Rename_Project_Folder",
215: new Object[] {}));
216: alsoRenameFolder.getAccessibleContext()
217: .setAccessibleDescription(
218: org.openide.util.NbBundle.getMessage(
219: DefaultProjectRenamePanel.class,
220: "ACSD_Also_Rename_Project_Folder",
221: new Object[] {}));
222:
223: org.openide.awt.Mnemonics.setLocalizedText(jLabel3,
224: org.openide.util.NbBundle.getMessage(
225: DefaultProjectRenamePanel.class,
226: "LBL_Rename_Dialog_Text",
227: new Object[] { ProjectUtils.getInformation(
228: project).getDisplayName() }));
229: gridBagConstraints = new java.awt.GridBagConstraints();
230: gridBagConstraints.gridwidth = 2;
231: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
232: add(jLabel3, gridBagConstraints);
233:
234: errorMessage.setForeground(UIManager
235: .getColor("nb.errorForeground"));
236: gridBagConstraints = new java.awt.GridBagConstraints();
237: gridBagConstraints.gridx = 0;
238: gridBagConstraints.gridy = 4;
239: gridBagConstraints.gridwidth = 2;
240: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
241: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
242: gridBagConstraints.weightx = 1.0;
243: gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 0);
244: add(errorMessage, gridBagConstraints);
245:
246: progress.setLayout(new java.awt.CardLayout());
247:
248: progress.add(jPanel4, "not-progress");
249:
250: progressImpl.setLayout(new java.awt.GridBagLayout());
251:
252: org.openide.awt.Mnemonics.setLocalizedText(jLabel5,
253: org.openide.util.NbBundle.getMessage(
254: DefaultProjectRenamePanel.class,
255: "LBL_Renaming_Project", new Object[] {}));
256: gridBagConstraints = new java.awt.GridBagConstraints();
257: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
258: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
259: gridBagConstraints.weightx = 1.0;
260: progressImpl.add(jLabel5, gridBagConstraints);
261:
262: jPanel3.setLayout(new java.awt.BorderLayout());
263:
264: jPanel3.add(ProgressHandleFactory
265: .createProgressComponent(handle));
266: gridBagConstraints = new java.awt.GridBagConstraints();
267: gridBagConstraints.gridx = 0;
268: gridBagConstraints.gridy = 1;
269: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
270: gridBagConstraints.weightx = 1.0;
271: progressImpl.add(jPanel3, gridBagConstraints);
272:
273: progress.add(progressImpl, "progress");
274:
275: gridBagConstraints = new java.awt.GridBagConstraints();
276: gridBagConstraints.gridx = 0;
277: gridBagConstraints.gridy = 5;
278: gridBagConstraints.gridwidth = 2;
279: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
280: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
281: gridBagConstraints.weightx = 1.0;
282: gridBagConstraints.weighty = 1.0;
283: gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 0);
284: add(progress, gridBagConstraints);
285:
286: }
287:
288: // </editor-fold>//GEN-END:initComponents
289:
290: private void alsoRenameFolderActionPerformed(
291: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alsoRenameFolderActionPerformed
292: updateProjectFolder();
293: validateDialog();
294: }//GEN-LAST:event_alsoRenameFolderActionPerformed
295:
296: // Variables declaration - do not modify//GEN-BEGIN:variables
297: private javax.swing.JCheckBox alsoRenameFolder;
298: private javax.swing.JLabel errorMessage;
299: private javax.swing.JLabel jLabel1;
300: private javax.swing.JLabel jLabel3;
301: private javax.swing.JLabel jLabel5;
302: private javax.swing.JPanel jPanel3;
303: private javax.swing.JPanel jPanel4;
304: private javax.swing.JPanel progress;
305: private javax.swing.JPanel progressImpl;
306: private javax.swing.JTextField projectFolder;
307: private javax.swing.JLabel projectFolderLabel;
308: private javax.swing.JTextField projectName;
309:
310: // End of variables declaration//GEN-END:variables
311:
312: public String getNewName() {
313: return projectName.getText();
314: }
315:
316: public boolean getRenameProjectFolder() {
317: return alsoRenameFolder.isSelected();
318: }
319:
320: public void changedUpdate(DocumentEvent e) {
321: //ignored
322: }
323:
324: public void insertUpdate(DocumentEvent e) {
325: updateProjectFolder();
326: validateDialog();
327: }
328:
329: public void removeUpdate(DocumentEvent e) {
330: updateProjectFolder();
331: validateDialog();
332: }
333:
334: private void updateProjectFolder() {
335: File location = FileUtil.toFile(project.getProjectDirectory()
336: .getParent());
337: File projectFolderFile;
338: if (alsoRenameFolder.isSelected()) {
339: projectFolderFile = new File(location, projectName
340: .getText());
341: } else {
342: projectFolderFile = new File(location, project
343: .getProjectDirectory().getName());
344: }
345:
346: projectFolder.setText(projectFolderFile.getAbsolutePath());
347: }
348:
349: public boolean isPanelValid() {
350: return " ".equals(errorMessage.getText());
351: }
352:
353: private void validateDialog() {
354: String newError = computeError();
355: boolean changed = false;
356: String currentError = errorMessage.getText();
357:
358: newError = newError != null ? newError : " ";
359: changed = !currentError.equals(newError);
360:
361: errorMessage.setText(newError);
362:
363: if (changed) {
364: changeSupport.fireChange();
365: }
366: }
367:
368: private String computeError() {
369: File location = FileUtil.toFile(project.getProjectDirectory()
370: .getParent());
371:
372: return DefaultProjectOperationsImplementation.computeError(
373: location, projectName.getText(),
374: !getRenameProjectFolder());
375: }
376:
377: public void showProgress() {
378: projectFolder.setEnabled(false);
379: projectName.setEnabled(false);
380: alsoRenameFolder.setEnabled(false);
381: progress.setVisible(true);
382:
383: ((CardLayout) progress.getLayout()).last(progress);
384: }
385:
386: }
|