Home | History | Annotate | Download | only in core
      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 
     17 package com.android.ide.eclipse.adt.internal.refactorings.core;
     18 
     19 import com.android.annotations.NonNull;
     20 import com.android.annotations.Nullable;
     21 import com.android.ide.eclipse.adt.AdtPlugin;
     22 import com.android.resources.ResourceType;
     23 
     24 import org.eclipse.core.resources.IProject;
     25 import org.eclipse.core.runtime.CoreException;
     26 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
     27 import org.eclipse.jdt.internal.ui.JavaPluginImages;
     28 import org.eclipse.jdt.internal.ui.refactoring.reorg.RenameRefactoringWizard;
     29 import org.eclipse.jdt.ui.refactoring.RefactoringSaveHelper;
     30 import org.eclipse.jface.dialogs.IDialogConstants;
     31 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
     32 import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;
     33 import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
     34 import org.eclipse.swt.widgets.Shell;
     35 
     36 /**
     37  * Rename refactoring wizard for Android resources such as {@code @id/foo}
     38  */
     39 @SuppressWarnings("restriction") // JDT refactoring UI
     40 public class RenameResourceWizard extends RenameRefactoringWizard {
     41 	private ResourceType mType;
     42     private boolean mCanClear;
     43 
     44     /**
     45      * Constructs a new {@linkplain RenameResourceWizard}
     46      *
     47      * @param refactoring the refactoring
     48      * @param type the type of resource being renamed
     49      * @param canClear whether the user can clear the value
     50      */
     51     public RenameResourceWizard(
     52             @NonNull RenameRefactoring refactoring,
     53             @NonNull ResourceType type,
     54             boolean canClear) {
     55         super(refactoring,
     56                 "Rename Resource",
     57                 "Enter the new name for this resource",
     58                 JavaPluginImages.DESC_WIZBAN_REFACTOR_FIELD,
     59                 IJavaHelpContextIds.RENAME_FIELD_WIZARD_PAGE);
     60         mType = type;
     61 		mCanClear = canClear;
     62 	}
     63 
     64 	@Override
     65 	protected void addUserInputPages() {
     66 	    RenameRefactoring refactoring = (RenameRefactoring) getRefactoring();
     67         RenameResourceProcessor processor = (RenameResourceProcessor) refactoring.getProcessor();
     68 	    String name = processor.getNewName();
     69         addPage(new RenameResourcePage(mType, name, mCanClear));
     70 	}
     71 
     72     /**
     73      * Initiates a renaming of a resource item
     74      *
     75      * @param shell the shell to parent the dialog to
     76      * @param project the project containing the resource references
     77      * @param type the type of resource
     78      * @param currentName the name of the resource
     79      * @param newName the new name, or null if not known
     80      * @param canClear whether the name is allowed to be cleared
     81      * @return false if initiating the rename failed
     82      */
     83     public static RenameResult renameResource(
     84             @NonNull Shell shell,
     85             @NonNull IProject project,
     86             @NonNull ResourceType type,
     87             @NonNull String currentName,
     88             @Nullable String newName,
     89             boolean canClear) {
     90         try {
     91             RenameResourceProcessor processor = new RenameResourceProcessor(project, type,
     92                     currentName, newName);
     93             RenameRefactoring refactoring = new RenameRefactoring(processor);
     94             if (!refactoring.isApplicable()) {
     95                 return RenameResult.unavailable();
     96             }
     97 
     98             if (!show(refactoring, processor, shell, type, canClear)) {
     99                 return RenameResult.canceled();
    100             }
    101             return RenameResult.name(processor.getNewName());
    102         } catch (CoreException e) {
    103             AdtPlugin.log(e, null);
    104         }
    105 
    106         return RenameResult.unavailable();
    107     }
    108 
    109     /**
    110      * Show a refactoring dialog for the given resource refactoring operation
    111      *
    112      * @param refactoring the rename refactoring
    113      * @param processor the field processor
    114      * @param parent the parent shell
    115      * @param type the resource type
    116      * @param canClear whether the user is allowed to clear/reset the name to
    117      *            nothing
    118      * @return true if the refactoring was performed, and false if it was
    119      *         canceled
    120      * @throws CoreException if an unexpected error occurs
    121      */
    122     private static boolean show(
    123             @NonNull RenameRefactoring refactoring,
    124             @NonNull RenameResourceProcessor processor,
    125             @NonNull Shell parent,
    126             @NonNull ResourceType type,
    127             boolean canClear) throws CoreException {
    128         RefactoringSaveHelper saveHelper = new RefactoringSaveHelper(
    129                 RefactoringSaveHelper.SAVE_REFACTORING);
    130         if (!saveHelper.saveEditors(parent)) {
    131             return false;
    132         }
    133 
    134         try {
    135             RenameResourceWizard wizard = new RenameResourceWizard(refactoring, type, canClear);
    136             RefactoringWizardOpenOperation operation = new RefactoringWizardOpenOperation(wizard);
    137             String dialogTitle = wizard.getDefaultPageTitle();
    138             int result = operation.run(parent, dialogTitle == null ? "" : dialogTitle);
    139             RefactoringStatus status = operation.getInitialConditionCheckingStatus();
    140             if (status.hasFatalError()) {
    141                 return false;
    142             }
    143             if (result == RefactoringWizardOpenOperation.INITIAL_CONDITION_CHECKING_FAILED
    144                     || result == IDialogConstants.CANCEL_ID) {
    145                 saveHelper.triggerIncrementalBuild();
    146                 return false;
    147             }
    148 
    149             // Save modified resources; need to trigger R file regeneration
    150             saveHelper.saveEditors(parent);
    151 
    152             return true;
    153         } catch (InterruptedException e) {
    154             return false; // Canceled
    155         }
    156     }
    157 }
    158