X-Git-Url: http://git.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/core/CopyElementsOperation.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/core/CopyElementsOperation.java new file mode 100644 index 0000000..ba92d34 --- /dev/null +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/core/CopyElementsOperation.java @@ -0,0 +1,262 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package net.sourceforge.phpdt.internal.core; + +import java.util.HashMap; +import java.util.Map; + +import net.sourceforge.phpdt.core.ICompilationUnit; +import net.sourceforge.phpdt.core.IJavaElement; +import net.sourceforge.phpdt.core.IJavaModelStatus; +import net.sourceforge.phpdt.core.IJavaModelStatusConstants; +import net.sourceforge.phpdt.core.IMember; +import net.sourceforge.phpdt.core.IParent; +import net.sourceforge.phpdt.core.JavaModelException; +import net.sourceforge.phpdt.core.jdom.DOMFactory; +import net.sourceforge.phpdt.core.jdom.IDOMCompilationUnit; +import net.sourceforge.phpdt.core.jdom.IDOMNode; + +/** + * This operation copies/moves a collection of elements from their current + * container to a new container, optionally renaming the + * elements. + *

Notes:

+ * + */ +public class CopyElementsOperation extends MultiOperation { + + + private Map fSources = new HashMap(); +/** + * When executed, this operation will copy the given elements to the + * given containers. The elements and destination containers must be in + * the correct order. If there is > 1 destination, the number of destinations + * must be the same as the number of elements being copied/moved/renamed. + */ +public CopyElementsOperation(IJavaElement[] elementsToCopy, IJavaElement[] destContainers, boolean force) { + super(elementsToCopy, destContainers, force); +} +/** + * When executed, this operation will copy the given elements to the + * given container. + */ +public CopyElementsOperation(IJavaElement[] elementsToCopy, IJavaElement destContainer, boolean force) { + this(elementsToCopy, new IJavaElement[]{destContainer}, force); +} +/** + * Returns the String to use as the main task name + * for progress monitoring. + */ +protected String getMainTaskName() { + return Util.bind("operation.copyElementProgress"); //$NON-NLS-1$ +} +/** + * Returns the nested operation to use for processing this element + */ +protected JavaModelOperation getNestedOperation(IJavaElement element) { + return null; +// try { +// IJavaElement dest = getDestinationParent(element); +// switch (element.getElementType()) { +// case IJavaElement.PACKAGE_DECLARATION : +// return new CreatePackageDeclarationOperation(element.getElementName(), (ICompilationUnit) dest); +// case IJavaElement.IMPORT_DECLARATION : +// return new CreateImportOperation(element.getElementName(), (ICompilationUnit) dest); +// case IJavaElement.TYPE : +// if (isRenamingMainType(element, dest)) { +// return new RenameResourceElementsOperation(new IJavaElement[] {dest}, new IJavaElement[] {dest.getParent()}, new String[]{getNewNameFor(element) + ".php"}, fForce); //$NON-NLS-1$ +// } else { +// return new CreateTypeOperation(dest, getSourceFor(element) + Util.LINE_SEPARATOR, fForce); +// } +// case IJavaElement.METHOD : +// return new CreateMethodOperation((IType) dest, getSourceFor(element) + Util.LINE_SEPARATOR, fForce); +// case IJavaElement.FIELD : +// return new CreateFieldOperation((IType) dest, getSourceFor(element) + Util.LINE_SEPARATOR, fForce); +// case IJavaElement.INITIALIZER : +// return new CreateInitializerOperation((IType) dest, getSourceFor(element) + Util.LINE_SEPARATOR); +// default : +// return null; +// } +// } catch (JavaModelException npe) { +// return null; +// } +} +/** + * Returns the cached source for this element or compute it if not already cached. + */ +private String getSourceFor(IJavaElement element) throws JavaModelException { + String source = (String) fSources.get(element); + if (source == null && element instanceof IMember) { + IMember member = (IMember)element; + ICompilationUnit cu = member.getCompilationUnit(); + String cuSource = cu.getSource(); + IDOMCompilationUnit domCU = new DOMFactory().createCompilationUnit(cuSource, cu.getElementName()); + IDOMNode node = ((JavaElement)element).findNode(domCU); + source = new String(node.getCharacters()); + fSources.put(element, source); + } + return source; +} +/** + * Returns true if this element is the main type of its compilation unit. + */ +protected boolean isRenamingMainType(IJavaElement element, IJavaElement dest) { + if ((isRename() || getNewNameFor(element) != null) + && dest.getElementType() == IJavaElement.COMPILATION_UNIT) { + String typeName = dest.getElementName(); + typeName = typeName.substring(0, typeName.length() - 5); + return element.getElementName().equals(typeName) && element.getParent().equals(dest); + } + return false; +} +/** + * Copy/move the element from the source to destination, renaming + * the elements as specified, honoring the collision policy. + * + * @exception JavaModelException if the operation is unable to + * be completed + */ +protected void processElement(IJavaElement element) throws JavaModelException { + JavaModelOperation op = getNestedOperation(element); + boolean createElementInCUOperation =op instanceof CreateElementInCUOperation; + if (op == null) { + return; + } + if (createElementInCUOperation) { + IJavaElement sibling = (IJavaElement) fInsertBeforeElements.get(element); + if (sibling != null) { + ((CreateElementInCUOperation) op).setRelativePosition(sibling, CreateElementInCUOperation.INSERT_BEFORE); + } else + if (isRename()) { + IJavaElement anchor = resolveRenameAnchor(element); + if (anchor != null) { + ((CreateElementInCUOperation) op).setRelativePosition(anchor, CreateElementInCUOperation.INSERT_AFTER); // insert after so that the anchor is found before when deleted below + } + } + String newName = getNewNameFor(element); + if (newName != null) { + ((CreateElementInCUOperation) op).setAlteredName(newName); + } + } + executeNestedOperation(op, 1); + + JavaElement destination = (JavaElement) getDestinationParent(element); + ICompilationUnit unit= destination.getCompilationUnit(); + if (!unit.isWorkingCopy()) { + unit.close(); + } + + if (createElementInCUOperation && isMove() && !isRenamingMainType(element, destination)) { + DeleteElementsOperation deleteOp = new DeleteElementsOperation(new IJavaElement[] { element }, fForce); + executeNestedOperation(deleteOp, 1); + } +} +/** + * Returns the anchor used for positioning in the destination for + * the element being renamed. For renaming, if no anchor has + * explicitly been provided, the element is anchored in the same position. + */ +private IJavaElement resolveRenameAnchor(IJavaElement element) throws JavaModelException { + IParent parent = (IParent) element.getParent(); + IJavaElement[] children = parent.getChildren(); + for (int i = 0; i < children.length; i++) { + IJavaElement child = children[i]; + if (child.equals(element)) { + return child; + } + } + return null; +} +/** + * Possible failures: + * + */ +protected IJavaModelStatus verify() { + IJavaModelStatus status = super.verify(); + if (!status.isOK()) { + return status; + } + if (fRenamingsList != null && fRenamingsList.length != fElementsToProcess.length) { + return new JavaModelStatus(IJavaModelStatusConstants.INDEX_OUT_OF_BOUNDS); + } + return JavaModelStatus.VERIFIED_OK; +} +/** + * @see MultiOperation + * + * Possible failure codes: + * + */ +protected void verify(IJavaElement element) throws JavaModelException { + if (element == null || !element.exists()) + error(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, element); + + if (element.getElementType() < IJavaElement.TYPE) + error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element); + + if (element.isReadOnly()) + error(IJavaModelStatusConstants.READ_ONLY, element); + + IJavaElement dest = getDestinationParent(element); + verifyDestination(element, dest); + verifySibling(element, dest); + if (fRenamingsList != null) { + verifyRenaming(element); + } +} +}