aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/refactorings/core/RenameResourceProcessor.java
blob: 5ea99411c3e273b768add73a952b5a10f20ab6c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.ide.eclipse.adt.internal.refactorings.core;

import static com.android.SdkConstants.PREFIX_RESOURCE_REF;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.ide.eclipse.adt.AdtConstants;
import com.android.ide.eclipse.adt.internal.resources.ResourceNameValidator;
import com.android.resources.ResourceType;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
import org.eclipse.ltk.core.refactoring.participants.ParticipantManager;
import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
import org.eclipse.ltk.core.refactoring.participants.RenameProcessor;
import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;

/**
 * A rename processor for Android resources.
 */
public class RenameResourceProcessor extends RenameProcessor {
    private IProject mProject;
    private ResourceType mType;
    private String mCurrentName;
    private String mNewName;
    private boolean mUpdateReferences = true;
    private ResourceNameValidator mValidator;
    private RenameArguments mRenameArguments;

    /**
     * Creates a new rename resource processor.
     *
     * @param project the project containing the renamed resource
     * @param type the type of the resource
     * @param currentName the current name of the resource
     * @param newName the new name of the resource, or null if not known
     */
    public RenameResourceProcessor(
            @NonNull IProject project,
            @NonNull ResourceType type,
            @NonNull String currentName,
            @Nullable String newName) {
        mProject = project;
        mType = type;
        mCurrentName = currentName;
        mNewName = newName != null ? newName : currentName;
        mUpdateReferences= true;
        mValidator = ResourceNameValidator.create(false, mProject, mType);
    }

    /**
     * Returns the project containing the renamed resource
     *
     * @return the project containing the renamed resource
     */
    @NonNull
    public IProject getProject() {
        return mProject;
    }

    /**
     * Returns the new resource name
     *
     * @return the new resource name
     */
    @NonNull
    public String getNewName() {
        return mNewName;
    }

    /**
     * Returns the current name of the resource
     *
     * @return the current name of the resource
     */
    public String getCurrentName() {
        return mCurrentName;
    }

    /**
     * Returns the type of the resource
     *
     * @return the type of the resource
     */
    @NonNull
    public ResourceType getType() {
        return mType;
    }

    /**
     * Sets the new name
     *
     * @param newName the new name
     */
    public void setNewName(@NonNull String newName) {
        mNewName = newName;
    }

    /**
     * Returns {@code true} if the refactoring processor also updates references
     *
     * @return {@code true} if the refactoring processor also updates references
     */
    public boolean isUpdateReferences() {
        return mUpdateReferences;
    }

    /**
     * Specifies if the refactoring processor also updates references. The
     * default behavior is to update references.
     *
     * @param updateReferences {@code true} if the refactoring processor should
     *            also updates references
     */
    public void setUpdateReferences(boolean updateReferences) {
        mUpdateReferences = updateReferences;
    }

    /**
     * Checks the given new potential name and returns a {@link RefactoringStatus} indicating
     * whether the potential new name is valid
     *
     * @param name the name to check
     * @return a {@link RefactoringStatus} with the validation result
     */
    public RefactoringStatus checkNewName(String name) {
        String error = mValidator.isValid(name);
        if (error != null) {
            return RefactoringStatus.createFatalErrorStatus(error);
        }

        return new RefactoringStatus();
    }

    @Override
    public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
        return new RefactoringStatus();
    }

    @Override
    public RefactoringStatus checkFinalConditions(IProgressMonitor pm,
            CheckConditionsContext context) throws CoreException {
        pm.beginTask("", 1);
        try {
            mRenameArguments = new RenameArguments(getNewName(), isUpdateReferences());
            return new RefactoringStatus();
        } finally {
            pm.done();
        }
    }

    @Override
    public Change createChange(IProgressMonitor pm) throws CoreException {
        pm.beginTask("", 1);
        try {
            // Added by {@link RenameResourceParticipant}
            return null;
        } finally {
            pm.done();
        }
    }

    @Override
    public Object[] getElements() {
        return new Object[0];
    }

    @Override
    public String getIdentifier() {
        return "com.android.ide.renameResourceProcessor"; //$NON-NLS-1$
    }

    @Override
    public String getProcessorName() {
        return "Rename Android Resource";
    }

    @Override
    public boolean isApplicable() {
        return true;
    }

    @Override
    public RefactoringParticipant[] loadParticipants(RefactoringStatus status,
            SharableParticipants shared) throws CoreException {
        String[] affectedNatures = new String[] { AdtConstants.NATURE_DEFAULT };
        String url = PREFIX_RESOURCE_REF + mType.getName() + '/' + mCurrentName;
        return ParticipantManager.loadRenameParticipants(status, this, url, mRenameArguments,
                null, affectedNatures, shared);
    }
}