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.refactoring.java.plugins;
042:
043: import java.io.IOException;
044: import java.text.MessageFormat;
045: import java.util.StringTokenizer;
046: import org.netbeans.api.fileinfo.NonRecursiveFolder;
047: import org.netbeans.api.java.classpath.ClassPath;
048: import org.netbeans.api.java.classpath.ClassPath;
049: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
050: import org.netbeans.modules.refactoring.spi.Transaction;
051: import org.netbeans.modules.refactoring.api.Problem;
052: import org.netbeans.modules.refactoring.api.RefactoringSession;
053: import org.netbeans.modules.refactoring.api.RenameRefactoring;
054: import org.netbeans.modules.refactoring.java.RetoucheUtils;
055: import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
056: import org.netbeans.modules.refactoring.spi.RefactoringPlugin;
057: import org.netbeans.modules.refactoring.spi.RefactoringPluginFactory;
058: import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImplementation;
059: import org.openide.ErrorManager;
060: import org.openide.filesystems.FileObject;
061: import org.openide.filesystems.FileSystem.AtomicAction;
062: import org.openide.loaders.DataFolder;
063: import org.openide.loaders.DataObject;
064: import org.openide.text.PositionBounds;
065: import org.openide.util.Exceptions;
066: import org.openide.util.Lookup;
067: import org.openide.util.NbBundle;
068: import org.openide.util.lookup.Lookups;
069:
070: /**
071: *
072: * @author Jan Becicka
073: */
074: public class PackageRename implements RefactoringPluginFactory {
075:
076: /** Creates a new instance of PackageRename */
077: public PackageRename() {
078: }
079:
080: public RefactoringPlugin createInstance(
081: AbstractRefactoring refactoring) {
082: if (refactoring instanceof RenameRefactoring) {
083: if (refactoring.getRefactoringSource().lookup(
084: NonRecursiveFolder.class) != null) {
085: return new PackageRenamePlugin(
086: (RenameRefactoring) refactoring);
087: }
088: }
089: return null;
090: }
091:
092: public class PackageRenamePlugin implements RefactoringPlugin {
093: private RenameRefactoring refactoring;
094:
095: /** Creates a new instance of PackageRenamePlugin */
096: public PackageRenamePlugin(RenameRefactoring refactoring) {
097: this .refactoring = refactoring;
098: }
099:
100: public Problem preCheck() {
101: return null;
102: }
103:
104: public Problem prepare(RefactoringElementsBag elements) {
105: elements
106: .addFileChange(refactoring,
107: new RenameNonRecursiveFolder(refactoring
108: .getRefactoringSource().lookup(
109: NonRecursiveFolder.class),
110: elements));
111: return null;
112: }
113:
114: public Problem fastCheckParameters() {
115: String newName = refactoring.getNewName();
116: if (!RetoucheUtils.isValidPackageName(newName)) {
117: String msg = new MessageFormat(NbBundle.getMessage(
118: RenameRefactoringPlugin.class,
119: "ERR_InvalidPackage"))
120: .format(new Object[] { newName });
121: return new Problem(true, msg);
122: }
123:
124: ClassPath projectClassPath = ClassPath.getClassPath(
125: refactoring.getRefactoringSource().lookup(
126: NonRecursiveFolder.class).getFolder(),
127: ClassPath.SOURCE);
128: if (projectClassPath
129: .findResource(newName.replace('.', '/')) != null) {
130: String msg = new MessageFormat(NbBundle.getMessage(
131: RenameRefactoringPlugin.class,
132: "ERR_PackageExists"))
133: .format(new Object[] { newName });
134: return new Problem(true, msg);
135: }
136: return null;
137: }
138:
139: public Problem checkParameters() {
140: return null;
141: }
142:
143: public void cancelRequest() {
144: }
145:
146: private class RenameNonRecursiveFolder extends
147: SimpleRefactoringElementImplementation {
148:
149: private FileObject folder;
150: private RefactoringElementsBag session;
151: private String oldName;
152: private FileObject root;
153: private String currentName;
154:
155: public RenameNonRecursiveFolder(NonRecursiveFolder nrfo,
156: RefactoringElementsBag session) {
157: this .folder = nrfo.getFolder();
158: this .session = session;
159: ClassPath cp = ClassPath.getClassPath(folder,
160: ClassPath.SOURCE);
161: this .currentName = cp.getResourceName(folder, '.',
162: false);
163: this .oldName = this .currentName;
164: this .root = cp.findOwnerRoot(folder);
165:
166: }
167:
168: public String getText() {
169: return NbBundle.getMessage(PackageRename.class,
170: "TXT_RenamePackage")
171: + folder.getNameExt();
172: }
173:
174: public String getDisplayText() {
175: return getText();
176: }
177:
178: public void performChange() {
179: atomicSetName(refactoring.getNewName());
180: }
181:
182: public void undoChange() {
183: atomicSetName(oldName);
184: }
185:
186: private void atomicSetName(final String name) {
187: try {
188: folder.getFileSystem().runAtomicAction(
189: new AtomicAction() {
190: public void run() throws IOException {
191: setName(name);
192: }
193: });
194: } catch (IOException ex) {
195: Exceptions.printStackTrace(ex);
196: }
197: }
198:
199: public Lookup getLookup() {
200: return Lookups.singleton(folder.getParent());
201: }
202:
203: public FileObject getParentFile() {
204: return folder.getParent();
205: }
206:
207: public PositionBounds getPosition() {
208: return null;
209: }
210:
211: /**
212: *copy paste from PackageViewChildren
213: */
214: public void setName(String name) {
215: if (currentName.equals(name)) {
216: return;
217: }
218: // if (!isValidPackageName (name)) {
219: // DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message (
220: // NbBundle.getMessage(PackageViewChildren.class,"MSG_InvalidPackageName"), NotifyDescriptor.INFORMATION_MESSAGE));
221: // return;
222: // }
223: name = name.replace('.', '/') + '/'; //NOI18N
224: currentName = currentName.replace('.', '/') + '/'; //NOI18N
225: int i;
226: for (i = 0; i < currentName.length()
227: && i < name.length(); i++) {
228: if (currentName.charAt(i) != name.charAt(i)) {
229: break;
230: }
231: }
232: i--;
233: int index = currentName.lastIndexOf('/', i); //NOI18N
234: String commonPrefix = index == -1 ? null : currentName
235: .substring(0, index);
236: String toCreate = (index + 1 == name.length()) ? ""
237: : name.substring(index + 1); //NOI18N
238: try {
239: FileObject commonFolder = commonPrefix == null ? this .root
240: : this .root.getFileObject(commonPrefix);
241: FileObject destination = commonFolder;
242: StringTokenizer dtk = new StringTokenizer(toCreate,
243: "/"); //NOI18N
244: while (dtk.hasMoreTokens()) {
245: String pathElement = dtk.nextToken();
246: FileObject tmp = destination
247: .getFileObject(pathElement);
248: if (tmp == null) {
249: tmp = destination.createFolder(pathElement);
250: }
251: destination = tmp;
252: }
253: FileObject folder = this .folder;
254: DataFolder sourceFolder = DataFolder
255: .findFolder(folder);
256: DataFolder destinationFolder = DataFolder
257: .findFolder(destination);
258: DataObject[] children = sourceFolder.getChildren();
259: for (int j = 0; j < children.length; j++) {
260: if (children[j].getPrimaryFile().isData()) {
261: children[j].move(destinationFolder);
262: }
263: }
264: while (!commonFolder.equals(folder)) {
265: if (folder.getChildren().length == 0) {
266: FileObject tmp = folder;
267: folder = folder.getParent();
268: tmp.delete();
269: } else {
270: break;
271: }
272: }
273: this .folder = destinationFolder.getPrimaryFile();
274: } catch (IOException ioe) {
275: ErrorManager.getDefault().notify(ioe);
276: }
277: this.currentName = name;
278: }
279: }
280: }
281: }
|