FileDocCategorySizeDatePackage
ExtractStringAction.javaAPI DocAndroid 1.5 API7072Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.adt.refactorings.extractstring

ExtractStringAction

public class ExtractStringAction extends Object implements org.eclipse.ui.IWorkbenchWindowActionDelegate
Action executed when the "Extract String" menu item is invoked.

The intent of the action is to start a refactoring that extracts a source string and replaces it by an Android string resource ID.

Workflow:

  • The action is currently located in the Refactoring menu in the main menu.
  • TODO: extend the popup refactoring menu in a Java or Android XML file.
  • The action is only enabled if the selection is 1 character or more. That is at least part of the string must be selected, it's not enough to just move the insertion point. This is a limitation due to {@link #selectionChanged(IAction, ISelection)} not being called when the insertion point is merely moved. TODO: address this limitation.
      The action gets the current {@link ISelection}. It also knows the current {@link IWorkbenchWindow}. However for the refactoring we are also interested in having the actual resource file. By looking at the Active Window > Active Page > Active Editor we can get the {@link IEditorInput} and find the {@link ICompilationUnit} (aka Java file) that is being edited.
        TODO: change this to find the {@link IFile} being manipulated. The {@link ICompilationUnit} can be inferred using {@link JavaCore#createCompilationUnitFrom(IFile)}. This will allow us to be able to work with a selection from an Android XML file later.
      • The action creates a new {@link ExtractStringRefactoring} and make it run on in a new {@link ExtractStringWizard}.

    Fields Summary
    private org.eclipse.ui.IWorkbenchWindow
    mWindow
    Keep track of the current workbench window.
    private org.eclipse.jface.text.ITextSelection
    mSelection
    private org.eclipse.core.resources.IFile
    mFile
    Constructors Summary
    Methods Summary
    public voiddispose()

            // Nothing to do
        
    private org.eclipse.core.resources.IFilegetSelectedFile()
    Returns the active {@link IFile} (hopefully matching our selection) or null. The file is only returned if it's a file from a project with an Android nature.

    At that point we do not try to analyze if the selection nor the file is suitable for the refactoring. This check is performed when the refactoring is invoked since it can then produce meaningful error messages as needed.

            IWorkbenchWindow wwin = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
            if (wwin != null) {
                IWorkbenchPage page = wwin.getActivePage();
                if (page != null) {
                    IEditorPart editor = page.getActiveEditor();
                    if (editor != null) {
                        IEditorInput input = editor.getEditorInput();
                        
                        if (input instanceof FileEditorInput) {
                            FileEditorInput fi = (FileEditorInput) input;
                            IFile file = fi.getFile();
                            if (file.exists()) {
                                IProject proj = file.getProject();
                                try {
                                    if (proj != null && proj.hasNature(AndroidConstants.NATURE)) {
                                        return file;
                                    }
                                } catch (CoreException e) {
                                    // ignore
                                }
                            }
                        }
                    }
                }
            }
            
            return null;
        
    public voidinit(org.eclipse.ui.IWorkbenchWindow window)
    Keep track of the current workbench window.

            mWindow = window;
        
    public voidrun(org.eclipse.jface.action.IAction action)
    Create a new instance of our refactoring and a wizard to configure it.

            if (mSelection != null && mFile != null) {
                ExtractStringRefactoring ref = new ExtractStringRefactoring(mFile, mSelection);
                RefactoringWizard wizard = new ExtractStringWizard(ref, mFile.getProject());
                RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard);
                try {
                    op.run(mWindow.getShell(), wizard.getDefaultPageTitle());
                } catch (InterruptedException e) {
                    // Interrupted. Pass.
                }
            }
        
    public voidselectionChanged(org.eclipse.jface.action.IAction action, org.eclipse.jface.viewers.ISelection selection)
    Examine the selection to determine if the action should be enabled or not.

    Keep a link to the relevant selection structure (i.e. a part of the Java AST).

    
            // Note, two kinds of selections are returned here:
            // ITextSelection on a Java source window
            // IStructuredSelection in the outline or navigator
            // This simply deals with the refactoring based on a non-empty selection.
            // At that point, just enable the action and later decide if it's valid when it actually
            // runs since we don't have access to the AST yet.
    
            mSelection = null;
            mFile = null;
            
            if (selection instanceof ITextSelection) {
                mSelection = (ITextSelection) selection;
                if (mSelection.getLength() > 0) {
                    mFile = getSelectedFile();
                }
            }
    
            action.setEnabled(mSelection != null && mFile != null);