| 1 | /** |
| 2 | * Copyright 2007 by SDQ, IPD, University of Karlsruhe, Germany |
| 3 | * |
| 4 | * $Id$ |
| 5 | */ |
| 6 | package de.uka.ipd.sdq.pcm.repository.presentation; |
| 7 | |
| 8 | import java.util.ArrayList; |
| 9 | import java.util.Collection; |
| 10 | |
| 11 | import org.eclipse.emf.common.ui.viewer.IViewerProvider; |
| 12 | import org.eclipse.emf.edit.domain.EditingDomain; |
| 13 | import org.eclipse.emf.edit.domain.IEditingDomainProvider; |
| 14 | import org.eclipse.emf.edit.ui.action.ControlAction; |
| 15 | import org.eclipse.emf.edit.ui.action.CreateChildAction; |
| 16 | import org.eclipse.emf.edit.ui.action.CreateSiblingAction; |
| 17 | import org.eclipse.emf.edit.ui.action.EditingDomainActionBarContributor; |
| 18 | import org.eclipse.emf.edit.ui.action.LoadResourceAction; |
| 19 | import org.eclipse.emf.edit.ui.action.ValidateAction; |
| 20 | import org.eclipse.jface.action.Action; |
| 21 | import org.eclipse.jface.action.ActionContributionItem; |
| 22 | import org.eclipse.jface.action.IAction; |
| 23 | import org.eclipse.jface.action.IContributionItem; |
| 24 | import org.eclipse.jface.action.IContributionManager; |
| 25 | import org.eclipse.jface.action.IMenuListener; |
| 26 | import org.eclipse.jface.action.IMenuManager; |
| 27 | import org.eclipse.jface.action.IToolBarManager; |
| 28 | import org.eclipse.jface.action.MenuManager; |
| 29 | import org.eclipse.jface.action.Separator; |
| 30 | import org.eclipse.jface.action.SubContributionItem; |
| 31 | import org.eclipse.jface.viewers.ISelection; |
| 32 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
| 33 | import org.eclipse.jface.viewers.ISelectionProvider; |
| 34 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 35 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
| 36 | import org.eclipse.jface.viewers.Viewer; |
| 37 | import org.eclipse.ui.IEditorPart; |
| 38 | import org.eclipse.ui.PartInitException; |
| 39 | |
| 40 | import de.uka.ipd.sdq.pcm.core.presentation.PalladioComponentModelEditorPlugin; |
| 41 | |
| 42 | /** |
| 43 | * This is the action bar contributor for the Repository model editor. |
| 44 | * <!-- begin-user-doc --> |
| 45 | * <!-- end-user-doc --> |
| 46 | * @generated |
| 47 | */ |
| 48 | public class RepositoryActionBarContributor |
| 49 | extends EditingDomainActionBarContributor |
| 50 | implements ISelectionChangedListener { |
| 51 | /** |
| 52 | * <!-- begin-user-doc --> |
| 53 | * <!-- end-user-doc --> |
| 54 | * @generated |
| 55 | */ |
| 56 | public static final String copyright = "Copyright 2005-2009 by SDQ, IPD, University of Karlsruhe, Germany"; |
| 57 | |
| 58 | /** |
| 59 | * This keeps track of the active editor. |
| 60 | * <!-- begin-user-doc --> |
| 61 | * <!-- end-user-doc --> |
| 62 | * @generated |
| 63 | */ |
| 64 | protected IEditorPart activeEditorPart; |
| 65 | |
| 66 | /** |
| 67 | * This keeps track of the current selection provider. |
| 68 | * <!-- begin-user-doc --> |
| 69 | * <!-- end-user-doc --> |
| 70 | * @generated |
| 71 | */ |
| 72 | protected ISelectionProvider selectionProvider; |
| 73 | |
| 74 | /** |
| 75 | * This action opens the Properties view. |
| 76 | * <!-- begin-user-doc --> |
| 77 | * <!-- end-user-doc --> |
| 78 | * @generated |
| 79 | */ |
| 80 | protected IAction showPropertiesViewAction = |
| 81 | new Action(PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_ShowPropertiesView_menu_item")) { |
| 82 | @Override |
| 83 | public void run() { |
| 84 | try { |
| 85 | getPage().showView("org.eclipse.ui.views.PropertySheet"); |
| 86 | } |
| 87 | catch (PartInitException exception) { |
| 88 | PalladioComponentModelEditorPlugin.INSTANCE.log(exception); |
| 89 | } |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | /** |
| 94 | * This action refreshes the viewer of the current editor if the editor |
| 95 | * implements {@link org.eclipse.emf.common.ui.viewer.IViewerProvider}. |
| 96 | * <!-- begin-user-doc --> |
| 97 | * <!-- end-user-doc --> |
| 98 | * @generated |
| 99 | */ |
| 100 | protected IAction refreshViewerAction = |
| 101 | new Action(PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_RefreshViewer_menu_item")) { |
| 102 | @Override |
| 103 | public boolean isEnabled() { |
| 104 | return activeEditorPart instanceof IViewerProvider; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void run() { |
| 109 | if (activeEditorPart instanceof IViewerProvider) { |
| 110 | Viewer viewer = ((IViewerProvider)activeEditorPart).getViewer(); |
| 111 | if (viewer != null) { |
| 112 | viewer.refresh(); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | /** |
| 119 | * This will contain one {@link org.eclipse.emf.edit.ui.action.CreateChildAction} corresponding to each descriptor |
| 120 | * generated for the current selection by the item provider. |
| 121 | * <!-- begin-user-doc --> |
| 122 | * <!-- end-user-doc --> |
| 123 | * @generated |
| 124 | */ |
| 125 | protected Collection<IAction> createChildActions; |
| 126 | |
| 127 | /** |
| 128 | * This is the menu manager into which menu contribution items should be added for CreateChild actions. |
| 129 | * <!-- begin-user-doc --> |
| 130 | * <!-- end-user-doc --> |
| 131 | * @generated |
| 132 | */ |
| 133 | protected IMenuManager createChildMenuManager; |
| 134 | |
| 135 | /** |
| 136 | * This will contain one {@link org.eclipse.emf.edit.ui.action.CreateSiblingAction} corresponding to each descriptor |
| 137 | * generated for the current selection by the item provider. |
| 138 | * <!-- begin-user-doc --> |
| 139 | * <!-- end-user-doc --> |
| 140 | * @generated |
| 141 | */ |
| 142 | protected Collection<IAction> createSiblingActions; |
| 143 | |
| 144 | /** |
| 145 | * This is the menu manager into which menu contribution items should be added for CreateSibling actions. |
| 146 | * <!-- begin-user-doc --> |
| 147 | * <!-- end-user-doc --> |
| 148 | * @generated |
| 149 | */ |
| 150 | protected IMenuManager createSiblingMenuManager; |
| 151 | |
| 152 | /** |
| 153 | * This creates an instance of the contributor. |
| 154 | * <!-- begin-user-doc --> |
| 155 | * <!-- end-user-doc --> |
| 156 | * @generated |
| 157 | */ |
| 158 | public RepositoryActionBarContributor() { |
| 159 | super(ADDITIONS_LAST_STYLE); |
| 160 | loadResourceAction = new LoadResourceAction(); |
| 161 | validateAction = new ValidateAction(); |
| 162 | controlAction = new ControlAction(); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * This adds Separators for editor additions to the tool bar. |
| 167 | * <!-- begin-user-doc --> |
| 168 | * <!-- end-user-doc --> |
| 169 | * @generated |
| 170 | */ |
| 171 | @Override |
| 172 | public void contributeToToolBar(IToolBarManager toolBarManager) { |
| 173 | toolBarManager.add(new Separator("repository-settings")); |
| 174 | toolBarManager.add(new Separator("repository-additions")); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * This adds to the menu bar a menu and some separators for editor additions, |
| 179 | * as well as the sub-menus for object creation items. |
| 180 | * <!-- begin-user-doc --> |
| 181 | * <!-- end-user-doc --> |
| 182 | * @generated |
| 183 | */ |
| 184 | @Override |
| 185 | public void contributeToMenu(IMenuManager menuManager) { |
| 186 | super.contributeToMenu(menuManager); |
| 187 | |
| 188 | IMenuManager submenuManager = new MenuManager(PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_RepositoryEditor_menu"), "de.uka.ipd.sdq.pcm.repositoryMenuID"); |
| 189 | menuManager.insertAfter("additions", submenuManager); |
| 190 | submenuManager.add(new Separator("settings")); |
| 191 | submenuManager.add(new Separator("actions")); |
| 192 | submenuManager.add(new Separator("additions")); |
| 193 | submenuManager.add(new Separator("additions-end")); |
| 194 | |
| 195 | // Prepare for CreateChild item addition or removal. |
| 196 | // |
| 197 | createChildMenuManager = new MenuManager(PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_CreateChild_menu_item")); |
| 198 | submenuManager.insertBefore("additions", createChildMenuManager); |
| 199 | |
| 200 | // Prepare for CreateSibling item addition or removal. |
| 201 | // |
| 202 | createSiblingMenuManager = new MenuManager(PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item")); |
| 203 | submenuManager.insertBefore("additions", createSiblingMenuManager); |
| 204 | |
| 205 | // Force an update because Eclipse hides empty menus now. |
| 206 | // |
| 207 | submenuManager.addMenuListener |
| 208 | (new IMenuListener() { |
| 209 | public void menuAboutToShow(IMenuManager menuManager) { |
| 210 | menuManager.updateAll(true); |
| 211 | } |
| 212 | }); |
| 213 | |
| 214 | addGlobalActions(submenuManager); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * When the active editor changes, this remembers the change and registers with it as a selection provider. |
| 219 | * <!-- begin-user-doc --> |
| 220 | * <!-- end-user-doc --> |
| 221 | * @generated |
| 222 | */ |
| 223 | @Override |
| 224 | public void setActiveEditor(IEditorPart part) { |
| 225 | super.setActiveEditor(part); |
| 226 | activeEditorPart = part; |
| 227 | |
| 228 | // Switch to the new selection provider. |
| 229 | // |
| 230 | if (selectionProvider != null) { |
| 231 | selectionProvider.removeSelectionChangedListener(this); |
| 232 | } |
| 233 | if (part == null) { |
| 234 | selectionProvider = null; |
| 235 | } |
| 236 | else { |
| 237 | selectionProvider = part.getSite().getSelectionProvider(); |
| 238 | selectionProvider.addSelectionChangedListener(this); |
| 239 | |
| 240 | // Fake a selection changed event to update the menus. |
| 241 | // |
| 242 | if (selectionProvider.getSelection() != null) { |
| 243 | selectionChanged(new SelectionChangedEvent(selectionProvider, selectionProvider.getSelection())); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * This implements {@link org.eclipse.jface.viewers.ISelectionChangedListener}, |
| 250 | * handling {@link org.eclipse.jface.viewers.SelectionChangedEvent}s by querying for the children and siblings |
| 251 | * that can be added to the selected object and updating the menus accordingly. |
| 252 | * <!-- begin-user-doc --> |
| 253 | * <!-- end-user-doc --> |
| 254 | * @generated |
| 255 | */ |
| 256 | public void selectionChanged(SelectionChangedEvent event) { |
| 257 | // Remove any menu items for old selection. |
| 258 | // |
| 259 | if (createChildMenuManager != null) { |
| 260 | depopulateManager(createChildMenuManager, createChildActions); |
| 261 | } |
| 262 | if (createSiblingMenuManager != null) { |
| 263 | depopulateManager(createSiblingMenuManager, createSiblingActions); |
| 264 | } |
| 265 | |
| 266 | // Query the new selection for appropriate new child/sibling descriptors |
| 267 | // |
| 268 | Collection<?> newChildDescriptors = null; |
| 269 | Collection<?> newSiblingDescriptors = null; |
| 270 | |
| 271 | ISelection selection = event.getSelection(); |
| 272 | if (selection instanceof IStructuredSelection && ((IStructuredSelection)selection).size() == 1) { |
| 273 | Object object = ((IStructuredSelection)selection).getFirstElement(); |
| 274 | |
| 275 | EditingDomain domain = ((IEditingDomainProvider)activeEditorPart).getEditingDomain(); |
| 276 | |
| 277 | newChildDescriptors = domain.getNewChildDescriptors(object, null); |
| 278 | newSiblingDescriptors = domain.getNewChildDescriptors(null, object); |
| 279 | } |
| 280 | |
| 281 | // Generate actions for selection; populate and redraw the menus. |
| 282 | // |
| 283 | createChildActions = generateCreateChildActions(newChildDescriptors, selection); |
| 284 | createSiblingActions = generateCreateSiblingActions(newSiblingDescriptors, selection); |
| 285 | |
| 286 | if (createChildMenuManager != null) { |
| 287 | populateManager(createChildMenuManager, createChildActions, null); |
| 288 | createChildMenuManager.update(true); |
| 289 | } |
| 290 | if (createSiblingMenuManager != null) { |
| 291 | populateManager(createSiblingMenuManager, createSiblingActions, null); |
| 292 | createSiblingMenuManager.update(true); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * This generates a {@link org.eclipse.emf.edit.ui.action.CreateChildAction} for each object in <code>descriptors</code>, |
| 298 | * and returns the collection of these actions. |
| 299 | * <!-- begin-user-doc --> |
| 300 | * <!-- end-user-doc --> |
| 301 | * @generated |
| 302 | */ |
| 303 | protected Collection<IAction> generateCreateChildActions(Collection<?> descriptors, ISelection selection) { |
| 304 | Collection<IAction> actions = new ArrayList<IAction>(); |
| 305 | if (descriptors != null) { |
| 306 | for (Object descriptor : descriptors) { |
| 307 | actions.add(new CreateChildAction(activeEditorPart, selection, descriptor)); |
| 308 | } |
| 309 | } |
| 310 | return actions; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * This generates a {@link org.eclipse.emf.edit.ui.action.CreateSiblingAction} for each object in <code>descriptors</code>, |
| 315 | * and returns the collection of these actions. |
| 316 | * <!-- begin-user-doc --> |
| 317 | * <!-- end-user-doc --> |
| 318 | * @generated |
| 319 | */ |
| 320 | protected Collection<IAction> generateCreateSiblingActions(Collection<?> descriptors, ISelection selection) { |
| 321 | Collection<IAction> actions = new ArrayList<IAction>(); |
| 322 | if (descriptors != null) { |
| 323 | for (Object descriptor : descriptors) { |
| 324 | actions.add(new CreateSiblingAction(activeEditorPart, selection, descriptor)); |
| 325 | } |
| 326 | } |
| 327 | return actions; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * This populates the specified <code>manager</code> with {@link org.eclipse.jface.action.ActionContributionItem}s |
| 332 | * based on the {@link org.eclipse.jface.action.IAction}s contained in the <code>actions</code> collection, |
| 333 | * by inserting them before the specified contribution item <code>contributionID</code>. |
| 334 | * If <code>contributionID</code> is <code>null</code>, they are simply added. |
| 335 | * <!-- begin-user-doc --> |
| 336 | * <!-- end-user-doc --> |
| 337 | * @generated |
| 338 | */ |
| 339 | protected void populateManager(IContributionManager manager, Collection<? extends IAction> actions, String contributionID) { |
| 340 | if (actions != null) { |
| 341 | for (IAction action : actions) { |
| 342 | if (contributionID != null) { |
| 343 | manager.insertBefore(contributionID, action); |
| 344 | } |
| 345 | else { |
| 346 | manager.add(action); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * This removes from the specified <code>manager</code> all {@link org.eclipse.jface.action.ActionContributionItem}s |
| 354 | * based on the {@link org.eclipse.jface.action.IAction}s contained in the <code>actions</code> collection. |
| 355 | * <!-- begin-user-doc --> |
| 356 | * <!-- end-user-doc --> |
| 357 | * @generated |
| 358 | */ |
| 359 | protected void depopulateManager(IContributionManager manager, Collection<? extends IAction> actions) { |
| 360 | if (actions != null) { |
| 361 | IContributionItem[] items = manager.getItems(); |
| 362 | for (int i = 0; i < items.length; i++) { |
| 363 | // Look into SubContributionItems |
| 364 | // |
| 365 | IContributionItem contributionItem = items[i]; |
| 366 | while (contributionItem instanceof SubContributionItem) { |
| 367 | contributionItem = ((SubContributionItem)contributionItem).getInnerItem(); |
| 368 | } |
| 369 | |
| 370 | // Delete the ActionContributionItems with matching action. |
| 371 | // |
| 372 | if (contributionItem instanceof ActionContributionItem) { |
| 373 | IAction action = ((ActionContributionItem)contributionItem).getAction(); |
| 374 | if (actions.contains(action)) { |
| 375 | manager.remove(contributionItem); |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * This populates the pop-up menu before it appears. |
| 384 | * <!-- begin-user-doc --> |
| 385 | * <!-- end-user-doc --> |
| 386 | * @generated |
| 387 | */ |
| 388 | @Override |
| 389 | public void menuAboutToShow(IMenuManager menuManager) { |
| 390 | super.menuAboutToShow(menuManager); |
| 391 | MenuManager submenuManager = null; |
| 392 | |
| 393 | submenuManager = new MenuManager(PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_CreateChild_menu_item")); |
| 394 | populateManager(submenuManager, createChildActions, null); |
| 395 | menuManager.insertBefore("edit", submenuManager); |
| 396 | |
| 397 | submenuManager = new MenuManager(PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item")); |
| 398 | populateManager(submenuManager, createSiblingActions, null); |
| 399 | menuManager.insertBefore("edit", submenuManager); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * This inserts global actions before the "additions-end" separator. |
| 404 | * <!-- begin-user-doc --> |
| 405 | * <!-- end-user-doc --> |
| 406 | * @generated |
| 407 | */ |
| 408 | @Override |
| 409 | protected void addGlobalActions(IMenuManager menuManager) { |
| 410 | menuManager.insertAfter("additions-end", new Separator("ui-actions")); |
| 411 | menuManager.insertAfter("ui-actions", showPropertiesViewAction); |
| 412 | |
| 413 | refreshViewerAction.setEnabled(refreshViewerAction.isEnabled()); |
| 414 | menuManager.insertAfter("ui-actions", refreshViewerAction); |
| 415 | |
| 416 | super.addGlobalActions(menuManager); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * This ensures that a delete action will clean up all references to deleted objects. |
| 421 | * <!-- begin-user-doc --> |
| 422 | * <!-- end-user-doc --> |
| 423 | * @generated |
| 424 | */ |
| 425 | @Override |
| 426 | protected boolean removeAllReferencesOnDelete() { |
| 427 | return true; |
| 428 | } |
| 429 | |
| 430 | } |