aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/refactorings/core/RenameResourceWizard.java
blob: 6ffe25d22f2857568cffd6cd23f3c5e67dd7a689 (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
/*
 * 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 com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.resources.ResourceType;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.internal.ui.refactoring.reorg.RenameRefactoringWizard;
import org.eclipse.jdt.ui.refactoring.RefactoringSaveHelper;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;
import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
import org.eclipse.swt.widgets.Shell;

/**
 * Rename refactoring wizard for Android resources such as {@code @id/foo}
 */
@SuppressWarnings("restriction") // JDT refactoring UI
public class RenameResourceWizard extends RenameRefactoringWizard {
	private ResourceType mType;
    private boolean mCanClear;

    /**
     * Constructs a new {@linkplain RenameResourceWizard}
     *
     * @param refactoring the refactoring
     * @param type the type of resource being renamed
     * @param canClear whether the user can clear the value
     */
    public RenameResourceWizard(
            @NonNull RenameRefactoring refactoring,
            @NonNull ResourceType type,
            boolean canClear) {
        super(refactoring,
                "Rename Resource",
                "Enter the new name for this resource",
                JavaPluginImages.DESC_WIZBAN_REFACTOR_FIELD,
                IJavaHelpContextIds.RENAME_FIELD_WIZARD_PAGE);
        mType = type;
		mCanClear = canClear;
	}

	@Override
	protected void addUserInputPages() {
	    RenameRefactoring refactoring = (RenameRefactoring) getRefactoring();
        RenameResourceProcessor processor = (RenameResourceProcessor) refactoring.getProcessor();
	    String name = processor.getNewName();
        addPage(new RenameResourcePage(mType, name, mCanClear));
	}

    /**
     * Initiates a renaming of a resource item
     *
     * @param shell the shell to parent the dialog to
     * @param project the project containing the resource references
     * @param type the type of resource
     * @param currentName the name of the resource
     * @param newName the new name, or null if not known
     * @param canClear whether the name is allowed to be cleared
     * @return false if initiating the rename failed
     */
    public static RenameResult renameResource(
            @NonNull Shell shell,
            @NonNull IProject project,
            @NonNull ResourceType type,
            @NonNull String currentName,
            @Nullable String newName,
            boolean canClear) {
        try {
            RenameResourceProcessor processor = new RenameResourceProcessor(project, type,
                    currentName, newName);
            RenameRefactoring refactoring = new RenameRefactoring(processor);
            if (!refactoring.isApplicable()) {
                return RenameResult.unavailable();
            }

            if (!show(refactoring, processor, shell, type, canClear)) {
                return RenameResult.canceled();
            }
            return RenameResult.name(processor.getNewName());
        } catch (CoreException e) {
            AdtPlugin.log(e, null);
        }

        return RenameResult.unavailable();
    }

    /**
     * Show a refactoring dialog for the given resource refactoring operation
     *
     * @param refactoring the rename refactoring
     * @param processor the field processor
     * @param parent the parent shell
     * @param type the resource type
     * @param canClear whether the user is allowed to clear/reset the name to
     *            nothing
     * @return true if the refactoring was performed, and false if it was
     *         canceled
     * @throws CoreException if an unexpected error occurs
     */
    private static boolean show(
            @NonNull RenameRefactoring refactoring,
            @NonNull RenameResourceProcessor processor,
            @NonNull Shell parent,
            @NonNull ResourceType type,
            boolean canClear) throws CoreException {
        RefactoringSaveHelper saveHelper = new RefactoringSaveHelper(
                RefactoringSaveHelper.SAVE_REFACTORING);
        if (!saveHelper.saveEditors(parent)) {
            return false;
        }

        try {
            RenameResourceWizard wizard = new RenameResourceWizard(refactoring, type, canClear);
            RefactoringWizardOpenOperation operation = new RefactoringWizardOpenOperation(wizard);
            String dialogTitle = wizard.getDefaultPageTitle();
            int result = operation.run(parent, dialogTitle == null ? "" : dialogTitle);
            RefactoringStatus status = operation.getInitialConditionCheckingStatus();
            if (status.hasFatalError()) {
                return false;
            }
            if (result == RefactoringWizardOpenOperation.INITIAL_CONDITION_CHECKING_FAILED
                    || result == IDialogConstants.CANCEL_ID) {
                saveHelper.triggerIncrementalBuild();
                return false;
            }

            // Save modified resources; need to trigger R file regeneration
            saveHelper.saveEditors(parent);

            return true;
        } catch (InterruptedException e) {
            return false; // Canceled
        }
    }
}