diff options
Diffstat (limited to 'plugins/infra/org.eclipse.papyrus.infra.newchild')
2 files changed, 141 insertions, 2 deletions
diff --git a/plugins/infra/org.eclipse.papyrus.infra.newchild/META-INF/MANIFEST.MF b/plugins/infra/org.eclipse.papyrus.infra.newchild/META-INF/MANIFEST.MF index 2497b8847e3..afb9b97724f 100644 --- a/plugins/infra/org.eclipse.papyrus.infra.newchild/META-INF/MANIFEST.MF +++ b/plugins/infra/org.eclipse.papyrus.infra.newchild/META-INF/MANIFEST.MF @@ -2,7 +2,8 @@ Manifest-Version: 1.0 Export-Package: org.eclipse.papyrus.infra.newchild,
org.eclipse.papyrus.infra.newchild.elementcreationmenumodel,
org.eclipse.papyrus.infra.newchild.elementcreationmenumodel.impl,
- org.eclipse.papyrus.infra.newchild.elementcreationmenumodel.util
+ org.eclipse.papyrus.infra.newchild.elementcreationmenumodel.util,
+ org.eclipse.papyrus.infra.newchild.ui
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
org.eclipse.emf;bundle-version="2.6.0",
@@ -16,7 +17,9 @@ Require-Bundle: org.eclipse.ui, org.eclipse.papyrus.infra.services.edit;bundle-version="1.1.0",
org.eclipse.papyrus.infra.gmfdiag.commands;bundle-version="1.1.0",
org.eclipse.papyrus.infra.core.log;bundle-version="1.1.0",
- org.eclipse.papyrus.infra.widgets;bundle-version="1.1.0"
+ org.eclipse.papyrus.infra.widgets;bundle-version="1.1.0",
+ org.eclipse.papyrus.infra.emf;bundle-version="1.1.0",
+ org.eclipse.papyrus.infra.core;bundle-version="1.1.0"
Bundle-Vendor: %providerName
Bundle-ActivationPolicy: lazy
Bundle-ClassPath: .
diff --git a/plugins/infra/org.eclipse.papyrus.infra.newchild/src/org/eclipse/papyrus/infra/newchild/ui/DynamicNewChild.java b/plugins/infra/org.eclipse.papyrus.infra.newchild/src/org/eclipse/papyrus/infra/newchild/ui/DynamicNewChild.java new file mode 100644 index 00000000000..ec502c3a7df --- /dev/null +++ b/plugins/infra/org.eclipse.papyrus.infra.newchild/src/org/eclipse/papyrus/infra/newchild/ui/DynamicNewChild.java @@ -0,0 +1,136 @@ +/***************************************************************************** + * Copyright (c) 2011, 2014 CEA LIST 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: + * + * CEA LIST - Initial API and implementation + * Christian W. Damus (CEA) - bug 413703 + * + *****************************************************************************/ +package org.eclipse.papyrus.infra.newchild.ui; + + +import java.util.ArrayList; +import java.util.Iterator; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EReference; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.emf.transaction.util.TransactionUtil; +import org.eclipse.jface.action.ContributionItem; +import org.eclipse.jface.action.IContributionItem; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.ITreeSelection; +import org.eclipse.jface.viewers.TreePath; +import org.eclipse.papyrus.infra.emf.utils.EMFHelper; +import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject; +import org.eclipse.papyrus.infra.newchild.CreationMenuFactory; +import org.eclipse.papyrus.infra.newchild.CreationMenuRegistry; +import org.eclipse.papyrus.infra.newchild.elementcreationmenumodel.Folder; +import org.eclipse.swt.widgets.Menu; +import org.eclipse.ui.ISelectionService; +import org.eclipse.ui.PlatformUI; + +/** + * This class has in charge to create menu from elementCreationMenuModel + * + */ +public class DynamicNewChild extends ContributionItem { + + protected TransactionalEditingDomain editingDomain; + + protected CreationMenuRegistry creationMenuRegistry; + + /** + * + * Constructor. + * + */ + public DynamicNewChild() { + creationMenuRegistry = new CreationMenuRegistry(); + } + + /** + * + * Constructor. + * + * @param id + */ + public DynamicNewChild(String id) { + super(id); + } + + + protected IContributionItem[] getContributionItems() { + return new IContributionItem[0]; + } + + @Override + public boolean isDynamic() { + return true; + } + + + + @Override + public void fill(Menu menu, int index) { + EObject eObject = getSelection(); + if (eObject != null && editingDomain != null) { + CreationMenuFactory creationMenuFactory = new CreationMenuFactory(editingDomain); + ArrayList<Folder> folders = creationMenuRegistry.getRootFolder(); + Iterator<Folder> iterFolder = folders.iterator(); + while (iterFolder.hasNext()) { + Folder currentFolder = iterFolder.next(); + boolean hasbeenBuild = creationMenuFactory.populateMenu(menu, currentFolder, eObject, index); + if (hasbeenBuild) { + index++; + } + } + } else { + super.fill(menu, index); + } + } + + + + + /** + * getSelected eObject in the model explorer + * + * @return eObject or null + */ + protected EObject getSelection() { + ISelectionService selectionService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService(); + ISelection selection = selectionService.getSelection(); + + if (selection instanceof IStructuredSelection) { + Object selectedobject = ((IStructuredSelection) selection).getFirstElement(); + EObject selectedEObject = EMFHelper.getEObject(selectedobject); + EObject editingDomainCitizen = selectedEObject; + + if ((editingDomainCitizen instanceof EReference) && (selection instanceof ITreeSelection)) { + // The user selected a reference in the Advanced presentation. Infer the editing domain from the parent node, which is the reference owner + ITreeSelection treeSel = (ITreeSelection) selection; + TreePath[] paths = treeSel.getPathsFor(selectedobject); + if ((paths != null) && (paths.length > 0) && (paths[0].getSegmentCount() > 1)) { + editingDomainCitizen = EMFHelper.getEObject(paths[0].getSegment(paths[0].getSegmentCount() - 2)); + } + } + + try { + editingDomain = ServiceUtilsForEObject.getInstance().getService(org.eclipse.emf.transaction.TransactionalEditingDomain.class, editingDomainCitizen); + } catch (Exception ex) { + // If the service/service registry is not available, try to retrieve directly from the EObject + editingDomain = TransactionUtil.getEditingDomain(editingDomainCitizen); + } + return selectedEObject; + } + return null; + } +} |