Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6db4bcc3baf8ceac637c77cbb6bf9eb5e2bd567c (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*****************************************************************************
 * 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) - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.helper;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.figures.ResizableCompartmentFigure;
import org.eclipse.gmf.runtime.diagram.ui.services.editpart.EditPartService;
import org.eclipse.gmf.runtime.draw2d.ui.figures.FigureUtilities;
import org.eclipse.gmf.runtime.emf.core.util.PackageUtil;
import org.eclipse.gmf.runtime.notation.FillStyle;
import org.eclipse.gmf.runtime.notation.FontStyle;
import org.eclipse.gmf.runtime.notation.JumpLinkStatus;
import org.eclipse.gmf.runtime.notation.JumpLinkType;
import org.eclipse.gmf.runtime.notation.Location;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.Routing;
import org.eclipse.gmf.runtime.notation.Smoothness;
import org.eclipse.gmf.runtime.notation.TitleStyle;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.papyrus.infra.emf.appearance.style.AnnotationStyleProvider;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.preferences.PreferencesConstantsHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.providers.ThemeInitializerManager;
import org.eclipse.papyrus.infra.gmfdiag.preferences.utils.GradientPreferenceConverter;
import org.eclipse.papyrus.uml.diagram.common.editparts.ILabelRoleProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;

/**
 * this is an helper that contains method use to initialize font color etc in
 * the view provider
 * 
 */
public class PreferenceInitializerForElementHelper {

	public static boolean usePreferenceInitializer(View view) {
		return ThemeInitializerManager.instance.usePreferenceInitializer(view);
	}

	public static String getpreferenceKey(View view, String elementName, int pref) {
		return PreferencesConstantsHelper.getElementConstant(view.getDiagram().getType() + "_" + elementName, pref); //$NON-NLS-1$
	}

	/**
	 * initialize the background for a graphical element
	 * 
	 * @param view
	 *        the element to initialize
	 * @param store
	 *        the preference store
	 * @param elementName
	 *        the name to the element
	 */
	public static void initBackgroundFromPrefs(View view, final IPreferenceStore store, String elementName) {
		if(!usePreferenceInitializer(view)) {
			return;
		}

		String fillColorConstant = getpreferenceKey(view, elementName, PreferencesConstantsHelper.COLOR_FILL);
		String gradientColorConstant = getpreferenceKey(view, elementName, PreferencesConstantsHelper.COLOR_GRADIENT);
		String gradientPolicyConstant = getpreferenceKey(view, elementName, PreferencesConstantsHelper.GRADIENT_POLICY);
		String shadowConstant = getpreferenceKey(view, elementName, PreferencesConstantsHelper.SHADOW);
		String elementIcon = getpreferenceKey(view, elementName, PreferencesConstantsHelper.ELEMENTICON);
		String qualifiedName = getpreferenceKey(view, elementName, PreferencesConstantsHelper.QUALIFIEDNAME);

		org.eclipse.swt.graphics.RGB fillRGB = PreferenceConverter.getColor(store, fillColorConstant);
		ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getFillStyle_FillColor(), FigureUtilities.RGBToInteger(fillRGB));

		FillStyle fillStyle = (FillStyle)view.getStyle(NotationPackage.Literals.FILL_STYLE);
		fillStyle.setFillColor(FigureUtilities.RGBToInteger(fillRGB).intValue());

		if(store.getBoolean(gradientPolicyConstant)) {
			GradientPreferenceConverter gradientPreferenceConverter = new GradientPreferenceConverter(store.getString(gradientColorConstant));
			fillStyle.setGradient(gradientPreferenceConverter.getGradientData());
			fillStyle.setTransparency(gradientPreferenceConverter.getTransparency());
		}

