Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 07529186ff9ee7f5b947682f157648bf2d61d977 (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
/*****************************************************************************
 * Copyright (c) 2010 Atos Origin.
 *
 *    
 * 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:
 *   Atos Origin - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.groups.core.ui.utils;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Control;
import org.eclipse.uml2.uml.edit.providers.UMLItemProviderAdapterFactory;

/**
 * Provides usefull utility methods for ICompositeCreator instances
 * 
 * @author vhemery
 */
public class CreatorUtils {

	/** The offset to use for form data */
	private static final int OFFSET = 5;

	/** The adapter factory to use as label provider */
	private static AdapterFactoryLabelProvider adapterFactory = null;

	/**
	 * Get the form layout data to place element at top of the parent composite
	 * 
	 * @return form data
	 */
	public static FormData getTopFormData() {
		FormData data = new FormData();
		data.right = new FormAttachment(100, -OFFSET);
		data.left = new FormAttachment(0, OFFSET);
		data.top = new FormAttachment(0, OFFSET);
		return data;
	}

	/**
	 * Get the form layout data to place element under another
	 * 
	 * @param upperControl
	 *        control under which the element must be located
	 * @return form data
	 */
	public static FormData getFormDataUnder(Control upperControl) {
		FormData data = new FormData();
		data.right = new FormAttachment(100, -OFFSET);
		data.left = new FormAttachment(0, OFFSET);
		data.top = new FormAttachment(upperControl, OFFSET);
		return data;
	}

	/**
	 * Get the form layout data to place element under another, aligned on the right of the parent composite
	 * 
	 * @param upperControl
	 *        control under which the element must be located
	 * @return form data
	 */
	public static FormData getFormDataRightAlignedUnder(Control upperControl) {
		FormData data = new FormData();
		data.right = new FormAttachment(100, -OFFSET);
		data.top = new FormAttachment(upperControl, OFFSET);
		return data;
	}

	/**
	 * Get the label of a graphical edit part
	 * 
	 * @param editPart
	 *        the input part
	 * @return the string label
	 */
	public static String getLabel(IGraphicalEditPart editPart) {
		EObject eObject = editPart.resolveSemanticElement();
		return getCustomLabelProvider().getText(eObject);
	}

	/**
	 * Gets the custom label provider that parses label for EClass
	 * 
	 * @return the custom label provider
	 */
	public static ILabelProvider getCustomLabelProvider() {
		if(adapterFactory == null) {
			adapterFactory = new AdapterFactoryLabelProvider(new UMLItemProviderAdapterFactory()) {

				/**
				 * Override label provider for EClass
				 * 
				 * @see org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider#getText(java.lang.Object)
				 */
				@Override
				public String getText(Object object) {
					String text = super.getText(object);
					if(object instanceof EClass) {
						return text.substring(0, text.indexOf("[") - 1);
					} else {
						return text;
					}
				}
			};
		}
		return adapterFactory;
	}

}

Back to the top