Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9570181e0bb819cd201fffb2dbcf8a8d0ed414b6 (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
/*****************************************************************************
 * Copyright (c) 2011 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:
 *	Amine EL KOUHEN (CEA LIST/LIFL) - Amine.Elkouhen@cea.fr 
 *****************************************************************************/
package org.eclipse.papyrus.core.ui.pagebookview;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.DecorationOverlayIcon;
import org.eclipse.jface.viewers.IDecoration;
import org.eclipse.papyrus.decoration.util.Decoration.PreferedPosition;
import org.eclipse.papyrus.modelexplorer.Activator;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;


// TODO: Auto-generated Javadoc
/**
 * The Class ModelExplorerDecorationAdapter.
 */
public class ModelExplorerDecorationAdapter {

	/** The decorator target. */
	protected Image decoratorTarget;

	/** The decoration. */
	protected ImageDescriptor decoration;

	/** The decoration position. */
	protected int decorationPosition;

	/** point corresponding to the size 16x16. */
	private final Point size16 = new Point(16, 16);


	/**
	 * Instantiates a new model explorer decoration adapter.
	 *
	 * @param baseImage the base image
	 */
	public ModelExplorerDecorationAdapter(Image baseImage) {
		this.decoratorTarget = baseImage;
	}

	/**
	 * Gets the decorated image.
	 *
	 * @return the decorated image
	 */
	public Image getDecoratedImage() {

		if(getDecoration() == null && getDecoratorTarget() != null)
			return getDecoratorTarget();

		if(getDecoration() == null && getDecoratorTarget() == null)
			return null;

		return getDecoratedImage(getDecoratorTarget(), getDecoration(), getDecorationPosition());
	}

	/**
	 * Gets the decoration.
	 *
	 * @return the decoration
	 */
	public ImageDescriptor getDecoration() {
		return this.decoration;
	}

	/**
	 * Gets the decorator target.
	 *
	 * @return the decorator target
	 */
	public Image getDecoratorTarget() {
		return this.decoratorTarget;
	}

	/**
	 * Sets the decoration.
	 *
	 * @param decoration the decoration
	 * @param decorationPosition the decoration position
	 */
	public void setDecoration(ImageDescriptor decoration, int decorationPosition) {
		setDecoratedImage(getDecoratorTarget(), decoration, decorationPosition);
		this.decoration = decoration;
		this.decorationPosition = decorationPosition;

	}

	/**
	 * Sets the decoration.
	 *
	 * @param decoration the new decoration
	 */
	public void setDecoration(ImageDescriptor decoration) {
		setDecoratedImage(getDecoratorTarget(), decoration, getDecorationPosition());
		this.decoration = decoration;
	}

	/**
	 * Sets the decorator target.
	 *
	 * @param decoratorTarget the new decorator target
	 */
	public void setDecoratorTarget(Image decoratorTarget) {
		this.decoratorTarget = decoratorTarget;
	}


	/**
	 * Gets the decoration position.
	 *
	 * @return the decoration position
	 */
	public int getDecorationPosition() {
		//0 for TOP_LEFT, 1 for TOP_RIGHT, 2 for BOTTOM_LEFT, 3 for BOTTOM_RIGHT (Default), 4 for an UNDERLAY
		return decorationPosition;
	}


	/**
	 * Sets the decoration position.
	 *
	 * @param decorationPosition the new decoration position
	 */
	public void setDecorationPosition(int decorationPosition) {
		this.decorationPosition = decorationPosition;
	}

	/**
	 * Sets the decorated image.
	 *
	 * @param baseImage the base image
	 * @param decoration the decoration
	 * @param decorationPosition the decoration position
	 */
	public void setDecoratedImage(Image baseImage, ImageDescriptor decoration, int decorationPosition) {

		if(decoration == null || baseImage == null)
			return;

		DecorationOverlayIcon decoratedImage = null;

		// Construct a new image identifier
		String decoratedImageId = baseImage.toString().concat(decoration.toString() + decorationPosition);

		// Return the stored image if we have one
		if(Activator.getDefault().getImageRegistry().get(decoratedImageId) == null) {
			// Otherwise create a new image and store it
			switch(decorationPosition) {

			case IDecoration.TOP_LEFT:
				decoratedImage = new DecorationOverlayIcon(baseImage, new ImageDescriptor[]{ decoration, null, null, null, null }, size16);
				break;
			case IDecoration.TOP_RIGHT:
				decoratedImage = new DecorationOverlayIcon(baseImage, new ImageDescriptor[]{ null, decoration, null, null, null }, size16);
				break;
			case IDecoration.BOTTOM_LEFT:
				decoratedImage = new DecorationOverlayIcon(baseImage, new ImageDescriptor[]{ null, null, decoration, null, null }, size16);
				break;
			case IDecoration.BOTTOM_RIGHT:
				decoratedImage = new DecorationOverlayIcon(baseImage, new ImageDescriptor[]{ null, null, null, decoration, null }, size16);
				break;
			case IDecoration.UNDERLAY:
				decoratedImage = new DecorationOverlayIcon(baseImage, new ImageDescriptor[]{ null, null, null, null, decoration }, size16);
				break;
			default:
				decoratedImage = new DecorationOverlayIcon(baseImage, new ImageDescriptor[]{ null, null, decoration, null, null }, size16);
				break;
			}
			Activator.getDefault().getImageRegistry().put(decoratedImageId, decoratedImage);
		}

	}

	/**
	 * Gets the decorated image.
	 *
	 * @param baseImage the base image
	 * @param decoration the decoration
	 * @param decorationPosition the decoration position
	 * @return the decorated image
	 */
	public Image getDecoratedImage(Image baseImage, ImageDescriptor decoration, int decorationPosition) {
		// Construct a new image identifier
		String decoratedImageId = baseImage.toString().concat(decoration.toString() + decorationPosition);

		// Return the stored image if we have one
		if(Activator.getDefault().getImageRegistry().get(decoratedImageId) == null) {
			setDecoratedImage(baseImage, decoration, decorationPosition);
		}
		return Activator.getDefault().getImageRegistry().get(decoratedImageId);
	}

	/**
	 * Sets the decorated image.
	 *
	 * @param baseImage the base image
	 * @param decorationArray the decoration array
	 * @param imageSize the image size
	 */
	public void setDecoratedImage(Image baseImage, ImageDescriptor[] decorationArray, Point imageSize) {
		DecorationOverlayIcon decoratedImage = null;

		// Construct a new image identifier
		String decoratedImageId = baseImage.toString().concat(decorationArray.toString());

		// Return the stored image if we have one
		if(Activator.getDefault().getImageRegistry().get(decoratedImageId) == null) {
			// Otherwise create a new image and store it
			decoratedImage = new DecorationOverlayIcon(baseImage, decorationArray, imageSize);

			Activator.getDefault().getImageRegistry().put(decoratedImageId, decoratedImage);
		}
	}

	/**
	 * Sets the decoration.
	 *
	 * @param decoration the decoration
	 * @param position the position
	 */
	public void setDecoration(ImageDescriptor decoration, PreferedPosition position) {
		switch(position) {

		case NORTH_WEST:
			setDecoration(decoration, IDecoration.TOP_LEFT);
			break;
		case NORTH:
			setDecoration(decoration, IDecoration.TOP_RIGHT);
			break;
		case NORTH_EAST:
			setDecoration(decoration, IDecoration.TOP_RIGHT);
			break;
		case EAST:
			setDecoration(decoration, IDecoration.BOTTOM_RIGHT);
			break;
		case SOUTH_EAST:
			setDecoration(decoration, IDecoration.BOTTOM_RIGHT);
			break;
		case SOUTH:
			setDecoration(decoration, IDecoration.BOTTOM_LEFT);
			break;
		case SOUTH_WEST:
			setDecoration(decoration, IDecoration.BOTTOM_LEFT);
			break;
		case WEST:
			setDecoration(decoration, IDecoration.TOP_LEFT);
			break;
		case CENTER:
			setDecoration(decoration, IDecoration.UNDERLAY);
			break;
		default:
			setDecoration(decoration, IDecoration.BOTTOM_LEFT);
			break;

		}
	}
}

Back to the top