1 17 package org.eclipse.emf.mapping.ecore2xml.presentation; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 23 import org.eclipse.emf.common.ui.viewer.IViewerProvider; 24 25 import org.eclipse.emf.edit.domain.EditingDomain; 26 import org.eclipse.emf.edit.domain.IEditingDomainProvider; 27 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 34 import org.eclipse.emf.mapping.ecore2xml.ui.Ecore2XMLUIPlugin; 35 36 import org.eclipse.jface.action.Action; 37 import org.eclipse.jface.action.ActionContributionItem; 38 import org.eclipse.jface.action.IAction; 39 import org.eclipse.jface.action.IContributionItem; 40 import org.eclipse.jface.action.IContributionManager; 41 import org.eclipse.jface.action.IMenuListener; 42 import org.eclipse.jface.action.IMenuManager; 43 import org.eclipse.jface.action.IToolBarManager; 44 import org.eclipse.jface.action.MenuManager; 45 import org.eclipse.jface.action.Separator; 46 import org.eclipse.jface.action.SubContributionItem; 47 48 import org.eclipse.jface.viewers.ISelection; 49 import org.eclipse.jface.viewers.ISelectionChangedListener; 50 import org.eclipse.jface.viewers.ISelectionProvider; 51 import org.eclipse.jface.viewers.IStructuredSelection; 52 import org.eclipse.jface.viewers.SelectionChangedEvent; 53 import org.eclipse.jface.viewers.Viewer; 54 55 import org.eclipse.ui.IEditorPart; 56 import org.eclipse.ui.PartInitException; 57 58 64 public class Ecore2XMLActionBarContributor 65 extends EditingDomainActionBarContributor 66 implements ISelectionChangedListener 67 { 68 74 protected IEditorPart activeEditorPart; 75 76 82 protected ISelectionProvider selectionProvider; 83 84 90 protected IAction showPropertiesViewAction = 91 new Action(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_ShowPropertiesView_menu_item")) { 93 public void run() 94 { 95 try 96 { 97 getPage().showView("org.eclipse.ui.views.PropertySheet"); } 99 catch (PartInitException exception) 100 { 101 Ecore2XMLUIPlugin.INSTANCE.log(exception); 102 } 103 } 104 }; 105 106 113 protected IAction refreshViewerAction = 114 new Action(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_RefreshViewer_menu_item")) { 116 public boolean isEnabled() 117 { 118 return activeEditorPart instanceof IViewerProvider; 119 } 120 121 public void run() 122 { 123 if (activeEditorPart instanceof IViewerProvider) 124 { 125 Viewer viewer = ((IViewerProvider)activeEditorPart).getViewer(); 126 if (viewer != null) 127 { 128 viewer.refresh(); 129 } 130 } 131 } 132 }; 133 134 141 protected Collection createChildActions; 142 143 149 protected IMenuManager createChildMenuManager; 150 151 158 protected Collection createSiblingActions; 159 160 166 protected IMenuManager createSiblingMenuManager; 167 168 174 public Ecore2XMLActionBarContributor() 175 { 176 loadResourceAction = new LoadResourceAction(); 177 validateAction = new ValidateAction(); 178 } 179 180 186 public void contributeToToolBar(IToolBarManager toolBarManager) 187 { 188 toolBarManager.add(new Separator("ecore2xml-settings")); toolBarManager.add(new Separator("ecore2xml-additions")); } 191 192 199 public void contributeToMenu(IMenuManager menuManager) 200 { 201 super.contributeToMenu(menuManager); 202 203 IMenuManager submenuManager = new MenuManager(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLEditor_menu"), "org.eclipse.emf.mapping.ecore2xmlMenuID"); menuManager.insertAfter("additions", submenuManager); submenuManager.add(new Separator("settings")); submenuManager.add(new Separator("actions")); submenuManager.add(new Separator("additions")); submenuManager.add(new Separator("additions-end")); 210 createChildMenuManager = new MenuManager(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_CreateChild_menu_item")); submenuManager.insertBefore("additions", createChildMenuManager); 215 createSiblingMenuManager = new MenuManager(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item")); submenuManager.insertBefore("additions", createSiblingMenuManager); 220 submenuManager.addMenuListener 223 (new IMenuListener() 224 { 225 public void menuAboutToShow(IMenuManager menuManager) 226 { 227 menuManager.updateAll(true); 228 } 229 }); 230 231 addGlobalActions(submenuManager); 232 } 233 234 240 public void setActiveEditor(IEditorPart part) 241 { 242 super.setActiveEditor(part); 243 activeEditorPart = part; 244 245 if (selectionProvider != null) 248 { 249 selectionProvider.removeSelectionChangedListener(this); 250 } 251 if (part == null) 252 { 253 selectionProvider = null; 254 } 255 else 256 { 257 selectionProvider = part.getSite().getSelectionProvider(); 258 selectionProvider.addSelectionChangedListener(this); 259 260 if (selectionProvider.getSelection() != null) 263 { 264 selectionChanged(new SelectionChangedEvent(selectionProvider, selectionProvider.getSelection())); 265 } 266 } 267 } 268 269 277 public void selectionChanged(SelectionChangedEvent event) 278 { 279 if (createChildMenuManager != null) 282 { 283 depopulateManager(createChildMenuManager, createChildActions); 284 } 285 if (createSiblingMenuManager != null) 286 { 287 depopulateManager(createSiblingMenuManager, createSiblingActions); 288 } 289 290 Collection newChildDescriptors = null; 293 Collection newSiblingDescriptors = null; 294 295 ISelection selection = event.getSelection(); 296 if (selection instanceof IStructuredSelection && ((IStructuredSelection)selection).size() == 1) 297 { 298 Object object = ((IStructuredSelection)selection).getFirstElement(); 299 300 EditingDomain domain = ((IEditingDomainProvider)activeEditorPart).getEditingDomain(); 301 302 newChildDescriptors = domain.getNewChildDescriptors(object, null); 303 newSiblingDescriptors = domain.getNewChildDescriptors(null, object); 304 } 305 306 createChildActions = generateCreateChildActions(newChildDescriptors, selection); 309 createSiblingActions = generateCreateSiblingActions(newSiblingDescriptors, selection); 310 311 if (createChildMenuManager != null) 312 { 313 populateManager(createChildMenuManager, createChildActions, null); 314 createChildMenuManager.update(true); 315 } 316 if (createSiblingMenuManager != null) 317 { 318 populateManager(createSiblingMenuManager, createSiblingActions, null); 319 createSiblingMenuManager.update(true); 320 } 321 } 322 323 330 protected Collection generateCreateChildActions(Collection descriptors, ISelection selection) 331 { 332 Collection actions = new ArrayList (); 333 if (descriptors != null) 334 { 335 for (Iterator i = descriptors.iterator(); i.hasNext(); ) 336 { 337 actions.add(new CreateChildAction(activeEditorPart, selection, i.next())); 338 } 339 } 340 return actions; 341 } 342 343 350 protected Collection generateCreateSiblingActions(Collection descriptors, ISelection selection) 351 { 352 Collection actions = new ArrayList (); 353 if (descriptors != null) 354 { 355 for (Iterator i = descriptors.iterator(); i.hasNext(); ) 356 { 357 actions.add(new CreateSiblingAction(activeEditorPart, selection, i.next())); 358 } 359 } 360 return actions; 361 } 362 363 372 protected void populateManager(IContributionManager manager, Collection actions, String contributionID) 373 { 374 if (actions != null) 375 { 376 for (Iterator i = actions.iterator(); i.hasNext(); ) 377 { 378 IAction action = (IAction)i.next(); 379 if (contributionID != null) 380 { 381 manager.insertBefore(contributionID, action); 382 } 383 else 384 { 385 manager.add(action); 386 } 387 } 388 } 389 } 390 391 398 protected void depopulateManager(IContributionManager manager, Collection actions) 399 { 400 if (actions != null) 401 { 402 IContributionItem[] items = manager.getItems(); 403 for (int i = 0; i < items.length; i++) 404 { 405 IContributionItem contributionItem = items[i]; 408 while (contributionItem instanceof SubContributionItem) 409 { 410 contributionItem = ((SubContributionItem)contributionItem).getInnerItem(); 411 } 412 413 if (contributionItem instanceof ActionContributionItem) 416 { 417 IAction action = ((ActionContributionItem)contributionItem).getAction(); 418 if (actions.contains(action)) 419 { 420 manager.remove(contributionItem); 421 } 422 } 423 } 424 } 425 } 426 427 433 public void menuAboutToShow(IMenuManager menuManager) 434 { 435 super.menuAboutToShow(menuManager); 436 MenuManager submenuManager = null; 437 438 submenuManager = new MenuManager(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_CreateChild_menu_item")); populateManager(submenuManager, createChildActions, null); 440 menuManager.insertBefore("additions", submenuManager); 442 submenuManager = new MenuManager(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item")); populateManager(submenuManager, createSiblingActions, null); 444 menuManager.insertBefore("additions", submenuManager); } 446 447 453 protected void addGlobalActions(IMenuManager menuManager) 454 { 455 menuManager.insertAfter("additions-end", new Separator("ui-actions")); menuManager.insertAfter("ui-actions", showPropertiesViewAction); 458 refreshViewerAction.setEnabled(refreshViewerAction.isEnabled()); 459 menuManager.insertAfter("ui-actions", refreshViewerAction); 461 super.addGlobalActions(menuManager); 462 } 463 464 } 465 | Popular Tags |