Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 232c67c59999f6a9b909d1fbc682d060c3724d2e (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
/**
 *  Copyright (c) 2011, 2012 Mia-Software.
 *
 *  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:
 *  	Gregoire Dupe (Mia-Software) - Bug 361794 - [Restructuring] EMF Facet customization meta-model
 *      Gregoire Dupe (Mia-Software) - Bug 369987 - [Restructuring][Table] Switch to the new customization and facet framework
 *      Nicolas Bros (Mia-Software) - Bug 379683 - customizable Tree content provider
 *      Gregoire Dupe (Mia-Software) - Bug 424122 - [Table] Images, fonts and colors are not shared between the instances of table
 */
package org.eclipse.papyrus.emf.facet.custom.ui.internal.query;

import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.ETypedElement;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.ComposedImage;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
import org.eclipse.papyrus.emf.facet.custom.ui.internal.Activator;
import org.eclipse.papyrus.emf.facet.custom.ui.internal.ImageProvider;
import org.eclipse.papyrus.emf.facet.custom.ui.internal.custompt.ImageWrapper;
import org.eclipse.papyrus.emf.facet.efacet.core.IFacetManager;
import org.eclipse.papyrus.emf.facet.efacet.core.exception.DerivedTypedElementException;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetAttribute;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetReference;
import org.eclipse.papyrus.emf.facet.query.java.core.IJavaQuery2;
import org.eclipse.papyrus.emf.facet.query.java.core.IParameterValueList2;
import org.eclipse.papyrus.emf.facet.util.swt.imageprovider.IImageProvider;
import org.eclipse.papyrus.emf.facet.util.swt.imageprovider.IImageProviderFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.papyrus.emf.facet.custom.metamodel.custompt.IImage;
import org.eclipse.swt.graphics.Image;

public class ImageQuery implements IJavaQuery2<EObject, IImage> {

	public IImage evaluate(final EObject source,
			final IParameterValueList2 parameterValues,
			final IFacetManager facetManager)
			throws DerivedTypedElementException {
		IImage result = null;
		ETypedElement sfParam = null;
		if (parameterValues != null) {
			sfParam = (ETypedElement) parameterValues
					.getParameterValueByName("eStructuralFeature").getValue(); //$NON-NLS-1$
		}
		if (sfParam == null) {
			result = getEObjectImage(source);
		} else {
			result = getLinkImage(sfParam);
		}
		return result;
	}

	public static IImage getEObjectImage(final EObject source) {
		IImage result = null;
		final ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(
				ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
		final IItemLabelProvider itemLabelProvider = (IItemLabelProvider) adapterFactory
				.adapt(source, IItemLabelProvider.class);
		if (itemLabelProvider != null) {
			Object imageObject = itemLabelProvider.getImage(source);
			Image image = ExtendedImageRegistry.getInstance().getImage(imageObject);
			result = new ImageWrapper(image);
////			final ImageDescriptor imgDescriptor = ExtendedImageRegistry
////					.getInstance().getImageDescriptor(source);
//			final IImageProvider imgProvider = IImageProviderFactory.DEFAULT
//					.createIImageProvider(Activator.getDefault());
//			if (imgDescriptor != null) {
//				final Image image = imgProvider.getImage(imgDescriptor);
//				result = new ImageWrapper(image);
//			}
		}
		return result;
	}

	/**
	 * Returns the image for an attribute or reference link.
	 *
	 * @param sfParam
	 *            the attribute or reference
	 * @param source
	 *            the EObject under which the attribute or reference appears
	 * @return the image
	 */
	private static IImage getLinkImage(final ETypedElement sfParam) {
		Image image = null;
		if (sfParam instanceof FacetReference) {
			image = ImageProvider.getInstance().getFacetLinkIcon();
		} else if (sfParam instanceof EReference) {
			image = getReferenceImage((EReference) sfParam);
		} else if (sfParam instanceof FacetAttribute) {
			image = ImageProvider.getInstance().getFacetAttributeIcon();
		} else if (sfParam instanceof EAttribute) {
			image = getAttributeImage();
		} else {
			image = ImageProvider.getInstance().getFeatureIcon();
		}
		return new ImageWrapper(image);
	}

	private static Image getReferenceImage(final EReference reference) {
		Image result;
		final EReference opposite = reference.getEOpposite();
		if (reference.isContainment()) {
			if (opposite == null) {
				result = ImageProvider.getInstance().getUnidirectionalAggregIcon();
			} else {
				result = ImageProvider.getInstance().getAggregIcon();
			}
		} else {
			if (opposite != null && opposite.isContainment()) {
				result = ImageProvider.getInstance().getInvAggregIcon();
			} else if (opposite == null) {
				result = ImageProvider.getInstance().getUnidirectionalLinkIcon();
			} else {
				result = ImageProvider.getInstance().getLinkIcon();
			}
		}
		return result;
	}

	private static Image getAttributeImage() {
		return ImageProvider.getInstance().getAttributeIcon();
	}
}

Back to the top