Skip to main content
summaryrefslogtreecommitdiffstats
blob: f4181a8219e931d638b90856b159f1ca45f7dd62 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*****************************************************************************
 * Copyright (c) 2008 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:
 *  Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.editparts;

import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.diagram.core.edithelpers.CreateElementRequestAdapter;
import org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ListCompartmentEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ResizableCompartmentEditPart;
import org.eclipse.gmf.runtime.diagram.ui.figures.ResizableCompartmentFigure;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewAndElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.FontStyle;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.Style;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.FontDescriptor;
import org.eclipse.papyrus.infra.emf.appearance.helper.VisualInformationPapyrusConstants;
import org.eclipse.papyrus.infra.gmfdiag.common.editpart.NodeEditPart;
import org.eclipse.papyrus.infra.gmfdiag.expansion.expansionmodel.rendering.ChildrenListRepresentation;
import org.eclipse.papyrus.infra.gmfdiag.expansion.expansionmodel.rendering.DiagramExpansionSingleton;
import org.eclipse.papyrus.infra.gmfdiag.expansion.expansionmodel.rendering.DiagramExpansionsRegistry;
import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype;
import org.eclipse.papyrus.uml.diagram.common.editpolicies.ApplyStereotypeEditPolicy;
import org.eclipse.papyrus.uml.diagram.common.figure.node.AppliedStereotypeCompartmentFigure;
import org.eclipse.papyrus.uml.diagram.common.figure.node.IPapyrusNodeUMLElementFigure;
import org.eclipse.papyrus.uml.diagram.common.helper.ICompartmentLayoutHelper;
import org.eclipse.papyrus.uml.diagram.common.service.ApplyStereotypeRequest;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.uml2.uml.Element;

/**
 * This UML edit part that can manage node and compartments. It also manages the
 * application of stereotypes on UML elements represented by nodes
 *
 */
public abstract class UMLNodeEditPart extends NodeEditPart implements IUMLEditPart {

	/**
	 * Save the fontDescriptor in order to dispose the font later
	 */
	private FontDescriptor cachedFontDescriptor;

	/** helper use to change the layout of compartment if exist **/
	private ICompartmentLayoutHelper compartmentLayoutHelper;

	/**
	 * @return the compartmentLayoutHelper
	 */
	protected ICompartmentLayoutHelper getCompartmentLayoutHelper() {
		return compartmentLayoutHelper;
	}

	/**
	 * @param compartmentLayoutHelper
	 *            the compartmentLayoutHelper to set
	 */
	protected void setCompartmentLayoutHelper(ICompartmentLayoutHelper compartmentLayoutHelper) {
		this.compartmentLayoutHelper = compartmentLayoutHelper;
	}

	/**
	 * Creates a new UmlNodeEditPart.
	 *
	 * @param view
	 *            the view controlled by this edit part
	 */
	public UMLNodeEditPart(View view) {
		super(view);
	}

