1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.commands.operations.IUndoContext; 18 19 import org.eclipse.core.runtime.PerformanceStats; 20 21 import org.eclipse.core.resources.ResourcesPlugin; 22 23 import org.eclipse.swt.events.MenuAdapter; 24 import org.eclipse.swt.events.MenuEvent; 25 import org.eclipse.swt.widgets.Menu; 26 27 import org.eclipse.jface.action.Action; 28 import org.eclipse.jface.action.IAction; 29 import org.eclipse.jface.action.IMenuListener; 30 import org.eclipse.jface.action.IMenuManager; 31 import org.eclipse.jface.action.MenuManager; 32 import org.eclipse.jface.action.Separator; 33 import org.eclipse.jface.commands.ActionHandler; 34 import org.eclipse.jface.viewers.ISelection; 35 import org.eclipse.jface.viewers.ISelectionChangedListener; 36 import org.eclipse.jface.viewers.ISelectionProvider; 37 38 import org.eclipse.jface.text.IDocument; 39 import org.eclipse.jface.text.ITextSelection; 40 41 import org.eclipse.ui.IActionBars; 42 import org.eclipse.ui.IEditorInput; 43 import org.eclipse.ui.IViewPart; 44 import org.eclipse.ui.IWorkbenchSite; 45 import org.eclipse.ui.actions.ActionFactory; 46 import org.eclipse.ui.actions.ActionGroup; 47 import org.eclipse.ui.handlers.IHandlerActivation; 48 import org.eclipse.ui.handlers.IHandlerService; 49 import org.eclipse.ui.operations.UndoRedoActionGroup; 50 import org.eclipse.ui.part.Page; 51 52 import org.eclipse.jdt.core.IJavaElement; 53 54 import org.eclipse.jdt.ui.IContextMenuConstants; 55 56 import org.eclipse.jdt.internal.ui.JavaPlugin; 57 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 58 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 59 import org.eclipse.jdt.internal.ui.actions.ExtractSuperClassAction; 60 import org.eclipse.jdt.internal.ui.actions.IntroduceParameterObjectAction; 61 import org.eclipse.jdt.internal.ui.actions.JDTQuickMenuAction; 62 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 63 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput; 64 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 65 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 66 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 67 68 78 public class RefactorActionGroup extends ActionGroup { 79 80 private static final String PERF_REFACTOR_ACTION_GROUP= "org.eclipse.jdt.ui/perf/explorer/RefactorActionGroup"; 82 87 public static final String MENU_ID= "org.eclipse.jdt.ui.refactoring.menu"; 89 95 public static final String GROUP_REORG= "reorgGroup"; 97 103 public static final String GROUP_TYPE= "typeGroup"; 105 111 public static final String GROUP_CODING= "codingGroup"; 113 119 public static final String GROUP_CODING2= "codingGroup2"; 121 127 private static final String GROUP_REORG2= "reorgGroup2"; 129 135 private static final String GROUP_TYPE2= "typeGroup2"; 137 private IWorkbenchSite fSite; 138 private JavaEditor fEditor; 139 private String fGroupName= IContextMenuConstants.GROUP_REORGANIZE; 140 private boolean fBinary= false; 141 142 private SelectionDispatchAction fMoveAction; 143 private SelectionDispatchAction fRenameAction; 144 private SelectionDispatchAction fModifyParametersAction; 145 private SelectionDispatchAction fConvertAnonymousToNestedAction; 146 private SelectionDispatchAction fConvertNestedToTopAction; 147 148 private SelectionDispatchAction fPullUpAction; 149 private SelectionDispatchAction fPushDownAction; 150 private SelectionDispatchAction fExtractInterfaceAction; 151 private SelectionDispatchAction fExtractSupertypeAction; 152 private SelectionDispatchAction fChangeTypeAction; 153 private SelectionDispatchAction fUseSupertypeAction; 154 private SelectionDispatchAction fInferTypeArgumentsAction; 155 156 private SelectionDispatchAction fInlineAction; 157 private SelectionDispatchAction fIntroduceIndirectionAction; 159 private SelectionDispatchAction fExtractMethodAction; 160 private SelectionDispatchAction fExtractTempAction; 161 private SelectionDispatchAction fExtractConstantAction; 162 private SelectionDispatchAction fIntroduceParameterAction; 163 private SelectionDispatchAction fIntroduceParameterObjectAction; 164 private SelectionDispatchAction fIntroduceFactoryAction; 165 private SelectionDispatchAction fConvertLocalToFieldAction; 166 private SelectionDispatchAction fSelfEncapsulateField; 167 168 private UndoRedoActionGroup fUndoRedoActionGroup; 169 170 private final List fActions= new ArrayList (); 171 172 private static final String QUICK_MENU_ID= "org.eclipse.jdt.ui.edit.text.java.refactor.quickMenu"; 174 private class RefactorQuickAccessAction extends JDTQuickMenuAction { 175 public RefactorQuickAccessAction(JavaEditor editor) { 176 super(editor, QUICK_MENU_ID); 177 } 178 protected void fillMenu(IMenuManager menu) { 179 fillQuickMenu(menu); 180 } 181 } 182 183 private JDTQuickMenuAction fQuickAccessAction; 184 private IHandlerActivation fQuickAccessHandlerActivation; 185 private IHandlerService fHandlerService; 186 187 private static class NoActionAvailable extends Action { 188 public NoActionAvailable() { 189 setEnabled(true); 190 setText(RefactoringMessages.RefactorActionGroup_no_refactoring_available); 191 } 192 } 193 private Action fNoActionAvailable= new NoActionAvailable(); 194 195 202 public RefactorActionGroup(IViewPart part) { 203 this(part.getSite()); 204 205 IUndoContext workspaceContext= (IUndoContext)ResourcesPlugin.getWorkspace().getAdapter(IUndoContext.class); 206 fUndoRedoActionGroup= new UndoRedoActionGroup(part.getViewSite(), workspaceContext, true); 207 } 208 209 216 public RefactorActionGroup(Page page) { 217 this(page.getSite()); 218 } 219 220 226 public RefactorActionGroup(JavaEditor editor, String groupName, boolean binary) { 227 228 final PerformanceStats stats= PerformanceStats.getStats(PERF_REFACTOR_ACTION_GROUP, this); 229 stats.startRun(); 230 231 fSite= editor.getEditorSite(); 232 fEditor= editor; 233 fGroupName= groupName; 234 fBinary= binary; 235 236 ISelectionProvider provider= editor.getSelectionProvider(); 237 ISelection selection= provider.getSelection(); 238 239 if (!fBinary) { 240 fRenameAction= new RenameAction(editor); 241 initAction(fRenameAction, selection, IJavaEditorActionDefinitionIds.RENAME_ELEMENT); 242 editor.setAction("RenameElement", fRenameAction); 244 fMoveAction= new MoveAction(editor); 245 initAction(fMoveAction, selection, IJavaEditorActionDefinitionIds.MOVE_ELEMENT); 246 editor.setAction("MoveElement", fMoveAction); 248 fModifyParametersAction= new ModifyParametersAction(editor); 249 initAction(fModifyParametersAction, selection, IJavaEditorActionDefinitionIds.MODIFY_METHOD_PARAMETERS); 250 editor.setAction("ModifyParameters", fModifyParametersAction); 252 fConvertAnonymousToNestedAction= new ConvertAnonymousToNestedAction(editor); 253 initUpdatingAction(fConvertAnonymousToNestedAction, provider, selection, IJavaEditorActionDefinitionIds.CONVERT_ANONYMOUS_TO_NESTED); 254 editor.setAction("ConvertAnonymousToNested", fConvertAnonymousToNestedAction); 256 fConvertNestedToTopAction= new ConvertNestedToTopAction(editor); 257 initAction(fConvertNestedToTopAction, selection, IJavaEditorActionDefinitionIds.MOVE_INNER_TO_TOP); 258 editor.setAction("MoveInnerToTop", fConvertNestedToTopAction); 260 fPullUpAction= new PullUpAction(editor); 261 initAction(fPullUpAction, selection, IJavaEditorActionDefinitionIds.PULL_UP); 262 editor.setAction("PullUp", fPullUpAction); 264 fPushDownAction= new PushDownAction(editor); 265 initAction(fPushDownAction, selection, IJavaEditorActionDefinitionIds.PUSH_DOWN); 266 editor.setAction("PushDown", fPushDownAction); 268 fExtractSupertypeAction= new ExtractSuperClassAction(editor); 269 initAction(fExtractSupertypeAction, selection, ExtractSuperClassAction.EXTRACT_SUPERTYPE); 270 editor.setAction("ExtractSupertype", fExtractSupertypeAction); 272 fExtractInterfaceAction= new ExtractInterfaceAction(editor); 273 initAction(fExtractInterfaceAction, selection, IJavaEditorActionDefinitionIds.EXTRACT_INTERFACE); 274 editor.setAction("ExtractInterface", fExtractInterfaceAction); 276 fChangeTypeAction= new ChangeTypeAction(editor); 277 initUpdatingAction(fChangeTypeAction, provider, selection, IJavaEditorActionDefinitionIds.CHANGE_TYPE); 278 editor.setAction("ChangeType", fChangeTypeAction); 280 fInferTypeArgumentsAction= new InferTypeArgumentsAction(editor); 281 initAction(fInferTypeArgumentsAction, selection, IJavaEditorActionDefinitionIds.INFER_TYPE_ARGUMENTS_ACTION); 282 editor.setAction("InferTypeArguments", fInferTypeArgumentsAction); 284 fExtractMethodAction= new ExtractMethodAction(editor); 285 initUpdatingAction(fExtractMethodAction, provider, selection, IJavaEditorActionDefinitionIds.EXTRACT_METHOD); 286 editor.setAction("ExtractMethod", fExtractMethodAction); 288 fExtractTempAction= new ExtractTempAction(editor); 289 initUpdatingAction(fExtractTempAction, provider, selection, IJavaEditorActionDefinitionIds.EXTRACT_LOCAL_VARIABLE); 290 editor.setAction("ExtractLocalVariable", fExtractTempAction); 292 fExtractConstantAction= new ExtractConstantAction(editor); 293 initUpdatingAction(fExtractConstantAction, provider, selection, IJavaEditorActionDefinitionIds.EXTRACT_CONSTANT); 294 editor.setAction("ExtractConstant", fExtractConstantAction); 296 fIntroduceParameterAction= new IntroduceParameterAction(editor); 297 initUpdatingAction(fIntroduceParameterAction, provider, selection, IJavaEditorActionDefinitionIds.INTRODUCE_PARAMETER); 298 editor.setAction("IntroduceParameter", fIntroduceParameterAction); 300 fIntroduceFactoryAction= new IntroduceFactoryAction(editor); 301 initUpdatingAction(fIntroduceFactoryAction, provider, selection, IJavaEditorActionDefinitionIds.INTRODUCE_FACTORY); 302 editor.setAction("IntroduceFactory", fIntroduceFactoryAction); 304 fConvertLocalToFieldAction= new ConvertLocalToFieldAction(editor); 305 initUpdatingAction(fConvertLocalToFieldAction, provider, selection, IJavaEditorActionDefinitionIds.PROMOTE_LOCAL_VARIABLE); 306 editor.setAction("PromoteTemp", fConvertLocalToFieldAction); 308 fSelfEncapsulateField= new SelfEncapsulateFieldAction(editor); 309 initAction(fSelfEncapsulateField, selection, IJavaEditorActionDefinitionIds.SELF_ENCAPSULATE_FIELD); 310 editor.setAction("SelfEncapsulateField", fSelfEncapsulateField); 312 fIntroduceParameterObjectAction= new IntroduceParameterObjectAction(editor); 313 initAction(fIntroduceParameterObjectAction, selection, IntroduceParameterObjectAction.ACTION_DEFINITION_ID); editor.setAction("IntroduceParameterObjectAction", fIntroduceParameterObjectAction); } 316 fIntroduceIndirectionAction= new IntroduceIndirectionAction(editor); 317 initUpdatingAction(fIntroduceIndirectionAction, provider, selection, IJavaEditorActionDefinitionIds.INTRODUCE_INDIRECTION); 318 editor.setAction("IntroduceIndirection", fIntroduceIndirectionAction); 320 fUseSupertypeAction= new UseSupertypeAction(editor); 321 initAction(fUseSupertypeAction, selection, IJavaEditorActionDefinitionIds.USE_SUPERTYPE); 322 editor.setAction("UseSupertype", fUseSupertypeAction); 324 fInlineAction= new InlineAction(editor); 325 initAction(fInlineAction, selection, IJavaEditorActionDefinitionIds.INLINE); 326 editor.setAction("Inline", fInlineAction); 328 installQuickAccessAction(); 329 330 stats.endRun(); 331 } 332 333 private RefactorActionGroup(IWorkbenchSite site) { 334 335 final PerformanceStats stats= PerformanceStats.getStats(PERF_REFACTOR_ACTION_GROUP, this); 336 stats.startRun(); 337 338 fSite= site; 339 ISelectionProvider provider= fSite.getSelectionProvider(); 340 ISelection selection= provider.getSelection(); 341 342 if (!fBinary) { 343 344 fMoveAction= new MoveAction(site); 345 initUpdatingAction(fMoveAction, provider, selection, IJavaEditorActionDefinitionIds.MOVE_ELEMENT); 346 347 fRenameAction= new RenameAction(site); 348 initUpdatingAction(fRenameAction, provider, selection, IJavaEditorActionDefinitionIds.RENAME_ELEMENT); 349 350 fModifyParametersAction= new ModifyParametersAction(fSite); 351 initUpdatingAction(fModifyParametersAction, provider, selection, IJavaEditorActionDefinitionIds.MODIFY_METHOD_PARAMETERS); 352 353 fPullUpAction= new PullUpAction(fSite); 354 initUpdatingAction(fPullUpAction, provider, selection, IJavaEditorActionDefinitionIds.PULL_UP); 355 356 fPushDownAction= new PushDownAction(fSite); 357 initUpdatingAction(fPushDownAction, provider, selection, IJavaEditorActionDefinitionIds.PUSH_DOWN); 358 359 fSelfEncapsulateField= new SelfEncapsulateFieldAction(fSite); 360 initUpdatingAction(fSelfEncapsulateField, provider, selection, IJavaEditorActionDefinitionIds.SELF_ENCAPSULATE_FIELD); 361 362 fIntroduceParameterObjectAction= new IntroduceParameterObjectAction(fSite); 363 initUpdatingAction(fIntroduceParameterObjectAction, provider, selection, IntroduceParameterObjectAction.ACTION_DEFINITION_ID); 364 365 fExtractSupertypeAction= new ExtractSuperClassAction(fSite); 366 initUpdatingAction(fExtractSupertypeAction, provider, selection, ExtractSuperClassAction.EXTRACT_SUPERTYPE); 367 368 fExtractInterfaceAction= new ExtractInterfaceAction(fSite); 369 initUpdatingAction(fExtractInterfaceAction, provider, selection, IJavaEditorActionDefinitionIds.EXTRACT_INTERFACE); 370 371 fChangeTypeAction= new ChangeTypeAction(fSite); 372 initUpdatingAction(fChangeTypeAction, provider, selection, IJavaEditorActionDefinitionIds.CHANGE_TYPE); 373 374 fConvertNestedToTopAction= new ConvertNestedToTopAction(fSite); 375 initUpdatingAction(fConvertNestedToTopAction, provider, selection, IJavaEditorActionDefinitionIds.MOVE_INNER_TO_TOP); 376 377 fInferTypeArgumentsAction= new InferTypeArgumentsAction(fSite); 378 initUpdatingAction(fInferTypeArgumentsAction, provider, selection, IJavaEditorActionDefinitionIds.INFER_TYPE_ARGUMENTS_ACTION); 379 380 fIntroduceFactoryAction= new IntroduceFactoryAction(fSite); 381 initUpdatingAction(fIntroduceFactoryAction, provider, selection, IJavaEditorActionDefinitionIds.INTRODUCE_FACTORY); 382 383 fConvertAnonymousToNestedAction= new ConvertAnonymousToNestedAction(fSite); 384 initUpdatingAction(fConvertAnonymousToNestedAction, provider, selection, IJavaEditorActionDefinitionIds.CONVERT_ANONYMOUS_TO_NESTED); 385 } 386 fInlineAction= new InlineAction(fSite); 387 initUpdatingAction(fInlineAction, provider, selection, IJavaEditorActionDefinitionIds.INLINE); 388 389 fUseSupertypeAction= new UseSupertypeAction(fSite); 390 initUpdatingAction(fUseSupertypeAction, provider, selection, IJavaEditorActionDefinitionIds.USE_SUPERTYPE); 391 392 fIntroduceIndirectionAction= new IntroduceIndirectionAction(fSite); 393 initUpdatingAction(fIntroduceIndirectionAction, provider, selection, IJavaEditorActionDefinitionIds.INTRODUCE_INDIRECTION); 394 395 installQuickAccessAction(); 396 397 stats.endRun(); 398 } 399 400 private void installQuickAccessAction() { 401 fHandlerService= (IHandlerService)fSite.getService(IHandlerService.class); 402 if (fHandlerService != null) { 403 fQuickAccessAction= new RefactorQuickAccessAction(fEditor); 404 fQuickAccessHandlerActivation= fHandlerService.activateHandler(fQuickAccessAction.getActionDefinitionId(), new ActionHandler(fQuickAccessAction)); 405 } 406 } 407 408 private void initAction(SelectionDispatchAction action, ISelection selection, String actionDefinitionId){ 409 initUpdatingAction(action, null, selection, actionDefinitionId); 410 } 411 412 421 private void initUpdatingAction(SelectionDispatchAction action, ISelectionProvider provider, ISelection selection, String actionDefinitionId){ 422 action.setActionDefinitionId(actionDefinitionId); 423 action.update(selection); 424 if (provider != null) 425 provider.addSelectionChangedListener(action); 426 fActions.add(action); 427 } 428 429 432 public void fillActionBars(IActionBars actionBars) { 433 super.fillActionBars(actionBars); 434 if (!fBinary) { 435 actionBars.setGlobalActionHandler(JdtActionConstants.SELF_ENCAPSULATE_FIELD, fSelfEncapsulateField); 436 actionBars.setGlobalActionHandler(JdtActionConstants.MOVE, fMoveAction); 437 actionBars.setGlobalActionHandler(JdtActionConstants.RENAME, fRenameAction); 438 actionBars.setGlobalActionHandler(JdtActionConstants.MODIFY_PARAMETERS, fModifyParametersAction); 439 actionBars.setGlobalActionHandler(JdtActionConstants.PULL_UP, fPullUpAction); 440 actionBars.setGlobalActionHandler(JdtActionConstants.PUSH_DOWN, fPushDownAction); 441 actionBars.setGlobalActionHandler(JdtActionConstants.EXTRACT_TEMP, fExtractTempAction); 442 actionBars.setGlobalActionHandler(JdtActionConstants.EXTRACT_CONSTANT, fExtractConstantAction); 443 actionBars.setGlobalActionHandler(JdtActionConstants.INTRODUCE_PARAMETER, fIntroduceParameterAction); 444 actionBars.setGlobalActionHandler(JdtActionConstants.INTRODUCE_FACTORY, fIntroduceFactoryAction); 445 actionBars.setGlobalActionHandler(JdtActionConstants.EXTRACT_METHOD, fExtractMethodAction); 446 actionBars.setGlobalActionHandler(JdtActionConstants.EXTRACT_INTERFACE, fExtractInterfaceAction); 448 actionBars.setGlobalActionHandler(ExtractSuperClassAction.EXTRACT_SUPERTYPES, fExtractSupertypeAction); 449 actionBars.setGlobalActionHandler(JdtActionConstants.CHANGE_TYPE, fChangeTypeAction); 450 actionBars.setGlobalActionHandler(JdtActionConstants.CONVERT_NESTED_TO_TOP, fConvertNestedToTopAction); 451 actionBars.setGlobalActionHandler(JdtActionConstants.INFER_TYPE_ARGUMENTS, fInferTypeArgumentsAction); 452 actionBars.setGlobalActionHandler(JdtActionConstants.CONVERT_LOCAL_TO_FIELD, fConvertLocalToFieldAction); 453 actionBars.setGlobalActionHandler(JdtActionConstants.CONVERT_ANONYMOUS_TO_NESTED, fConvertAnonymousToNestedAction); 454 actionBars.setGlobalActionHandler(IntroduceParameterObjectAction.ACTION_ID, fIntroduceParameterObjectAction); } 456 actionBars.setGlobalActionHandler(JdtActionConstants.INLINE, fInlineAction); 457 actionBars.setGlobalActionHandler(JdtActionConstants.USE_SUPERTYPE, fUseSupertypeAction); 458 actionBars.setGlobalActionHandler(JdtActionConstants.INTRODUCE_INDIRECTION, fIntroduceIndirectionAction); 459 if (fUndoRedoActionGroup != null) { 460 fUndoRedoActionGroup.fillActionBars(actionBars); 461 } 462 } 463 464 469 public void retargetFileMenuActions(IActionBars actionBars) { 470 actionBars.setGlobalActionHandler(ActionFactory.RENAME.getId(), fRenameAction); 471 actionBars.setGlobalActionHandler(ActionFactory.MOVE.getId(), fMoveAction); 472 } 473 474 477 public void fillContextMenu(IMenuManager menu) { 478 super.fillContextMenu(menu); 479 addRefactorSubmenu(menu); 480 } 481 482 485 public void dispose() { 486 ISelectionProvider provider= fSite.getSelectionProvider(); 487 if (!fBinary) { 488 disposeAction(fSelfEncapsulateField, provider); 489 disposeAction(fMoveAction, provider); 490 disposeAction(fRenameAction, provider); 491 disposeAction(fModifyParametersAction, provider); 492 disposeAction(fPullUpAction, provider); 493 disposeAction(fPushDownAction, provider); 494 disposeAction(fExtractTempAction, provider); 495 disposeAction(fExtractConstantAction, provider); 496 disposeAction(fIntroduceParameterAction, provider); 497 disposeAction(fIntroduceParameterObjectAction, provider); 498 disposeAction(fIntroduceFactoryAction, provider); 499 disposeAction(fExtractMethodAction, provider); 500 disposeAction(fExtractInterfaceAction, provider); 502 disposeAction(fExtractSupertypeAction, provider); 503 disposeAction(fChangeTypeAction, provider); 504 disposeAction(fConvertNestedToTopAction, provider); 505 disposeAction(fInferTypeArgumentsAction, provider); 506 disposeAction(fConvertLocalToFieldAction, provider); 507 disposeAction(fConvertAnonymousToNestedAction, provider); 508 } 509 disposeAction(fIntroduceIndirectionAction, provider); 510 disposeAction(fInlineAction, provider); 511 disposeAction(fUseSupertypeAction, provider); 512 if (fQuickAccessHandlerActivation != null && fHandlerService != null) { 513 fHandlerService.deactivateHandler(fQuickAccessHandlerActivation); 514 } 515 if (fUndoRedoActionGroup != null) { 516 fUndoRedoActionGroup.dispose(); 517 } 518 super.dispose(); 519 } 520 521 private void disposeAction(ISelectionChangedListener action, ISelectionProvider provider) { 522 if (action != null) 523 provider.removeSelectionChangedListener(action); 524 } 525 526 private void addRefactorSubmenu(IMenuManager menu) { 527 String menuText= ActionMessages.RefactorMenu_label; 528 if (fQuickAccessAction != null) { 529 menuText= fQuickAccessAction.addShortcut(menuText); 530 } 531 IMenuManager refactorSubmenu= new MenuManager(menuText, MENU_ID); 532 if (fEditor != null) { 533 IJavaElement element= SelectionConverter.getInput(fEditor); 534 if (element != null && ActionUtil.isOnBuildPath(element)) { 535 refactorSubmenu.addMenuListener(new IMenuListener() { 536 public void menuAboutToShow(IMenuManager manager) { 537 refactorMenuShown(manager); 538 } 539 }); 540 refactorSubmenu.add(fNoActionAvailable); 541 menu.appendToGroup(fGroupName, refactorSubmenu); 542 } 543 } else { 544 ISelection selection= fSite.getSelectionProvider().getSelection(); 545 for (Iterator iter= fActions.iterator(); iter.hasNext(); ) { 546 ((SelectionDispatchAction)iter.next()).update(selection); 547 } 548 if (fillRefactorMenu(refactorSubmenu) > 0) 549 menu.appendToGroup(fGroupName, refactorSubmenu); 550 } 551 } 552 553 private int fillRefactorMenu(IMenuManager refactorSubmenu) { 554 int added= 0; 555 refactorSubmenu.add(new Separator(GROUP_REORG)); 556 added+= addAction(refactorSubmenu, fRenameAction); 557 added+= addAction(refactorSubmenu, fMoveAction); 558 refactorSubmenu.add(new Separator(GROUP_CODING)); 559 added+= addAction(refactorSubmenu, fModifyParametersAction); 560 added+= addAction(refactorSubmenu, fExtractMethodAction); 561 added+= addAction(refactorSubmenu, fExtractTempAction); 562 added+= addAction(refactorSubmenu, fExtractConstantAction); 563 added+= addAction(refactorSubmenu, fInlineAction); 564 refactorSubmenu.add(new Separator(GROUP_REORG2)); 565 added+= addAction(refactorSubmenu, fConvertAnonymousToNestedAction); 566 added+= addAction(refactorSubmenu, fConvertNestedToTopAction); 567 added+= addAction(refactorSubmenu, fConvertLocalToFieldAction); 568 refactorSubmenu.add(new Separator(GROUP_TYPE)); 569 added+= addAction(refactorSubmenu, fExtractInterfaceAction); 570 added+= addAction(refactorSubmenu, fExtractSupertypeAction); 571 added+= addAction(refactorSubmenu, fUseSupertypeAction); 572 added+= addAction(refactorSubmenu, fPullUpAction); 573 added+= addAction(refactorSubmenu, fPushDownAction); 574 refactorSubmenu.add(new Separator(GROUP_CODING2)); 575 added+= addAction(refactorSubmenu, fIntroduceIndirectionAction); 576 added+= addAction(refactorSubmenu, fIntroduceFactoryAction); 577 added+= addAction(refactorSubmenu, fIntroduceParameterAction); 578 added+= addAction(refactorSubmenu, fIntroduceParameterObjectAction); 579 added+= addAction(refactorSubmenu, fSelfEncapsulateField); 580 refactorSubmenu.add(new Separator(GROUP_TYPE2)); 582 added+= addAction(refactorSubmenu, fChangeTypeAction); 583 added+= addAction(refactorSubmenu, fInferTypeArgumentsAction); 584 return added; 585 } 586 587 private int addAction(IMenuManager menu, IAction action) { 588 if (action != null && action.isEnabled()) { 589 menu.add(action); 590 return 1; 591 } 592 return 0; 593 } 594 595 private void refactorMenuShown(final IMenuManager refactorSubmenu) { 596 Menu menu= ((MenuManager)refactorSubmenu).getMenu(); 599 menu.addMenuListener(new MenuAdapter() { 600 public void menuHidden(MenuEvent e) { 601 refactorMenuHidden(refactorSubmenu); 602 } 603 }); 604 ITextSelection textSelection= (ITextSelection)fEditor.getSelectionProvider().getSelection(); 605 JavaTextSelection javaSelection= new JavaTextSelection( 606 getEditorInput(), getDocument(), textSelection.getOffset(), textSelection.getLength()); 607 608 for (Iterator iter= fActions.iterator(); iter.hasNext(); ) { 609 SelectionDispatchAction action= (SelectionDispatchAction)iter.next(); 610 action.update(javaSelection); 611 } 612 refactorSubmenu.removeAll(); 613 if (fillRefactorMenu(refactorSubmenu) == 0) 614 refactorSubmenu.add(fNoActionAvailable); 615 } 616 617 private void refactorMenuHidden(IMenuManager manager) { 618 ITextSelection textSelection= (ITextSelection)fEditor.getSelectionProvider().getSelection(); 619 for (Iterator iter= fActions.iterator(); iter.hasNext(); ) { 620 SelectionDispatchAction action= (SelectionDispatchAction)iter.next(); 621 action.update(textSelection); 622 } 623 } 624 625 private IJavaElement getEditorInput() { 626 final IEditorInput input= fEditor.getEditorInput(); 627 if (input instanceof IClassFileEditorInput) { 628 IClassFileEditorInput extended= (IClassFileEditorInput) input; 629 return extended.getClassFile(); 630 } 631 return JavaPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(input); 632 } 633 634 private IDocument getDocument() { 635 return JavaPlugin.getDefault().getCompilationUnitDocumentProvider(). 636 getDocument(fEditor.getEditorInput()); 637 } 638 639 private void fillQuickMenu(IMenuManager menu) { 640 if (fEditor != null) { 641 IJavaElement element= SelectionConverter.getInput(fEditor); 642 if (element == null || !ActionUtil.isOnBuildPath(element)) { 643 menu.add(fNoActionAvailable); 644 return; 645 } 646 ITextSelection textSelection= (ITextSelection)fEditor.getSelectionProvider().getSelection(); 647 JavaTextSelection javaSelection= new JavaTextSelection( 648 getEditorInput(), getDocument(), textSelection.getOffset(), textSelection.getLength()); 649 650 for (Iterator iter= fActions.iterator(); iter.hasNext(); ) { 651 ((SelectionDispatchAction)iter.next()).update(javaSelection); 652 } 653 fillRefactorMenu(menu); 654 for (Iterator iter= fActions.iterator(); iter.hasNext(); ) { 655 ((SelectionDispatchAction)iter.next()).update(textSelection); 656 } 657 658 } else { 659 ISelection selection= fSite.getSelectionProvider().getSelection(); 660 for (Iterator iter= fActions.iterator(); iter.hasNext(); ) { 661 ((SelectionDispatchAction)iter.next()).update(selection); 662 } 663 fillRefactorMenu(menu); 664 } 665 } 666 } 667 | Popular Tags |