1 7 package Olj.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 52 public class OljActionBarContributor 53 extends EditingDomainActionBarContributor 54 implements ISelectionChangedListener { 55 61 protected IEditorPart activeEditorPart; 62 63 69 protected ISelectionProvider selectionProvider; 70 71 77 protected IAction showPropertiesViewAction = 78 new Action(OljEditorPlugin.INSTANCE.getString("_UI_ShowPropertiesView_menu_item")) { 79 public void run() { 80 try { 81 getPage().showView("org.eclipse.ui.views.PropertySheet"); 82 } 83 catch (PartInitException exception) { 84 OljEditorPlugin.INSTANCE.log(exception); 85 } 86 } 87 }; 88 89 96 protected IAction refreshViewerAction = 97 new Action(OljEditorPlugin.INSTANCE.getString("_UI_RefreshViewer_menu_item")) { 98 public boolean isEnabled() { 99 return activeEditorPart instanceof IViewerProvider; 100 } 101 102 public void run() { 103 if (activeEditorPart instanceof IViewerProvider) { 104 Viewer viewer = ((IViewerProvider)activeEditorPart).getViewer(); 105 if (viewer != null) { 106 viewer.refresh(); 107 } 108 } 109 } 110 }; 111 112 119 protected Collection createChildActions; 120 121 127 protected IMenuManager createChildMenuManager; 128 129 136 protected Collection createSiblingActions; 137 138 144 protected IMenuManager createSiblingMenuManager; 145 146 152 public OljActionBarContributor() { 153 loadResourceAction = new LoadResourceAction(); 154 validateAction = new ValidateAction(); 155 } 156 157 163 public void contributeToToolBar(IToolBarManager toolBarManager) { 164 toolBarManager.add(new Separator("olj-settings")); 165 toolBarManager.add(new Separator("olj-additions")); 166 } 167 168 175 public void contributeToMenu(IMenuManager menuManager) { 176 super.contributeToMenu(menuManager); 177 178 IMenuManager submenuManager = new MenuManager(OljEditorPlugin.INSTANCE.getString("_UI_OljEditor_menu"), "OljMenuID"); 179 menuManager.insertAfter("additions", submenuManager); 180 submenuManager.add(new Separator("settings")); 181 submenuManager.add(new Separator("actions")); 182 submenuManager.add(new Separator("additions")); 183 submenuManager.add(new Separator("additions-end")); 184 185 createChildMenuManager = new MenuManager(OljEditorPlugin.INSTANCE.getString("_UI_CreateChild_menu_item")); 188 submenuManager.insertBefore("additions", createChildMenuManager); 189 190 createSiblingMenuManager = new MenuManager(OljEditorPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item")); 193 submenuManager.insertBefore("additions", createSiblingMenuManager); 194 195 submenuManager.addMenuListener 198 (new IMenuListener() { 199 public void menuAboutToShow(IMenuManager menuManager) { 200 menuManager.updateAll(true); 201 } 202 }); 203 204 addGlobalActions(submenuManager); 205 } 206 207 213 public void setActiveEditor(IEditorPart part) { 214 super.setActiveEditor(part); 215 activeEditorPart = part; 216 217 if (selectionProvider != null) { 220 selectionProvider.removeSelectionChangedListener(this); 221 } 222 if (part == null) { 223 selectionProvider = null; 224 } 225 else { 226 selectionProvider = part.getSite().getSelectionProvider(); 227 selectionProvider.addSelectionChangedListener(this); 228 229 if (selectionProvider.getSelection() != null) { 232 selectionChanged(new SelectionChangedEvent(selectionProvider, selectionProvider.getSelection())); 233 } 234 } 235 } 236 237 245 public void selectionChanged(SelectionChangedEvent event) { 246 if (createChildMenuManager != null) { 249 depopulateManager(createChildMenuManager, createChildActions); 250 } 251 if (createSiblingMenuManager != null) { 252 depopulateManager(createSiblingMenuManager, createSiblingActions); 253 } 254 255 Collection newChildDescriptors = null; 258 Collection newSiblingDescriptors = null; 259 260 ISelection selection = event.getSelection(); 261 if (selection instanceof IStructuredSelection && ((IStructuredSelection)selection).size() == 1) { 262 Object object = ((IStructuredSelection)selection).getFirstElement(); 263 264 EditingDomain domain = ((IEditingDomainProvider)activeEditorPart).getEditingDomain(); 265 266 newChildDescriptors = domain.getNewChildDescriptors(object, null); 267 newSiblingDescriptors = domain.getNewChildDescriptors(null, object); 268 } 269 270 createChildActions = generateCreateChildActions(newChildDescriptors, selection); 273 createSiblingActions = generateCreateSiblingActions(newSiblingDescriptors, selection); 274 275 if (createChildMenuManager != null) { 276 populateManager(createChildMenuManager, createChildActions, null); 277 createChildMenuManager.update(true); 278 } 279 if (createSiblingMenuManager != null) { 280 populateManager(createSiblingMenuManager, createSiblingActions, null); 281 createSiblingMenuManager.update(true); 282 } 283 } 284 285 292 protected Collection generateCreateChildActions(Collection descriptors, ISelection selection) { 293 Collection actions = new ArrayList (); 294 if (descriptors != null) { 295 for (Iterator i = descriptors.iterator(); i.hasNext(); ) { 296 actions.add(new CreateChildAction(activeEditorPart, selection, i.next())); 297 } 298 } 299 return actions; 300 } 301 302 309 protected Collection generateCreateSiblingActions(Collection descriptors, ISelection selection) { 310 Collection actions = new ArrayList (); 311 if (descriptors != null) { 312 for (Iterator i = descriptors.iterator(); i.hasNext(); ) { 313 actions.add(new CreateSiblingAction(activeEditorPart, selection, i.next())); 314 } 315 } 316 return actions; 317 } 318 319 328 protected void populateManager(IContributionManager manager, Collection actions, String contributionID) { 329 if (actions != null) { 330 for (Iterator i = actions.iterator(); i.hasNext(); ) { 331 IAction action = (IAction)i.next(); 332 if (contributionID != null) { 333 manager.insertBefore(contributionID, action); 334 } 335 else { 336 manager.add(action); 337 } 338 } 339 } 340 } 341 342 349 protected void depopulateManager(IContributionManager manager, Collection actions) { 350 if (actions != null) { 351 IContributionItem[] items = manager.getItems(); 352 for (int i = 0; i < items.length; i++) { 353 IContributionItem contributionItem = items[i]; 356 while (contributionItem instanceof SubContributionItem) { 357 contributionItem = ((SubContributionItem)contributionItem).getInnerItem(); 358 } 359 360 if (contributionItem instanceof ActionContributionItem) { 363 IAction action = ((ActionContributionItem)contributionItem).getAction(); 364 if (actions.contains(action)) { 365 manager.remove(contributionItem); 366 } 367 } 368 } 369 } 370 } 371 372 378 public void menuAboutToShow(IMenuManager menuManager) { 379 super.menuAboutToShow(menuManager); 380 MenuManager submenuManager = null; 381 382 submenuManager = new MenuManager(OljEditorPlugin.INSTANCE.getString("_UI_CreateChild_menu_item")); 383 populateManager(submenuManager, createChildActions, null); 384 menuManager.insertBefore("additions", submenuManager); 385 386 submenuManager = new MenuManager(OljEditorPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item")); 387 populateManager(submenuManager, createSiblingActions, null); 388 menuManager.insertBefore("additions", submenuManager); 389 } 390 391 397 protected void addGlobalActions(IMenuManager menuManager) { 398 menuManager.insertAfter("additions-end", new Separator("ui-actions")); 399 menuManager.insertAfter("ui-actions", showPropertiesViewAction); 400 401 refreshViewerAction.setEnabled(refreshViewerAction.isEnabled()); 402 menuManager.insertAfter("ui-actions", refreshViewerAction); 403 404 super.addGlobalActions(menuManager); 405 } 406 407 } 408 | Popular Tags |