1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import org.eclipse.core.runtime.IPath; 17 import org.eclipse.jdt.core.ICompilationUnit; 18 import org.eclipse.jdt.core.IImportDeclaration; 19 import org.eclipse.jdt.core.IJavaElement; 20 import org.eclipse.jdt.core.IJavaModelStatus; 21 import org.eclipse.jdt.core.IJavaModelStatusConstants; 22 import org.eclipse.jdt.core.IMember; 23 import org.eclipse.jdt.core.IParent; 24 import org.eclipse.jdt.core.IType; 25 import org.eclipse.jdt.core.JavaModelException; 26 import org.eclipse.jdt.internal.compiler.util.SuffixConstants; 27 import org.eclipse.jdt.internal.core.util.Messages; 28 29 62 public class CopyElementsOperation extends MultiOperation implements SuffixConstants { 63 64 65 private Map sources = new HashMap (); 66 72 public CopyElementsOperation(IJavaElement[] elementsToCopy, IJavaElement[] destContainers, boolean force) { 73 super(elementsToCopy, destContainers, force); 74 } 75 79 public CopyElementsOperation(IJavaElement[] elementsToCopy, IJavaElement destContainer, boolean force) { 80 this(elementsToCopy, new IJavaElement[]{destContainer}, force); 81 } 82 86 protected String getMainTaskName() { 87 return Messages.operation_copyElementProgress; 88 } 89 92 protected JavaModelOperation getNestedOperation(IJavaElement element) { 93 try { 94 IJavaElement dest = getDestinationParent(element); 95 switch (element.getElementType()) { 96 case IJavaElement.PACKAGE_DECLARATION : 97 return new CreatePackageDeclarationOperation(element.getElementName(), (ICompilationUnit) dest); 98 case IJavaElement.IMPORT_DECLARATION : 99 IImportDeclaration importDeclaration = (IImportDeclaration) element; 100 return new CreateImportOperation(element.getElementName(), (ICompilationUnit) dest, importDeclaration.getFlags()); 101 case IJavaElement.TYPE : 102 if (isRenamingMainType(element, dest)) { 103 IPath path = element.getPath(); 104 String extension = path.getFileExtension(); 105 return new RenameResourceElementsOperation(new IJavaElement[] {dest}, new IJavaElement[] {dest.getParent()}, new String []{getNewNameFor(element) + '.' + extension}, this.force); 106 } else { 107 String source = getSourceFor(element); 108 String lineSeparator = org.eclipse.jdt.internal.core.util.Util.getLineSeparator(source, element.getJavaProject()); 109 return new CreateTypeOperation(dest, source + lineSeparator, this.force); 110 } 111 case IJavaElement.METHOD : 112 String source = getSourceFor(element); 113 String lineSeparator = org.eclipse.jdt.internal.core.util.Util.getLineSeparator(source, element.getJavaProject()); 114 return new CreateMethodOperation((IType) dest, source + lineSeparator, this.force); 115 case IJavaElement.FIELD : 116 source = getSourceFor(element); 117 lineSeparator = org.eclipse.jdt.internal.core.util.Util.getLineSeparator(source, element.getJavaProject()); 118 return new CreateFieldOperation((IType) dest, source + lineSeparator, this.force); 119 case IJavaElement.INITIALIZER : 120 source = getSourceFor(element); 121 lineSeparator = org.eclipse.jdt.internal.core.util.Util.getLineSeparator(source, element.getJavaProject()); 122 return new CreateInitializerOperation((IType) dest, source + lineSeparator); 123 default : 124 return null; 125 } 126 } catch (JavaModelException npe) { 127 return null; 128 } 129 } 130 133 private String getSourceFor(IJavaElement element) throws JavaModelException { 134 String source = (String ) this.sources.get(element); 135 if (source == null && element instanceof IMember) { 136 source = ((IMember)element).getSource(); 137 this.sources.put(element, source); 138 } 139 return source; 140 } 141 144 protected boolean isRenamingMainType(IJavaElement element, IJavaElement dest) throws JavaModelException { 145 if ((isRename() || getNewNameFor(element) != null) 146 && dest.getElementType() == IJavaElement.COMPILATION_UNIT) { 147 String typeName = dest.getElementName(); 148 typeName = org.eclipse.jdt.internal.core.util.Util.getNameWithoutJavaLikeExtension(typeName); 149 return element.getElementName().equals(typeName) && element.getParent().equals(dest); 150 } 151 return false; 152 } 153 160 protected void processElement(IJavaElement element) throws JavaModelException { 161 JavaModelOperation op = getNestedOperation(element); 162 boolean createElementInCUOperation =op instanceof CreateElementInCUOperation; 163 if (op == null) { 164 return; 165 } 166 if (createElementInCUOperation) { 167 IJavaElement sibling = (IJavaElement) this.insertBeforeElements.get(element); 168 if (sibling != null) { 169 ((CreateElementInCUOperation) op).setRelativePosition(sibling, CreateElementInCUOperation.INSERT_BEFORE); 170 } else 171 if (isRename()) { 172 IJavaElement anchor = resolveRenameAnchor(element); 173 if (anchor != null) { 174 ((CreateElementInCUOperation) op).setRelativePosition(anchor, CreateElementInCUOperation.INSERT_AFTER); } 176 } 177 String newName = getNewNameFor(element); 178 if (newName != null) { 179 ((CreateElementInCUOperation) op).setAlteredName(newName); 180 } 181 } 182 executeNestedOperation(op, 1); 183 184 JavaElement destination = (JavaElement) getDestinationParent(element); 185 ICompilationUnit unit= destination.getCompilationUnit(); 186 if (!unit.isWorkingCopy()) { 187 unit.close(); 188 } 189 190 if (createElementInCUOperation && isMove() && !isRenamingMainType(element, destination)) { 191 DeleteElementsOperation deleteOp = new DeleteElementsOperation(new IJavaElement[] { element }, this.force); 192 executeNestedOperation(deleteOp, 1); 193 } 194 } 195 200 private IJavaElement resolveRenameAnchor(IJavaElement element) throws JavaModelException { 201 IParent parent = (IParent) element.getParent(); 202 IJavaElement[] children = parent.getChildren(); 203 for (int i = 0; i < children.length; i++) { 204 IJavaElement child = children[i]; 205 if (child.equals(element)) { 206 return child; 207 } 208 } 209 return null; 210 } 211 219 protected IJavaModelStatus verify() { 220 IJavaModelStatus status = super.verify(); 221 if (!status.isOK()) { 222 return status; 223 } 224 if (this.renamingsList != null && this.renamingsList.length != this.elementsToProcess.length) { 225 return new JavaModelStatus(IJavaModelStatusConstants.INDEX_OUT_OF_BOUNDS); 226 } 227 return JavaModelStatus.VERIFIED_OK; 228 } 229 252 protected void verify(IJavaElement element) throws JavaModelException { 253 if (element == null || !element.exists()) 254 error(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, element); 255 256 if (element.getElementType() < IJavaElement.TYPE) 257 error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element); 258 259 if (element.isReadOnly()) 260 error(IJavaModelStatusConstants.READ_ONLY, element); 261 262 IJavaElement dest = getDestinationParent(element); 263 verifyDestination(element, dest); 264 verifySibling(element, dest); 265 if (this.renamingsList != null) { 266 verifyRenaming(element); 267 } 268 } 269 } 270 | Popular Tags |