Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d65250405064c2d002028526ee495697e0aa7e3d (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 *
 * 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.nattable.menu;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.expressions.EvaluationResult;
import org.eclipse.core.expressions.Expression;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.papyrus.infra.nattable.manager.axis.IAxisManager;
import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.FeatureIdAxis;
import org.eclipse.papyrus.infra.ui.util.EditorHelper;
import org.eclipse.papyrus.infra.widgets.Activator;
import org.eclipse.papyrus.infra.widgets.providers.HierarchicToFlatContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.IRestrictedContentProvider;
import org.eclipse.papyrus.uml.nattable.manager.axis.UMLStereotypePropertyAxisManager;
import org.eclipse.papyrus.uml.nattable.utils.Constants;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.menus.ExtensionContributionFactory;
import org.eclipse.ui.menus.IContributionRoot;
import org.eclipse.ui.services.IServiceLocator;
import org.eclipse.uml2.uml.Property;

/**
 *
 * @author VL222926
 *
 */
public abstract class AbstractCreateStereotypePropertyMenuFactory extends ExtensionContributionFactory {

	/**
	 * the label for the created menu
	 */
	private final String menuLabel;

	private final String iconPath;

	/**
	 *
	 * Constructor.
	 *
	 * @param menuLabel
	 *            the label to use for the menu
	 */
	public AbstractCreateStereotypePropertyMenuFactory(final String menuLabel, final String iconPath) {
		this.menuLabel = menuLabel;
		this.iconPath = iconPath;
	}

	/**
	 *
	 * @param tableManager
	 *            the current table manager
	 * @return
	 *         the uml stereotype property axis manager or <code>null</code> if not found
	 */
	protected abstract UMLStereotypePropertyAxisManager getStereotypeAxisManager(final INattableModelManager tableManager);

	/**
	 *
	 * {@inheritDoc}
	 */
	@Override
	public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
		final INattableModelManager tableManager = getTableManager();
		if (tableManager != null) {
			final IAxisManager steretoypeAxisManager = getStereotypeAxisManager(tableManager);
			if (steretoypeAxisManager == null) {
				return;
			}
			final IRestrictedContentProvider provider = steretoypeAxisManager.createPossibleAxisContentProvider(true);
			final HierarchicToFlatContentProvider flatProvider = new HierarchicToFlatContentProvider(provider);
			final Object[] elementss = flatProvider.getElements();

			final Map<String, Property> nameToPropertyMap = new HashMap<String, Property>();
			for (final Object current : elementss) {
				if (current instanceof Property) {
					nameToPropertyMap.put(((Property) current).getQualifiedName(), (Property) current);
				}
			}


			final Collection<String> initialSelection = new ArrayList<String>();

			for (final Object current : getAxisElementList(tableManager)) {
				if (current instanceof FeatureIdAxis) {
					String el = ((FeatureIdAxis) current).getElement();
					if (el.startsWith(Constants.PROPERTY_OF_STEREOTYPE_PREFIX)) {
						initialSelection.add(((FeatureIdAxis) current).getElement().replace(Constants.PROPERTY_OF_STEREOTYPE_PREFIX, "")); //$NON-NLS-1$
					}
				}
			}

			if (tableManager != null) {
				if (true) {
					MenuManager manager = new MenuManager(menuLabel) {

						/**
						 *
						 * {@inheritDoc}
						 */
						@Override
						public void fill(Menu parent, int index) {
							super.fill(parent, index);
							getMenu().getParentItem().setEnabled(true);
							Image image = Activator.getDefault().getImage(org.eclipse.papyrus.uml.nattable.Activator.getDefault().PLUGIN_ID, iconPath);
							getMenu().getParentItem().setImage(image);
						}


					};

					manager.setVisible(true);



					final IContributionItem item = new ContributionItem() {

						/**
						 *
						 * {@inheritDoc}
						 */
						@Override
						public void fill(final Menu menu, int index) {
							fillMenu(menu, initialSelection, nameToPropertyMap);
						}
					};

					Menu menu = manager.getMenu();
					if (menu != null) {
						menu.setEnabled(false);
					}
					manager.add(item);
					Expression visibleWhen = new Expression() {

						/**
						 *
						 * {@inheritDoc}
						 */
						@Override
						public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
							return EvaluationResult.TRUE;
						}
					};

					additions.addContributionItem(manager, visibleWhen);
				}
			}
		}
		
	}

	/**
	 *
	 * @param menu
	 *            the menu to fill
	 * @param initialSelection
	 *            the initial selection
	 * @param nameToPropertyMap
	 *            the map with the available properties
	 */
	protected abstract void fillMenu(final Menu menu, final Collection<String> initialSelection, final Map<String, Property> nameToPropertyMap);

	/**
	 *
	 * @param menu
	 *            the menu to fill
	 * @param tableManager
	 *            the table manager
	 * @param initialSelection
	 *            the initial selection
	 * @param nameToPropertyMap
	 *            the map with the available properties
	 * 
	 * @Deprecated since Eclipse Mars
	 */
	@Deprecated
	protected abstract void fillMenu(final Menu menu, final INattableModelManager tableManager, final Collection<String> initialSelection, final Map<String, Property> nameToPropertyMap);

	/**
	 *
	 * @param tableManager
	 *            the table manager
	 * @return
	 *         the list of the element currently know by the axis
	 */
	protected abstract Collection<?> getAxisElementList(final INattableModelManager tableManager);

	/**
	 *
	 * @return
	 *         the table manager
	 */
	protected final INattableModelManager getTableManager() {
		final IWorkbenchPart workbenchPart = EditorHelper.getActivePart();
		INattableModelManager tableManager = null;
		if (workbenchPart != null) {
			tableManager = (INattableModelManager) workbenchPart.getAdapter(INattableModelManager.class);
		}
		return tableManager;
	}

}

Back to the top