1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.util.List ; 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 22 import org.eclipse.jdt.core.IJavaElement; 23 import org.eclipse.jdt.core.JavaModelException; 24 25 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 26 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 27 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 28 29 import org.eclipse.jdt.internal.ui.JavaPlugin; 30 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 31 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 32 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 33 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 34 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 35 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 36 37 46 public class InferTypeArgumentsAction extends SelectionDispatchAction { 47 48 private JavaEditor fEditor; 49 50 55 public InferTypeArgumentsAction(JavaEditor editor) { 56 this(editor.getEditorSite()); 57 fEditor= editor; 58 setEnabled(SelectionConverter.canOperateOn(fEditor)); 59 } 60 61 68 public InferTypeArgumentsAction(IWorkbenchSite site) { 69 super(site); 70 setText(RefactoringMessages.InferTypeArgumentsAction_label); 71 } 72 73 76 public void selectionChanged(ITextSelection selection) { 77 setEnabled(true); 78 } 79 80 83 public void selectionChanged(JavaTextSelection selection) { 84 if (selection.getLength() != 0) 85 setEnabled(false); 86 else 87 setEnabled(SelectionConverter.canOperateOn(fEditor)); 88 } 89 90 93 public void selectionChanged(IStructuredSelection selection) { 94 try { 95 setEnabled(RefactoringAvailabilityTester.isInferTypeArgumentsAvailable(selection)); 96 } catch (JavaModelException e) { 97 if (JavaModelUtil.isExceptionToBeLogged(e)) 99 JavaPlugin.log(e); 100 setEnabled(false); } 102 } 103 104 107 public void run(IStructuredSelection selection) { 108 IJavaElement[] elements= getSelectedElements(selection); 109 try { 110 if (! ActionUtil.areProcessable(getShell(), elements)) 111 return; 112 113 if (RefactoringAvailabilityTester.isInferTypeArgumentsAvailable(elements)) { 114 RefactoringExecutionStarter.startInferTypeArgumentsRefactoring(elements, getShell()); 115 } else { 116 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.InferTypeArgumentsAction_unavailable); 117 } 118 } catch (JavaModelException e) { 119 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 120 } 121 } 122 123 126 public void run(ITextSelection selection) { 127 if (!ActionUtil.isEditable(fEditor)) 128 return; 129 IJavaElement element= SelectionConverter.getInput(fEditor); 130 IJavaElement[] array= new IJavaElement[] {element}; 131 try { 132 if (element != null && RefactoringAvailabilityTester.isInferTypeArgumentsAvailable(array)){ 133 RefactoringExecutionStarter.startInferTypeArgumentsRefactoring(array, getShell()); 134 } else { 135 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.InferTypeArgumentsAction_unavailable); 136 } 137 } catch (JavaModelException e) { 138 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 139 } 140 } 141 142 private static IJavaElement[] getSelectedElements(IStructuredSelection selection){ 143 List list= selection.toList(); 144 IJavaElement[] elements= new IJavaElement[list.size()]; 145 for (int i= 0; i < list.size(); i++) { 146 Object object= list.get(i); 147 if (object instanceof IJavaElement) 148 elements[i]= (IJavaElement) object; 149 else 150 return new IJavaElement[0]; 151 } 152 return elements; 153 } 154 } 155 | Popular Tags |