1 11 package org.eclipse.jdt.ui.actions; 12 13 14 import org.eclipse.jface.viewers.IStructuredSelection; 15 16 import org.eclipse.jface.text.ITextSelection; 17 18 import org.eclipse.ui.IWorkbenchSite; 19 import org.eclipse.ui.PlatformUI; 20 21 import org.eclipse.jdt.core.ICompilationUnit; 22 import org.eclipse.jdt.core.ISourceRange; 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.util.ExceptionHandler; 38 39 48 public class ConvertAnonymousToNestedAction extends SelectionDispatchAction { 49 50 private final JavaEditor fEditor; 51 52 56 public ConvertAnonymousToNestedAction(JavaEditor editor) { 57 super(editor.getEditorSite()); 58 setText(RefactoringMessages.ConvertAnonymousToNestedAction_Convert_Anonymous); 59 fEditor= editor; 60 setEnabled(SelectionConverter.getInputAsCompilationUnit(fEditor) != null); 61 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.CONVERT_ANONYMOUS_TO_NESTED_ACTION); 62 } 63 64 71 public ConvertAnonymousToNestedAction(IWorkbenchSite site) { 72 super(site); 73 fEditor= null; 74 setText(RefactoringMessages.ConvertAnonymousToNestedAction_Convert_Anonymous); 75 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.CONVERT_ANONYMOUS_TO_NESTED_ACTION); 76 } 77 78 80 83 public void selectionChanged(IStructuredSelection selection) { 84 try { 85 setEnabled(RefactoringAvailabilityTester.isConvertAnonymousAvailable(selection)); 86 } catch (JavaModelException e) { 87 if (JavaModelUtil.isExceptionToBeLogged(e)) 88 JavaPlugin.log(e); 89 setEnabled(false); 90 } 91 } 92 93 96 public void run(IStructuredSelection selection) { 97 IType type= getElement(selection); 98 if (type == null) 99 return; 100 ISourceRange range; 101 try { 102 range= type.getNameRange(); 103 run(type.getCompilationUnit(), range.getOffset(), range.getLength()); 104 } catch (JavaModelException e) { 105 ExceptionHandler.handle(e, RefactoringMessages.ConvertAnonymousToNestedAction_dialog_title, RefactoringMessages.NewTextRefactoringAction_exception); 106 } 107 } 108 109 private IType getElement(IStructuredSelection selection) { 110 if (selection.size() != 1) 111 return null; 112 Object element= selection.getFirstElement(); 113 if (!(element instanceof IType)) 114 return null; 115 IType type= (IType)element; 116 try { 117 if (type.isAnonymous()) 118 return type; 119 } catch (JavaModelException e) { 120 } 122 return null; 123 } 124 125 127 130 public void run(ITextSelection selection) { 131 try{ 132 run(SelectionConverter.getInputAsCompilationUnit(fEditor), selection.getOffset(), selection.getLength()); 133 } catch (JavaModelException e){ 134 ExceptionHandler.handle(e, RefactoringMessages.ConvertAnonymousToNestedAction_dialog_title, RefactoringMessages.NewTextRefactoringAction_exception); 135 } 136 } 137 138 141 public void selectionChanged(ITextSelection selection) { 142 setEnabled(fEditor != null && SelectionConverter.getInputAsCompilationUnit(fEditor) != null); 143 } 144 145 148 public void selectionChanged(JavaTextSelection selection) { 149 try { 150 setEnabled(RefactoringAvailabilityTester.isConvertAnonymousAvailable(selection)); 151 } catch (JavaModelException e) { 152 setEnabled(false); 153 } 154 } 155 156 158 private void run(ICompilationUnit unit, int offset, int length) throws JavaModelException { 159 if (!ActionUtil.isEditable(fEditor, getShell(), unit)) 160 return; 161 RefactoringExecutionStarter.startConvertAnonymousRefactoring(unit, offset, length, getShell()); 162 } 163 } 164 | Popular Tags |