| /******************************************************************************* |
| * Copyright (c) 2013 RCP Vision (http://www.rcp-vision.com) and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Lorenzo Bettini - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.emf.parsley.edit.action; |
| |
| import java.util.ArrayList; |
| import java.util.Collection; |
| |
| import org.eclipse.emf.edit.command.CommandParameter; |
| import org.eclipse.emf.edit.domain.EditingDomain; |
| import org.eclipse.emf.edit.ui.action.CreateChildAction; |
| import org.eclipse.emf.edit.ui.action.CreateSiblingAction; |
| import org.eclipse.jface.action.ActionContributionItem; |
| import org.eclipse.jface.action.IAction; |
| import org.eclipse.jface.action.IContributionItem; |
| import org.eclipse.jface.action.IContributionManager; |
| import org.eclipse.jface.action.IMenuManager; |
| import org.eclipse.jface.action.MenuManager; |
| import org.eclipse.jface.action.SubContributionItem; |
| import org.eclipse.jface.resource.ImageDescriptor; |
| import org.eclipse.jface.viewers.ILabelProvider; |
| import org.eclipse.jface.viewers.ISelection; |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| import org.eclipse.swt.graphics.Image; |
| |
| import com.google.inject.Inject; |
| |
| public class EmfActionManager { |
| |
| protected Collection<IAction> createSiblingActions; |
| |
| protected IMenuManager createChildMenuManager; |
| |
| protected IMenuManager createSiblingMenuManager; |
| |
| protected Collection<IAction> createChildActions; |
| |
| @Inject |
| protected ILabelProvider iLabelProvider; |
| |
| private void generateCreateChildActions(EditingDomain domain, |
| Collection<?> descriptors, ISelection selection) { |
| Collection<IAction> actions = new ArrayList<IAction>(); |
| if (descriptors != null) { |
| for (Object descriptor : descriptors) { |
| CreateChildAction act = new CreateChildAction(domain, |
| selection, descriptor); |
| Object imageObj = iLabelProvider |
| .getImage(((CommandParameter) descriptor).getValue()); |
| act.setImageDescriptor(ImageDescriptor |
| .createFromImage((Image) imageObj)); |
| actions.add(act); |
| } |
| } |
| createChildActions = actions; |
| } |
| |
| private void generateCreateSiblingActions(EditingDomain domain, |
| Collection<?> descriptors, ISelection selection) { |
| Collection<IAction> actions = new ArrayList<IAction>(); |
| if (descriptors != null) { |
| for (Object descriptor : descriptors) { |
| CreateSiblingAction act = new CreateSiblingAction(domain, |
| selection, descriptor); |
| Object imageObj = iLabelProvider |
| .getImage(((CommandParameter) descriptor).getValue()); |
| act.setImageDescriptor(ImageDescriptor |
| .createFromImage((Image) imageObj)); |
| actions.add(act); |
| } |
| } |
| createSiblingActions = actions; |
| } |
| |
| public void depopulateChildManager(IMenuManager menuManager) { |
| depopulateManager(menuManager, createChildActions); |
| } |
| |
| public void depopulateSibilingManager(IMenuManager menuManager) { |
| depopulateManager(menuManager, createSiblingActions); |
| } |
| |
| public Collection<IAction> getCreateSiblingActions() { |
| return createSiblingActions; |
| } |
| |
| public Collection<IAction> getCreateChildActions() { |
| return createChildActions; |
| } |
| |
| public void menuAboutToShow(IMenuManager menuManager) { |
| MenuManager submenuManager = null; |
| |
| submenuManager = new MenuManager("&New Child"); |
| populateManager(submenuManager, createChildActions); |
| menuManager.insertBefore("edit", submenuManager); |
| |
| submenuManager = new MenuManager("N&ew Sibling"); |
| populateManager(submenuManager, createSiblingActions); |
| menuManager.insertBefore("edit", submenuManager); |
| } |
| |
| public void populateChildMenuManager(IMenuManager menuManager) { |
| populateManager(menuManager, createChildActions); |
| } |
| |
| public void populateSibilingMenuManager(IMenuManager menuManager) { |
| populateManager(menuManager, createSiblingActions); |
| } |
| |
| public void contributeToMenu(IMenuManager submenuManager) { |
| createChildMenuManager = new MenuManager("&New Child"); |
| submenuManager.insertBefore("additions", createChildMenuManager); |
| |
| createSiblingMenuManager = new MenuManager("N&ew Sibling"); |
| submenuManager.insertBefore("additions", createSiblingMenuManager); |
| } |
| |
| public void updateSelection(ISelection selection, EditingDomain domain) { |
| depopulateChildManager(createChildMenuManager); |
| depopulateSibilingManager(createSiblingMenuManager); |
| |
| Collection<?> newChildDescriptors = null; |
| Collection<?> newSiblingDescriptors = null; |
| |
| if (selection instanceof IStructuredSelection |
| && ((IStructuredSelection) selection).size() == 1) { |
| Object object = ((IStructuredSelection) selection) |
| .getFirstElement(); |
| |
| newChildDescriptors = domain.getNewChildDescriptors(object, null); |
| newSiblingDescriptors = domain.getNewChildDescriptors(null, object); |
| } |
| |
| generateCreateChildActions(domain, newChildDescriptors, selection); |
| generateCreateSiblingActions(domain, newSiblingDescriptors, selection); |
| |
| if (createChildMenuManager != null) { |
| populateChildMenuManager(createChildMenuManager); |
| createChildMenuManager.update(true); |
| } |
| if (createSiblingMenuManager != null) { |
| populateSibilingMenuManager(createSiblingMenuManager); |
| createSiblingMenuManager.update(true); |
| } |
| } |
| |
| protected void depopulateManager(IContributionManager manager, |
| Collection<? extends IAction> actions) { |
| if (manager != null && actions != null) { |
| IContributionItem[] items = manager.getItems(); |
| for (int i = 0; i < items.length; i++) { |
| // Look into SubContributionItems |
| // |
| IContributionItem contributionItem = items[i]; |
| while (contributionItem instanceof SubContributionItem) { |
| contributionItem = ((SubContributionItem) contributionItem) |
| .getInnerItem(); |
| } |
| |
| // Delete the ActionContributionItems with matching action. |
| // |
| if (contributionItem instanceof ActionContributionItem) { |
| IAction action = ((ActionContributionItem) contributionItem) |
| .getAction(); |
| if (actions.contains(action)) { |
| manager.remove(contributionItem); |
| } |
| } |
| } |
| } |
| } |
| |
| protected void populateManager(IContributionManager manager, |
| Collection<? extends IAction> actions) { |
| if (actions != null) { |
| for (IAction action : actions) { |
| manager.add(action); |
| } |
| } |
| } |
| } |