5d3fc42b3ce5b2903de99d6f335af522b87db887
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / RenameElementsOperation.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 net.sourceforge.phpdt.core.IJavaElement;
14 import net.sourceforge.phpdt.core.IJavaModelStatus;
15 import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
16 import net.sourceforge.phpdt.core.ISourceReference;
17 import net.sourceforge.phpdt.core.JavaModelException;
18
19 /**
20  * This operation renames elements.
21  *
22  * <p>Notes:<ul>
23  * <li>Resource rename is not supported - this operation only renames
24  *         elements contained in compilation units.
25  * <li>When a main type is renamed, its compilation unit and constructors are renamed.
26  * <li>Constructors cannot be renamed.
27  * </ul>
28  */
29 public class RenameElementsOperation extends MoveElementsOperation {
30 /**
31  * When executed, this operation will rename the specified elements with the given names in the
32  * corresponding destinations.
33  */
34 public RenameElementsOperation(IJavaElement[] elements, IJavaElement[] destinations, String[] newNames, boolean force) {
35         //a rename is a move to the same parent with a new name specified
36         //these elements are from different parents
37         super(elements, destinations, force);
38         setRenamings(newNames);
39 }
40 /**
41  * @see MultiOperation
42  */
43 protected String getMainTaskName() {
44         return Util.bind("operation.renameElementProgress"); //$NON-NLS-1$
45 }
46 /**
47  * @see CopyElementsOperation#isRename()
48  */
49 protected boolean isRename() {
50         return true;
51 }
52 /**
53  * @see MultiOperation
54  */
55 protected IJavaModelStatus verify() {
56         IJavaModelStatus status = super.verify();
57         if (! status.isOK())
58                 return status;
59         if (fRenamingsList == null || fRenamingsList.length == 0)
60                 return new JavaModelStatus(IJavaModelStatusConstants.NULL_NAME);
61         return JavaModelStatus.VERIFIED_OK;
62 }
63 /**
64  * @see MultiOperation
65  */
66 protected void verify(IJavaElement element) throws JavaModelException {
67         int elementType = element.getElementType();
68         
69         if (element == null || !element.exists())
70                 error(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, element);
71                 
72         if (element.isReadOnly())
73                 error(IJavaModelStatusConstants.READ_ONLY, element);
74                 
75         if (!(element instanceof ISourceReference))
76                 error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);
77                 
78         if (elementType < IJavaElement.TYPE || elementType == IJavaElement.INITIALIZER)
79                 error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);
80                 
81         verifyRenaming(element);
82 }
83 }