1 11 package org.eclipse.jdt.ui.actions; 12 13 14 import java.io.CharConversionException ; 15 16 import org.eclipse.jface.dialogs.MessageDialog; 17 import org.eclipse.jface.viewers.IStructuredSelection; 18 19 import org.eclipse.jface.text.ITextSelection; 20 21 import org.eclipse.ui.IWorkbenchSite; 22 import org.eclipse.ui.PlatformUI; 23 24 import org.eclipse.jdt.core.ICompilationUnit; 25 import org.eclipse.jdt.core.IType; 26 import org.eclipse.jdt.core.JavaModelException; 27 28 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 29 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 30 import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil; 31 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 32 33 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 34 import org.eclipse.jdt.internal.ui.JavaPlugin; 35 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 36 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 37 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 38 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 39 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 40 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 41 42 51 public class ConvertNestedToTopAction extends SelectionDispatchAction { 52 53 private JavaEditor fEditor; 54 55 60 public ConvertNestedToTopAction(JavaEditor editor) { 61 this(editor.getEditorSite()); 62 fEditor= editor; 63 setEnabled(SelectionConverter.canOperateOn(fEditor)); 64 } 65 66 75 public ConvertNestedToTopAction(IWorkbenchSite site) { 76 super(site); 77 setText(RefactoringMessages.ConvertNestedToTopAction_Convert); 78 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.MOVE_INNER_TO_TOP_ACTION); 79 } 80 81 83 86 public void selectionChanged(IStructuredSelection selection) { 87 try { 88 setEnabled(RefactoringAvailabilityTester.isMoveInnerAvailable(selection)); 89 } catch (JavaModelException e) { 90 if (!(e.getException() instanceof CharConversionException ) && JavaModelUtil.isExceptionToBeLogged(e)) 92 JavaPlugin.log(e); 93 setEnabled(false); } 95 } 96 97 100 public void run(IStructuredSelection selection) { 101 try { 102 if (RefactoringAvailabilityTester.isMoveInnerAvailable(selection)) { 105 IType singleSelectedType= getSingleSelectedType(selection); 106 if (! ActionUtil.isEditable(getShell(), singleSelectedType)) 107 return; 108 RefactoringExecutionStarter.startMoveInnerRefactoring(singleSelectedType, getShell()); 109 } 110 } catch (JavaModelException e) { 111 ExceptionHandler.handle(e, 112 RefactoringMessages.OpenRefactoringWizardAction_refactoring, 113 RefactoringMessages.OpenRefactoringWizardAction_exception); 114 } 115 } 116 117 private static IType getSingleSelectedType(IStructuredSelection selection) throws JavaModelException { 118 if (selection.isEmpty() || selection.size() != 1) 119 return null; 120 121 Object first= selection.getFirstElement(); 122 if (first instanceof IType) 123 return (IType)first; 124 if (first instanceof ICompilationUnit) 125 return JavaElementUtil.getMainType((ICompilationUnit)first); 126 return null; 127 } 128 129 131 134 public void selectionChanged(ITextSelection selection) { 135 setEnabled(true); 136 } 137 138 141 public void selectionChanged(JavaTextSelection selection) { 142 try { 143 setEnabled(RefactoringAvailabilityTester.isMoveInnerAvailable(selection)); 144 } catch (JavaModelException e) { 145 setEnabled(false); 146 } 147 } 148 149 152 public void run(ITextSelection selection) { 153 try { 154 IType type= RefactoringAvailabilityTester.getDeclaringType(SelectionConverter.resolveEnclosingElement(fEditor, selection)); 155 if (type != null && RefactoringAvailabilityTester.isMoveInnerAvailable(type)) { 156 if (! ActionUtil.isEditable(fEditor, getShell(), type)) 157 return; 158 RefactoringExecutionStarter.startMoveInnerRefactoring(type, getShell()); 159 } else { 160 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.ConvertNestedToTopAction_To_activate); 161 } 162 } catch (JavaModelException e) { 163 ExceptionHandler.handle(e, 164 RefactoringMessages.OpenRefactoringWizardAction_refactoring, 165 RefactoringMessages.OpenRefactoringWizardAction_exception); 166 } 167 } 168 } 169 | Popular Tags |