blob: 10c6bae455d791e2e8962621c2e5080128c970b0 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2000, 2003 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.ui.actions;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.help.WorkbenchHelp;
import org.eclipse.jdt.internal.corext.refactoring.code.ConvertAnonymousToNestedRefactoring;
import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
import org.eclipse.jdt.internal.ui.actions.ActionUtil;
import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
import org.eclipse.jdt.internal.ui.refactoring.ConvertAnonymousToNestedWizard;
import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
import org.eclipse.jdt.internal.ui.refactoring.RefactoringWizard;
import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringActions;
import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter;
import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
/**
* Action to convert an anonymous inner class to a nested class.
*
* <p>
* This class may be instantiated; it is not intended to be subclassed.
* </p>
*
* @since 2.1
*/
public class ConvertAnonymousToNestedAction extends SelectionDispatchAction {
private static final String DIALOG_MESSAGE_TITLE= RefactoringMessages.getString("ConvertAnonymousToNestedAction.dialog_title"); //$NON-NLS-1$
private final CompilationUnitEditor fEditor;
/**
* Note: This constructor is for internal use only. Clients should not call this constructor.
*/
public ConvertAnonymousToNestedAction(CompilationUnitEditor editor) {
super(editor.getEditorSite());
setText(RefactoringMessages.getString("ConvertAnonymousToNestedAction.Convert_Anonymous")); //$NON-NLS-1$
fEditor= editor;
setEnabled(SelectionConverter.getInputAsCompilationUnit(fEditor) != null);
WorkbenchHelp.setHelp(this, IJavaHelpContextIds.CONVERT_ANONYMOUS_TO_NESTED_ACTION);
}
public ConvertAnonymousToNestedAction(IWorkbenchSite site) {
super(site);
fEditor= null;
setText(RefactoringMessages.getString("ConvertAnonymousToNestedAction.Convert_Anonymous")); //$NON-NLS-1$
WorkbenchHelp.setHelp(this, IJavaHelpContextIds.CONVERT_ANONYMOUS_TO_NESTED_ACTION);
}
//---- Structured selection -----------------------------------------------------
/* (non-Javadoc)
* Method declared on SelectionDispatchAction
*/
public void selectionChanged(IStructuredSelection selection) {
setEnabled(getElement(selection) != null);
}
/* (non-Javadoc)
* Method declared on SelectionDispatchAction
*/
public void run(IStructuredSelection selection) {
IType type= getElement(selection);
if (type == null)
return;
ISourceRange range;
try {
range= type.getNameRange();
run(type.getCompilationUnit(), range.getOffset(), range.getLength());
} catch (JavaModelException e) {
ExceptionHandler.handle(e, DIALOG_MESSAGE_TITLE, RefactoringMessages.getString("NewTextRefactoringAction.exception")); //$NON-NLS-1$
}
}
private IType getElement(IStructuredSelection selection) {
if (selection.size() != 1)
return null;
Object element= selection.getFirstElement();
if (!(element instanceof IType))
return null;
IType type= (IType)element;
try {
if (type.isAnonymous())
return type;
} catch (JavaModelException e) {
// fall through
}
return null;
}
//---- Text selection -----------------------------------------------------------
/* (non-Javadoc)
* Method declared on SelectionDispatchAction
*/
public void run(ITextSelection selection) {
try{
run(SelectionConverter.getInputAsCompilationUnit(fEditor), selection.getOffset(), selection.getLength());
} catch (JavaModelException e){
ExceptionHandler.handle(e, DIALOG_MESSAGE_TITLE, RefactoringMessages.getString("NewTextRefactoringAction.exception")); //$NON-NLS-1$
}
}
/* (non-Javadoc)
* Method declared on SelectionDispatchAction
*/
public void selectionChanged(ITextSelection selection) {
setEnabled(canEnable(selection));
}
private boolean canEnable(ITextSelection selection) {
return fEditor != null && SelectionConverter.getInputAsCompilationUnit(fEditor) != null;
}
/**
* Note: This method is for internal use only. Clients should not call this method.
*/
public void selectionChanged(JavaTextSelection selection) {
try {
setEnabled(canEnable(selection));
} catch (JavaModelException e) {
setEnabled(false);
}
}
private boolean canEnable(JavaTextSelection selection) throws JavaModelException {
IType type= RefactoringActions.getEnclosingType(selection);
if (type == null)
return false;
return ConvertAnonymousToNestedRefactoring.isAvailable(type);
}
//---- helpers -------------------------------------------------------------------
private void run(ICompilationUnit unit, int offset, int length) throws JavaModelException {
if (!ActionUtil.isProcessable(getShell(), unit))
return;
ConvertAnonymousToNestedRefactoring refactoring= createRefactoring(unit, offset, length);
if (refactoring == null)
return;
new RefactoringStarter().activate(refactoring, createWizard(refactoring), getShell(), DIALOG_MESSAGE_TITLE, false);
}
private static ConvertAnonymousToNestedRefactoring createRefactoring(ICompilationUnit cunit, int offset, int length) {
return ConvertAnonymousToNestedRefactoring.create(cunit, offset, length);
}
private static RefactoringWizard createWizard(ConvertAnonymousToNestedRefactoring refactoring) {
return new ConvertAnonymousToNestedWizard(refactoring);
}
}