Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4972e4febca27408ff7e018b8ac3061f98ee98fb (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package org.eclipse.papyrus.sysml.diagram.blockdefinition.tests.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Tool;
import org.eclipse.gef.util.EditPartUtilities;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IPrimaryEditPart;
import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramCommandStack;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramEditDomain;
import org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.core.services.ServiceException;
import org.eclipse.papyrus.core.services.ServicesRegistry;
import org.eclipse.papyrus.core.utils.ServiceUtils;
import org.eclipse.papyrus.sysml.diagram.blockdefinition.BlockDefinitionDiagramForMultiEditor;
import org.eclipse.papyrus.sysml.diagram.blockdefinition.factory.DiagramPaletteFactory;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;


public class EditorUtils {

	public static BlockDefinitionDiagramForMultiEditor getDiagramEditor() throws Exception {

		ServicesRegistry serviceRegistry = (ServicesRegistry)getEditor().getAdapter(ServicesRegistry.class);
		try {
			return (BlockDefinitionDiagramForMultiEditor)ServiceUtils.getInstance().getNestedActiveIEditorPart(serviceRegistry);

		} catch (ServiceException e) {
			throw new Exception("Unable to retrieve service.", e);
		} catch (ClassCastException e) {
			throw new Exception("Active diagram is not a BDD." + e.getMessage(), e);
		}
	}

	public static DiagramEditPart getDiagramEditPart() throws Exception {

		try {
			return getDiagramEditor().getDiagramEditPart();

		} catch (NullPointerException e) {
			throw new Exception("Could not find diagram edit part.", e);
		}
	}



	public static Diagram getDiagramView() throws Exception {

		try {
			return getDiagramEditor().getDiagram();

		} catch (NullPointerException e) {
			throw new Exception("Could not find diagram view.", e);
		}
	}

	public static EditPart getEditPart(View view) throws Exception {

		// Test if the container is the diagram itself first
		if(getDiagramEditPart().getModel() == view) {
			return getDiagramEditPart();
		}

		// Test diagram children and look for the view
		@SuppressWarnings("unchecked")
		Iterator<EditPart> it = EditPartUtilities.getAllChildren(getDiagramEditPart()).iterator();
		while(it.hasNext()) {
			EditPart editPart = it.next();
			if(editPart.getModel() == view) {
				return editPart;
			}
		}

		// Test diagram nested connections and look for the view
		@SuppressWarnings("unchecked")
		Iterator<EditPart> itLinks = EditPartUtilities.getAllNestedConnectionEditParts(getDiagramEditPart()).iterator();
		while(itLinks.hasNext()) {
			EditPart editPart = itLinks.next();
			if(editPart.getModel() == view) {
				return editPart;
			}
		}

		throw new Exception("Unable to find edit part for the given view.");
	}

	public static Tool getPaletteTool(String toolId) throws Exception {
		DiagramPaletteFactory factory = new DiagramPaletteFactory();
		return factory.createTool(toolId);
	}

	public static IDiagramEditDomain getDiagramEditingDomain() throws Exception {
		return getDiagramEditor().getDiagramEditDomain();
	}

	public static DiagramCommandStack getDiagramCommandStack() throws Exception {
		return getDiagramEditingDomain().getDiagramCommandStack();
	}

	public static CommandStack getCommandStack() throws Exception {
		return getTransactionalEditingDomain().getCommandStack();
	}

	public static TransactionalEditingDomain getTransactionalEditingDomain() throws Exception {

		ServicesRegistry serviceRegistry = (ServicesRegistry)getEditor().getAdapter(ServicesRegistry.class);
		try {
			return (TransactionalEditingDomain)ServiceUtils.getInstance().getTransactionalEditingDomain(serviceRegistry);

		} catch (ServiceException e) {
			throw new Exception("Unable to retrieve service.", e);
		}
	}

	public static IEditorPart getEditor() throws Exception {
		return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
	}

	/**
	 * retrieves the list of edit parts of the element with given parameters and adds it to the list of results
	 * 
	 * @param parent
	 *        the parent edit part
	 * @param elementName
	 *        the name of the element, or <code>null</code>
	 * @param semanticHint
	 *        the semantic hint of the edit part to find
	 * @param deepSearch
	 *        <code>true</code> if all the content should be looked, not only the direct children
	 * @param results
	 *        the list of results to complete
	 * @throws Exception
	 *         exception thrown in case of problem
	 */
	public static void findEditParts(GraphicalEditPart parent, String elementName, String semanticHint, boolean deepSearch, List<GraphicalEditPart> results) throws Exception {
		Iterator<EditPart> childrenIterator = EditPartUtilities.getAllChildren(parent).iterator();
		while(childrenIterator.hasNext()) {
			EditPart part = childrenIterator.next();
			if(part instanceof GraphicalEditPart && part instanceof IPrimaryEditPart) {
				GraphicalEditPart graphicalEditPart = (GraphicalEditPart)part;
				Object model = graphicalEditPart.getModel();
				if(model instanceof View) {
					String type = ((View)model).getType();
					if(semanticHint != null) {
						if(semanticHint.equals(type)) {
							// test name
							String name = (EMFCoreUtil.getName(((View)model).getElement()));
							if(elementName != null) {
								if(elementName.equals(name)) {
									results.add(graphicalEditPart);
								}
							} else {
								results.add(graphicalEditPart);
							}
						}
					} else { // no interest in semantic hint. Check By name, which should not be null in this case 
						String name = EMFCoreUtil.getName(((View)model).getElement());
						if(elementName != null) {
							if(elementName.equals(name)) {
								results.add(graphicalEditPart);
							}
						} else {
							throw new Exception("At least semantic hint or name should be given to look for an element");
						}
					}
				}
				if(deepSearch) {
					findEditParts(graphicalEditPart, elementName, semanticHint, deepSearch, results);
				}
			}
		}
	}

	/**
	 * Returns the list of edit parts of the element with given parameters
	 * 
	 * @param parent
	 *        the parent edit part
	 * @param elementName
	 *        the name of the element, or <code>null</code>
	 * @param semanticHint
	 *        the semantic hint of the edit part to find
	 * @param deepSearch
	 *        <code>true</code> if all the content should be looked, not only the direct children
	 * @param zeroAccepted
	 *        <code>true</code> if 0 Edit Part is a valid result. If <code>false</code>, at least one edit part should be found
	 * @param severalAccepted
	 *        <code>true</code> if more than one Edit Part is a valid result. If <code>false</code>, not more than one edit part should be found
	 * @return the edit part found or <code>null</code> if none was found and zeroAccepted was <code>true</code>
	 * @throws Exception
	 *         exception thrown in case of problem
	 */
	public static GraphicalEditPart getEditPart(GraphicalEditPart parent, String elementName, String semanticHint, boolean deepSearch, boolean zeroAccepted, boolean severalAccepted) throws Exception {
		List<GraphicalEditPart> results = new ArrayList<GraphicalEditPart>();
		findEditParts(parent, elementName, semanticHint, deepSearch, results);
		
		// convert into a list of unique elements
		Set<GraphicalEditPart> set = new LinkedHashSet<GraphicalEditPart>(results);
		List<GraphicalEditPart> uniqueResults = new ArrayList<GraphicalEditPart>(set);

		if(uniqueResults.size() == 0) {
			if(zeroAccepted) {
				return null;	
			}
			throw new RuntimeException("At least one element was expected for parent "+parent+"\n element name: "+elementName+"\n semantic hint: "+semanticHint+"\n deep search allowed "+deepSearch);
		}
		
		if(uniqueResults.size() > 1) {
			if(!severalAccepted) {
				throw new RuntimeException("Not more than one element was expected for parent " + parent + "\n element name: " + elementName + "\n semantic hint: " + semanticHint + "\n deep search allowed " + deepSearch);
			} else {
				return uniqueResults.get(0);
			}
		}

		return uniqueResults.get(0);
	}

	/**
	 * Returns the list of edit parts of the element with given parameters
	 * 
	 * @param parent
	 *        the parent edit part
	 * @param elementName
	 *        the name of the element, or <code>null</code>
	 * @param semanticHint
	 *        the semantic hint of the edit part to find
	 * @param deepSearch
	 *        <code>true</code> if all the content should be looked, not only the direct children
	 * @return the list of edit parts found
	 * @throws Exception
	 *         exception thrown in case of problem
	 */
	public static List<GraphicalEditPart> getEditParts(GraphicalEditPart parent, String elementName, String semanticHint, boolean deepSearch) throws Exception {
		List<GraphicalEditPart> results = new ArrayList<GraphicalEditPart>();
		findEditParts(parent, elementName, semanticHint, deepSearch, results);
		return results;
	}

}

Back to the top