Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 129b736977a3775e07c14834ecbd39a88ed09783 (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
/*******************************************************************************
 * Copyright (c) 2009 Conselleria de Infraestructuras y Transporte, Generalitat 
 * de la Comunitat Valenciana . 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: Francisco Javier Cano Muñoz (Prodevelop) – Initial API 
 * implementation.
 *
 ******************************************************************************/
package org.eclipse.papyrus.navigator.providers;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.viewers.IDecorationContext;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.LabelDecorator;
import org.eclipse.swt.graphics.Image;

/**
 * A {@link LabelDecorator} that removes the type prefix that appears with some label providers.
 * 
 * @author <a href="mailto:fjcano@prodevelop.es">Francisco Javier Cano Muñoz</a>
 * 
 */
// fjcano #291192
public class NoTypePrefixLabelDecorator extends LabelDecorator {

	@Override
	public Image decorateImage(Image image, Object element, IDecorationContext context) {
		return image;
	}

	@Override
	public String decorateText(String text, Object element, IDecorationContext context) {
		return removeTypeName(element, text);
	}

	private static String prefixTypeReplacement = "";

	protected String removeTypeName(Object element, String label) {
		if(element != null && label != null) {
			String typePrefix = getTypePrefix(element);
			if(typePrefix != null) {
				label = label.replaceAll(typePrefix, prefixTypeReplacement);
				label = label.trim();
			}
		}
		return label;
	}

	protected String getTypePrefix(Object element) {
		String typeName = getTypeName(element);
		return typeName != null ? "<" + typeName + ">" : null;
	}

	protected String getTypeName(Object element) {
		if(element instanceof EObject) {
			EObject eObject = (EObject)element;
			if(eObject.eClass() != null) {
				if(eObject.eClass().getName() != null) {
					return convertNameToDisplayName(eObject.eClass().getName());
				}
			}
		}
		return null;
	}

	protected String convertNameToDisplayName(String name) {
		if(name == null) {
			return name;
		}
		StringBuilder displayNameBuilder = new StringBuilder(name.length() + 5);
		char character = 'a';
		for(int index = 0; index < name.length(); index++) {
			character = name.charAt(index);
			if(Character.isUpperCase(character) && index > 0) {
				displayNameBuilder.append(" ");
				displayNameBuilder.append(character);
			} else {
				displayNameBuilder.append(character);
			}
		}
		return displayNameBuilder.toString();
	}

	@Override
	public boolean prepareDecoration(Object element, String originalText, IDecorationContext context) {
		// TODO Auto-generated method stub
		return false;
	}

	public Image decorateImage(Image image, Object element) {
		// TODO Auto-generated method stub
		return null;
	}

	public String decorateText(String text, Object element) {
		return removeTypeName(element, text);
	}

	public void addListener(ILabelProviderListener listener) {
		// TODO Auto-generated method stub

	}

	public void dispose() {
		// TODO Auto-generated method stub

	}

	public boolean isLabelProperty(Object element, String property) {
		// TODO Auto-generated method stub
		return false;
	}

	public void removeListener(ILabelProviderListener listener) {
		// TODO Auto-generated method stub

	}

}

Back to the top