Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e50b6f5c982d99b50ebc5b551deb8c5fcf8fe954 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*****************************************************************************
 * 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.views.modelexplorer.newchild;


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.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) {
			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) {
				//Nothing to do. We can't handle this case
			}
			return selectedEObject;
		}
		return null;
	}
}

Back to the top