Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4926945c8395e49c4956a79741c6b40156d7932d (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
/*******************************************************************************
 * Copyright (c) 2013 Atos
 * 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:
 *     Arthur Daussy - initial implementation
 *******************************************************************************/
package org.eclipse.papyrus.team.collaborative.strategy.ui.providers;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.facet.infra.browser.uicore.internal.model.ModelElementItem;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.IFontProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;

import com.google.common.base.Predicate;


/**
 * The Class ExtensiveLabelProvider. {@link ILabelProvider} that extend the one use by the model explorer view.
 * This label provider can customize label and color of display object using predicate
 */
public class ExtensivePartitionNameLabelProvider implements IFontProvider, IColorProvider, ILabelProvider {


	private ILabelProvider internal;


	/** The color. */
	private Color color;

	/** The font. */
	private Font font;

	/** The predicate. */
	private Predicate<EObject> predicate;

	/**
	 * Instantiates a new extensive label provider.
	 * 
	 * @param predicate
	 *        the predicate
	 */
	public ExtensivePartitionNameLabelProvider(Predicate<EObject> predicate, ILabelProvider internal) {
		super();
		this.predicate = predicate;
		this.internal = internal;
	}

	public ExtensivePartitionNameLabelProvider(ILabelProvider internal) {
		super();
		this.predicate = null;
		this.internal = internal;
	}


	/**
	 * Sets the color.
	 * 
	 * @param color
	 *        the new color
	 */
	public void setColor(Color color) {
		this.color = color;
	}


	/**
	 * Sets the font.
	 * 
	 * @param font
	 *        the new font
	 */
	public void setFont(Font font) {
		this.font = font;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.modelexplorer.MoDiscoLabelProvider#getText(java.lang.Object)
	 */
	public String getText(Object element) {
		String superText = internal.getText(element);
		if(element instanceof ModelElementItem) {
			ModelElementItem elem = (ModelElementItem)element;
			EObject eObject = elem.getEObject();
			if(eObject != null) {
				String partitionName = getPartitionName(eObject);
				if(partitionName != null) {
					StringBuilder stringBuilder = new StringBuilder();
					stringBuilder.append(partitionName);
					stringBuilder.append(superText);
					superText = stringBuilder.toString();
				}
			}
		}
		return superText;
	}

	private static final String CLOSING_BRACKET = "] ";

	private static final String OPEN_BRACKET = "[";


	/**
	 * Gets the partition name.
	 * 
	 * @param eObject
	 *        the e object
	 * @return the partition name
	 */
	protected String getPartitionName(EObject eObject) {
		Resource resource = eObject.eResource();
		if(resource != null) {
			URI uri = resource.getURI().trimFileExtension();
			String partitionName = uri.segment(uri.segmentCount() - 1);
			StringBuilder builder = new StringBuilder(OPEN_BRACKET);
			builder.append(partitionName);
			builder.append(CLOSING_BRACKET);
			return builder.toString();
		}
		return null;
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.facet.infra.browser.uicore.CustomizableModelLabelProvider#getFont(java.lang.Object)
	 */
	@SuppressWarnings("restriction")
	public Font getFont(Object element) {
		if(predicate != null) {
			if(element instanceof ModelElementItem) {
				ModelElementItem eObject = (ModelElementItem)element;
				if(font != null) {
					if(predicate.apply(eObject.getEObject())) {
						return font;
					}
				}
			}
		}
		if(internal instanceof IFontProvider) {
			return ((IFontProvider)internal).getFont(element);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.facet.infra.browser.uicore.CustomizableModelLabelProvider#getForeground(java.lang.Object)
	 */
	@SuppressWarnings("restriction")
	public Color getForeground(Object element) {
		if(predicate != null) {
			if(element instanceof ModelElementItem) {
				ModelElementItem eObject = (ModelElementItem)element;
				if(color != null) {
					if(predicate.apply(eObject.getEObject())) {
						return color;
					}
				}
			}
		}
		if(internal instanceof IColorProvider) {
			return ((IColorProvider)internal).getForeground(element);
		}
		return null;
	}


	public void addListener(ILabelProviderListener listener) {
		internal.addListener(listener);

	}


	public void dispose() {
		internal.dispose();

	}


	public boolean isLabelProperty(Object element, String property) {
		return internal.isLabelProperty(element, property);
	}


	public void removeListener(ILabelProviderListener listener) {
		internal.removeListener(listener);

	}


	public Image getImage(Object element) {
		return internal.getImage(element);
	}


	public Color getBackground(Object element) {
		if(internal instanceof IColorProvider) {
			return ((IColorProvider)internal).getBackground(element);
		}
		return null;
	}
}

Back to the top