1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.util.Arrays ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Set ; 17 18 import org.eclipse.jface.dialogs.MessageDialog; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 21 import org.eclipse.jface.text.ITextSelection; 22 23 import org.eclipse.ui.IWorkbenchSite; 24 import org.eclipse.ui.PlatformUI; 25 26 import org.eclipse.jdt.core.IJavaElement; 27 import org.eclipse.jdt.core.IMember; 28 import org.eclipse.jdt.core.IType; 29 import org.eclipse.jdt.core.JavaModelException; 30 31 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 32 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 33 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 34 35 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 36 import org.eclipse.jdt.internal.ui.JavaPlugin; 37 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 38 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 39 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 40 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 41 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 42 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 43 44 57 public class PullUpAction extends SelectionDispatchAction { 58 59 private static IMember[] getSelectedMembers(IStructuredSelection selection) { 60 if (selection.isEmpty()) 61 return null; 62 if (selection.size() == 1) { 63 try { 64 final IType type= RefactoringAvailabilityTester.getSingleSelectedType(selection); 65 if (type != null) 66 return new IType[] { type}; 67 } catch (JavaModelException exception) { 68 JavaPlugin.log(exception); 69 } 70 } 71 for (Iterator iter= selection.iterator(); iter.hasNext();) { 72 if (!(iter.next() instanceof IMember)) 73 return null; 74 } 75 Set memberSet= new HashSet (); 76 memberSet.addAll(Arrays.asList(selection.toArray())); 77 return (IMember[]) memberSet.toArray(new IMember[memberSet.size()]); 78 } 79 80 private JavaEditor fEditor; 81 82 89 public PullUpAction(JavaEditor editor) { 90 this(editor.getEditorSite()); 91 fEditor= editor; 92 setEnabled(SelectionConverter.canOperateOn(fEditor)); 93 } 94 95 103 public PullUpAction(IWorkbenchSite site) { 104 super(site); 105 setText(RefactoringMessages.RefactoringGroup_pull_Up_label); 106 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.PULL_UP_ACTION); 107 } 108 109 private IMember getSelectedMemberFromEditor() throws JavaModelException { 110 IJavaElement element= SelectionConverter.resolveEnclosingElement(fEditor, (ITextSelection) fEditor.getSelectionProvider().getSelection()); 111 if (element == null || !(element instanceof IMember)) 112 return null; 113 return (IMember) element; 114 } 115 116 119 public void run(IStructuredSelection selection) { 120 try { 121 IMember[] members= getSelectedMembers(selection); 122 if (RefactoringAvailabilityTester.isPullUpAvailable(members) && ActionUtil.isEditable(getShell(), members[0])) 123 RefactoringExecutionStarter.startPullUpRefactoring(members, getShell()); 124 } catch (JavaModelException e) { 125 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 126 } 127 } 128 129 132 public void run(ITextSelection selection) { 133 try { 134 if (!ActionUtil.isEditable(fEditor)) 135 return; 136 IMember member= getSelectedMemberFromEditor(); 137 IMember[] array= new IMember[] { member}; 138 if (member != null && RefactoringAvailabilityTester.isPullUpAvailable(array)) { 139 RefactoringExecutionStarter.startPullUpRefactoring(array, getShell()); 140 } else { 141 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.PullUpAction_unavailable); 142 } 143 } catch (JavaModelException e) { 144 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 145 } 146 } 147 148 151 public void selectionChanged(IStructuredSelection selection) { 152 try { 153 setEnabled(RefactoringAvailabilityTester.isPullUpAvailable(selection)); 154 } catch (JavaModelException e) { 155 if (JavaModelUtil.isExceptionToBeLogged(e)) 157 JavaPlugin.log(e); 158 setEnabled(false); } 160 } 161 162 165 public void selectionChanged(ITextSelection selection) { 166 setEnabled(true); 167 } 168 169 173 public void selectionChanged(JavaTextSelection selection) { 174 try { 175 setEnabled(RefactoringAvailabilityTester.isPullUpAvailable(selection)); 176 } catch (JavaModelException e) { 177 setEnabled(false); 178 } 179 } 180 } | Popular Tags |