Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 09a3c753c3210fd5de46ea734f8beeab6b07560f (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
269
270
271
272
273
274
275
276
277
278
279
280
281
/*****************************************************************************
 * Copyright (c) 2010 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:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.properties.runtime.propertyeditor;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.papyrus.properties.runtime.Activator;
import org.eclipse.papyrus.properties.runtime.controller.PropertyEditorController;
import org.eclipse.papyrus.properties.runtime.propertyeditor.descriptor.IPropertyEditorDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.services.IDisposable;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;


/**
 * Abstract class for all proprty editors
 */
public abstract class AbstractPropertyEditor implements IDisposable {

	/** margin added to the computed text */
	protected static final int TEXT_MARGIN = 5;
	
	/** margin added to the computed text */
	protected static final int LABEL_MAX_WIDTH= 100;

	/** controller that manages this editor */
	private PropertyEditorController controller;

	/** widget factory used to create controls. Not a {@link FormToolkit} because there are missing elements (CCombo, CLabel, etc.) */
	private TabbedPropertySheetWidgetFactory widgetFactory;

	/** read oonly status for the editor */
	private boolean isReadOnly = false;

	/** configuration for the property editor */
	private IPropertyEditorDescriptor descriptor = null;

	/** the tooltip for this controller */
	private String tooltipText = null;


	/**
	 * Creates a new {@link AbstractPropertyEditor}. It uses a created widget factory each time a property editor is created, the other constructor
	 * {@link #AbstractPropertyEditor(TabbedPropertySheetWidgetFactory)} should be used instead.
	 */
	public AbstractPropertyEditor() {
		this(new TabbedPropertySheetWidgetFactory());
	}

	/**
	 * Creates a new {@link AbstractPropertyEditor}.
	 * 
	 * @param widgetFactory
	 *        widget factory used to create {@link Control}
	 */
	public AbstractPropertyEditor(TabbedPropertySheetWidgetFactory widgetFactory) {
		this.widgetFactory = widgetFactory;
	}


	/**
	 * Returns the controller that manages this editor
	 * 
	 * @return the controller that manages this editor
	 */
	protected PropertyEditorController getController() {
		return controller;
	}

	/**
	 * Returns the widget factory used to create composites in tabbed property sheet pages
	 * 
	 * @return the widget factory
	 */
	public TabbedPropertySheetWidgetFactory getWidgetFactory() {
		return widgetFactory;
	}


	/**
	 * Sets the new widget factory used to create composites
	 * 
	 * @param widgetFactory
	 *        the widgetFactory to set
	 */
	public void setWidgetFactory(TabbedPropertySheetWidgetFactory widgetFactory) {
		this.widgetFactory = widgetFactory;
	}

	/**
	 * Sets the new controller.
	 * 
	 * @param controller
	 *        the new controller
	 */
	public void setController(PropertyEditorController controller) {
		this.controller = controller;
	}

	/**
	 * Sets the read only status of the editor
	 * 
	 * @param isReadOnly
	 *        <code>true</code> if the editor should be read-only.
	 */
	public void setIsReadOnly(boolean isReadOnly) {
		this.isReadOnly = isReadOnly;
	}

	/**
	 * Returns the read-only status of the editor
	 * 
	 * @return <code>true</code> if the editor is in read-only mode
	 */
	public boolean getIsReadOnly() {
		return isReadOnly;
	}

	/**
	 * Returns the tooltip text of the editor
	 * 
	 * @return the tooltip text
	 */
	public String getTooltipText() {
		return tooltipText;
	}

	/**
	 * Sets the tooltip text of the editor
	 * 
	 * @param toolTipText
	 *        the tooltip for the editor.
	 */
	public void setTooltipText(String toolTipText) {
		this.tooltipText = toolTipText;
	}


	/**
	 * Creates the display for this editor
	 * 
	 * @param parent
	 *        the parent composite for created elements
	 * @return the newly created composite
	 */
	public abstract Composite createContent(Composite parent);

	/**
	 * Indicates that the content of the editor has changed, and that the model should be updated
	 */
	public abstract void handleContentChanged();

	/**
	 * Initializes the content of the property editor, for example enumeration literals for enumeration, etc.
	 * 
	 * @param descriptor
	 *        the descriptor used for initialization
	 * @return the result of the initialization
	 */
	public abstract IStatus init(IPropertyEditorDescriptor descriptor);

	/**
	 * Returns the configuration for this editor
	 * 
	 * @return the configuration for this editor
	 */
	public IPropertyEditorDescriptor getDescriptor() {
		return descriptor;
	}

	/**
	 * Sets the configuration for this editor.
	 * 
	 * @param descriptor
	 *        the configuration for this editor
	 */
	public void setDescriptor(IPropertyEditorDescriptor descriptor) {
		this.descriptor = descriptor;
	}

	/**
	 * Returns the current value in the editor
	 * 
	 * @return the current value in the editor
	 */
	public abstract Object getValue();

	/**
	 * Sets the value to edit
	 * 
	 * @param valueToEdit
	 *        the value to edit
	 */
	public abstract void setValue(Object valueToEdit);

	/**
	 * {@inheritDoc}
	 */
	public abstract void dispose();

	/**
	 * Checks if the editor is disposed or not.
	 * 
	 * @return <code>true</code> if the editor is disposed
	 */
	public abstract boolean isDisposed();

	/**
	 * Indicates if the control is valid, i.e. not null and not disposed
	 * 
	 * @param control
	 *        the control to test
	 * @return
	 */
	protected boolean isValid(Control control) {
		return (control != null && !control.isDisposed());
	}

	/**
	 * Creates the label area for the property editor
	 * 
	 * @param parent
	 *        the composite parent of the label
	 * @return the label
	 */
	protected Control createLabel(Composite parent) {
		GridData data = new GridData(SWT.FILL, SWT.CENTER, false, false);
		String text = getDescriptor().getLabel();
		int size = computeLabelSize(parent, text);
		data.minimumWidth = Math.max(LABEL_MAX_WIDTH, size);
		data.widthHint = data.minimumWidth;
		return createLabel(parent, data);
	}

	/**
	 * Computes the size of the given label
	 * @param text the text to compute
	 * @return the approximate size of the text
	 */
	protected int computeLabelSize(Composite parent, String text) {
		GC gc = new GC (parent);
        FontMetrics fm = gc.getFontMetrics ();
        int width = text.length() * fm.getAverageCharWidth ();
        gc.dispose ();
        return width;
	}

	/**
	 * Creates the label area for the property editor
	 * 
	 * @param parent
	 *        the composite parent of the label
	 * @param layoutData
	 *        layout data to apply to this editor
	 * @return the label
	 */
	protected Control createLabel(Composite parent, Object layoutData) {
		CLabel label = getWidgetFactory().createCLabel(parent, getDescriptor().getLabel());
		if(getDescriptor().getLabelImageDescriptor() != null) {
			label.setImage(Activator.getImageFromDescriptor(getDescriptor().getLabelImageDescriptor()));
		}
		label.setToolTipText(getTooltipText());
		label.setLayoutData(layoutData);
		return label;
	}
}

Back to the top