1 17 package org.eclipse.emf.ecore.sdo.presentation; 18 19 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 import java.util.LinkedList ; 23 24 import org.eclipse.emf.common.ui.viewer.IViewerProvider; 25 import org.eclipse.emf.ecore.sdo.action.EvaluatePathAction; 26 import org.eclipse.emf.edit.domain.EditingDomain; 27 import org.eclipse.emf.edit.domain.IEditingDomainProvider; 28 import org.eclipse.emf.edit.ui.action.CreateChildAction; 29 import org.eclipse.emf.edit.ui.action.CreateSiblingAction; 30 import org.eclipse.emf.edit.ui.action.EditingDomainActionBarContributor; 31 import org.eclipse.emf.edit.ui.action.LoadResourceAction; 32 import org.eclipse.emf.edit.ui.action.ValidateAction; 33 import org.eclipse.jface.action.Action; 34 import org.eclipse.jface.action.ActionContributionItem; 35 import org.eclipse.jface.action.IAction; 36 import org.eclipse.jface.action.IContributionItem; 37 import org.eclipse.jface.action.IContributionManager; 38 import org.eclipse.jface.action.IMenuListener; 39 import org.eclipse.jface.action.IMenuManager; 40 import org.eclipse.jface.action.IToolBarManager; 41 import org.eclipse.jface.action.MenuManager; 42 import org.eclipse.jface.action.Separator; 43 import org.eclipse.jface.action.SubContributionItem; 44 import org.eclipse.jface.viewers.ISelection; 45 import org.eclipse.jface.viewers.ISelectionChangedListener; 46 import org.eclipse.jface.viewers.ISelectionProvider; 47 import org.eclipse.jface.viewers.IStructuredSelection; 48 import org.eclipse.jface.viewers.SelectionChangedEvent; 49 import org.eclipse.jface.viewers.Viewer; 50 import org.eclipse.ui.IEditorPart; 51 import org.eclipse.ui.PartInitException; 52 import org.eclipse.ui.actions.BaseSelectionListenerAction; 53 54 60 public class SDOActionBarContributor 61 extends EditingDomainActionBarContributor 62 implements ISelectionChangedListener 63 { 64 70 protected IEditorPart activeEditorPart; 71 72 78 protected ISelectionProvider selectionProvider; 79 80 86 protected IAction showPropertiesViewAction = 87 new Action(SDOEditorPlugin.INSTANCE.getString("_UI_ShowPropertiesView_menu_item")) 88 { 89 public void run() 90 { 91 try 92 { 93 getPage().showView("org.eclipse.ui.views.PropertySheet"); 94 } 95 catch (PartInitException exception) 96 { 97 SDOEditorPlugin.INSTANCE.log(exception); 98 } 99 } 100 }; 101 102 109 protected IAction refreshViewerAction = 110 new Action(SDOEditorPlugin.INSTANCE.getString("_UI_RefreshViewer_menu_item")) 111 { 112 public boolean isEnabled() 113 { 114 return activeEditorPart instanceof IViewerProvider; 115 } 116 117 public void run() 118 { 119 if (activeEditorPart instanceof IViewerProvider) 120 { 121 Viewer viewer = ((IViewerProvider)activeEditorPart).getViewer(); 122 if (viewer != null) 123 { 124 viewer.refresh(); 125 } 126 } 127 } 128 }; 129 130 137 protected Collection createChildActions; 138 139 145 protected IMenuManager createChildMenuManager; 146 147 154 protected Collection createSiblingActions; 155 156 162 protected IMenuManager createSiblingMenuManager; 163 164 170 public SDOActionBarContributor() 171 { 172 loadResourceAction = new LoadResourceAction(); 173 validateAction = new ValidateAction(); 174 } 175 176 182 public void contributeToToolBar(IToolBarManager toolBarManager) 183 { 184 toolBarManager.add(new Separator("sdo-settings")); 185 toolBarManager.add(new Separator("sdo-additions")); 186 } 187 188 195 public void contributeToMenu(IMenuManager menuManager) 196 { 197 super.contributeToMenu(menuManager); 198 199 IMenuManager submenuManager = new MenuManager(SDOEditorPlugin.INSTANCE.getString("_UI_SDOEditor_menu"), "org.eclipse.emf.ecore.sdoMenuID"); 200 menuManager.insertAfter("additions", submenuManager); 201 submenuManager.add(new Separator("settings")); 202 submenuManager.add(new Separator("actions")); 203 submenuManager.add(new Separator("additions")); 204 submenuManager.add(new Separator("additions-end")); 205 206 createChildMenuManager = new MenuManager(SDOEditorPlugin.INSTANCE.getString("_UI_CreateChild_menu_item")); 209 submenuManager.insertBefore("additions", createChildMenuManager); 210 211 createSiblingMenuManager = new MenuManager(SDOEditorPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item")); 214 submenuManager.insertBefore("additions", createSiblingMenuManager); 215 216 submenuManager.addMenuListener 219 (new IMenuListener() 220 { 221 public void menuAboutToShow(IMenuManager menuManager) 222 { 223 menuManager.updateAll(true); 224 } 225 }); 226 227 addGlobalActions(submenuManager); 228 } 229 230 236 public void setActiveEditor(IEditorPart part) 237 { 238 super.setActiveEditor(part); 239 activeEditorPart = part; 240 241 if (selectionProvider != null) 244 { 245 selectionProvider.removeSelectionChangedListener(this); 246 } 247 if (part == null) 248 { 249 selectionProvider = null; 250 } 251 else 252 { 253 selectionProvider = part.getSite().getSelectionProvider(); 254 selectionProvider.addSelectionChangedListener(this); 255 256 if (selectionProvider.getSelection() != null) 259 { 260 selectionChanged(new SelectionChangedEvent(selectionProvider, selectionProvider.getSelection())); 261 } 262 } 263 } 264 265 273 public void selectionChanged(SelectionChangedEvent event) 274 { 275 if (createChildMenuManager != null) 278 { 279 depopulateManager(createChildMenuManager, createChildActions); 280 } 281 if (createSiblingMenuManager != null) 282 { 283 depopulateManager(createSiblingMenuManager, createSiblingActions); 284 } 285 286 Collection newChildDescriptors = null; 289 Collection newSiblingDescriptors = null; 290 291 ISelection selection = event.getSelection(); 292 if (selection instanceof IStructuredSelection && ((IStructuredSelection)selection).size() == 1) 293 { 294 Object object = ((IStructuredSelection)selection).getFirstElement(); 295 296 EditingDomain domain = 297 ((IEditingDomainProvider) activeEditorPart).getEditingDomain(); 298 299 newChildDescriptors = domain.getNewChildDescriptors(object, null); 300 newSiblingDescriptors = domain.getNewChildDescriptors(null, object); 301 } 302 303 createChildActions = generateCreateChildActions(newChildDescriptors, selection); 306 createSiblingActions = generateCreateSiblingActions(newSiblingDescriptors, selection); 307 308 if (createChildMenuManager != null) 309 { 310 populateManager(createChildMenuManager, createChildActions, null); 311 createChildMenuManager.update(true); 312 } 313 if (createSiblingMenuManager != null) 314 { 315 populateManager(createSiblingMenuManager, createSiblingActions, null); 316 createSiblingMenuManager.update(true); 317 } 318 } 319 320 327 protected Collection generateCreateChildActions(Collection descriptors, ISelection selection) 328 { 329 Collection actions = new LinkedList (); 330 if (descriptors != null) 331 { 332 for (Iterator i = descriptors.iterator(); i.hasNext(); ) 333 { 334 actions.add(new CreateChildAction(activeEditorPart, selection, i.next())); 335 } 336 } 337 return actions; 338 } 339 340 347 protected Collection generateCreateSiblingActions(Collection descriptors, ISelection selection) 348 { 349 Collection actions = new LinkedList (); 350 if (descriptors != null) 351 { 352 for (Iterator i = descriptors.iterator(); i.hasNext(); ) 353 { 354 actions.add(new CreateSiblingAction(activeEditorPart, selection, i.next())); 355 } 356 } 357 return actions; 358 } 359 360 369 protected void populateManager(IContributionManager manager, Collection actions, String contributionID) 370 { 371 if (actions != null) 372 { 373 for (Iterator i = actions.iterator(); i.hasNext(); ) 374 { 375 IAction action = (IAction) i.next(); 376 if (contributionID != null) 377 { 378 manager.insertBefore(contributionID, action); 379 } 380 else 381 { 382 manager.add(action); 383 } 384 } 385 } 386 } 387 388 395 protected void depopulateManager(IContributionManager manager, Collection actions) 396 { 397 if (actions != null) 398 { 399 IContributionItem[] items = manager.getItems(); 400 for (int i = 0; i < items.length; i++) 401 { 402 IContributionItem contributionItem = items[i]; 405 while (contributionItem instanceof SubContributionItem) 406 { 407 contributionItem = ((SubContributionItem)contributionItem).getInnerItem(); 408 } 409 410 if (contributionItem instanceof ActionContributionItem) 413 { 414 IAction action = ((ActionContributionItem) contributionItem).getAction(); 415 if (actions.contains(action)) 416 { 417 manager.remove(contributionItem); 418 } 419 } 420 } 421 } 422 } 423 424 430 public void menuAboutToShow(IMenuManager menuManager) 431 { 432 refreshViewerAction.setEnabled(refreshViewerAction.isEnabled()); 433 434 super.menuAboutToShow(menuManager); 435 MenuManager submenuManager = null; 436 437 submenuManager = new MenuManager(SDOEditorPlugin.INSTANCE.getString("_UI_CreateChild_menu_item")); 438 populateManager(submenuManager, createChildActions, null); 439 menuManager.insertBefore("additions", submenuManager); 440 441 submenuManager = new MenuManager(SDOEditorPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item")); 442 populateManager(submenuManager, createSiblingActions, null); 443 menuManager.insertBefore("additions", submenuManager); 444 445 menuManager.insertBefore("additions", new Separator()); 446 menuManager.insertBefore 447 ("additions", 448 new BaseSelectionListenerAction(SDOEditorPlugin.INSTANCE.getString("_UI_EvaluatePath_menu_item")) 449 { 450 EvaluatePathAction evaluatePathAction = new EvaluatePathAction(); 451 452 { 453 evaluatePathAction.selectionChanged(this, (IStructuredSelection)((ISelectionProvider)activeEditor).getSelection()); 454 } 455 456 public void run() 457 { 458 evaluatePathAction.run(this); 459 } 460 }); 461 } 462 463 469 protected void addGlobalActions(IMenuManager menuManager) 470 { 471 menuManager.insertAfter("additions-end", new Separator("ui-actions")); 472 menuManager.insertAfter("ui-actions", showPropertiesViewAction); 473 474 refreshViewerAction.setEnabled(refreshViewerAction.isEnabled()); 475 menuManager.insertAfter("ui-actions", refreshViewerAction); 476 477 super.addGlobalActions(menuManager); 478 } 479 480 } 481 | Popular Tags |