# HG changeset patch # User Ralph Benjamin Ruijs # Parent c4a3abc5fbb0dd646d89ff43d18d3fb864273968 [mq]: CopyRefactoringAPI.diff diff --git a/refactoring.api/apichanges.xml b/refactoring.api/apichanges.xml --- a/refactoring.api/apichanges.xml +++ b/refactoring.api/apichanges.xml @@ -49,6 +49,21 @@ Refactoring API + + + Added CopyRefactoring to support Copy of multiple files. + + + + + +

+ Added support to copy multiple files at once. +

+
+ + +
Scope added to allow to specify a limited scope for WhereUsedQuery diff --git a/refactoring.api/manifest.mf b/refactoring.api/manifest.mf --- a/refactoring.api/manifest.mf +++ b/refactoring.api/manifest.mf @@ -2,6 +2,6 @@ OpenIDE-Module: org.netbeans.modules.refactoring.api OpenIDE-Module-Layer: org/netbeans/modules/refactoring/api/resources/layer.xml OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/refactoring/api/resources/Bundle.properties -OpenIDE-Module-Specification-Version: 1.20 +OpenIDE-Module-Specification-Version: 1.21 AutoUpdate-Show-In-Client: false diff --git a/refactoring.api/nbproject/project.xml b/refactoring.api/nbproject/project.xml --- a/refactoring.api/nbproject/project.xml +++ b/refactoring.api/nbproject/project.xml @@ -200,6 +200,11 @@ org.netbeans.modules.projectui + + org.openide.util.lookup + + + diff --git a/refactoring.api/src/org/netbeans/modules/refactoring/api/CopyRefactoring.java b/refactoring.api/src/org/netbeans/modules/refactoring/api/CopyRefactoring.java new file mode 100644 --- /dev/null +++ b/refactoring.api/src/org/netbeans/modules/refactoring/api/CopyRefactoring.java @@ -0,0 +1,109 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ + +package org.netbeans.modules.refactoring.api; + +import org.netbeans.api.annotations.common.CheckForNull; +import org.netbeans.api.annotations.common.NonNull; +import org.openide.util.Lookup; +import org.openide.util.Parameters; + +/** + * This class is just holder for parameters of Copy Refactoring. + * Refactoring itself is implemented in plugins. + * + * @see org.netbeans.modules.refactoring.spi.RefactoringPlugin + * @see org.netbeans.modules.refactoring.spi.RefactoringPluginFactory + * @see AbstractRefactoring + * @see RefactoringSession + * @author Ralph Ruijs + * @since 1.21 + */ +public final class CopyRefactoring extends AbstractRefactoring { + + private Lookup target; + + /** + * Creates a new instance of CopyRefactoring. + * Copy Refactoring implementations currently understand the following types: + * + * + * + * + *
ModuleTypes the Module UnderstandsImplementation
Refactoring API (Default impl.){@link org.openide.filesystems.FileObject}(s)Does file copy
Java Refactoring
    + *
  • {@link org.openide.filesystems.FileObject}(s) with content type text/x-java (class copy) + *
+ *
Updates name, package declaration and import statements
+ * @param objectsToCopy Object to be copied stored into Lookup + */ + public CopyRefactoring (@NonNull Lookup objectsToCopy) { + super(objectsToCopy); + } + + /** + * Target for copying. + * Copy Refactoring implementations currently understand the following types: + * + * + * + * + * + *
ModuleTypes the Module UnderstandsImplementation
Refactoring API (Default impl.){@link java.net.URL}Creates directory corresponding to specified URL (if it does not exist) and copies all FileObjects into this folder.
Java Refactoring{@link java.net.URL}Updates name, package declaration and import statements
+ * @param target + */ + public void setTarget(@NonNull Lookup target) { + Parameters.notNull("target", target); // NOI18N + this.target = target; + } + + /** + * Target for moving + * @see #setTarget + * @return target + */ + @CheckForNull + public Lookup getTarget() { + return this.target; + } +} diff --git a/refactoring.api/src/org/netbeans/modules/refactoring/plugins/CopyFile.java b/refactoring.api/src/org/netbeans/modules/refactoring/plugins/CopyFile.java new file mode 100644 --- /dev/null +++ b/refactoring.api/src/org/netbeans/modules/refactoring/plugins/CopyFile.java @@ -0,0 +1,133 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.modules.refactoring.plugins; + +import java.io.IOException; +import java.net.URL; +import java.util.Arrays; +import org.netbeans.modules.refactoring.api.Context; +import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImplementation; +import org.openide.ErrorManager; +import org.openide.filesystems.FileObject; +import org.openide.loaders.DataFolder; +import org.openide.loaders.DataObject; +import org.openide.text.PositionBounds; +import org.openide.util.Lookup; +import org.openide.util.NbBundle; + +/** + * + * @author Ralph Ruijs + */ +public class CopyFile extends SimpleRefactoringElementImplementation { + + private FileObject fo; + private DataObject newOne; + private final URL target; + private final String newName; + private final Context context; + + public CopyFile(FileObject fo, URL target, String newName, Context context) { + this.fo = fo; + this.target = target; + this.newName = newName; + this.context = context; + } + + @Override + public String getText() { + return NbBundle.getMessage(CopyFile.class, "TXT_CopyFile", fo.getNameExt()); + } + + @Override + public String getDisplayText() { + return getText(); + } + + @Override + public void performChange() { + try { + FileObject targetFo = FileHandlingFactory.getOrCreateFolder(target); + FileObject Fo = fo; + DataObject dob = DataObject.find(Fo); + newOne = dob.copy(DataFolder.findFolder(targetFo)); + newOne.rename(newName == null ? fo.getName() : newName); + FileObject[] newFiles = context.lookup(FileObject[].class); + if (newFiles == null) { + newFiles = new FileObject[]{newOne.getPrimaryFile()}; + } else { + newFiles = Arrays.copyOf(newFiles, newFiles.length + 1); + newFiles[newFiles.length - 1] = newOne.getPrimaryFile(); + } + context.add(newFiles); + context.add(newOne.getPrimaryFile()); + } catch (IOException ex) { + throw new IllegalStateException(ex); + } + } + + @Override + public void undoChange() { + try { + if (newOne != null) { + newOne.delete(); + } + } catch (IOException ex) { + ErrorManager.getDefault().notify(ex); + } + } + + @Override + public Lookup getLookup() { + return Lookup.EMPTY; + } + + @Override + public FileObject getParentFile() { + return fo; + } + + @Override + public PositionBounds getPosition() { + return null; + } +} diff --git a/refactoring.api/src/org/netbeans/modules/refactoring/plugins/FileHandlingFactory.java b/refactoring.api/src/org/netbeans/modules/refactoring/plugins/FileHandlingFactory.java --- a/refactoring.api/src/org/netbeans/modules/refactoring/plugins/FileHandlingFactory.java +++ b/refactoring.api/src/org/netbeans/modules/refactoring/plugins/FileHandlingFactory.java @@ -49,11 +49,7 @@ import java.net.URL; import java.util.Collection; import org.netbeans.api.fileinfo.NonRecursiveFolder; -import org.netbeans.modules.refactoring.api.AbstractRefactoring; -import org.netbeans.modules.refactoring.api.MoveRefactoring; -import org.netbeans.modules.refactoring.api.RenameRefactoring; -import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring; -import org.netbeans.modules.refactoring.api.SingleCopyRefactoring; +import org.netbeans.modules.refactoring.api.*; import org.netbeans.modules.refactoring.spi.RefactoringPlugin; import org.netbeans.modules.refactoring.spi.RefactoringPluginFactory; import org.openide.filesystems.FileObject; @@ -94,9 +90,9 @@ return new FileDeletePlugin((SafeDeleteRefactoring) refactoring); } } - } else if (refactoring instanceof SingleCopyRefactoring) { + } else if (refactoring instanceof SingleCopyRefactoring || refactoring instanceof CopyRefactoring) { if (!o.isEmpty()) { - return new FileCopyPlugin((SingleCopyRefactoring) refactoring); + return new FilesCopyPlugin(refactoring); } } return null; diff --git a/refactoring.api/src/org/netbeans/modules/refactoring/plugins/FileCopyPlugin.java b/refactoring.api/src/org/netbeans/modules/refactoring/plugins/FilesCopyPlugin.java rename from refactoring.api/src/org/netbeans/modules/refactoring/plugins/FileCopyPlugin.java rename to refactoring.api/src/org/netbeans/modules/refactoring/plugins/FilesCopyPlugin.java --- a/refactoring.api/src/org/netbeans/modules/refactoring/plugins/FileCopyPlugin.java +++ b/refactoring.api/src/org/netbeans/modules/refactoring/plugins/FilesCopyPlugin.java @@ -43,31 +43,25 @@ */ package org.netbeans.modules.refactoring.plugins; -import java.io.IOException; import java.net.URL; +import java.util.Collection; +import org.netbeans.modules.refactoring.api.AbstractRefactoring; +import org.netbeans.modules.refactoring.api.CopyRefactoring; +import org.netbeans.modules.refactoring.api.Problem; import org.netbeans.modules.refactoring.api.SingleCopyRefactoring; -import org.netbeans.modules.refactoring.api.Problem; -import org.netbeans.modules.refactoring.api.RefactoringSession; import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; import org.netbeans.modules.refactoring.spi.RefactoringPlugin; -import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImplementation; -import org.openide.ErrorManager; import org.openide.filesystems.FileObject; -import org.openide.loaders.DataFolder; -import org.openide.loaders.DataObject; -import org.openide.text.PositionBounds; -import org.openide.util.Lookup; -import org.openide.util.NbBundle; /** * - * @author Jan Becicka + * @author Ralph Ruijs */ -public class FileCopyPlugin implements RefactoringPlugin { - private SingleCopyRefactoring refactoring; +public class FilesCopyPlugin implements RefactoringPlugin { + private AbstractRefactoring refactoring; /** Creates a new instance of WhereUsedQuery */ - public FileCopyPlugin(SingleCopyRefactoring refactoring) { + public FilesCopyPlugin(AbstractRefactoring refactoring) { this.refactoring = refactoring; } @@ -78,7 +72,21 @@ @Override public Problem prepare(RefactoringElementsBag elements) { - elements.add(refactoring, new CopyFile(refactoring.getRefactoringSource().lookup(FileObject.class), elements.getSession())); + URL target = null; + String newName = null; + if(refactoring instanceof SingleCopyRefactoring) { + SingleCopyRefactoring scr = (SingleCopyRefactoring) refactoring; + target = scr.getTarget().lookup(URL.class); + newName = scr.getNewName(); + } else if(refactoring instanceof CopyRefactoring) { + CopyRefactoring scr = (CopyRefactoring) refactoring; + target = scr.getTarget().lookup(URL.class); + } + + Collection fileObjects = refactoring.getRefactoringSource().lookupAll(FileObject.class); + for (FileObject fileObject : fileObjects) { + elements.add(refactoring, new CopyFile(fileObject, target, newName, refactoring.getContext())); + } return null; } @@ -95,64 +103,4 @@ @Override public void cancelRequest() { } - - private class CopyFile extends SimpleRefactoringElementImplementation { - - private FileObject fo; - private RefactoringSession session; - private DataObject newOne; - public CopyFile(FileObject fo, RefactoringSession session) { - this.fo = fo; - this.session = session; - } - @Override - public String getText() { - return NbBundle.getMessage(FileCopyPlugin.class, "TXT_CopyFile", fo.getNameExt()); - } - - @Override - public String getDisplayText() { - return getText(); - } - - @Override - public void performChange() { - try { - FileObject fo = FileHandlingFactory.getOrCreateFolder(refactoring.getTarget().lookup(URL.class)); - FileObject source = refactoring.getRefactoringSource().lookup(FileObject.class); - DataObject dob = DataObject.find(source); - newOne = dob.copy(DataFolder.findFolder(fo)); - newOne.rename(refactoring.getNewName()); - refactoring.getContext().add(newOne.getPrimaryFile()); - } catch (IOException ex) { - throw new IllegalStateException(ex); - } - } - - @Override - public void undoChange() { - try { - if (newOne != null) { - newOne.delete(); - } - } catch (IOException ex) { - ErrorManager.getDefault().notify(ex); - } - } - - @Override - public Lookup getLookup() { - return Lookup.EMPTY; - } - - @Override - public FileObject getParentFile() { - return fo; - } - - @Override - public PositionBounds getPosition() { - return null; - } - } } diff --git a/refactoring.api/test/unit/src/org/netbeans/modules/refactoring/plugins/FilesCopyPluginTest.java b/refactoring.api/test/unit/src/org/netbeans/modules/refactoring/plugins/FilesCopyPluginTest.java new file mode 100644 --- /dev/null +++ b/refactoring.api/test/unit/src/org/netbeans/modules/refactoring/plugins/FilesCopyPluginTest.java @@ -0,0 +1,154 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.modules.refactoring.plugins; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import org.junit.*; +import org.netbeans.junit.NbTestCase; +import org.netbeans.modules.refactoring.api.AbstractRefactoring; +import org.netbeans.modules.refactoring.api.CopyRefactoring; +import org.netbeans.modules.refactoring.api.RefactoringSession; +import org.netbeans.modules.refactoring.api.SingleCopyRefactoring; +import org.netbeans.modules.refactoring.spi.RefactoringPlugin; +import org.netbeans.modules.refactoring.spi.RefactoringPluginFactory; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.util.lookup.Lookups; +import org.openide.util.test.MockLookup; + +/** + * + * @author Ralph Ruijs + */ +public class FilesCopyPluginTest extends NbTestCase { + + private FileObject f; + private FileObject f1; + + public FilesCopyPluginTest(String name) { + super(name); + } + + @BeforeClass + public static void setUpClass() throws Exception { + MockLookup.setInstances(new RefactoringPluginFactory() { + + @Override + public RefactoringPlugin createInstance(AbstractRefactoring refactoring) { + return new FilesCopyPlugin(refactoring); + } + }); + } + + @AfterClass + public static void tearDownClass() throws Exception { + } + + @Before + public void setUp() throws IOException { + clearWorkDir(); + f = FileUtil.createData(FileUtil.toFileObject(getWorkDir()), "test"); + OutputStream outputStream = f.getOutputStream(); + outputStream.write("test".getBytes()); + outputStream.close(); + f1 = FileUtil.createData(FileUtil.toFileObject(getWorkDir()), "test1"); + outputStream = f1.getOutputStream(); + outputStream.write("test1".getBytes()); + outputStream.close(); + } + + @After + public void tearDown() { + } + + /** + * Test of preCheck method, of class FilesCopyPlugin. + */ + @Test + public void testChecks() { + FilesCopyPlugin copyPlugin = new FilesCopyPlugin(null); + assertEquals(null, copyPlugin.preCheck()); + assertEquals(null, copyPlugin.fastCheckParameters()); + assertEquals(null, copyPlugin.checkParameters()); + } + + /** + * Test of prepare method, of class FilesCopyPlugin. + */ + @Test + public void testPrepare() throws Exception { + RefactoringSession session = RefactoringSession.create("junit"); + CopyRefactoring copyRefactoring = new CopyRefactoring(Lookups.fixed(f)); + File target = new File(getWorkDirPath() + File.separatorChar + "junit" + File.separatorChar); + copyRefactoring.setTarget(Lookups.fixed(target.toURI().toURL())); + copyRefactoring.prepare(session); + session.doRefactoring(true); + FileObject newOne = copyRefactoring.getContext().lookup(FileObject.class); + assertEquals(newOne.asText(), f.asText()); + + session = RefactoringSession.create("junit1"); + target = new File(getWorkDirPath() + File.separatorChar + "junit1" + File.separatorChar); + copyRefactoring = new CopyRefactoring(Lookups.fixed(f, f1)); + copyRefactoring.setTarget(Lookups.fixed(target.toURI().toURL())); + copyRefactoring.prepare(session); + session.doRefactoring(true); + FileObject[] newOnes = copyRefactoring.getContext().lookup(FileObject[].class); + assertEquals(newOnes[0].asText(), f.asText()); + assertEquals(newOnes[0].getName(), f.getName()); + assertEquals(newOnes[1].asText(), f1.asText()); + assertEquals(newOnes[1].getName(), f1.getName()); + + session = RefactoringSession.create("junit2"); + SingleCopyRefactoring singleCopyRefactoring = new SingleCopyRefactoring(Lookups.fixed(f)); + target = new File(getWorkDirPath() + File.separatorChar + "junit" + File.separatorChar); + singleCopyRefactoring.setTarget(Lookups.fixed(target.toURI().toURL())); + String newName = "HelloWorld"; + singleCopyRefactoring.setNewName(newName); + singleCopyRefactoring.prepare(session); + session.doRefactoring(true); + FileObject newOneRenamed = singleCopyRefactoring.getContext().lookup(FileObject.class); + assertEquals(newOneRenamed.asText(), f.asText()); + assertEquals(newOneRenamed.getName(), newName); + } +}