1 7 package org.enhydra.dods.editor.Doml.presentation; 8 9 import java.util.ArrayList ; 10 import java.util.Collection ; 11 import java.util.Iterator ; 12 13 import org.eclipse.emf.common.ui.viewer.IViewerProvider; 14 15 import org.eclipse.emf.edit.domain.EditingDomain; 16 import org.eclipse.emf.edit.domain.IEditingDomainProvider; 17 18 import org.eclipse.emf.edit.ui.action.CreateChildAction; 19 import org.eclipse.emf.edit.ui.action.CreateSiblingAction; 20 import org.eclipse.emf.edit.ui.action.EditingDomainActionBarContributor; 21 import org.eclipse.emf.edit.ui.action.LoadResourceAction; 22 import org.eclipse.emf.edit.ui.action.ValidateAction; 23 24 import org.eclipse.jface.action.Action; 25 import org.eclipse.jface.action.ActionContributionItem; 26 import org.eclipse.jface.action.IAction; 27 import org.eclipse.jface.action.IContributionItem; 28 import org.eclipse.jface.action.IContributionManager; 29 import org.eclipse.jface.action.IMenuListener; 30 import org.eclipse.jface.action.IMenuManager; 31 import org.eclipse.jface.action.IToolBarManager; 32 import org.eclipse.jface.action.MenuManager; 33 import org.eclipse.jface.action.Separator; 34 import org.eclipse.jface.action.SubContributionItem; 35 36 import org.eclipse.jface.viewers.ISelection; 37 import org.eclipse.jface.viewers.ISelectionChangedListener; 38 import org.eclipse.jface.viewers.ISelectionProvider; 39 import org.eclipse.jface.viewers.IStructuredSelection; 40 import org.eclipse.jface.viewers.SelectionChangedEvent; 41 import org.eclipse.jface.viewers.Viewer; 42 43 import org.eclipse.ui.IEditorPart; 44 import org.eclipse.ui.PartInitException; 45 46 import org.enhydra.dods.editor.Doml.provider.DomlEditPlugin; 47 48 54 public class DomlActionBarContributor 55 extends EditingDomainActionBarContributor 56 implements ISelectionChangedListener { 57 63 protected IEditorPart activeEditorPart; 64 65 71 protected ISelectionProvider selectionProvider; 72 73 79 protected IAction showPropertiesViewAction = 80 new Action(DomlEditPlugin.INSTANCE.getString("_UI_ShowPropertiesView_menu_item")) { 81 public void run() { 82 try { 83 getPage().showView("org.eclipse.ui.views.PropertySheet"); 84 } 85 catch (PartInitException exception) { 86 DomlEditPlugin.INSTANCE.log(exception); 87 } 88 } 89 }; 90 91 98 protected IAction refreshViewerAction = 99 new Action(DomlEditPlugin.INSTANCE.getString("_UI_RefreshViewer_menu_item")) { 100 public boolean isEnabled() { 101 return activeEditorPart instanceof IViewerProvider; 102 } 103 104 public void run() { 105 if (activeEditorPart instanceof IViewerProvider) { 106 Viewer viewer = ((IViewerProvider)activeEditorPart).getViewer(); 107 if (viewer != null) { 108 viewer.refresh(); 109 } 110 } 111 } 112 }; 113 114 121 protected Collection createChildActions; 122 123 129 protected IMenuManager createChildMenuManager; 130 131 138 protected Collection createSiblingActions; 139 140 146 protected IMenuManager createSiblingMenuManager; 147 148 154 public DomlActionBarContributor() { 155 loadResourceAction = new LoadResourceAction(); 156 validateAction = new ValidateAction(); 157 } 158 159 165 public void contributeToToolBar(IToolBarManager toolBarManager) { 166 toolBarManager.add(new Separator("doml-settings")); 167 toolBarManager.add(new Separator("doml-additions")); 168 } 169 170 177 public void contributeToMenu(IMenuManager menuManager) { 178 super.contributeToMenu(menuManager); 179 180 IMenuManager submenuManager = new MenuManager(DomlEditPlugin.INSTANCE.getString("_UI_DomlEditor_menu"), "org.enhydra.dods.editor.DomlMenuID"); 181 menuManager.insertAfter("additions", submenuManager); 182 submenuManager.add(new Separator("settings")); 183 submenuManager.add(new Separator("actions")); 184 submenuManager.add(new Separator("additions")); 185 submenuManager.add(new Separator("additions-end")); 186 187 createChildMenuManager = new MenuManager(DomlEditPlugin.INSTANCE.getString("_UI_CreateChild_menu_item")); 190 submenuManager.insertBefore("additions", createChildMenuManager); 191 192 createSiblingMenuManager = new MenuManager(DomlEditPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item")); 195 submenuManager.insertBefore("additions", createSiblingMenuManager); 196 197 submenuManager.addMenuListener 200 (new IMenuListener() { 201 public void menuAboutToShow(IMenuManager menuManager) { 202 menuManager.updateAll(true); 203 } 204 }); 205 206 addGlobalActions(submenuManager); 207 } 208 209 215 public void setActiveEditor(IEditorPart part) { 216 super.setActiveEditor(part); 217 activeEditorPart = part; 218 219 if (selectionProvider != null) { 222 selectionProvider.removeSelectionChangedListener(this); 223 } 224 if (part == null) { 225 selectionProvider = null; 226 } 227 else { 228 selectionProvider = part.getSite().getSelectionProvider(); 229 selectionProvider.addSelectionChangedListener(this); 230 231 if (selectionProvider.getSelection() != null) { 234 selectionChanged(new SelectionChangedEvent(selectionProvider, selectionProvider.getSelection())); 235 } 236 } 237 } 238 239 247 public void selectionChanged(SelectionChangedEvent event) { 248 if (createChildMenuManager != null) { 251 depopulateManager(createChildMenuManager, createChildActions); 252 } 253 if (createSiblingMenuManager != null) { 254 depopulateManager(createSiblingMenuManager, createSiblingActions); 255 } 256 257 Collection newChildDescriptors = null; 260 Collection newSiblingDescriptors = null; 261 262 ISelection selection = event.getSelection(); 263 if (selection instanceof IStructuredSelection && ((IStructuredSelection)selection).size() == 1) { 264 Object object = ((IStructuredSelection)selection).getFirstElement(); 265 266 EditingDomain domain = ((IEditingDomainProvider)activeEditorPart).getEditingDomain(); 267 268 newChildDescriptors = domain.getNewChildDescriptors(object, null); 269 newSiblingDescriptors = domain.getNewChildDescriptors(null, object); 270 } 271 272 createChildActions = generateCreateChildActions(newChildDescriptors, selection); 275 createSiblingActions = generateCreateSiblingActions(newSiblingDescriptors, selection); 276 277 if (createChildMenuManager != null) { 278 populateManager(createChildMenuManager, createChildActions, null); 279 createChildMenuManager.update(true); 280 } 281 if (createSiblingMenuManager != null) { 282 populateManager(createSiblingMenuManager, createSiblingActions, null); 283 createSiblingMenuManager.update(true); 284 } 285 } 286 287 294 protected Collection generateCreateChildActions(Collection descriptors, ISelection selection) { 295 Collection actions = new ArrayList (); 296 if (descriptors != null) { 297 for (Iterator i = descriptors.iterator(); i.hasNext(); ) { 298 actions.add(new CreateChildAction(activeEditorPart, selection, i.next())); 299 } 300 } 301 return actions; 302 } 303 304 311 protected Collection generateCreateSiblingActions(Collection descriptors, ISelection selection) { 312 Collection actions = new ArrayList (); 313 if (descriptors != null) { 314 for (Iterator i = descriptors.iterator(); i.hasNext(); ) { 315 actions.add(new CreateSiblingAction(activeEditorPart, selection, i.next())); 316 } 317 } 318 return actions; 319 } 320 321 330 protected void populateManager(IContributionManager manager, Collection actions, String contributionID) { 331 if (actions != null) { 332 for (Iterator i = actions.iterator(); i.hasNext(); ) { 333 IAction action = (IAction)i.next(); 334 if (contributionID != null) { 335 manager.insertBefore(contributionID, action); 336 } 337 else { 338 manager.add(action); 339 } 340 } 341 } 342 } 343 344 351 protected void depopulateManager(IContributionManager manager, Collection actions) { 352 if (actions != null) { 353 IContributionItem[] items = manager.getItems(); 354 for (int i = 0; i < items.length; i++) { 355 IContributionItem contributionItem = items[i]; 358 while (contributionItem instanceof SubContributionItem) { 359 contributionItem = ((SubContributionItem)contributionItem).getInnerItem(); 360 } 361 362 if (contributionItem instanceof ActionContributionItem) { 365 IAction action = ((ActionContributionItem)contributionItem).getAction(); 366 if (actions.contains(action)) { 367 manager.remove(contributionItem); 368 } 369 } 370 } 371 } 372 } 373 374 380 public void menuAboutToShow(IMenuManager menuManager) { 381 super.menuAboutToShow(menuManager); 382 MenuManager submenuManager = null; 383 384 submenuManager = new MenuManager(DomlEditPlugin.INSTANCE.getString("_UI_CreateChild_menu_item")); 385 populateManager(submenuManager, createChildActions, null); 386 menuManager.insertBefore("additions", submenuManager); 387 388 submenuManager = new MenuManager(DomlEditPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item")); 389 populateManager(submenuManager, createSiblingActions, null); 390 menuManager.insertBefore("additions", submenuManager); 391 } 392 393 399 protected void addGlobalActions(IMenuManager menuManager) { 400 menuManager.insertAfter("additions-end", new Separator("ui-actions")); 401 menuManager.insertAfter("ui-actions", showPropertiesViewAction); 402 403 refreshViewerAction.setEnabled(refreshViewerAction.isEnabled()); 404 menuManager.insertAfter("ui-actions", refreshViewerAction); 405 406 super.addGlobalActions(menuManager); 407 } 408 409 } 410 | Popular Tags |