		EditingDomain domain = EMFHelper.resolveEditingDomain(view);
		if(domain instanceof TransactionalEditingDomain) {
			TransactionalEditingDomain editingDomain = (TransactionalEditingDomain)domain;
			// shadow
			RecordingCommand shadowcommand = AnnotationStyleProvider.getSetShadowCommand(editingDomain, view, store.getBoolean(shadowConstant));
			if(shadowcommand.canExecute()) {
				shadowcommand.execute();
			}
			// icon label
			RecordingCommand namelabelIconCommand = AnnotationStyleProvider.getSetElementIconCommand(editingDomain, view, store.getBoolean(elementIcon));
			if(namelabelIconCommand.canExecute()) {
				namelabelIconCommand.execute();
			}
			// qualified name
			if(!store.getBoolean(qualifiedName)) {
				RecordingCommand qualifiedNameCommand = AnnotationStyleProvider.getSetQualifiedNameDepthCommand(editingDomain, view, 1000);
				if(qualifiedNameCommand.canExecute()) {
					qualifiedNameCommand.execute();
				}
			}
		}
	}

	/**
	 * initialize the font for a graphical element
	 * 
	 * @param view
	 *        the element to initialize
	 * @param store
	 *        the preference store
	 * @param elementName
	 *        the name to the element
	 */
	public static void initFontStyleFromPrefs(View view, final IPreferenceStore store, String elementName) {
		if(!usePreferenceInitializer(view)) {
			return;
		}

		String fontConstant = getpreferenceKey(view, elementName, PreferencesConstantsHelper.FONT);
		String fontColorConstant = getpreferenceKey(view, elementName, PreferencesConstantsHelper.COLOR_FONT);

		FontStyle viewFontStyle = (FontStyle)view.getStyle(NotationPackage.Literals.FONT_STYLE);
		if(viewFontStyle != null) {
			FontData fontData = PreferenceConverter.getFontData(store, fontConstant);
			viewFontStyle.setFontName(fontData.getName());
			viewFontStyle.setFontHeight(fontData.getHeight());
			viewFontStyle.setBold((fontData.getStyle() & SWT.BOLD) != 0);
			viewFontStyle.setItalic((fontData.getStyle() & SWT.ITALIC) != 0);

			org.eclipse.swt.graphics.RGB fontRGB = PreferenceConverter.getColor(store, fontColorConstant);
			viewFontStyle.setFontColor(FigureUtilities.RGBToInteger(fontRGB).intValue());
		}
	}

	/**
	 * initialize the foreground for a graphical element
	 * 
	 * @param view
	 *        the element to initialize
	 * @param store
	 *        the preference store
	 * @param elementName
	 *        the name to the element
	 */
	public static void initForegroundFromPrefs(View view, final IPreferenceStore store, String elementName) {
		if(!usePreferenceInitializer(view)) {
			return;
		}

		String lineColorConstant = getpreferenceKey(view, elementName, PreferencesConstantsHelper.COLOR_LINE);
		org.eclipse.swt.graphics.RGB lineRGB = PreferenceConverter.getColor(store, lineColorConstant);
		ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLineStyle_LineColor(), FigureUtilities.RGBToInteger(lineRGB));
	}

	/**
	 * initialize the routing for a graphical element
	 * 
	 * @param view
	 *        the element to initialize
	 * @param store
	 *        the preference store
	 * @param elementName
	 *        the name to the element
	 * 
	 * @deprecated call {@link PreferenceInitializerForElementHelper#initRoutingFromPrefs(View, IPreferenceStore, String)} instead.
	 */
	@Deprecated
	public static void initRountingFromPrefs(View view, final IPreferenceStore store, String elementName) {
		if(!usePreferenceInitializer(view)) {
			return;
		}
		initRoutingFromPrefs(view, store, elementName);
	}

	/**
	 * initialize the routing for a graphical element
	 * 
	 * @param view
	 *        the element to initialize
	 * @param store
	 *        the preference store
	 * @param elementName
	 *        the name to the element
	 */
	public static void initRoutingFromPrefs(View view, final IPreferenceStore store, String elementName) {
		if(!usePreferenceInitializer(view)) {
			return;
		}

		Routing routing = Routing.get(store.getInt(getpreferenceKey(view, elementName, PreferencesConstantsHelper.ROUTING_STYLE)));
		if(routing != null) {
			ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getRoutingStyle_Routing(), routing);
		}
		Smoothness smoothness = Smoothness.get(store.getInt(getpreferenceKey(view, elementName, PreferencesConstantsHelper.SMOOTHNESS)));
		if(routing != null) {
			ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getRoutingStyle_Smoothness(), smoothness);
		}
		JumpLinkStatus jumpLinkStatus = JumpLinkStatus.get(getpreferenceKey(view, elementName, PreferencesConstantsHelper.JUMPLINK_STATUS));
		if(routing != null) {
			ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getRoutingStyle_JumpLinkStatus(), jumpLinkStatus);
		}

		JumpLinkType jumpLinkType = JumpLinkType.get(getpreferenceKey(view, elementName, PreferencesConstantsHelper.JUMPLINK_TYPE));
		if(routing != null) {
			ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getRoutingStyle_JumpLinkType(), jumpLinkType);
		}
		boolean jumpLinksReverse = store.getBoolean(getpreferenceKey(view, elementName, PreferencesConstantsHelper.JUMPLINK_REVERSE));
		ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getRoutingStyle_JumpLinksReverse(), jumpLinksReverse);

		boolean routingObstruction = store.getBoolean(getpreferenceKey(view, elementName, PreferencesConstantsHelper.ROUTING_POLICY_OBSTRUCTION));
		ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getRoutingStyle_AvoidObstructions(), routingObstruction);

		boolean routingDistance = store.getBoolean(getpreferenceKey(view, elementName, PreferencesConstantsHelper.ROUTING_POLICY_DISTANCE));
		ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getRoutingStyle_ClosestDistance(), routingDistance);
	}

	/**
	 * initialize the status of the compartment for the node (Showed or hidden)
	 * 
	 * @param view
	 *        the element to initialize
	 * @param store
	 *        the preference store
	 * @param elementName
	 *        the name to the element
	 */
	public static void initCompartmentsStatusFromPrefs(View view, final IPreferenceStore store, String elementName) {
		if(!ThemeInitializerManager.instance.usePreferenceInitializer(view)) {
			return;
		}

		EList<?> children = view.getPersistedChildren();
		if(children != null) {
			for(Object object : children) {

				// we look for the name of the compartment for this view
				EditPart dummyEP = EditPartService.getInstance().createGraphicEditPart((View)object);
				IGraphicalEditPart epp = (IGraphicalEditPart)dummyEP;
				IFigure fig1 = epp.getFigure();

				if(fig1 instanceof ResizableCompartmentFigure) {
					String compartmentName = ((ResizableCompartmentFigure)fig1).getCompartmentTitle();
					if(compartmentName != null) {
						String diagramKind = view.getDiagram().getType();
						String preferenceKey = PreferencesConstantsHelper.getCompartmentElementConstant(diagramKind + "_" + elementName, compartmentName, PreferencesConstantsHelper.COMPARTMENT_VISIBILITY); //$NON-NLS-1$
						boolean value = store.getBoolean(preferenceKey);

						if(!value) {// the default value is true : nothing to
									// do
							ENamedElement namedElement = PackageUtil.getElement("notation.View.visible"); //$NON-NLS-1$
							ViewUtil.setStructuralFeatureValue((View)object, (EStructuralFeature)namedElement, value);
						}

						String compartmentNameVisibilityPreference = PreferencesConstantsHelper.getCompartmentElementConstant(diagramKind + "_" + elementName, compartmentName, PreferencesConstantsHelper.COMPARTMENT_NAME_VISIBILITY); //$NON-NLS-1$
						boolean showCompartmentName = store.getBoolean(compartmentNameVisibilityPreference);
						View childView = (View)object;
						TitleStyle style = (TitleStyle)childView.getStyle(NotationPackage.eINSTANCE.getTitleStyle());
						if(style != null) {
							style.setShowTitle(showCompartmentName);
						}
					}
				}
				dummyEP = null;
			}
		}
	}

	/**
	 * initialize the status of each label for the node or for the link (Showed
	 * or hidden)
	 * 
	 * @param view
	 *        the element to initialize
	 * @param store
	 *        the preference store
	 * @param elementName
	 *        the name to the element
	 */
	public static void initLabelVisibilityFromPrefs(View view, final IPreferenceStore store, String elementName) {
		if(!ThemeInitializerManager.instance.usePreferenceInitializer(view)) {
			return;
		}

		EList<?> children = view.getPersistedChildren();
		if(children != null) {
			for(Object object : children) {

				// we look for the name of the label for this view
				EditPart dummyEP = EditPartService.getInstance().createGraphicEditPart((View)object);
				if(dummyEP instanceof ILabelRoleProvider) {
					String role = ((ILabelRoleProvider)dummyEP).getLabelRole();
					String diagramKind = view.getDiagram().getType();
					String key = PreferencesConstantsHelper.getLabelElementConstant(diagramKind + "_" + elementName, role, PreferencesConstantsHelper.LABEL_VISIBILITY); //$NON-NLS-1$
					((View)object).setVisible(store.getBoolean(key));
				}
				dummyEP = null;
			}
		}
	}

	/**
	 * initialize node dimension.
	 * 
	 * @param view
	 *        the element to initialize
	 * @param store
	 *        the preference store
	 * @param elementName
	 *        the name to the element
	 */
	public static Dimension getDimensionFromPref(View view, final IPreferenceStore store, String elementName) {
		Dimension dim = new Dimension();
		String width = getpreferenceKey(view, elementName, PreferencesConstantsHelper.WIDTH);
		String height = getpreferenceKey(view, elementName, PreferencesConstantsHelper.HEIGHT);
		dim = new Dimension(store.getInt(width), store.getInt(height));
		return dim;
	}

	/**
	 * initialize label location.
	 * 
	 * @param view
	 *        the element to initialize
	 * @param store
	 *        the preference store
	 * @param elementName
	 *        the name to the element
	 */
	public static void initLabelLocationFromPrefs(View view, final IPreferenceStore store, String elementName) {
		assert (view instanceof Node);

		Node label = (Node)view;
		Location location = (Location)label.getLayoutConstraint();

		String xKey = getpreferenceKey(view, elementName, PreferencesConstantsHelper.LOCATION_X);
		String yKey = getpreferenceKey(view, elementName, PreferencesConstantsHelper.LOCATION_Y);

		location.setX(store.getInt(xKey));
		location.setY(store.getInt(yKey));

		label.setLayoutConstraint(location);
	}
}

Back to the top