Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e1747112cc7842f7eda6a4fad26812bebc6490c5 (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*****************************************************************************
 * Copyright (c) 2008, 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:
 *  Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 410346
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.provider.EcoreItemProviderAdapterFactory;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory;
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.papyrus.infra.core.log.LogHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.OverlayVisibilityIcon;
import org.eclipse.papyrus.uml.diagram.common.providers.AlternativeUMLItemProviderAdapterFactory;
import org.eclipse.papyrus.uml.diagram.common.util.ColorManager;
import org.eclipse.papyrus.uml.diagram.common.util.FontManager;
import org.eclipse.papyrus.uml.tools.utils.ElementUtil;
import org.eclipse.papyrus.uml.tools.utils.ImageUtil;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.VisibilityKind;
import org.eclipse.uml2.uml.edit.providers.UMLItemProviderAdapterFactory;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	/** The Constant ID. */
	public static final String ID = "org.eclipse.papyrus.uml.diagram.common";

	/** Shared instance of the plug-in (singleton pattern) */
	private static Activator plugin;

	/** Logging helper */
	public static LogHelper log;

	/** The Constant UML_VIS_ICONS_16x16. */
	public static final String UML_VIS_ICONS_16x16 = "icons/obj16/";

	/** Default image. */
	public static final String DEFAULT_IMAGE = "icons/papyrus/PapyrusLogo16x16.gif";

	/** Color manager. */
	protected static ColorManager colorManager = /* new ColorManager(); */null;

	/** Font manager. */
	protected static FontManager fontManager = /* new FontManager(); */null;

	/** left Stereotype delimiters ('Guillemets francais'). */
	public static final String ST_LEFT = String.valueOf("\u00AB"); //$NON-NLS-1$

	/** Right Stereotype delimiters ('Guillemets francais'). */
	public static final String ST_RIGHT = String.valueOf("\u00BB"); //$NON-NLS-1$

	/**
	 * The constructor
	 */
	public Activator() {
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

	/**
	 * Get an image for a ENamedElement in plug-in image registry.
	 *
	 * @param element
	 *            the element.
	 * @return the image.
	 */
	public Image getImage(ENamedElement element) {

		String imageKey = element.getName();

		Image image = getImageRegistry().get(imageKey);
		if (image == null) {
			ImageDescriptor imageDescriptor = getProvidedImageDescriptor(element);
			if (imageDescriptor == null) {
				imageDescriptor = ImageDescriptor.getMissingImageDescriptor();
			}
			getImageRegistry().put(imageKey, imageDescriptor);
			image = getImageRegistry().get(imageKey);
		}
		return image;
	}
	
	private ImageDescriptor getProvidedImageDescriptor(ENamedElement element) {
		if (element instanceof EStructuralFeature) {
			EStructuralFeature feature = ((EStructuralFeature) element);
			EClass eContainingClass = feature.getEContainingClass();
			EClassifier eType = feature.getEType();
			if (eContainingClass != null && !eContainingClass.isAbstract()) {
				element = eContainingClass;
			} else if (eType instanceof EClass && !((EClass) eType).isAbstract()) {
				element = eType;
			}
		}
		if (element instanceof EClass) {
			EClass eClass = (EClass) element;
			if (!eClass.isAbstract()) {
				return getItemImageDescriptor(eClass.getEPackage().getEFactoryInstance().create(eClass));
			}
		}
		// TODO : support structural features
		return null;
	}
	/**
	 * Get an image descriptor for current item.
	 *
	 * @param item
	 *            the item for which an image descriptor searched.
	 * @return the image descriptor.
	 */
	public ImageDescriptor getItemImageDescriptor(Object item) {
		IItemLabelProvider labelProvider = (IItemLabelProvider) getItemProvidersAdapterFactory().adapt(item, IItemLabelProvider.class);
		if (labelProvider != null) {
			return ExtendedImageRegistry.getInstance().getImageDescriptor(labelProvider.getImage(item));
		}
		return null;
	}
	/**
	 * Return the color manager. Initialize it if required.
	 *
	 * @return
	 */
	public static ColorManager getColorManager() {
		if (colorManager == null) {
			colorManager = new ColorManager();
		}
		return colorManager;
	}

	/**
	 * Return the font managet. Initialize it if required.
	 *
	 * @return
	 */
	public static FontManager getFontManager() {
		if (fontManager == null) {
			fontManager = new FontManager();
		}
		return fontManager;
	}

	/**
	 * Returns an image descriptor for the image file at the given plug-in
	 * relative path.
	 *
	 * @param path
	 *            the path for the image file
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return AbstractUIPlugin.imageDescriptorFromPlugin(ID, path);
	}

	public static Image getImage(String key) {
		return getImage(key, "");
	}

	/**
	 * Returns an <code>org.eclipse.swt.graphics.Image</code> identified by its
	 * key.<BR>
	 * By default, it returns a default image. This image is the image placed in
	 * the directory <em>resources/icons/default.gif</em>
	 *
	 * @param key
	 *            the key of the image
	 * @param visKey
	 *            must not be null : "" || "public" || "private" ...
	 * @return the Image
	 */
	public static Image getImage(String key, String visKey) {

		String image_id = key;
		if (!"".equals(visKey)) {
			image_id = image_id + "::" + visKey;
		}

		ImageRegistry registry = getDefault().getImageRegistry();
		Image image = registry.get(image_id);

		if (image == null) { // Image not yet in registry

			// Get the descriptor of the image without visibility
			ImageDescriptor desc = AbstractUIPlugin.imageDescriptorFromPlugin(ID, key);

			if (!"".equals(visKey)) { // Add visibility overlay

				Image initialImage = desc.createImage();
				getDefault();
				ImageDescriptor visDesc = Activator.getImageDescriptor(UML_VIS_ICONS_16x16 + "VisibilityKind_" + visKey + ".gif");

				// Overlay custom image over base image
				OverlayVisibilityIcon overlayIcon = new OverlayVisibilityIcon(initialImage, visDesc);
				image = overlayIcon.getImage();

				registry.put(image_id, image);

			} else {
				registry.put(key, desc);
			}

			image = registry.get(image_id);

		}

		if ((image == null) && !image_id.equals(DEFAULT_IMAGE)) {
			image = getImage(DEFAULT_IMAGE);
		}

		return image;
	}

	/**
	 * This method returns an <code>org.eclipse.swt.graphics.Image</code> identified by its pluginId and iconPath.<BR>
	 *
	 * By default, it returns a default image. This image is the image placed in
	 * the directory <em>resources/icons/default.gif</em>
	 *
	 * @param pluginId
	 *            id of plugin
	 * @param iconpPath
	 *            the path of the icon image relative to the plugin
	 * @return the Image
	 */
	public static Image getPluginIconImage(String pluginId, String iconPath) {
		String key = pluginId + iconPath;
		ImageRegistry registry = getDefault().getImageRegistry();
		Image image = registry.get(key);

		if (image == null) {

			ImageDescriptor desc = AbstractUIPlugin.imageDescriptorFromPlugin(pluginId, iconPath);

			registry.put(key, desc);
			image = registry.get(key);

		}
		if ((image == null) && !iconPath.equals(DEFAULT_IMAGE)) {
			image = getImage(DEFAULT_IMAGE);
		}

		return image;
	}

	/**
	 * This method returns an <code>org.eclipse.swt.graphics.Image</code> identified by its pluginId and image Descriptor.<BR>
	 *
	 * By default, it returns a default image. This image is the image placed in
	 * the directory <em>resources/icons/default.gif</em>
	 *
	 * @param pluginId
	 *            id of plugin
	 * @param descriptor
	 *            the image descriptor of the image
	 * @return the Image
	 */
	public static Image getPluginIconImage(String pluginId, ImageDescriptor descriptor) {
		String key = pluginId + descriptor;
		ImageRegistry registry = getDefault().getImageRegistry();
		Image image = registry.get(key);

		if (image == null) {

			registry.put(key, descriptor);
			image = registry.get(key);

		}
		return image;
	}

	/**
	 * this method returns the shape image that represents the first applied
	 * stereotype.
	 *
	 * @param elt
	 *            the stereotyped element
	 * @return {@link image} of the shape
	 */
	public static Image getShapeElement(Element elt) {
		// getStereotypeImage can return null
		org.eclipse.uml2.uml.Image shape = ElementUtil.getStereotypeImage(elt, "shape");
		if (shape != null) {
			return getImageInRegistry(shape, null);
		} else {
			return null;
		}
	}

	/**
	 * this method returns the icon image that represents the first applied
	 * stereotype on the element.
	 *
	 * @param element
	 *            The element for which we will retrieve the stereotype icon
	 * @return
	 *         The element's first applied stereotype's Image
	 */
	public static Image getIconElement(Element element) {
		return getIconElement(element, false);
	}

	/**
	 * this method returns the icon image that represents the first applied
	 * stereotype.
	 *
	 * @param element
	 *            the stereotyped element
	 * @return {@link image} of the icon
	 */
	public static Image getIconElement(Element element, boolean withVisibilityDecorator) {

		List<Stereotype> stereotypeList = element.getAppliedStereotypes();
		if (stereotypeList == null || stereotypeList.isEmpty()) {
			return null;
		}
		return getIconElement(element, stereotypeList.get(0), withVisibilityDecorator);
	}

	/**
	 * this method returns the collection of icon images that represents the
	 * applied stereotypes.
	 *
	 * @param elt
	 *            the stereotyped element
	 * @param stereotypes
	 *            the collection of stereotypes which icon has to be displayed
	 * @param kind
	 *            the kind of display "icon" or "shape"
	 * @return {@link image} of the icon
	 */
	public static Collection<Image> getIconElements(Element elt, Collection<Stereotype> stereotypes, boolean withVisibilityDecorator) {
		Collection<Image> images = new ArrayList<Image>();
		VisibilityKind vis = null;
		if ((elt instanceof NamedElement) && (withVisibilityDecorator)) {
			vis = ((NamedElement) elt).getVisibility();
		}

		// look in each stereotype and get the image for each of them
		for (Stereotype stereotype : stereotypes) {
			// getStereotypeImage can return null
			org.eclipse.uml2.uml.Image icon = ElementUtil.getStereotypeImage(elt, stereotype, "icon");
			if (icon != null) {
				images.add(getImageInRegistry(icon, vis));
			}
		}
		return images;
	}

	/**
	 * this method returns the icon image that represents the first applied
	 * stereotype.
	 *
	 * @param elt
	 *            the stereotyped element
	 * @param stereotype
	 *            the stereotype which icon has to be displayed
	 * @param kind
	 *            the kind of display "icon" or "shape"
	 * @return {@link image} of the icon
	 */
	public static Image getIconElement(Element elt, Stereotype stereotype, boolean withVisibilityDecorator) {

		VisibilityKind vis = null;
		if ((elt instanceof NamedElement) && (withVisibilityDecorator)) {

			vis = ((NamedElement) elt).getVisibility();
		}

		// getStereotypeImage can return null
		org.eclipse.uml2.uml.Image icon = ElementUtil.getStereotypeImage(elt, stereotype, "icon");
		if (icon != null) {
			return getImageInRegistry(icon, vis);
		} else {
			return null;
		}
	}

	/** get the imageassociated to a image uml element **/
	public static Image getShape(Element elt, Stereotype stereotype, boolean withVisibilityDecorator) {

		VisibilityKind vis = null;
		if ((elt instanceof NamedElement) && (withVisibilityDecorator)) {

			vis = ((NamedElement) elt).getVisibility();
		}

		// getStereotypeImage can return null
		org.eclipse.uml2.uml.Image icon = ElementUtil.getStereotypeImage(elt, stereotype, "shape");
		if (icon != null) {
			return getImageInRegistry(icon, vis);
		} else {
			return null;
		}
	}

	/**
	 * Find the image (SWT) in registry Store image in registry if it is not
	 * found
	 *
	 * @param umlImage
	 *            to retrieve as SWT Image in registry
	 * @return the stored SWT image
	 */
	public static Image getImageInRegistry(org.eclipse.uml2.uml.Image umlImage, VisibilityKind visibility) {
		// Retrieve registry
		ImageRegistry papyrusRegistry = getDefault().getImageRegistry();

		// Get image id for registry
		String image_id = ImageUtil.getImageId(umlImage);
		if (visibility != null) {
			image_id = image_id + "_" + visibility.getLiteral();
		}

		// Get SWT image for shape in the registry
		Image image = papyrusRegistry.get(image_id);

		// If image was not found in registry,
		// try to find an image and to update registry
		if (image == null) {

			try {
				// Try to retrieve image from UML Image content property
				image = ImageUtil.getContent(umlImage);
			} catch (Exception e) {
				// Activator.log.error(e);
			}

			// If no image was found in Content
			// Try to retrieve image from UML Image location property
			if (image == null) {
				image = ImageUtil.getImageFromLocation(umlImage);
			}

			// Add visibility decorator
			if (visibility != null) {

				getDefault();
				ImageDescriptor visDesc = Activator.getImageDescriptor(UML_VIS_ICONS_16x16 + "VisibilityKind_" + visibility.getLiteral() + ".gif");

				// Overlay custom image over base image
				OverlayVisibilityIcon overlayIcon = new OverlayVisibilityIcon(image, visDesc);
				image = overlayIcon.getImage();
			}

			if (image != null) {
				// Store image in registry
				ImageData imdata = image.getImageData();
				papyrusRegistry.put(image_id, ImageDescriptor.createFromImageData(imdata));
				image = papyrusRegistry.get(image_id);
			}
		}

		return image;
	}

	/** The Constant DIAGRAM_PREFERENCES_HINT. */
	public static final PreferencesHint DIAGRAM_PREFERENCES_HINT = new PreferencesHint(ID);

	/** The adapter factory. */
	private ComposedAdapterFactory adapterFactory;

	/**
	 * Start.
	 *
	 * @param context
	 *            the context
	 *
	 * @throws Exception
	 *             the exception
	 *
	 * @generated
	 */
	@Override
	public void start(BundleContext context) throws Exception {
		super.start(context);
		PreferencesHint.registerPreferenceStore(DIAGRAM_PREFERENCES_HINT, getPreferenceStore());
		plugin = this;
		log = new LogHelper(plugin);
	}

	/**
	 * Stop.
	 *
	 * @param context
	 *            the context
	 *
	 * @throws Exception
	 *             the exception
	 *
	 * @generated
	 */
	@Override
	public void stop(BundleContext context) throws Exception {
		super.stop(context);
		log = null;
		plugin = null;
	}

	/**
	 * Log error.
	 *
	 * @param error
	 *            the error
	 */
	public void logError(String error) {
		logError(error, null);
	}

	/**
	 * Log error.
	 *
	 * @param throwable
	 *            actual error or null could be passed
	 * @param error
	 *            the error
	 */
	public void logError(String error, Throwable throwable) {
		if (error == null && throwable != null) {
			error = throwable.getMessage();
		}
		getLog().log(new Status(IStatus.ERROR, Activator.ID, IStatus.OK, error, throwable));
		debug(error, throwable);
	}

	/**
	 * Log info.
	 *
	 * @param message
	 *            the message
	 */
	public void logInfo(String message) {
		logInfo(message, null);
	}

	/**
	 * Log info.
	 *
	 * @param throwable
	 *            actual error or null could be passed
	 * @param message
	 *            the message
	 */
	public void logInfo(String message, Throwable throwable) {
		if (message == null && throwable != null) {
			message = throwable.getMessage();
		}
		getLog().log(new Status(IStatus.INFO, Activator.ID, IStatus.OK, message, throwable));
		debug(message, throwable);
	}

	public void logWarning(String warning, Throwable throwable) {
		if (warning == null && throwable != null) {
			warning = throwable.getMessage();
		}
		getLog().log(new Status(IStatus.WARNING, ID, IStatus.OK, warning, throwable));
	}

	/**
	 * Debug.
	 *
	 * @param message
	 *            the message
	 * @param throwable
	 *            the throwable
	 */
	private void debug(String message, Throwable throwable) {
		if (!isDebugging()) {
			return;
		}
		if (message != null) {
			System.err.println(message);
		}
		if (throwable != null) {
			throwable.printStackTrace();
		}
	}

	/**
	 * Creates the adapter factory.
	 *
	 * @return the composed adapter factory
	 */
	protected ComposedAdapterFactory createAdapterFactory() {
		List<AdapterFactory> factories = new ArrayList<AdapterFactory>();
		fillItemProviderFactories(factories);
		return new ComposedAdapterFactory(factories);
	}

	/**
	 * Use alternative UMLItemProviderAdapterFactory insteadof
	 * UMLItemProviderAdapterFactory.
	 *
	 * @param factories
	 *            the factories
	 *
	 * @see org.eclipse.emf.edit.provider.ComposedAdapterFactory#getFactoryForTypes(java.util.Collection)
	 */
	protected void fillItemProviderFactories(List<AdapterFactory> factories) {
		// custom icons for diagram elements
		factories.add(new AlternativeUMLItemProviderAdapterFactory(getPreferenceStore()));

		factories.add(new UMLItemProviderAdapterFactory());
		factories.add(new EcoreItemProviderAdapterFactory());
		factories.add(new ResourceItemProviderAdapterFactory());
		factories.add(new ReflectiveItemProviderAdapterFactory());
	}

	/**
	 * Gets the item providers adapter factory.
	 *
	 * @return the item providers adapter factory
	 */
	public AdapterFactory getItemProvidersAdapterFactory() {
		if (adapterFactory == null) {
			adapterFactory = createAdapterFactory();
		}
		return adapterFactory;
	}
}

Back to the top