1 11 package org.eclipse.jdt.ui.actions; 12 13 import org.eclipse.core.runtime.CoreException; 14 15 import org.eclipse.jface.viewers.IStructuredSelection; 16 17 import org.eclipse.jface.text.ITextSelection; 18 import org.eclipse.jface.text.TextSelection; 19 20 import org.eclipse.ui.IWorkbenchSite; 21 import org.eclipse.ui.PlatformUI; 22 23 import org.eclipse.jdt.core.IMethod; 24 import org.eclipse.jdt.core.ISourceRange; 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.util.JavaModelUtil; 30 31 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 32 import org.eclipse.jdt.internal.ui.JavaPlugin; 33 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 34 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 35 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 36 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 37 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 38 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 39 40 49 public class IntroduceFactoryAction extends SelectionDispatchAction { 50 51 private JavaEditor fEditor; 52 53 57 public IntroduceFactoryAction(JavaEditor editor) { 58 this(editor.getEditorSite()); 59 fEditor= editor; 60 setEnabled(SelectionConverter.getInputAsCompilationUnit(fEditor) != null); 61 } 62 63 70 public IntroduceFactoryAction(IWorkbenchSite site) { 71 super(site); 72 setText(RefactoringMessages.IntroduceFactoryAction_label); 73 setToolTipText(RefactoringMessages.IntroduceFactoryAction_tooltipText); 74 setDescription(RefactoringMessages.IntroduceFactoryAction_description); 75 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.INTRODUCE_FACTORY_ACTION); 76 } 77 78 80 83 public void selectionChanged(IStructuredSelection selection) { 84 try { 85 setEnabled(RefactoringAvailabilityTester.isIntroduceFactoryAvailable(selection)); 86 } catch (JavaModelException e) { 87 if (JavaModelUtil.isExceptionToBeLogged(e)) 88 JavaPlugin.log(e); 89 setEnabled(false); } 91 } 92 93 96 public void run(IStructuredSelection selection) { 97 try { 98 if (RefactoringAvailabilityTester.isIntroduceFactoryAvailable(selection)) { 100 IMethod method= (IMethod) selection.getFirstElement(); 101 if (!ActionUtil.isEditable(getShell(), method)) 102 return; 103 ISourceRange range= method.getNameRange(); 104 RefactoringExecutionStarter.startIntroduceFactoryRefactoring(method.getCompilationUnit(), new TextSelection(range.getOffset(), range.getLength()), getShell()); 105 } 106 } catch (CoreException e) { 107 ExceptionHandler.handle(e, RefactoringMessages.IntroduceFactoryAction_dialog_title, RefactoringMessages.IntroduceFactoryAction_exception); 108 } 109 } 110 111 114 public void selectionChanged(ITextSelection selection) { 115 setEnabled(fEditor != null && SelectionConverter.getInputAsCompilationUnit(fEditor) != null); 116 } 117 118 121 public void selectionChanged(JavaTextSelection selection) { 122 try { 123 setEnabled(RefactoringAvailabilityTester.isIntroduceFactoryAvailable(selection)); 124 } catch (JavaModelException e) { 125 setEnabled(false); 126 } 127 } 128 129 public void run(ITextSelection selection) { 130 if (!ActionUtil.isEditable(fEditor)) 131 return; 132 try { 133 RefactoringExecutionStarter.startIntroduceFactoryRefactoring(SelectionConverter.getInputAsCompilationUnit(fEditor), selection, getShell()); 134 } catch (CoreException e) { 135 ExceptionHandler.handle(e, RefactoringMessages.IntroduceFactoryAction_dialog_title, RefactoringMessages.IntroduceFactoryAction_exception); 136 } 137 } 138 } 139 | Popular Tags |