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.runtime.Assert; 18 19 import org.eclipse.jface.action.IAction; 20 import org.eclipse.jface.action.IMenuManager; 21 import org.eclipse.jface.action.MenuManager; 22 import org.eclipse.jface.action.Separator; 23 import org.eclipse.jface.commands.ActionHandler; 24 import org.eclipse.jface.viewers.ISelection; 25 import org.eclipse.jface.viewers.ISelectionChangedListener; 26 import org.eclipse.jface.viewers.ISelectionProvider; 27 import org.eclipse.jface.viewers.IStructuredSelection; 28 29 import org.eclipse.ui.IActionBars; 30 import org.eclipse.ui.IViewPart; 31 import org.eclipse.ui.IWorkbenchSite; 32 import org.eclipse.ui.actions.ActionGroup; 33 import org.eclipse.ui.actions.AddBookmarkAction; 34 import org.eclipse.ui.handlers.IHandlerActivation; 35 import org.eclipse.ui.handlers.IHandlerService; 36 import org.eclipse.ui.part.Page; 37 import org.eclipse.ui.texteditor.IUpdate; 38 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds; 39 40 import org.eclipse.ui.ide.IDEActionFactory; 41 42 import org.eclipse.jdt.ui.IContextMenuConstants; 43 44 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 45 import org.eclipse.jdt.internal.ui.actions.AddTaskAction; 46 import org.eclipse.jdt.internal.ui.actions.AllCleanUpsAction; 47 import org.eclipse.jdt.internal.ui.actions.FindBrokenNLSKeysAction; 48 import org.eclipse.jdt.internal.ui.actions.JDTQuickMenuAction; 49 import org.eclipse.jdt.internal.ui.actions.MultiSortMembersAction; 50 import org.eclipse.jdt.internal.ui.javaeditor.AddImportOnSelectionAction; 51 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor; 52 53 63 public class GenerateActionGroup extends ActionGroup { 64 65 70 public static final String MENU_ID= "org.eclipse.jdt.ui.source.menu"; 72 78 public static final String GROUP_IMPORT= "importGroup"; 80 86 public static final String GROUP_GENERATE= "generateGroup"; 88 94 public static final String GROUP_CODE= "codeGroup"; 96 102 private static final String GROUP_EXTERNALIZE= "externalizeGroup"; 104 110 private static final String GROUP_COMMENT= "commentGroup"; 112 118 private static final String GROUP_EDIT= "editGroup"; 120 private CompilationUnitEditor fEditor; 121 private IWorkbenchSite fSite; 122 private String fGroupName= IContextMenuConstants.GROUP_REORGANIZE; 123 private List fRegisteredSelectionListeners; 124 125 private AddImportOnSelectionAction fAddImport; 126 private OverrideMethodsAction fOverrideMethods; 127 private GenerateHashCodeEqualsAction fHashCodeEquals; 128 private AddGetterSetterAction fAddGetterSetter; 129 private AddDelegateMethodsAction fAddDelegateMethods; 130 private AddUnimplementedConstructorsAction fAddUnimplementedConstructors; 131 private GenerateNewConstructorUsingFieldsAction fGenerateConstructorUsingFields; 132 private AddJavaDocStubAction fAddJavaDocStub; 133 private AddBookmarkAction fAddBookmark; 134 private AddTaskAction fAddTaskAction; 135 private ExternalizeStringsAction fExternalizeStrings; 136 private AllCleanUpsAction fCleanUp; 137 private FindBrokenNLSKeysAction fFindNLSProblems; 138 139 private OrganizeImportsAction fOrganizeImports; 140 private MultiSortMembersAction fSortMembers; 141 private FormatAllAction fFormatAll; 142 143 private static final String QUICK_MENU_ID= "org.eclipse.jdt.ui.edit.text.java.source.quickMenu"; 145 private class SourceQuickAccessAction extends JDTQuickMenuAction { 146 public SourceQuickAccessAction(CompilationUnitEditor editor) { 147 super(editor, QUICK_MENU_ID); 148 } 149 protected void fillMenu(IMenuManager menu) { 150 fillQuickMenu(menu); 151 } 152 } 153 154 private JDTQuickMenuAction fQuickAccessAction; 155 private IHandlerActivation fQuickAccessHandlerActivation; 156 private IHandlerService fHandlerService; 157 158 163 public GenerateActionGroup(CompilationUnitEditor editor, String groupName) { 164 fSite= editor.getSite(); 165 fEditor= editor; 166 fGroupName= groupName; 167 168 fAddImport= new AddImportOnSelectionAction(editor); 169 fAddImport.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_IMPORT); 170 fAddImport.update(); 171 editor.setAction("AddImport", fAddImport); 173 fOrganizeImports= new OrganizeImportsAction(editor); 174 fOrganizeImports.setActionDefinitionId(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS); 175 editor.setAction("OrganizeImports", fOrganizeImports); 177 fSortMembers= new MultiSortMembersAction(editor); 178 fSortMembers.setActionDefinitionId(IJavaEditorActionDefinitionIds.SORT_MEMBERS); 179 editor.setAction("SortMembers", fSortMembers); 181 fOverrideMethods= new OverrideMethodsAction(editor); 182 fOverrideMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.OVERRIDE_METHODS); 183 editor.setAction("OverrideMethods", fOverrideMethods); 185 fAddGetterSetter= new AddGetterSetterAction(editor); 186 fAddGetterSetter.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_GETTER_SETTER); 187 editor.setAction("AddGetterSetter", fAddGetterSetter); 189 fAddDelegateMethods= new AddDelegateMethodsAction(editor); 190 fAddDelegateMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_DELEGATE_METHODS); 191 editor.setAction("AddDelegateMethods", fAddDelegateMethods); 193 fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(editor); 194 fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS); 195 editor.setAction("AddUnimplementedConstructors", fAddUnimplementedConstructors); 197 fGenerateConstructorUsingFields= new GenerateNewConstructorUsingFieldsAction(editor); 198 fGenerateConstructorUsingFields.setActionDefinitionId(IJavaEditorActionDefinitionIds.GENERATE_CONSTRUCTOR_USING_FIELDS); 199 editor.setAction("GenerateConstructorUsingFields", fGenerateConstructorUsingFields); 201 fHashCodeEquals= new GenerateHashCodeEqualsAction(editor); 202 fHashCodeEquals.setActionDefinitionId(IJavaEditorActionDefinitionIds.GENERATE_HASHCODE_EQUALS); 203 editor.setAction("GenerateHashCodeEquals", fHashCodeEquals); 205 fAddJavaDocStub= new AddJavaDocStubAction(editor); 206 fAddJavaDocStub.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_JAVADOC_COMMENT); 207 editor.setAction("AddJavadocComment", fAddJavaDocStub); 209 fCleanUp= new AllCleanUpsAction(editor); 210 fCleanUp.setActionDefinitionId(IJavaEditorActionDefinitionIds.CLEAN_UP); 211 editor.setAction("CleanUp", fCleanUp); 213 fExternalizeStrings= new ExternalizeStringsAction(editor); 214 fExternalizeStrings.setActionDefinitionId(IJavaEditorActionDefinitionIds.EXTERNALIZE_STRINGS); 215 editor.setAction("ExternalizeStrings", fExternalizeStrings); 217 installQuickAccessAction(); 218 } 219 220 227 public GenerateActionGroup(Page page) { 228 this(page.getSite()); 229 } 230 231 238 public GenerateActionGroup(IViewPart part) { 239 this(part.getSite()); 240 } 241 242 private GenerateActionGroup(IWorkbenchSite site) { 243 fSite= site; 244 ISelectionProvider provider= fSite.getSelectionProvider(); 245 ISelection selection= provider.getSelection(); 246 247 fOverrideMethods= new OverrideMethodsAction(site); 248 fOverrideMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.OVERRIDE_METHODS); 249 250 fAddGetterSetter= new AddGetterSetterAction(site); 251 fAddGetterSetter.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_GETTER_SETTER); 252 253 fAddDelegateMethods= new AddDelegateMethodsAction(site); 254 fAddDelegateMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_DELEGATE_METHODS); 255 256 fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(site); 257 fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS); 258 259 fGenerateConstructorUsingFields= new GenerateNewConstructorUsingFieldsAction(site); 260 fGenerateConstructorUsingFields.setActionDefinitionId(IJavaEditorActionDefinitionIds.GENERATE_CONSTRUCTOR_USING_FIELDS); 261 262 fHashCodeEquals= new GenerateHashCodeEqualsAction(site); 263 fHashCodeEquals.setActionDefinitionId(IJavaEditorActionDefinitionIds.GENERATE_HASHCODE_EQUALS); 264 265 fAddJavaDocStub= new AddJavaDocStubAction(site); 266 fAddJavaDocStub.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_JAVADOC_COMMENT); 267 268 fAddBookmark= new AddBookmarkAction(site.getShell()); 269 fAddBookmark.setActionDefinitionId(IWorkbenchActionDefinitionIds.ADD_BOOKMARK); 270 271 273 fAddTaskAction= new AddTaskAction(site); 274 fAddTaskAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.ADD_TASK); 275 276 fExternalizeStrings= new ExternalizeStringsAction(site); 277 fExternalizeStrings.setActionDefinitionId(IJavaEditorActionDefinitionIds.EXTERNALIZE_STRINGS); 278 279 fFindNLSProblems= new FindBrokenNLSKeysAction(site); 280 fFindNLSProblems.setActionDefinitionId(FindBrokenNLSKeysAction.FIND_BROKEN_NLS_KEYS_ACTION_ID); 281 282 fOrganizeImports= new OrganizeImportsAction(site); 283 fOrganizeImports.setActionDefinitionId(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS); 284 285 fSortMembers= new MultiSortMembersAction(site); 286 fSortMembers.setActionDefinitionId(IJavaEditorActionDefinitionIds.SORT_MEMBERS); 287 288 fFormatAll= new FormatAllAction(site); 289 fFormatAll.setActionDefinitionId(IJavaEditorActionDefinitionIds.FORMAT); 290 291 fCleanUp= new AllCleanUpsAction(site); 292 fCleanUp.setActionDefinitionId(IJavaEditorActionDefinitionIds.CLEAN_UP); 293 294 295 fOverrideMethods.update(selection); 296 fAddGetterSetter.update(selection); 297 fAddDelegateMethods.update(selection); 298 fAddUnimplementedConstructors.update(selection); 299 fGenerateConstructorUsingFields.update(selection); 300 fHashCodeEquals.update(selection); 301 fAddJavaDocStub.update(selection); 302 fExternalizeStrings.update(selection); 303 fFindNLSProblems.update(selection); 304 fCleanUp.update(selection); 305 fAddTaskAction.update(selection); 306 fOrganizeImports.update(selection); 307 fSortMembers.update(selection); 308 fFormatAll.update(selection); 309 if (selection instanceof IStructuredSelection) { 310 IStructuredSelection ss= (IStructuredSelection)selection; 311 fAddBookmark.selectionChanged(ss); 312 } else { 313 fAddBookmark.setEnabled(false); 314 } 315 316 registerSelectionListener(provider, fOverrideMethods); 317 registerSelectionListener(provider, fAddGetterSetter); 318 registerSelectionListener(provider, fAddDelegateMethods); 319 registerSelectionListener(provider, fAddUnimplementedConstructors); 320 registerSelectionListener(provider, fGenerateConstructorUsingFields); 321 registerSelectionListener(provider, fHashCodeEquals); 322 registerSelectionListener(provider, fAddJavaDocStub); 323 registerSelectionListener(provider, fAddBookmark); 324 registerSelectionListener(provider, fExternalizeStrings); 325 registerSelectionListener(provider, fFindNLSProblems); 326 registerSelectionListener(provider, fOrganizeImports); 327 registerSelectionListener(provider, fFormatAll); 328 registerSelectionListener(provider, fSortMembers); 329 registerSelectionListener(provider, fAddTaskAction); 330 registerSelectionListener(provider, fCleanUp); 331 332 installQuickAccessAction(); 333 } 334 335 private void installQuickAccessAction() { 336 fHandlerService= (IHandlerService)fSite.getService(IHandlerService.class); 337 if (fHandlerService != null) { 338 fQuickAccessAction= new SourceQuickAccessAction(fEditor); 339 fQuickAccessHandlerActivation= fHandlerService.activateHandler(fQuickAccessAction.getActionDefinitionId(), new ActionHandler(fQuickAccessAction)); 340 } 341 } 342 343 private void registerSelectionListener(ISelectionProvider provider, ISelectionChangedListener listener) { 344 if (fRegisteredSelectionListeners == null) 345 fRegisteredSelectionListeners= new ArrayList (20); 346 provider.addSelectionChangedListener(listener); 347 fRegisteredSelectionListeners.add(listener); 348 } 349 350 355 358 public void editorStateChanged() { 359 Assert.isTrue(isEditorOwner()); 360 } 361 362 365 public void fillActionBars(IActionBars actionBar) { 366 super.fillActionBars(actionBar); 367 setGlobalActionHandlers(actionBar); 368 } 369 370 373 public void fillContextMenu(IMenuManager menu) { 374 super.fillContextMenu(menu); 375 String menuText= ActionMessages.SourceMenu_label; 376 if (fQuickAccessAction != null) { 377 menuText= fQuickAccessAction.addShortcut(menuText); 378 } 379 IMenuManager subMenu= new MenuManager(menuText, MENU_ID); 380 int added= 0; 381 if (isEditorOwner()) { 382 added= fillEditorSubMenu(subMenu); 383 } else { 384 added= fillViewSubMenu(subMenu); 385 } 386 if (added > 0) 387 menu.appendToGroup(fGroupName, subMenu); 388 } 389 390 private void fillQuickMenu(IMenuManager menu) { 391 if (isEditorOwner()) { 392 fillEditorSubMenu(menu); 393 } else { 394 fillViewSubMenu(menu); 395 } 396 } 397 398 private int fillEditorSubMenu(IMenuManager source) { 399 int added= 0; 400 source.add(new Separator(GROUP_COMMENT)); 401 added+= addEditorAction(source, "ToggleComment"); added+= addEditorAction(source, "AddBlockComment"); added+= addEditorAction(source, "RemoveBlockComment"); added+= addAction(source, fAddJavaDocStub); 405 source.add(new Separator(GROUP_EDIT)); 406 added+= addEditorAction(source, "Indent"); added+= addEditorAction(source, "Format"); source.add(new Separator(GROUP_IMPORT)); 409 added+= addAction(source, fAddImport); 410 added+= addAction(source, fOrganizeImports); 411 added+= addAction(source, fSortMembers); 412 added+= addAction(source, fCleanUp); 413 source.add(new Separator(GROUP_GENERATE)); 414 added+= addAction(source, fOverrideMethods); 415 added+= addAction(source, fAddGetterSetter); 416 added+= addAction(source, fAddDelegateMethods); 417 added+= addAction(source, fHashCodeEquals); 418 added+= addAction(source, fGenerateConstructorUsingFields); 419 added+= addAction(source, fAddUnimplementedConstructors); 420 source.add(new Separator(GROUP_CODE)); 421 source.add(new Separator(GROUP_EXTERNALIZE)); 422 added+= addAction(source, fExternalizeStrings); 423 return added; 424 } 425 426 private int fillViewSubMenu(IMenuManager source) { 427 int added= 0; 428 source.add(new Separator(GROUP_COMMENT)); 429 added+= addAction(source, fAddJavaDocStub); 430 source.add(new Separator(GROUP_EDIT)); 431 added+= addAction(source, fFormatAll); 432 source.add(new Separator(GROUP_IMPORT)); 433 added+= addAction(source, fAddImport); 434 added+= addAction(source, fOrganizeImports); 435 added+= addAction(source, fSortMembers); 436 added+= addAction(source, fCleanUp); 437 source.add(new Separator(GROUP_GENERATE)); 438 added+= addAction(source, fOverrideMethods); 439 added+= addAction(source, fAddGetterSetter); 440 added+= addAction(source, fAddDelegateMethods); 441 added+= addAction(source, fHashCodeEquals); 442 added+= addAction(source, fGenerateConstructorUsingFields); 443 added+= addAction(source, fAddUnimplementedConstructors); 444 source.add(new Separator(GROUP_CODE)); 445 source.add(new Separator(GROUP_EXTERNALIZE)); 446 added+= addAction(source, fExternalizeStrings); 447 added+= addAction(source, fFindNLSProblems); 448 return added; 449 } 450 451 454 public void dispose() { 455 if (fRegisteredSelectionListeners != null) { 456 ISelectionProvider provider= fSite.getSelectionProvider(); 457 for (Iterator iter= fRegisteredSelectionListeners.iterator(); iter.hasNext();) { 458 ISelectionChangedListener listener= (ISelectionChangedListener) iter.next(); 459 provider.removeSelectionChangedListener(listener); 460 } 461 } 462 if (fQuickAccessHandlerActivation != null && fHandlerService != null) { 463 fHandlerService.deactivateHandler(fQuickAccessHandlerActivation); 464 } 465 fEditor= null; 466 fCleanUp.dispose(); 467 super.dispose(); 468 } 469 470 private void setGlobalActionHandlers(IActionBars actionBar) { 471 actionBar.setGlobalActionHandler(JdtActionConstants.ADD_IMPORT, fAddImport); 472 actionBar.setGlobalActionHandler(JdtActionConstants.OVERRIDE_METHODS, fOverrideMethods); 473 actionBar.setGlobalActionHandler(JdtActionConstants.GENERATE_GETTER_SETTER, fAddGetterSetter); 474 actionBar.setGlobalActionHandler(JdtActionConstants.GENERATE_DELEGATE_METHODS, fAddDelegateMethods); 475 actionBar.setGlobalActionHandler(JdtActionConstants.ADD_CONSTRUCTOR_FROM_SUPERCLASS, fAddUnimplementedConstructors); 476 actionBar.setGlobalActionHandler(JdtActionConstants.GENERATE_CONSTRUCTOR_USING_FIELDS, fGenerateConstructorUsingFields); 477 actionBar.setGlobalActionHandler(JdtActionConstants.GENERATE_HASHCODE_EQUALS, fHashCodeEquals); 478 actionBar.setGlobalActionHandler(JdtActionConstants.ADD_JAVA_DOC_COMMENT, fAddJavaDocStub); 479 actionBar.setGlobalActionHandler(JdtActionConstants.EXTERNALIZE_STRINGS, fExternalizeStrings); 480 actionBar.setGlobalActionHandler(JdtActionConstants.CLEAN_UP, fCleanUp); 481 actionBar.setGlobalActionHandler(FindBrokenNLSKeysAction.ACTION_HANDLER_ID, fFindNLSProblems); 482 actionBar.setGlobalActionHandler(JdtActionConstants.ORGANIZE_IMPORTS, fOrganizeImports); 483 actionBar.setGlobalActionHandler(JdtActionConstants.SORT_MEMBERS, fSortMembers); 484 if (!isEditorOwner()) { 485 actionBar.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(), fAddBookmark); 487 actionBar.setGlobalActionHandler(IDEActionFactory.ADD_TASK.getId(), fAddTaskAction); 488 actionBar.setGlobalActionHandler(JdtActionConstants.FORMAT, fFormatAll); 489 } 490 } 491 492 private int addAction(IMenuManager menu, IAction action) { 493 if (action != null && action.isEnabled()) { 494 menu.add(action); 495 return 1; 496 } 497 return 0; 498 } 499 500 private int addEditorAction(IMenuManager menu, String actionID) { 501 if (fEditor == null) 502 return 0; 503 IAction action= fEditor.getAction(actionID); 504 if (action == null) 505 return 0; 506 if (action instanceof IUpdate) 507 ((IUpdate)action).update(); 508 if (action.isEnabled()) { 509 menu.add(action); 510 return 1; 511 } 512 return 0; 513 } 514 515 private boolean isEditorOwner() { 516 return fEditor != null; 517 } 518 } 519 | Popular Tags |