KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > refactoring > descriptors > RenameLocalVariableDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.core.refactoring.descriptors;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.ltk.core.refactoring.RefactoringContribution;
16 import org.eclipse.ltk.core.refactoring.RefactoringCore;
17 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
18
19 import org.eclipse.jdt.core.ICompilationUnit;
20 import org.eclipse.jdt.core.ISourceRange;
21 import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
22
23 import org.eclipse.jdt.internal.core.refactoring.descriptors.DescriptorMessages;
24
25 /**
26  * Refactoring descriptor for the rename local variable refactoring.
27  * <p>
28  * An instance of this refactoring descriptor may be obtained by calling
29  * {@link RefactoringContribution#createDescriptor()} on a refactoring
30  * contribution requested by invoking
31  * {@link RefactoringCore#getRefactoringContribution(String)} with the
32  * appropriate refactoring id.
33  * </p>
34  * <p>
35  * Note: this class is not intended to be instantiated by clients.
36  * </p>
37  *
38  * @since 3.3
39  */

40 public final class RenameLocalVariableDescriptor extends JavaRefactoringDescriptor {
41
42     /** The name attribute */
43     private String JavaDoc fName= null;
44
45     /** The references attribute */
46     private boolean fReferences= false;
47
48     /** The selection attribute */
49     private ISourceRange fSelection= null;
50
51     /** The compilation unit attribute */
52     private ICompilationUnit fUnit= null;
53
54     /**
55      * Creates a new refactoring descriptor.
56      */

57     public RenameLocalVariableDescriptor() {
58         super(IJavaRefactorings.RENAME_LOCAL_VARIABLE);
59     }
60
61     /**
62      * {@inheritDoc}
63      */

64     protected void populateArgumentMap() {
65         super.populateArgumentMap();
66         fArguments.put(JavaRefactoringDescriptor.ATTRIBUTE_NAME, fName);
67         fArguments.put(JavaRefactoringDescriptor.ATTRIBUTE_INPUT, elementToHandle(getProject(), fUnit));
68         fArguments.put(JavaRefactoringDescriptor.ATTRIBUTE_SELECTION, new Integer JavaDoc(fSelection.getOffset()).toString() + " " + new Integer JavaDoc(fSelection.getLength()).toString()); //$NON-NLS-1$
69
fArguments.put(JavaRefactoringDescriptor.ATTRIBUTE_REFERENCES, Boolean.toString(fReferences));
70     }
71
72     /**
73      * Sets the compilation unit which contains the local variable.
74      *
75      * @param unit
76      * the compilation unit to set
77      */

78     public void setCompilationUnit(final ICompilationUnit unit) {
79         Assert.isNotNull(unit);
80         fUnit= unit;
81     }
82
83     /**
84      * Sets the new name to rename the local variable to.
85      *
86      * @param name
87      * the non-empty new name to set
88      */

89     public void setNewName(final String JavaDoc name) {
90         Assert.isNotNull(name);
91         Assert.isLegal(!"".equals(name), "Name must not be empty"); //$NON-NLS-1$//$NON-NLS-2$
92
fName= name;
93     }
94
95     /**
96      * Sets the selection within the compilation unit which references the local
97      * variable to rename.
98      *
99      * @param selection
100      * the selection to set
101      */

102     public void setSelection(final ISourceRange selection) {
103         Assert.isNotNull(selection);
104         fSelection= selection;
105     }
106
107     /**
108      * Determines whether references to the local variable should be renamed.
109      * <p>
110      * The default is to not update references.
111      * </p>
112      *
113      * @param update
114      * <code>true</code> to update references, <code>false</code>
115      * otherwise
116      */

117     public void setUpdateReferences(final boolean update) {
118         fReferences= update;
119     }
120
121     /**
122      * {@inheritDoc}
123      */

124     public RefactoringStatus validateDescriptor() {
125         RefactoringStatus status= super.validateDescriptor();
126         if (fName == null || "".equals(fName)) //$NON-NLS-1$
127
status.merge(RefactoringStatus.createFatalErrorStatus(DescriptorMessages.RenameResourceDescriptor_no_new_name));
128         if (fUnit == null)
129             status.merge(RefactoringStatus.createFatalErrorStatus(DescriptorMessages.RenameLocalVariableDescriptor_no_compilation_unit));
130         if (fSelection == null)
131             status.merge(RefactoringStatus.createFatalErrorStatus(DescriptorMessages.RenameLocalVariableDescriptor_no_selection));
132         return status;
133     }
134 }
Popular Tags