1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.io.CharConversionException ; 14 15 import org.eclipse.jface.dialogs.MessageDialog; 16 import org.eclipse.jface.viewers.IStructuredSelection; 17 18 import org.eclipse.jface.text.ITextSelection; 19 20 import org.eclipse.ui.IWorkbenchSite; 21 import org.eclipse.ui.PlatformUI; 22 23 import org.eclipse.jdt.core.IType; 24 import org.eclipse.jdt.core.JavaModelException; 25 26 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 27 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 28 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 29 30 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 31 import org.eclipse.jdt.internal.ui.JavaPlugin; 32 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 33 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 34 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 35 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 36 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 37 import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringActions; 38 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 39 40 49 public class ExtractInterfaceAction extends SelectionDispatchAction { 50 51 private JavaEditor fEditor; 52 53 57 public ExtractInterfaceAction(JavaEditor editor) { 58 this(editor.getEditorSite()); 59 fEditor= editor; 60 setEnabled(SelectionConverter.canOperateOn(fEditor)); 61 } 62 63 70 public ExtractInterfaceAction(IWorkbenchSite site) { 71 super(site); 72 setText(RefactoringMessages.ExtractInterfaceAction_Extract_Interface); 73 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.EXTRACT_INTERFACE_ACTION); 74 } 75 76 78 81 public void selectionChanged(IStructuredSelection selection) { 82 try { 83 setEnabled(RefactoringAvailabilityTester.isExtractInterfaceAvailable(selection)); 84 } catch (JavaModelException e) { 85 if (!(e.getException() instanceof CharConversionException ) && JavaModelUtil.isExceptionToBeLogged(e)) 87 JavaPlugin.log(e); 88 setEnabled(false); } 90 } 91 92 95 public void run(IStructuredSelection selection) { 96 try { 97 if (RefactoringAvailabilityTester.isExtractInterfaceAvailable(selection)) { 98 IType singleSelectedType= RefactoringAvailabilityTester.getSingleSelectedType(selection); 99 if (! ActionUtil.isEditable(getShell(), singleSelectedType)) 100 return; 101 RefactoringExecutionStarter.startExtractInterfaceRefactoring(singleSelectedType, getShell()); 102 } 103 } catch (JavaModelException e) { 104 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 105 } 106 } 107 108 111 public void selectionChanged(ITextSelection selection) { 112 setEnabled(true); 113 } 114 115 118 public void selectionChanged(JavaTextSelection selection) { 119 try { 120 setEnabled(RefactoringAvailabilityTester.isExtractInterfaceAvailable(selection)); 121 } catch (JavaModelException e) { 122 setEnabled(false); 123 } 124 } 125 126 129 public void run(ITextSelection selection) { 130 try { 131 IType type= RefactoringActions.getEnclosingOrPrimaryType(fEditor); 132 if (RefactoringAvailabilityTester.isExtractInterfaceAvailable(type)) { 133 if (! ActionUtil.isEditable(fEditor, getShell(), type)) 134 return; 135 RefactoringExecutionStarter.startExtractInterfaceRefactoring(type, getShell()); 136 } else { 137 String unavailable= RefactoringMessages.ExtractInterfaceAction_To_activate; 138 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, unavailable); 139 } 140 } catch (JavaModelException e) { 141 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 142 } 143 } 144 } 145 | Popular Tags |