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.ICompilationUnit; 24 import org.eclipse.jdt.core.IType; 25 import org.eclipse.jdt.core.JavaModelException; 26 27 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 28 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 29 import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil; 30 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 31 32 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 33 import org.eclipse.jdt.internal.ui.JavaPlugin; 34 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 35 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 36 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 37 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 38 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 39 import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringActions; 40 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 41 42 51 public class UseSupertypeAction extends SelectionDispatchAction{ 54 55 private JavaEditor fEditor; 56 57 61 public UseSupertypeAction(JavaEditor editor) { 62 this(editor.getEditorSite()); 63 fEditor= editor; 64 setEnabled(SelectionConverter.canOperateOn(fEditor)); 65 } 66 67 74 public UseSupertypeAction(IWorkbenchSite site) { 75 super(site); 76 setText(RefactoringMessages.UseSupertypeAction_use_Supertype); 77 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.USE_SUPERTYPE_ACTION); 78 } 79 80 82 85 public void selectionChanged(IStructuredSelection selection) { 86 try { 87 setEnabled(RefactoringAvailabilityTester.isUseSuperTypeAvailable(selection)); 88 } catch (JavaModelException e) { 89 if (!(e.getException() instanceof CharConversionException ) && JavaModelUtil.isExceptionToBeLogged(e)) 91 JavaPlugin.log(e); 92 setEnabled(false); } 94 } 95 96 99 public void run(IStructuredSelection selection) { 100 try { 101 if (RefactoringAvailabilityTester.isUseSuperTypeAvailable(selection)) { 102 IType singleSelectedType= getSingleSelectedType(selection); 103 if (! ActionUtil.isEditable(getShell(), singleSelectedType)) 104 return; 105 RefactoringExecutionStarter.startUseSupertypeRefactoring(singleSelectedType, getShell()); 106 } 107 } catch (JavaModelException e) { 108 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 109 } 110 } 111 112 private static IType getSingleSelectedType(IStructuredSelection selection) throws JavaModelException{ 113 if (selection.isEmpty() || selection.size() != 1) 114 return null; 115 116 Object first= selection.getFirstElement(); 117 if (first instanceof IType) 118 return (IType)first; 119 if (first instanceof ICompilationUnit) 120 return JavaElementUtil.getMainType((ICompilationUnit)first); 121 return null; 122 } 123 124 126 129 public void selectionChanged(ITextSelection selection) { 130 setEnabled(true); 131 } 132 133 136 public void selectionChanged(JavaTextSelection selection) { 137 try { 138 setEnabled(RefactoringAvailabilityTester.isUseSuperTypeAvailable(selection)); 139 } catch (JavaModelException e) { 140 setEnabled(false); 141 } 142 } 143 144 147 public void run(ITextSelection selection) { 148 try { 149 IType type= RefactoringActions.getEnclosingOrPrimaryType(fEditor); 150 if (RefactoringAvailabilityTester.isUseSuperTypeAvailable(type)) { 151 if (! ActionUtil.isEditable(fEditor, getShell(), type)) 152 return; 153 RefactoringExecutionStarter.startUseSupertypeRefactoring(type, getShell()); 154 } else { 155 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.UseSupertypeAction_to_activate); 156 } 157 } catch (JavaModelException e) { 158 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 159 } 160 } 161 } 162 | Popular Tags |