• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.ide.eclipse.adt.internal.refactorings.core;
17 
18 import static com.android.SdkConstants.PREFIX_RESOURCE_REF;
19 import static com.android.SdkConstants.R_CLASS;
20 
21 import com.android.ide.eclipse.adt.internal.resources.ResourceNameValidator;
22 import com.android.resources.ResourceType;
23 
24 import org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage;
25 import org.eclipse.jface.dialogs.Dialog;
26 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
27 import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.events.SelectionListener;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Label;
36 import org.eclipse.swt.widgets.Text;
37 
38 import java.util.Set;
39 
40 @SuppressWarnings("restriction") // JDT refactoring UI
41 class RenameResourcePage extends TextInputWizardPage implements SelectionListener {
42     private Label mXmlLabel;
43     private Label mJavaLabel;
44     private Button mUpdateReferences;
45     private boolean mCanClear;
46     private ResourceType mType;
47     private ResourceNameValidator mValidator;
48 
49     /**
50      * Create the wizard.
51      * @param type the type of the resource to be renamed
52      * @param initial initial renamed value
53      * @param canClear whether the dialog should allow clearing the field
54      */
RenameResourcePage(ResourceType type, String initial, boolean canClear)55     public RenameResourcePage(ResourceType type, String initial, boolean canClear) {
56         super(type.getName(), true, initial);
57         mType = type;
58         mCanClear = canClear;
59 
60         mValidator = ResourceNameValidator.create(false /*allowXmlExtension*/,
61                 (Set<String>) null, mType);
62     }
63 
64     @SuppressWarnings("unused") // SWT constructors aren't really unused, they have side effects
65     @Override
createControl(Composite parent)66     public void createControl(Composite parent) {
67         Composite container = new Composite(parent, SWT.NULL);
68         setControl(container);
69         initializeDialogUnits(container);
70         container.setLayout(new GridLayout(2, false));
71         Label nameLabel = new Label(container, SWT.NONE);
72         nameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
73         nameLabel.setText("New Name:");
74         Text text = super.createTextInputField(container);
75         text.selectAll();
76         text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
77         Label xmlLabel = new Label(container, SWT.NONE);
78         xmlLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
79         xmlLabel.setText("XML:");
80         mXmlLabel = new Label(container, SWT.NONE);
81         mXmlLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
82         Label javaLabel = new Label(container, SWT.NONE);
83         javaLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
84         javaLabel.setText("Java:");
85         mJavaLabel = new Label(container, SWT.NONE);
86         mJavaLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
87         new Label(container, SWT.NONE);
88         new Label(container, SWT.NONE);
89         mUpdateReferences = new Button(container, SWT.CHECK);
90         mUpdateReferences.setSelection(true);
91         mUpdateReferences.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
92         mUpdateReferences.setText("Update References");
93         mUpdateReferences.addSelectionListener(this);
94 
95         Dialog.applyDialogFont(container);
96     }
97 
98     @Override
setVisible(boolean visible)99     public void setVisible(boolean visible) {
100         if (visible) {
101             RenameResourceProcessor processor = getProcessor();
102             String newName = processor.getNewName();
103             if (newName != null && newName.length() > 0
104                     && !newName.equals(getInitialValue())) {
105                 Text textField = getTextField();
106                 textField.setText(newName);
107                 textField.setSelection(0, newName.length());
108             }
109         }
110 
111         super.setVisible(visible);
112     }
113 
114     @Override
validateTextField(String newName)115     protected RefactoringStatus validateTextField(String newName) {
116         if (newName.isEmpty() && isEmptyInputValid()) {
117             getProcessor().setNewName("");
118             return RefactoringStatus.createWarningStatus(
119                     "The resource definition will be deleted");
120         }
121 
122         String error = mValidator.isValid(newName);
123         if (error != null) {
124             return RefactoringStatus.createErrorStatus(error);
125         }
126 
127         RenameResourceProcessor processor = getProcessor();
128         processor.setNewName(newName);
129         return processor.checkNewName(newName);
130     }
131 
getProcessor()132     private RenameResourceProcessor getProcessor() {
133         RenameRefactoring refactoring = (RenameRefactoring) getRefactoring();
134         return (RenameResourceProcessor) refactoring.getProcessor();
135     }
136 
137     @Override
isEmptyInputValid()138     protected boolean isEmptyInputValid() {
139         return mCanClear;
140     }
141 
142     @Override
isInitialInputValid()143     protected boolean isInitialInputValid() {
144         RenameResourceProcessor processor = getProcessor();
145         return processor.getNewName() != null
146                 && !processor.getNewName().equals(processor.getCurrentName());
147     }
148 
149     @Override
textModified(String text)150     protected void textModified(String text) {
151         super.textModified(text);
152         if (mXmlLabel != null && mJavaLabel != null) {
153             String xml = PREFIX_RESOURCE_REF + mType.getName() + '/' + text;
154             String java = R_CLASS + '.' + mType.getName() + '.' + text;
155             if (text.isEmpty()) {
156                 xml = java = "";
157             }
158             mXmlLabel.setText(xml);
159             mJavaLabel.setText(java);
160         }
161     }
162 
163     // ---- Implements SelectionListener ----
164 
165     @Override
widgetSelected(SelectionEvent e)166     public void widgetSelected(SelectionEvent e) {
167         if (e.getSource() == mUpdateReferences) {
168             RenameResourceProcessor processor = getProcessor();
169             boolean update = mUpdateReferences.getSelection();
170             processor.setUpdateReferences(update);
171         }
172     }
173 
174     @Override
widgetDefaultSelected(SelectionEvent e)175     public void widgetDefaultSelected(SelectionEvent e) {
176     }
177 }
178