aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/refactorings/core/RenameResourcePage.java
blob: 6779fd3227ad3d93c8c96ad9f90e2c3b9adcf293 (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
/*
 * 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 static com.android.SdkConstants.R_CLASS;

import com.android.ide.eclipse.adt.internal.resources.ResourceNameValidator;
import com.android.resources.ResourceType;

import org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import java.util.Set;

@SuppressWarnings("restriction") // JDT refactoring UI
class RenameResourcePage extends TextInputWizardPage implements SelectionListener {
    private Label mXmlLabel;
    private Label mJavaLabel;
    private Button mUpdateReferences;
    private boolean mCanClear;
    private ResourceType mType;
    private ResourceNameValidator mValidator;

    /**
     * Create the wizard.
     * @param type the type of the resource to be renamed
     * @param initial initial renamed value
     * @param canClear whether the dialog should allow clearing the field
     */
    public RenameResourcePage(ResourceType type, String initial, boolean canClear) {
        super(type.getName(), true, initial);
        mType = type;
        mCanClear = canClear;

        mValidator = ResourceNameValidator.create(false /*allowXmlExtension*/,
                (Set<String>) null, mType);
    }

    @SuppressWarnings("unused") // SWT constructors aren't really unused, they have side effects
    @Override
    public void createControl(Composite parent) {
        Composite container = new Composite(parent, SWT.NULL);
        setControl(container);
        initializeDialogUnits(container);
        container.setLayout(new GridLayout(2, false));
        Label nameLabel = new Label(container, SWT.NONE);
        nameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        nameLabel.setText("New Name:");
        Text text = super.createTextInputField(container);
        text.selectAll();
        text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
        Label xmlLabel = new Label(container, SWT.NONE);
        xmlLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        xmlLabel.setText("XML:");
        mXmlLabel = new Label(container, SWT.NONE);
        mXmlLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
        Label javaLabel = new Label(container, SWT.NONE);
        javaLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        javaLabel.setText("Java:");
        mJavaLabel = new Label(container, SWT.NONE);
        mJavaLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        mUpdateReferences = new Button(container, SWT.CHECK);
        mUpdateReferences.setSelection(true);
        mUpdateReferences.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
        mUpdateReferences.setText("Update References");
        mUpdateReferences.addSelectionListener(this);

        Dialog.applyDialogFont(container);
    }

    @Override
    public void setVisible(boolean visible) {
        if (visible) {
            RenameResourceProcessor processor = getProcessor();
            String newName = processor.getNewName();
            if (newName != null && newName.length() > 0
                    && !newName.equals(getInitialValue())) {
                Text textField = getTextField();
                textField.setText(newName);
                textField.setSelection(0, newName.length());
            }
        }

        super.setVisible(visible);
    }

    @Override
    protected RefactoringStatus validateTextField(String newName) {
        if (newName.isEmpty() && isEmptyInputValid()) {
            getProcessor().setNewName("");
            return RefactoringStatus.createWarningStatus(
                    "The resource definition will be deleted");
        }

        String error = mValidator.isValid(newName);
        if (error != null) {
            return RefactoringStatus.createErrorStatus(error);
        }

        RenameResourceProcessor processor = getProcessor();
        processor.setNewName(newName);
        return processor.checkNewName(newName);
    }

    private RenameResourceProcessor getProcessor() {
        RenameRefactoring refactoring = (RenameRefactoring) getRefactoring();
        return (RenameResourceProcessor) refactoring.getProcessor();
    }

    @Override
    protected boolean isEmptyInputValid() {
        return mCanClear;
    }

    @Override
    protected boolean isInitialInputValid() {
        RenameResourceProcessor processor = getProcessor();
        return processor.getNewName() != null
                && !processor.getNewName().equals(processor.getCurrentName());
    }

    @Override
    protected void textModified(String text) {
        super.textModified(text);
        if (mXmlLabel != null && mJavaLabel != null) {
            String xml = PREFIX_RESOURCE_REF + mType.getName() + '/' + text;
            String java = R_CLASS + '.' + mType.getName() + '.' + text;
            if (text.isEmpty()) {
                xml = java = "";
            }
            mXmlLabel.setText(xml);
            mJavaLabel.setText(java);
        }
    }

    // ---- Implements SelectionListener ----

    @Override
    public void widgetSelected(SelectionEvent e) {
        if (e.getSource() == mUpdateReferences) {
            RenameResourceProcessor processor = getProcessor();
            boolean update = mUpdateReferences.getSelection();
            processor.setUpdateReferences(update);
        }
    }

    @Override
    public void widgetDefaultSelected(SelectionEvent e) {
    }
}