misc changes in the internal builder
[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.JavaModelException;
32 import net.sourceforge.phpdt.core.JavaCore;
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.core.jdom.IDOMPackage;
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.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, fForce);
168                                 }
169                                 parentFolder = parentFolder.getFolder(new Path(subFolderName));
170                                 sourceFolder = sourceFolder.getFolder(new Path(subFolderName));
171                                 if (sourceFolder.isReadOnly()) {
172                                         parentFolder.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.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.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.isExcluded(sourceElement) || net.sourceforge.phpdt.internal.core.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 (fForce) {
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 = fForce ? 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                                                 fForce ? 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 (!fForce) {
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                                                 fForce ? 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                         fResultElements = new IJavaElement[fCreatedElements.size()];
367                         fCreatedElements.toArray(fResultElements);
368                         processDeltas();
369                 }
370         }
371         /**
372          * Copies/moves a package fragment with the name <code>newName</code>
373          * to the destination package.<br>
374          *
375          * @exception JavaModelException if the operation is unable to
376          * complete
377          */
378         private void processPackageFragmentResource(IPackageFragment source, IPackageFragmentRoot root, String newName) throws JavaModelException {
379                 try {
380                         String newFragName = (newName == null) ? source.getElementName() : newName;
381                         IPackageFragment newFrag = root.getPackageFragment(newFragName);
382                         IResource[] resources = collectResourcesOfInterest(source);
383                         
384                         // if isMove() can we move the folder itself ? (see http://bugs.eclipse.org/bugs/show_bug.cgi?id=22458)
385                         boolean shouldMoveFolder = isMove() && !newFrag.getResource().exists(); // if new pkg fragment exists, it is an override
386                         IFolder srcFolder = (IFolder)source.getResource();
387                         IPath destPath = newFrag.getPath();
388                         if (shouldMoveFolder) {
389                                 // check if destination is not included in source
390                                 if (srcFolder.getFullPath().isPrefixOf(destPath)) {
391                                         shouldMoveFolder = false;
392                                 } else {
393                                         // check if there are no sub-packages
394                                         IResource[] members = srcFolder.members();
395                                         for (int i = 0; i < members.length; i++) {
396                                                 if ( members[i] instanceof IFolder) {
397                                                         shouldMoveFolder = false;
398                                                         break;
399                                                 }
400                                         }
401                                 }       
402                         }
403                         createNeededPackageFragments((IContainer) source.getParent().getResource(), root, newFragName, shouldMoveFolder);
404         
405                         // Process resources
406                         if (shouldMoveFolder) {
407                                 // move underlying resource
408                                 srcFolder.move(destPath, fForce, true /* keep history */, getSubProgressMonitor(1));
409                                 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE); 
410                         } else {
411                                 // process the leaf resources
412                                 if (resources.length > 0) {
413                                         if (isRename()) {
414                                                 if (! destPath.equals(source.getPath())) {
415                                                         moveResources(resources, destPath);
416                                                 }
417                                         } else if (isMove()) {
418                                                 // we need to delete this resource if this operation wants to override existing resources
419                                                 for (int i = 0, max = resources.length; i < max; i++) {
420                                                         IResource destinationResource = ResourcesPlugin.getWorkspace().getRoot().findMember(destPath.append(resources[i].getName()));
421                                                         if (destinationResource != null) {
422                                                                 if (fForce) {
423                                                                         deleteResource(destinationResource, IResource.KEEP_HISTORY);
424                                                                 } else {
425                                                                         throw new JavaModelException(new JavaModelStatus(
426                                                                                 IJavaModelStatusConstants.NAME_COLLISION, 
427                                                                                 Util.bind("status.nameCollision", destinationResource.getFullPath().toString()))); //$NON-NLS-1$
428                                                                 }
429                                                         }
430                                                 }
431                                                 moveResources(resources, destPath);
432                                         } else {
433                                                 // we need to delete this resource if this operation wants to override existing resources
434                                                 for (int i = 0, max = resources.length; i < max; i++) {
435                                                         IResource destinationResource = ResourcesPlugin.getWorkspace().getRoot().findMember(destPath.append(resources[i].getName()));
436                                                         if (destinationResource != null) {
437                                                                 if (fForce) {
438                                                                         // we need to delete this resource if this operation wants to override existing resources
439                                                                         deleteResource(destinationResource, IResource.KEEP_HISTORY);
440                                                                 } else {
441                                                                         throw new JavaModelException(new JavaModelStatus(
442                                                                                 IJavaModelStatusConstants.NAME_COLLISION, 
443                                                                                 Util.bind("status.nameCollision", destinationResource.getFullPath().toString()))); //$NON-NLS-1$
444                                                                 }
445                                                         }
446                                                 }
447                                                 copyResources(resources, destPath);
448                                         }
449                                 }
450                         }
451         
452                         // Discard empty old package (if still empty after the rename)
453                         boolean isEmpty = true;
454                         if (isMove()) {
455                                 // delete remaining files in this package (.class file in the case where Proj=src=bin)
456                                 if (srcFolder.exists()) {
457                                         IResource[] remaingFiles = srcFolder.members();
458                                         for (int i = 0, length = remaingFiles.length; i < length; i++) {
459                                                 IResource file = remaingFiles[i];
460                                                 if (file instanceof IFile) {
461                                                         this.deleteResource(file, IResource.FORCE | IResource.KEEP_HISTORY);
462                                                 } else {
463                                                         isEmpty = false;
464                                                 }
465                                         }
466                                 }
467                                 if (isEmpty) {
468                                         IResource rootResource;
469                                         // check if source is included in destination
470                                         if (destPath.isPrefixOf(srcFolder.getFullPath())) {
471                                                 rootResource = newFrag.getResource();
472                                         } else {
473                                                 rootResource =  source.getParent().getResource();
474                                         }
475                                         
476                                         // delete recursively empty folders
477                                         deleteEmptyPackageFragment(source, false, rootResource);
478                                 }
479                         }
480         
481                         // Update package statement in compilation unit if needed
482                         if (!newFrag.getElementName().equals(source.getElementName())) { // if package has been renamed, update the compilation units
483                                 for (int i = 0; i < resources.length; i++) {
484                                         if (resources[i].getName().endsWith(".java")) { //$NON-NLS-1$
485                                                 // we only consider potential compilation units
486                                                 ICompilationUnit cu = newFrag.getCompilationUnit(resources[i].getName());
487                                                 IDOMCompilationUnit domCU = fFactory.createCompilationUnit(cu.getSource(), cu.getElementName());
488                                                 if (domCU != null) {
489                                                         updatePackageStatement(domCU, newFragName);
490                                                         IBuffer buffer = cu.getBuffer();
491                                                         if (buffer == null) continue;
492                                                         String bufferContents = buffer.getContents();
493                                                         if (bufferContents == null) continue;
494                                                         String domCUContents = domCU.getContents();
495                                                         String cuContents = null;
496                                                         if (domCUContents != null) {
497                                                                 cuContents = net.sourceforge.phpdt.internal.core.Util.normalizeCRs(domCU.getContents(), bufferContents);
498                                                         } else {
499                                                                 // See PR http://dev.eclipse.org/bugs/show_bug.cgi?id=11285
500                                                                 cuContents = bufferContents;//$NON-NLS-1$
501                                                         }
502                                                         buffer.setContents(cuContents);
503                                                         cu.save(null, false);
504                                                 }
505                                         }
506                                 }
507                         }
508         
509                         //register the correct change deltas
510                         prepareDeltas(source, newFrag, isMove() && isEmpty);
511                 } catch (DOMException dom) {
512                         throw new JavaModelException(dom, IJavaModelStatusConstants.DOM_EXCEPTION);
513                 } catch (JavaModelException e) {
514                         throw e;
515                 } catch (CoreException ce) {
516                         throw new JavaModelException(ce);
517                 }
518         }
519         /**
520          * Updates the content of <code>cu</code>, modifying the type name and/or package
521          * declaration as necessary.
522          *
523          * @return the new source
524          */
525         private String updatedContent(ICompilationUnit cu, IPackageFragment dest, String newName) throws JavaModelException {
526                 String currPackageName = cu.getParent().getElementName();
527                 String destPackageName = dest.getElementName();
528                 if (currPackageName.equals(destPackageName) && newName == null) {
529                         return null; //nothing to change
530                 } else {
531                         String typeName = cu.getElementName();
532                         typeName = typeName.substring(0, typeName.length() - 5);
533                         IDOMCompilationUnit cuDOM = null;
534                         IBuffer buffer = cu.getBuffer();
535                         if (buffer == null) return null;
536                         char[] contents = buffer.getCharacters();
537                         if (contents == null) return null;
538                         cuDOM = fFactory.createCompilationUnit(contents, typeName);
539                         updateTypeName(cu, cuDOM, cu.getElementName(), newName);
540                         updatePackageStatement(cuDOM, destPackageName);
541                         return cuDOM.getContents();
542                 }
543         }
544         /**
545          * Makes sure that <code>cu</code> declares to be in the <code>pkgName</code> package.
546          */
547         private void updatePackageStatement(IDOMCompilationUnit domCU, String pkgName) throws JavaModelException {
548                 boolean defaultPackage = pkgName.equals(IPackageFragment.DEFAULT_PACKAGE_NAME);
549                 boolean seenPackageNode = false;
550                 Enumeration enum = domCU.getChildren();
551                 while (enum.hasMoreElements()) {
552                         IDOMNode node = (IDOMNode) enum.nextElement();
553                         if (node.getNodeType() == IDOMNode.PACKAGE) {
554                                 if (! defaultPackage) {
555                                         node.setName(pkgName);
556                                 } else {
557                                         node.remove();
558                                 }
559                                 seenPackageNode = true;
560                                 break;
561                         }
562                 }
563                 if (!seenPackageNode && !defaultPackage) {
564                         //the cu was in a default package...no package declaration
565                         //create the new package declaration as the first child of the cu
566 //                      IDOMPackage pkg = fFactory.createPackage("package " + pkgName + ";" + net.sourceforge.phpdt.internal.compiler.util.Util.LINE_SEPARATOR); //$NON-NLS-1$ //$NON-NLS-2$
567 //                      IDOMNode firstChild = domCU.getFirstChild();
568 //                      if (firstChild != null) {
569 //                              firstChild.insertSibling(pkg);
570 //                      } // else the cu was empty: leave it empty
571                 }
572         }
573                 /**
574                  * Renames the main type in <code>cu</code>.
575                  */
576                 private void updateTypeName(ICompilationUnit cu, IDOMCompilationUnit domCU, String oldName, String newName) throws JavaModelException {
577                         if (newName != null) {
578                                 if (fRenamedCompilationUnits == null) {
579                                         fRenamedCompilationUnits= new ArrayList(1);
580                                 }
581                                 fRenamedCompilationUnits.add(cu);
582                                 String oldTypeName= oldName.substring(0, oldName.length() - 5);
583                                 String newTypeName= newName.substring(0, newName.length() - 5);
584                                 // update main type name
585                                 IType[] types = cu.getTypes();
586                                 for (int i = 0, max = types.length; i < max; i++) {
587                                         IType currentType = types[i];
588                                         if (currentType.getElementName().equals(oldTypeName)) {
589                                                 IDOMNode typeNode = ((JavaElement) currentType).findNode(domCU);
590                                                 if (typeNode != null) {
591                                                         typeNode.setName(newTypeName);
592                                                 }
593                                         }
594                                 }
595                         }
596                 }
597         /**
598          * Possible failures:
599          * <ul>
600          *  <li>NO_ELEMENTS_TO_PROCESS - no elements supplied to the operation
601          *      <li>INDEX_OUT_OF_BOUNDS - the number of renamings supplied to the operation
602          *              does not match the number of elements that were supplied.
603          * </ul>
604          */
605         protected IJavaModelStatus verify() {
606                 IJavaModelStatus status = super.verify();
607                 if (!status.isOK()) {
608                         return status;
609                 }
610         
611                 if (fRenamingsList != null && fRenamingsList.length != fElementsToProcess.length) {
612                         return new JavaModelStatus(IJavaModelStatusConstants.INDEX_OUT_OF_BOUNDS);
613                 }
614                 return JavaModelStatus.VERIFIED_OK;
615         }
616         /**
617          * @see MultiOperation
618          */
619         protected void verify(IJavaElement element) throws JavaModelException {
620                 if (element == null || !element.exists())
621                         error(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, element);
622                         
623                 if (element.isReadOnly() && (isRename() || isMove()))
624                         error(IJavaModelStatusConstants.READ_ONLY, element);
625
626                 IResource resource = element.getResource();
627                 if (resource instanceof IFolder) {
628                         if (resource.isLinked()) {
629                                 error(JavaModelStatus.INVALID_RESOURCE, element);
630                         }
631                 }
632         
633                 int elementType = element.getElementType();
634         
635                 if (elementType == IJavaElement.COMPILATION_UNIT) {
636                         if (isMove() && ((ICompilationUnit) element).isWorkingCopy())
637                                 error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);
638                 } else if (elementType != IJavaElement.PACKAGE_FRAGMENT) {
639                         error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);
640                 }
641                 
642                 JavaElement dest = (JavaElement) getDestinationParent(element);
643                 verifyDestination(element, dest);
644                 if (fRenamings != null) {
645                         verifyRenaming(element);
646                 }
647 }
648 }