	@Override
	public void refresh() {
		super.refresh();
		changeLayoutCompartment();
		fixCompartmentTitleVisibility();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Element getUMLElement() {
		return (Element) resolveSemanticElement();
	}

	@Override
	public void setLayoutConstraint(EditPart child, IFigure childFigure, Object constraint) {
		if (!(childFigure instanceof AppliedStereotypeCompartmentFigure)) {
			getContentPaneFor((IGraphicalEditPart) child).setConstraint(childFigure, constraint);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void handleNotificationEvent(Notification event) {
		super.handleNotificationEvent(event);

		if (event.getNotifier() instanceof EAnnotation) {
			if (VisualInformationPapyrusConstants.LAYOUTFIGURE.equals(((EAnnotation) event.getNotifier()).getSource())) {
				changeLayoutCompartment();
			}
		}

		Object feature = event.getFeature();
		if (NotationPackage.eINSTANCE.getFontStyle_FontColor().equals(feature)) {
			refreshFontColor();
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void createDefaultEditPolicies() {
		super.createDefaultEditPolicies();
		// adds the stereotype application edit policy
		installEditPolicy(ApplyStereotypeRequest.APPLY_STEREOTYPE_REQUEST, new ApplyStereotypeEditPolicy());
	}

	/**
	 * this method has in charge to apply the good layout policy on compartments
	 */
	protected void changeLayoutCompartment() {
		if (getCompartmentLayoutHelper() != null) {
			Iterator<EditPart> childrenIterator = getChildren().iterator();
			while (childrenIterator.hasNext()) {
				EditPart currentEditPart = childrenIterator.next();
				if (currentEditPart instanceof ListCompartmentEditPart) {
					getCompartmentLayoutHelper().applyLayout((ListCompartmentEditPart) currentEditPart);
				}
			}
		}
	}

	/**
	 * Models created with Papyrus 0.7.x do not have a TitleStyle in the
	 * compartment views. In this case, the GMF code does not touch the
	 * visibility of the title, i.e. it remains true by default. This does not
	 * correspond to the diagrams in Papyrus 0.7.x having no title visible. See
	 * bug 351084
	 */
	public void fixCompartmentTitleVisibility() {
		for (Object currentEditPart : getChildren()) {
			if (currentEditPart instanceof ResizableCompartmentEditPart) {
				ResizableCompartmentEditPart rcep = (ResizableCompartmentEditPart) currentEditPart;
				View compartmentView = (View) rcep.getModel();
				Style titleStyle = compartmentView.getStyle(NotationPackage.eINSTANCE.getTitleStyle());
				if (titleStyle == null) {
					if (rcep.getFigure() instanceof ResizableCompartmentFigure) {
						ResizableCompartmentFigure rcf = (ResizableCompartmentFigure) rcep.getFigure();
						rcf.setTitleVisibility(false);
					}
				}
			}
		}
	}

	/**
	 * get the diagram type from a view.
	 * @param currentView the current view
	 * @return the diagram type it can be also a view point
	 */
	protected static String getDiagramType(View currentView) {
		Diagram diagram=currentView.getDiagram();
		String currentDiagramType=null;
		ViewPrototype viewPrototype=org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramUtils.getPrototype(diagram);
		if(viewPrototype!=null){
			currentDiagramType=viewPrototype.getLabel();
		}
		else{
			currentDiagramType=diagram.getType();
		}
		return currentDiagramType;
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public EditPart getTargetEditPart(Request request) {
		if (ApplyStereotypeRequest.APPLY_STEREOTYPE_REQUEST.equals(request.getType())) {
			return this;
		}

		if (request instanceof CreateViewAndElementRequest) {
			String  elementHint= ((CreateViewAndElementRequest) request).getViewAndElementDescriptor().getSemanticHint();
			DiagramExpansionsRegistry diagramExpansionRegistry = DiagramExpansionSingleton.getInstance().getDiagramExpansionRegistry();
			String diagramType= getDiagramType(getNotationView());
			String currentElementType=getNotationView().getType();
			//there is an extension for the current diagram?
			if(diagramExpansionRegistry.mapChildreen.get(diagramType)!=null){
				List<String> possibleChildreenIDs=diagramExpansionRegistry.mapChildreen.get(diagramType).parentChildrenRelation.get(currentElementType);
				//The current element has expansion, it may be compartment?
				if (possibleChildreenIDs!=null){
					//maybe the container can create it.
					if (possibleChildreenIDs.contains(elementHint)){
						return this;
					}
					//look for accepted element in compartments to find good compartment
					for (String compartmentHint : possibleChildreenIDs) {
						List<String> compartmentChildreenIDs=diagramExpansionRegistry.mapChildreen.get(diagramType).parentChildrenRelation.get(compartmentHint);
						
						if(  compartmentChildreenIDs!=null &&compartmentChildreenIDs.contains(elementHint)){
							//find the edipart that can respond to this accepted hint
							List<?> subEditParts=getChildren();
							for (Object object : subEditParts) {
								if(object instanceof IGraphicalEditPart){
									if(((IGraphicalEditPart)object).getNotationView().getType().equals(compartmentHint)){
										return ((IGraphicalEditPart)object);
									}

								}
							}
						}
					}
				}
			}
		}
		return super.getTargetEditPart(request);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void refreshVisuals() {
		super.refreshVisuals();
		refreshShadow();
		refreshFontColor();
	}

	/**
	 * Refresh the font. This method shouldn't be overriden by subclasses. To
	 * refresh labels font, the method refreshLabelsFont should be used. {@inheritDoc}
	 */
	@Override
	protected void refreshFont() {
		FontStyle style = (FontStyle) getPrimaryView().getStyle(NotationPackage.Literals.FONT_STYLE);
		if (style != null) {
			// Get the font
			FontDescriptor fontDescriptor = FontDescriptor.createFrom(getFontData(style));
			Font newFont = getResourceManager().createFont(fontDescriptor);

			refreshLabelsFont(newFont);

			// Dispose previous Font and FontDescriptor
			if (cachedFontDescriptor != null) {
				getResourceManager().destroyFont(cachedFontDescriptor);
			}
			cachedFontDescriptor = fontDescriptor;
		}
	}

	/**
	 * A method to specify the labels to be update when the font is refreshed.
	 * Subclasses should call super.refreshLabelsFont(font)
	 *
	 * @param font
	 *            the font to use
	 */
	protected void refreshLabelsFont(Font font) {
		if (((IPapyrusNodeUMLElementFigure) getPrimaryShape()).getStereotypesLabel() != null) {
			((IPapyrusNodeUMLElementFigure) getPrimaryShape()).getStereotypesLabel().setFont(font);
		}

	}

	/**
	 * Update the fontData
	 *
	 * @param style
	 *            the font style of the figure
	 * @return the new font data to use
	 */
	protected FontData getFontData(FontStyle style) {
		return new FontData(style.getFontName(), style.getFontHeight(), (style.isBold() ? SWT.BOLD : SWT.NORMAL) | (style.isItalic() ? SWT.ITALIC : SWT.NORMAL));
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void setFontColor(Color color) {
		super.setFontColor(color);
		if (((IPapyrusNodeUMLElementFigure) getPrimaryShape()).getStereotypesLabel() != null) {
			((IPapyrusNodeUMLElementFigure) getPrimaryShape()).getStereotypesLabel().setForegroundColor(color);
		}
	}

}

Back to the top