1 17 package org.eclipse.emf.ecore.presentation; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 24 import org.eclipse.jface.action.ActionContributionItem; 25 import org.eclipse.jface.action.IAction; 26 import org.eclipse.jface.action.IContributionItem; 27 import org.eclipse.jface.action.IContributionManager; 28 import org.eclipse.jface.action.IMenuListener; 29 import org.eclipse.jface.action.IMenuManager; 30 import org.eclipse.jface.action.IToolBarManager; 31 import org.eclipse.jface.action.MenuManager; 32 import org.eclipse.jface.action.Separator; 33 import org.eclipse.jface.action.SubContributionItem; 34 import org.eclipse.jface.viewers.ISelection; 35 import org.eclipse.jface.viewers.ISelectionChangedListener; 36 import org.eclipse.jface.viewers.ISelectionProvider; 37 import org.eclipse.jface.viewers.IStructuredSelection; 38 import org.eclipse.jface.viewers.SelectionChangedEvent; 39 import org.eclipse.jface.viewers.Viewer; 40 41 import org.eclipse.ui.IEditorPart; 42 43 import org.eclipse.ui.PartInitException; 44 45 import org.eclipse.emf.edit.domain.EditingDomain; 46 import org.eclipse.emf.edit.domain.IEditingDomainProvider; 47 import org.eclipse.emf.common.ui.viewer.IViewerProvider; 48 49 import org.eclipse.jface.action.Action; 50 import org.eclipse.emf.edit.ui.action.CreateChildAction; 51 import org.eclipse.emf.edit.ui.action.CreateSiblingAction; 52 import org.eclipse.emf.edit.ui.action.EditingDomainActionBarContributor; 53 import org.eclipse.emf.edit.ui.action.LoadResourceAction; 54 import org.eclipse.emf.edit.ui.action.ValidateAction; 55 56 57 63 public class EcoreActionBarContributor 64 extends EditingDomainActionBarContributor 65 implements ISelectionChangedListener 66 { 67 public static class Reflective extends EcoreActionBarContributor 68 { 69 public Reflective() 70 { 71 } 72 73 protected IMenuManager createSubmenuManager() 74 { 75 return new MenuManager(EcoreEditorPlugin.getPlugin().getString("_UI_ReflectiveEditor_menu"), "org.eclipse.emf.ecoreMenuID"); 76 } 77 } 78 79 85 protected IEditorPart activeEditorPart; 86 87 93 protected ISelectionProvider selectionProvider; 94 95 101 protected IAction showPropertiesViewAction = 102 new Action(EcoreEditorPlugin.INSTANCE.getString("_UI_ShowPropertiesView_menu_item")) 103 { 104 public void run() 105 { 106 try 107 { 108 getPage().showView("org.eclipse.ui.views.PropertySheet"); 109 } 110 catch (PartInitException exception) 111 { 112 EcoreEditorPlugin.INSTANCE.log(exception); 113 } 114 } 115 }; 116 117 124 protected IAction refreshViewerAction = 125 new Action(EcoreEditorPlugin.INSTANCE.getString("_UI_RefreshViewer_menu_item")) 126 { 127 public boolean isEnabled() 128 { 129 return activeEditorPart instanceof IViewerProvider; 130 } 131 132 public void run() 133 { 134 if (activeEditorPart instanceof IViewerProvider) 135 { 136 Viewer viewer = ((IViewerProvider)activeEditorPart).getViewer(); 137 if (viewer != null) 138 { 139 viewer.refresh(); 140 } 141 } 142 } 143 }; 144 145 152 protected Collection createChildActions; 153 154 160 protected IMenuManager createChildMenuManager; 161 162 169 protected Collection createSiblingActions; 170 171 177 protected IMenuManager createSiblingMenuManager; 178 179 185 public EcoreActionBarContributor() 186 { 187 loadResourceAction = new LoadResourceAction(); 188 validateAction = new ValidateAction(); 189 } 190 191 197 public void contributeToToolBar(IToolBarManager toolBarManager) 198 { 199 toolBarManager.add(new Separator("ecore-settings")); 200 toolBarManager.add(new Separator("ecore-additions")); 201 } 202 203 210 public void contributeToMenu(IMenuManager menuManager) 211 { 212 super.contributeToMenu(menuManager); 213 214 IMenuManager submenuManager = createSubmenuManager(); 215 216 menuManager.insertAfter("additions", submenuManager); 217 submenuManager.add(new Separator("settings")); 218 submenuManager.add(new Separator("actions")); 219 submenuManager.add(new Separator("additions")); 220 submenuManager.add(new Separator("additions-end")); 221 222 createChildMenuManager = new MenuManager(EcoreEditorPlugin.getPlugin().getString("_UI_CreateChild_menu_item")); 225 submenuManager.insertBefore("additions", createChildMenuManager); 226 227 createSiblingMenuManager = new MenuManager(EcoreEditorPlugin.getPlugin().getString("_UI_CreateSibling_menu_item")); 230 submenuManager.insertBefore("additions", createSiblingMenuManager); 231 232 submenuManager.addMenuListener 235 (new IMenuListener() 236 { 237 public void menuAboutToShow(IMenuManager menuManager) 238 { 239 menuManager.updateAll(true); 240 } 241 }); 242 243 addGlobalActions(submenuManager); 244 } 245 246 protected IMenuManager createSubmenuManager() 247 { 248 return new MenuManager(EcoreEditorPlugin.getPlugin().getString("_UI_EcoreEditor_menu"), "org.eclipse.emf.ecoreMenuID"); 249 } 250 251 257 public void setActiveEditor(IEditorPart part) 258 { 259 super.setActiveEditor(part); 260 activeEditorPart = part; 261 262 if (selectionProvider != null) 265 { 266 selectionProvider.removeSelectionChangedListener(this); 267 } 268 if (part == null) 269 { 270 selectionProvider = null; 271 } 272 else 273 { 274 selectionProvider = part.getSite().getSelectionProvider(); 275 selectionProvider.addSelectionChangedListener(this); 276 277 if (selectionProvider.getSelection() != null) 280 { 281 selectionChanged(new SelectionChangedEvent(selectionProvider, selectionProvider.getSelection())); 282 } 283 } 284 } 285 286 294 public void selectionChanged(SelectionChangedEvent event) 295 { 296 if (createChildMenuManager != null) 299 { 300 depopulateManager(createChildMenuManager, createChildActions); 301 } 302 if (createSiblingMenuManager != null) 303 { 304 depopulateManager(createSiblingMenuManager, createSiblingActions); 305 } 306 307 Collection newChildDescriptors = null; 310 Collection newSiblingDescriptors = null; 311 312 ISelection selection = event.getSelection(); 313 if (selection instanceof IStructuredSelection && ((IStructuredSelection)selection).size() == 1) 314 { 315 Object object = ((IStructuredSelection)selection).getFirstElement(); 316 317 EditingDomain domain = ((IEditingDomainProvider)activeEditorPart).getEditingDomain(); 318 319 newChildDescriptors = domain.getNewChildDescriptors(object, null); 320 newSiblingDescriptors = domain.getNewChildDescriptors(null, object); 321 } 322 323 createChildActions = generateCreateChildActions(newChildDescriptors, selection); 326 createSiblingActions = generateCreateSiblingActions(newSiblingDescriptors, selection); 327 328 if (createChildMenuManager != null) 329 { 330 populateManager(createChildMenuManager, createChildActions, null); 331 createChildMenuManager.update(true); 332 } 333 if (createSiblingMenuManager != null) 334 { 335 populateManager(createSiblingMenuManager, createSiblingActions, null); 336 createSiblingMenuManager.update(true); 337 } 338 } 339 340 347 protected Collection generateCreateChildActions(Collection descriptors, ISelection selection) 348 { 349 Collection actions = new ArrayList (); 350 if (descriptors != null) 351 { 352 for (Iterator i = descriptors.iterator(); i.hasNext(); ) 353 { 354 actions.add(new CreateChildAction(activeEditorPart, selection, i.next())); 355 } 356 } 357 return actions; 358 } 359 360 367 protected Collection generateCreateSiblingActions(Collection descriptors, ISelection selection) 368 { 369 Collection actions = new ArrayList (); 370 if (descriptors != null) 371 { 372 for (Iterator i = descriptors.iterator(); i.hasNext(); ) 373 { 374 actions.add(new CreateSiblingAction(activeEditorPart, selection, i.next())); 375 } 376 } 377 return actions; 378 } 379 380 389 protected void populateManager(IContributionManager manager, Collection actions, String contributionID) 390 { 391 if (actions != null) 392 { 393 for (Iterator i = actions.iterator(); i.hasNext(); ) 394 { 395 IAction action = (IAction)i.next(); 396 if (contributionID != null) 397 { 398 manager.insertBefore(contributionID, action); 399 } 400 else 401 { 402 manager.add(action); 403 } 404 } 405 } 406 } 407 408 415 protected void depopulateManager(IContributionManager manager, Collection actions) 416 { 417 if (actions != null) 418 { 419 IContributionItem[] items = manager.getItems(); 420 for (int i = 0; i < items.length; i++) 421 { 422 IContributionItem contributionItem = items[i]; 425 while (contributionItem instanceof SubContributionItem) 426 { 427 contributionItem = ((SubContributionItem)contributionItem).getInnerItem(); 428 } 429 430 if (contributionItem instanceof ActionContributionItem) 433 { 434 IAction action = ((ActionContributionItem)contributionItem).getAction(); 435 if (actions.contains(action)) 436 { 437 manager.remove(contributionItem); 438 } 439 } 440 } 441 } 442 } 443 444 450 public void menuAboutToShow(IMenuManager menuManager) 451 { 452 super.menuAboutToShow(menuManager); 453 MenuManager submenuManager = null; 454 455 submenuManager = new MenuManager(EcoreEditorPlugin.INSTANCE.getString("_UI_CreateChild_menu_item")); 456 populateManager(submenuManager, createChildActions, null); 457 menuManager.insertBefore("additions", submenuManager); 458 459 submenuManager = new MenuManager(EcoreEditorPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item")); 460 populateManager(submenuManager, createSiblingActions, null); 461 menuManager.insertBefore("additions", submenuManager); 462 } 463 464 470 protected void addGlobalActions(IMenuManager menuManager) 471 { 472 menuManager.insertAfter("additions-end", new Separator("ui-actions")); 473 menuManager.insertAfter("ui-actions", showPropertiesViewAction); 474 475 refreshViewerAction.setEnabled(refreshViewerAction.isEnabled()); 476 menuManager.insertAfter("ui-actions", refreshViewerAction); 477 478 super.addGlobalActions(menuManager); 479 } 480 481 } 482 | Popular Tags |