replaced a lot of deprecated code; if someone runs into a commit conflict afterwards...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / CopyResourceElementsOperation.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import java.io.ByteArrayInputStream;
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.Enumeration;
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.Map;
20
21 import net.sourceforge.phpdt.core.IBuffer;
22 import net.sourceforge.phpdt.core.ICompilationUnit;
23 import net.sourceforge.phpdt.core.IJavaElement;
24 import net.sourceforge.phpdt.core.IJavaElementDelta;
25 import net.sourceforge.phpdt.core.IJavaModelStatus;
26 import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
27 import net.sourceforge.phpdt.core.IJavaProject;
28 import net.sourceforge.phpdt.core.IPackageFragment;
29 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
30 import net.sourceforge.phpdt.core.IType;
31 import net.sourceforge.phpdt.core.JavaCore;
32 import net.sourceforge.phpdt.core.JavaModelException;
33 import net.sourceforge.phpdt.core.jdom.DOMException;
34 import net.sourceforge.phpdt.core.jdom.DOMFactory;
35 import net.sourceforge.phpdt.core.jdom.IDOMCompilationUnit;
36 import net.sourceforge.phpdt.core.jdom.IDOMNode;
37 import net.sourceforge.phpdt.internal.core.util.Util;
38
39 import org.eclipse.core.resources.IContainer;
40 import org.eclipse.core.resources.IFile;
41 import org.eclipse.core.resources.IFolder;
42 import org.eclipse.core.resources.IResource;
43 import org.eclipse.core.resources.ResourcesPlugin;
44 import org.eclipse.core.runtime.CoreException;
45 import org.eclipse.core.runtime.IPath;
46 import org.eclipse.core.runtime.Path;
47
48 /**
49  * This operation copies/moves/renames a collection of resources from their current
50  * container to a new container, optionally renaming the
51  * elements.
52  * <p>Notes:<ul>
53  *    <li>If there is already an resource with the same name in
54  *    the new container, the operation either overwrites or aborts,
55  *    depending on the collision policy setting. The default setting is
56  *        abort.
57  *
58  *    <li>When a compilation unit is copied to a new package, the
59  *    package declaration in the compilation unit is automatically updated.
60  *
61  *    <li>The collection of elements being copied must all share the
62  *    same type of container.
63  *
64  *    <li>This operation can be used to copy and rename elements within
65  *    the same container.
66  *
67  *    <li>This operation only copies compilation units and package fragments.
68  *    It does not copy package fragment roots - a platform operation must be used for that.
69  * </ul>
70  *
71  */
72 public class CopyResourceElementsOperation extends MultiOperation {
73         /**
74          * A collection of renamed compilation units.  These cus do
75          * not need to be saved as they no longer exist.
76          */
77         protected ArrayList fRenamedCompilationUnits = null;
78         /**
79          * Table specifying deltas for elements being
80          * copied/moved/renamed. Keyed by elements' project(s), and
81          * values are the corresponding deltas.
82          */
83         protected Map fDeltasPerProject= new HashMap(1);
84         /**
85          * The <code>DOMFactory</code> used to manipulate the source code of
86          * <code>ICompilationUnit</code>.
87          */
88         protected DOMFactory fFactory;
89         /**
90          * The list of new resources created during this operation.
91          */
92         protected ArrayList fCreatedElements;
93         /**
94          * When executed, this operation will copy the given resources to the
95          * given containers.  The resources and destination containers must be in
96          * the correct order. If there is > 1 destination, the number of destinations
97          * must be the same as the number of resources being copied/moved.
98          */
99         public CopyResourceElementsOperation(IJavaElement[] resourcesToCopy, IJavaElement[] destContainers, boolean force) {
100                 super(resourcesToCopy, destContainers, force);
101                 fFactory = new DOMFactory();
102         }
103         /**
104          * When executed, this operation will copy the given resources to the
105          * given container.
106          */
107         public CopyResourceElementsOperation(IJavaElement[] resourcesToCopy, IJavaElement destContainer, boolean force) {
108                 this(resourcesToCopy, new IJavaElement[]{destContainer}, force);
109         }
110         /**
111          * Returns the children of <code>source</code> which are affected by this operation.
112          * If <code>source</code> is a <code>K_SOURCE</code>, these are the <code>.java</code>
113          * files, if it is a <code>K_BINARY</code>, they are the <code>.class</code> files.
114          */
115         private IResource[] collectResourcesOfInterest(IPackageFragment source) throws JavaModelException {
116                 IJavaElement[] children = source.getChildren();
117                 int childOfInterest = IJavaElement.COMPILATION_UNIT;
118 //              if (source.getKind() == IPackageFragmentRoot.K_BINARY) {
119 //                      childOfInterest = IJavaElement.CLASS_FILE;
120 //              }
121                 ArrayList correctKindChildren = new ArrayList(children.length);
122                 for (int i = 0; i < children.length; i++) {
123                         IJavaElement child = children[i];
124                         if (child.getElementType() == childOfInterest) {
125                                 correctKindChildren.add(child.getResource());
126                         }
127                 }
128                 // Gather non-java resources
129 //              Object[] nonJavaResources = source.getNonJavaResources();
130 //              int actualNonJavaResourceCount = 0;
131 //              for (int i = 0, max = nonJavaResources.length; i < max; i++){
132 //                      if (nonJavaResources[i] instanceof IResource) actualNonJavaResourceCount++;
133 //              }
134 //              IResource[] actualNonJavaResources = new IResource[actualNonJavaResourceCount];
135 //              for (int i = 0, max = nonJavaResources.length, index = 0; i < max; i++){
136 //                      if (nonJavaResources[i] instanceof IResource) actualNonJavaResources[index++] = (IResource)nonJavaResources[i];
137 //              }
138
139 //              if (actualNonJavaResourceCount != 0) {
140 //                      int correctKindChildrenSize = correctKindChildren.size();
141 //                      IResource[] result = new IResource[correctKindChildrenSize + actualNonJavaResourceCount];
142 //                      correctKindChildren.toArray(result);
143 //                      System.arraycopy(actualNonJavaResources, 0, result, correctKindChildrenSize, actualNonJavaResourceCount);
144 //                      return result;
145 //              } else {
146                         IResource[] result = new IResource[correctKindChildren.size()];
147                         correctKindChildren.toArray(result);
148                         return result;
149 //              }
150         }
151         /**
152          * Creates any destination package fragment(s) which do not exists yet.
153          */
154         private void createNeededPackageFragments(IContainer sourceFolder, IPackageFragmentRoot root, String newFragName, boolean moveFolder) throws JavaModelException {
155                 IContainer parentFolder = (IContainer) root.getResource();
156                 JavaElementDelta projectDelta = null;
157                 String[] names = net.sourceforge.phpdt.internal.core.util.Util.getTrimmedSimpleNames(newFragName);
158                 StringBuffer sideEffectPackageName = new StringBuffer();
159                 char[][] exclusionsPatterns = ((PackageFragmentRoot)root).fullExclusionPatternChars();
160                 for (int i = 0; i < names.length; i++) {
161                         String subFolderName = names[i];
162                         sideEffectPackageName.append(subFolderName);
163                         IResource subFolder = parentFolder.findMember(subFolderName);
164                         if (subFolder == null) {
165                                 // create deepest folder only if not a move (folder will be moved in processPackageFragmentResource)
166                                 if (!(moveFolder && i == names.length-1)) {
167                                         createFolder(parentFolder, subFolderName, force);
168                                 }
169                                 parentFolder = parentFolder.getFolder(new Path(subFolderName));
170                                 sourceFolder = sourceFolder.getFolder(new Path(subFolderName));
171                                 if (sourceFolder.getResourceAttributes().isReadOnly()) {
172                                         parentFolder.getResourceAttributes().setReadOnly(true);
173                                 }
174                                 IPackageFragment sideEffectPackage = root.getPackageFragment(sideEffectPackageName.toString());
175                                 if (i < names.length - 1 // all but the last one are side effect packages
176                                                 && !net.sourceforge.phpdt.internal.core.util.Util.isExcluded(parentFolder, exclusionsPatterns)) {
177                                         if (projectDelta == null) {
178                                                 projectDelta = getDeltaFor(root.getJavaProject());
179                                         }
180                                         projectDelta.added(sideEffectPackage);
181                                 }
182                                 fCreatedElements.add(sideEffectPackage);
183                         } else {
184                                 parentFolder = (IContainer) subFolder;
185                         }
186                         sideEffectPackageName.append('.');
187                 }
188         }
189         /**
190          * Returns the <code>JavaElementDelta</code> for <code>javaProject</code>,
191          * creating it and putting it in <code>fDeltasPerProject</code> if
192          * it does not exist yet.
193          */
194         private JavaElementDelta getDeltaFor(IJavaProject javaProject) {
195                 JavaElementDelta delta = (JavaElementDelta) fDeltasPerProject.get(javaProject);
196                 if (delta == null) {
197                         delta = new JavaElementDelta(javaProject);
198                         fDeltasPerProject.put(javaProject, delta);
199                 }
200                 return delta;
201         }
202         /**
203          * @see MultiOperation
204          */
205         protected String getMainTaskName() {
206                 return net.sourceforge.phpdt.internal.core.util.Util.bind("operation.copyResourceProgress"); //$NON-NLS-1$
207         }
208         /**
209          * Sets the deltas to register the changes resulting from this operation
210          * for this source element and its destination.
211          * If the operation is a cross project operation<ul>
212          * <li>On a copy, the delta should be rooted in the dest project
213          * <li>On a move, two deltas are generated<ul>
214          *                      <li>one rooted in the source project
215          *                      <li>one rooted in the destination project</ul></ul>
216          * If the operation is rooted in a single project, the delta is rooted in that project
217          *
218          */
219         protected void prepareDeltas(IJavaElement sourceElement, IJavaElement destinationElement, boolean isMove) {
220                 if (net.sourceforge.phpdt.internal.core.util.Util.isExcluded(sourceElement) || net.sourceforge.phpdt.internal.core.util.Util.isExcluded(destinationElement)) return;
221                 IJavaProject destProject = destinationElement.getJavaProject();
222                 if (isMove) {
223                         IJavaProject sourceProject = sourceElement.getJavaProject();
224                         getDeltaFor(sourceProject).movedFrom(sourceElement, destinationElement);
225                         getDeltaFor(destProject).movedTo(destinationElement, sourceElement);
226                 } else {
227                         getDeltaFor(destProject).added(destinationElement);
228                 }
229         }
230         /**
231          * Copies/moves a compilation unit with the name <code>newCUName</code>
232          * to the destination package.<br>
233          * The package statement in the compilation unit is updated if necessary.
234          * The main type of the compilation unit is renamed if necessary.
235          *
236          * @exception JavaModelException if the operation is unable to
237          * complete
238          */
239         private void processCompilationUnitResource(ICompilationUnit source, IPackageFragment dest) throws JavaModelException {
240                 String newCUName = getNewNameFor(source);
241                 String destName = (newCUName != null) ? newCUName : source.getElementName();
242                 String newContent = updatedContent(source, dest, newCUName); // null if unchanged
243
244                 // copy resource
245                 IFile sourceResource = (IFile)(source.isWorkingCopy() ? source.getOriginalElement() : source).getResource();
246                 IContainer destFolder = (IContainer)dest.getResource(); // can be an IFolder or an IProject
247                 IFile destFile = destFolder.getFile(new Path(destName));
248                 if (!destFile.equals(sourceResource)) {
249                         try {
250                                 if (destFile.exists()) {
251                                         if (force) {
252                                                 // we can remove it
253                                                 deleteResource(destFile, IResource.KEEP_HISTORY);
254                                         } else {
255                                                 // abort
256                                                 throw new JavaModelException(new JavaModelStatus(
257                                                         IJavaModelStatusConstants.NAME_COLLISION,
258                                                         Util.bind("status.nameCollision", destFile.getFullPath().toString()))); //$NON-NLS-1$
259                                         }
260                                 }
261                                 int flags = force ? IResource.FORCE : IResource.NONE;
262                                 if (this.isMove()) {
263                                         flags |= IResource.KEEP_HISTORY;
264                                         sourceResource.move(destFile.getFullPath(), flags, getSubProgressMonitor(1));
265                                 } else {
266                                         if (newContent != null) flags |= IResource.KEEP_HISTORY;
267                                         sourceResource.copy(destFile.getFullPath(), flags, getSubProgressMonitor(1));
268                                 }
269                                 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
270                         } catch (JavaModelException e) {
271                                 throw e;
272                         } catch (CoreException e) {
273                                 throw new JavaModelException(e);
274                         }
275
276                         // update new resource content
277                         try {
278                                 if (newContent != null){
279                                         String encoding = source.getJavaProject().getOption(JavaCore.CORE_ENCODING, true);
280                                         destFile.setContents(
281                                                 new ByteArrayInputStream(encoding == null ? newContent.getBytes() : newContent.getBytes(encoding)),
282                                                 force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY,
283                                                 getSubProgressMonitor(1));
284                                 }
285                         } catch(IOException e) {
286                                 throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
287                         } catch (CoreException e) {
288                                 throw new JavaModelException(e);
289                         }
290
291                         // register the correct change deltas
292                         ICompilationUnit destCU = dest.getCompilationUnit(destName);
293                         prepareDeltas(source, destCU, isMove());
294                         if (newCUName != null) {
295                                 //the main type has been renamed
296                                 String oldName = source.getElementName();
297                                 oldName = oldName.substring(0, oldName.length() - 5);
298                                 String newName = newCUName;
299                                 newName = newName.substring(0, newName.length() - 5);
300                                 prepareDeltas(source.getType(oldName), destCU.getType(newName), isMove());
301                         }
302                 } else {
303                         if (!force) {
304                                 throw new JavaModelException(new JavaModelStatus(
305                                         IJavaModelStatusConstants.NAME_COLLISION,
306                                         Util.bind("status.nameCollision", destFile.getFullPath().toString()))); //$NON-NLS-1$
307                         }
308                         // update new resource content
309                         // in case we do a saveas on the same resource we have to simply update the contents
310                         // see http://dev.eclipse.org/bugs/show_bug.cgi?id=9351
311                         try {
312                                 if (newContent != null){
313                                         String encoding = source.getJavaProject().getOption(JavaCore.CORE_ENCODING, true);
314                                         destFile.setContents(
315                                                 new ByteArrayInputStream(encoding == null ? newContent.getBytes() : newContent.getBytes(encoding)),
316                                                 force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY,
317                                                 getSubProgressMonitor(1));
318                                 }
319                         } catch(IOException e) {
320                                 throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
321                         } catch (CoreException e) {
322                                 throw new JavaModelException(e);
323                         }
324                 }
325         }
326         /**
327          * Process all of the changed deltas generated by this operation.
328          */
329         protected void processDeltas() {
330                 for (Iterator deltas = this.fDeltasPerProject.values().iterator(); deltas.hasNext();){
331                         addDelta((IJavaElementDelta) deltas.next());
332                 }
333         }
334         /**
335          * @see MultiOperation
336          * This method delegates to <code>processCompilationUnitResource</code> or
337          * <code>processPackageFragmentResource</code>, depending on the type of
338          * <code>element</code>.
339          */
340         protected void processElement(IJavaElement element) throws JavaModelException {
341                 IJavaElement dest = getDestinationParent(element);
342                 switch (element.getElementType()) {
343                         case IJavaElement.COMPILATION_UNIT :
344                                 processCompilationUnitResource((ICompilationUnit) element, (IPackageFragment) dest);
345                                 fCreatedElements.add(((IPackageFragment) dest).getCompilationUnit(element.getElementName()));
346                                 break;
347                         case IJavaElement.PACKAGE_FRAGMENT :
348                                 processPackageFragmentResource((IPackageFragment) element, (IPackageFragmentRoot) dest, getNewNameFor(element));
349                                 break;
350                         default :
351                                 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element));
352                 }
353         }
354         /**
355          * @see MultiOperation
356          * Overridden to allow special processing of <code>JavaElementDelta</code>s
357          * and <code>fResultElements</code>.
358          */
359         protected void processElements() throws JavaModelException {
360                 fCreatedElements = new ArrayList(fElementsToProcess.length);
361                 try {
362                         super.processElements();
363                 } catch (JavaModelException jme) {
364                         throw jme;
365                 } finally {
366                         resultElements = new IJavaElement[fCreatedElements.size()];
367                         fCreatedElements.toArray(resultElements);
368                         processDeltas();
369                 }
370         }
371
372         /**
373          * Copies/moves a package fragment with the name <code>newName</code>
374          * to the destination package.<br>
375          *
376          * @exception JavaModelException if the operation is unable to
377          * complete
378          */
379         private void processPackageFragmentResource(IPackageFragment source, IPackageFragmentRoot root, String newName) throws JavaModelException {
380                 try {
381                         String newFragName = (newName == null) ? source.getElementName() : newName;
382                         IPackageFragment newFrag = root.getPackageFragment(newFragName);
383                         IResource[] resources = collectResourcesOfInterest(source);
384
385                         // if isMove() can we move the folder itself ? (see http://bugs.eclipse.org/bugs/show_bug.cgi?id=22458)
386                         boolean shouldMoveFolder = isMove() && !newFrag.getResource().exists(); // if new pkg fragment exists, it is an override
387                         IFolder srcFolder = (IFolder)source.getResource();
388                         IPath destPath = newFrag.getPath();
389                         if (shouldMoveFolder) {
390                                 // check if destination is not included in source
391                                 if (srcFolder.getFullPath().isPrefixOf(destPath)) {
392                                         shouldMoveFolder = false;
393                                 } else {
394                                         // check if there are no sub-packages
395                                         IResource[] members = srcFolder.members();
396                                         for (int i = 0; i < members.length; i++) {
397                                                 if ( members[i] instanceof IFolder) {
398                                                         shouldMoveFolder = false;
399                                                         break;
400                                                 }
401                                         }
402                                 }
403                         }
404                         createNeededPackageFragments((IContainer) source.getParent().getResource(), root, newFragName, shouldMoveFolder);
405
406                         // Process resources
407                         if (shouldMoveFolder) {
408                                 // move underlying resource
409                                 srcFolder.move(destPath, force, true /* keep history */, getSubProgressMonitor(1));
410                                 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
411                         } else {
412                                 // process the leaf resources
413                                 if (resources.length > 0) {
414                                         if (isRename()) {
415                                                 if (! destPath.equals(source.getPath())) {
416                                                         moveResources(resources, destPath);
417                                                 }
418                                         } else if (isMove()) {
419                                                 // we need to delete this resource if this operation wants to override existing resources
420                                                 for (int i = 0, max = resources.length; i < max; i++) {
421                                                         IResource destinationResource = ResourcesPlugin.getWorkspace().getRoot().findMember(destPath.append(resources[i].getName()));
422                                                         if (destinationResource != null) {
423                                                                 if (force) {
424                                                                         deleteResource(destinationResource, IResource.KEEP_HISTORY);
425                                                                 } else {
426                                                                         throw new JavaModelException(new JavaModelStatus(
427                                                                                 IJavaModelStatusConstants.NAME_COLLISION,
428                                                                                 Util.bind("status.nameCollision", destinationResource.getFullPath().toString()))); //$NON-NLS-1$
429                                                                 }
430                                                         }
431                                                 }
432                                                 moveResources(resources, destPath);
433                                         } else {
434                                                 // we need to delete this resource if this operation wants to override existing resources
435                                                 for (int i = 0, max = resources.length; i < max; i++) {
436                                                         IResource destinationResource = ResourcesPlugin.getWorkspace().getRoot().findMember(destPath.append(resources[i].getName()));
437                                                         if (destinationResource != null) {
438                                                                 if (force) {
439                                                                         // we need to delete this resource if this operation wants to override existing resources
440                                                                         deleteResource(destinationResource, IResource.KEEP_HISTORY);
441                                                                 } else {
442                                                                         throw new JavaModelException(new JavaModelStatus(
443                                                                                 IJavaModelStatusConstants.NAME_COLLISION,
444                                                                                 Util.bind("status.nameCollision", destinationResource.getFullPath().toString()))); //$NON-NLS-1$
445                                                                 }
446                                                         }
447                                                 }
448                                                 copyResources(resources, destPath);
449                                         }
450                                 }
451                         }
452
453                         // Discard empty old package (if still empty after the rename)
454                         boolean isEmpty = true;
455                         if (isMove()) {
456                                 // delete remaining files in this package (.class file in the case where Proj=src=bin)
457                                 if (srcFolder.exists()) {
458                                         IResource[] remaingFiles = srcFolder.members();
459                                         for (int i = 0, length = remaingFiles.length; i < length; i++) {
460                                                 IResource file = remaingFiles[i];
461                                                 if (file instanceof IFile) {
462                                                         this.deleteResource(file, IResource.FORCE | IResource.KEEP_HISTORY);
463                                                 } else {
464                                                         isEmpty = false;
465                                                 }
466                                         }
467                                 }
468                                 if (isEmpty) {
469                                         IResource rootResource;
470                                         // check if source is included in destination
471                                         if (destPath.isPrefixOf(srcFolder.getFullPath())) {
472                                                 rootResource = newFrag.getResource();
473                                         } else {
474                                                 rootResource =  source.getParent().getResource();
475                                         }
476
477                                         // delete recursively empty folders
478                                         deleteEmptyPackageFragment(source, false, rootResource);
479                                 }
480                         }
481
482                         // Update package statement in compilation unit if needed
483                         if (!newFrag.getElementName().equals(source.getElementName())) { // if package has been renamed, update the compilation units
484                                 for (int i = 0; i < resources.length; i++) {
485                                         if (resources[i].getName().endsWith(".java")) { //$NON-NLS-1$
486                                                 // we only consider potential compilation units
487                                                 ICompilationUnit cu = newFrag.getCompilationUnit(resources[i].getName());
488                                                 IDOMCompilationUnit domCU = fFactory.createCompilationUnit(cu.getSource(), cu.getElementName());
489                                                 if (domCU != null) {
490                                                         updatePackageStatement(domCU, newFragName);
491                                                         IBuffer buffer = cu.getBuffer();
492                                                         if (buffer == null) continue;
493                                                         String bufferContents = buffer.getContents();
494                                                         if (bufferContents == null) continue;
495                                                         String domCUContents = domCU.getContents();
496                                                         String cuContents = null;
497                                                         if (domCUContents != null) {
498                                                                 cuContents = net.sourceforge.phpdt.internal.core.util.Util.normalizeCRs(domCU.getContents(), bufferContents);
499                                                         } else {
500                                                                 // See PR http://dev.eclipse.org/bugs/show_bug.cgi?id=11285
501                                                                 cuContents = bufferContents;//$NON-NLS-1$
502                                                         }
503                                                         buffer.setContents(cuContents);
504                                                         cu.save(null, false);
505                                                 }
506                                         }
507                                 }
508                         }
509
510                         //register the correct change deltas
511                         prepareDeltas(source, newFrag, isMove() && isEmpty);
512                 } catch (DOMException dom) {
513                         throw new JavaModelException(dom, IJavaModelStatusConstants.DOM_EXCEPTION);
514                 } catch (JavaModelException e) {
515                         throw e;
516                 } catch (CoreException ce) {
517                         throw new JavaModelException(ce);
518                 }
519         }
520         /**
521          * Updates the content of <code>cu</code>, modifying the type name and/or package
522          * declaration as necessary.
523          *
524          * @return the new source
525          */
526         private String updatedContent(ICompilationUnit cu, IPackageFragment dest, String newName) throws JavaModelException {
527                 String currPackageName = cu.getParent().getElementName();
528                 String destPackageName = dest.getElementName();
529                 if (currPackageName.equals(destPackageName) && newName == null) {
530                         return null; //nothing to change
531                 } else {
532                         String typeName = cu.getElementName();
533                         typeName = typeName.substring(0, typeName.length() - 5);
534                         IDOMCompilationUnit cuDOM = null;
535                         IBuffer buffer = cu.getBuffer();
536                         if (buffer == null) return null;
537                         char[] contents = buffer.getCharacters();
538                         if (contents == null) return null;
539                         cuDOM = fFactory.createCompilationUnit(contents, typeName);
540                         updateTypeName(cu, cuDOM, cu.getElementName(), newName);
541                         updatePackageStatement(cuDOM, destPackageName);
542                         return cuDOM.getContents();
543                 }
544         }
545         /**
546          * Makes sure that <code>cu</code> declares to be in the <code>pkgName</code> package.
547          */
548         private void updatePackageStatement(IDOMCompilationUnit domCU, String pkgName) throws JavaModelException {
549                 boolean defaultPackage = pkgName.equals(IPackageFragment.DEFAULT_PACKAGE_NAME);
550                 boolean seenPackageNode = false;
551                 Enumeration e = domCU.getChildren();
552                 while (e.hasMoreElements()) {
553                         IDOMNode node = (IDOMNode) e.nextElement();
554                         if (node.getNodeType() == IDOMNode.PACKAGE) {
555                                 if (! defaultPackage) {
556                                         node.setName(pkgName);
557                                 } else {
558                                         node.remove();
559                                 }
560                                 seenPackageNode = true;
561                                 break;
562                         }
563                 }
564                 if (!seenPackageNode && !defaultPackage) {
565                         //the cu was in a default package...no package declaration
566                         //create the new package declaration as the first child of the cu
567 //                      IDOMPackage pkg = fFactory.createPackage("package " + pkgName + ";" + net.sourceforge.phpdt.internal.compiler.util.ProjectPrefUtil.LINE_SEPARATOR); //$NON-NLS-1$ //$NON-NLS-2$
568 //                      IDOMNode firstChild = domCU.getFirstChild();
569 //                      if (firstChild != null) {
570 //                              firstChild.insertSibling(pkg);
571 //                      } // else the cu was empty: leave it empty
572                 }
573         }
574                 /**
575                  * Renames the main type in <code>cu</code>.
576                  */
577                 private void updateTypeName(ICompilationUnit cu, IDOMCompilationUnit domCU, String oldName, String newName) throws JavaModelException {
578                         if (newName != null) {
579                                 if (fRenamedCompilationUnits == null) {
580                                         fRenamedCompilationUnits= new ArrayList(1);
581                                 }
582                                 fRenamedCompilationUnits.add(cu);
583                                 String oldTypeName= oldName.substring(0, oldName.length() - 5);
584                                 String newTypeName= newName.substring(0, newName.length() - 5);
585                                 // update main type name
586                                 IType[] types = cu.getTypes();
587                                 for (int i = 0, max = types.length; i < max; i++) {
588                                         IType currentType = types[i];
589                                         if (currentType.getElementName().equals(oldTypeName)) {
590                                                 IDOMNode typeNode = ((JavaElement) currentType).findNode(domCU);
591                                                 if (typeNode != null) {
592                                                         typeNode.setName(newTypeName);
593                                                 }
594                                         }
595                                 }
596                         }
597                 }
598         /**
599          * Possible failures:
600          * <ul>
601          *  <li>NO_ELEMENTS_TO_PROCESS - no elements supplied to the operation
602          *      <li>INDEX_OUT_OF_BOUNDS - the number of renamings supplied to the operation
603          *              does not match the number of elements that were supplied.
604          * </ul>
605          */
606         protected IJavaModelStatus verify() {
607                 IJavaModelStatus status = super.verify();
608                 if (!status.isOK()) {
609                         return status;
610                 }
611
612                 if (fRenamingsList != null && fRenamingsList.length != fElementsToProcess.length) {
613                         return new JavaModelStatus(IJavaModelStatusConstants.INDEX_OUT_OF_BOUNDS);
614                 }
615                 return JavaModelStatus.VERIFIED_OK;
616         }
617         /**
618          * @see MultiOperation
619          */
620         protected void verify(IJavaElement element) throws JavaModelException {
621                 if (element == null || !element.exists())
622                         error(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, element);
623
624                 if (element.isReadOnly() && (isRename() || isMove()))
625                         error(IJavaModelStatusConstants.READ_ONLY, element);
626
627                 IResource resource = element.getResource();
628                 if (resource instanceof IFolder) {
629                         if (resource.isLinked()) {
630                                 error(JavaModelStatus.INVALID_RESOURCE, element);
631                         }
632                 }
633
634                 int elementType = element.getElementType();
635
636                 if (elementType == IJavaElement.COMPILATION_UNIT) {
637                         if (isMove() && ((ICompilationUnit) element).isWorkingCopy())
638                                 error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);
639                 } else if (elementType != IJavaElement.PACKAGE_FRAGMENT) {
640                         error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);
641                 }
642
643                 JavaElement dest = (JavaElement) getDestinationParent(element);
644                 verifyDestination(element, dest);
645                 if (fRenamings != null) {
646                         verifyRenaming(element);
647                 }
648 }
649 }