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.ISelection; 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 import org.eclipse.jface.viewers.SelectionChangedEvent; 22 import org.eclipse.jface.viewers.StructuredSelection; 23 24 import org.eclipse.jface.text.ITextSelection; 25 26 import org.eclipse.ui.IWorkbenchSite; 27 import org.eclipse.ui.PlatformUI; 28 29 import org.eclipse.jdt.core.IJavaElement; 30 import org.eclipse.jdt.core.IMember; 31 import org.eclipse.jdt.core.IMethod; 32 import org.eclipse.jdt.core.JavaModelException; 33 34 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 35 36 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 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.refactoring.RefactoringMessages; 41 import org.eclipse.jdt.internal.ui.refactoring.actions.MoveInstanceMethodAction; 42 import org.eclipse.jdt.internal.ui.refactoring.actions.MoveStaticMembersAction; 43 import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgMoveAction; 44 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 45 46 60 public class MoveAction extends SelectionDispatchAction{ 61 70 private JavaEditor fEditor; 71 private MoveInstanceMethodAction fMoveInstanceMethodAction; 72 private MoveStaticMembersAction fMoveStaticMembersAction; 73 private ReorgMoveAction fReorgMoveAction; 74 75 82 public MoveAction(IWorkbenchSite site) { 83 super(site); 84 setText(RefactoringMessages.MoveAction_text); 85 fMoveStaticMembersAction= new MoveStaticMembersAction(site); 86 fMoveInstanceMethodAction= new MoveInstanceMethodAction(site); 87 fReorgMoveAction= new ReorgMoveAction(site); 88 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.MOVE_ACTION); 89 } 90 91 95 public MoveAction(JavaEditor editor) { 96 super(editor.getEditorSite()); 97 fEditor= editor; 98 setText(RefactoringMessages.MoveAction_text); 99 fMoveStaticMembersAction= new MoveStaticMembersAction(editor); 100 fMoveInstanceMethodAction= new MoveInstanceMethodAction(editor); 101 fReorgMoveAction= new ReorgMoveAction(editor.getEditorSite()); 102 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.MOVE_ACTION); 103 } 104 105 108 public void selectionChanged(SelectionChangedEvent event) { 109 fMoveStaticMembersAction.selectionChanged(event); 110 fMoveInstanceMethodAction.selectionChanged(event); 111 fReorgMoveAction.selectionChanged(event); 112 setEnabled(computeEnableState()); 113 } 114 115 118 public void run(IStructuredSelection selection) { 119 try { 120 if (fMoveInstanceMethodAction.isEnabled() && tryMoveInstanceMethod(selection)) 121 return; 122 123 if (fMoveStaticMembersAction.isEnabled() && tryMoveStaticMembers(selection)) 124 return; 125 126 if (fReorgMoveAction.isEnabled()) 127 fReorgMoveAction.run(); 128 129 } catch (JavaModelException e) { 130 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 131 } 132 133 } 134 135 138 public void run(ITextSelection selection) { 139 try { 140 if (!ActionUtil.isEditable(fEditor)) 141 return; 142 if (fMoveStaticMembersAction.isEnabled() && tryMoveStaticMembers(selection)) 143 return; 144 145 if (fMoveInstanceMethodAction.isEnabled() && tryMoveInstanceMethod(selection)) 146 return; 147 148 if (tryReorgMove(selection)) 149 return; 150 151 MessageDialog.openInformation(getShell(), RefactoringMessages.MoveAction_Move, RefactoringMessages.MoveAction_select); 152 } catch (JavaModelException e) { 153 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 154 } 155 } 156 157 private boolean tryMoveStaticMembers(ITextSelection selection) throws JavaModelException { 158 IJavaElement element= SelectionConverter.getElementAtOffset(fEditor); 159 if (element == null || !(element instanceof IMember)) 160 return false; 161 IMember[] array= new IMember[] { (IMember) element}; 162 if (!RefactoringAvailabilityTester.isMoveStaticMembersAvailable(array)) 163 return false; 164 fMoveStaticMembersAction.run(selection); 165 return true; 166 } 167 168 private static IMember[] getSelectedMembers(IStructuredSelection selection){ 169 if (selection.isEmpty()) 170 return null; 171 172 for (Iterator iter= selection.iterator(); iter.hasNext(); ) { 173 if (! (iter.next() instanceof IMember)) 174 return null; 175 } 176 return convertToMemberArray(selection.toArray()); 177 } 178 179 private static IMember[] convertToMemberArray(Object [] obj) { 180 if (obj == null) 181 return null; 182 Set memberSet= new HashSet (); 183 memberSet.addAll(Arrays.asList(obj)); 184 return (IMember[]) memberSet.toArray(new IMember[memberSet.size()]); 185 } 186 187 private boolean tryMoveStaticMembers(IStructuredSelection selection) throws JavaModelException { 188 IMember[] array= getSelectedMembers(selection); 189 if (!RefactoringAvailabilityTester.isMoveStaticMembersAvailable(array)) 190 return false; 191 fMoveStaticMembersAction.run(selection); 192 return true; 193 } 194 195 private boolean tryMoveInstanceMethod(ITextSelection selection) throws JavaModelException { 196 IJavaElement element= SelectionConverter.getElementAtOffset(fEditor); 197 if (element == null || !(element instanceof IMethod)) 198 return false; 199 200 IMethod method= (IMethod) element; 201 if (!RefactoringAvailabilityTester.isMoveMethodAvailable(method)) 202 return false; 203 fMoveInstanceMethodAction.run(selection); 204 return true; 205 } 206 207 private boolean tryMoveInstanceMethod(IStructuredSelection selection) throws JavaModelException { 208 IMethod method= getSingleSelectedMethod(selection); 209 if (method == null) 210 return false; 211 if (!RefactoringAvailabilityTester.isMoveMethodAvailable(method)) 212 return false; 213 fMoveInstanceMethodAction.run(selection); 214 return true; 215 } 216 217 private static IMethod getSingleSelectedMethod(IStructuredSelection selection) { 218 if (selection.isEmpty() || selection.size() != 1) 219 return null; 220 221 Object first= selection.getFirstElement(); 222 if (! (first instanceof IMethod)) 223 return null; 224 return (IMethod) first; 225 } 226 227 228 private boolean tryReorgMove(ITextSelection selection) throws JavaModelException{ 229 IJavaElement element= SelectionConverter.getElementAtOffset(fEditor); 230 if (element == null) 231 return false; 232 StructuredSelection mockStructuredSelection= new StructuredSelection(element); 233 fReorgMoveAction.selectionChanged(mockStructuredSelection); 234 if (!fReorgMoveAction.isEnabled()) 235 return false; 236 237 fReorgMoveAction.run(mockStructuredSelection); 238 return true; 239 } 240 241 242 245 public void update(ISelection selection) { 246 fMoveStaticMembersAction.update(selection); 247 fMoveInstanceMethodAction.update(selection); 248 fReorgMoveAction.update(selection); 249 setEnabled(computeEnableState()); 250 } 251 252 private boolean computeEnableState(){ 253 return fMoveStaticMembersAction.isEnabled() 254 || fMoveInstanceMethodAction.isEnabled() 255 || fReorgMoveAction.isEnabled(); 256 } 257 } 258 | Popular Tags |