Skip to main content
summaryrefslogtreecommitdiffstats
blob: a80b9ff723ace31144f13a3ded75c75c822b59bf (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
/**
 * Copyright (c) 2012 Mia-Software.
 *  
 * 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:
 *  	Alban Ménager (Soft-Maint) - Bug 387470 - [EFacet][Custom] Editors
 *  	Grégoire Dupé (Mia-Software) - Bug 387470 - [EFacet][Custom] Editors
 */
package org.eclipse.emf.facet.util.ui.internal.exported.util.widget.component.getorcreate;

import org.eclipse.emf.facet.util.ui.internal.exported.util.widget.AbstractWidget;
import org.eclipse.emf.facet.util.ui.internal.exported.util.widget.component.properties.name.AbstractGetElementNameWidget;
import org.eclipse.emf.facet.util.ui.internal.exported.widget.getorcreate.IAbstractGetOrCreateElementWidget;
import org.eclipse.emf.facet.util.ui.utils.PropertyElement2;
import org.eclipse.emf.facet.util.ui.utils.UIUtils;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

/**
 * Abstract class for the creation of a composite that allows the selection or
 * creation of an element. Classes implementing this class must overrides
 * {@link #getSubWidgets()} method. This class prepare the composite for
 * {@link #COLUMN_NUMBER} subComposites and provide the method
 * {@link #createTextField(boolean)} for the creation of a textField area.
 * 
 * @see AbstractGetOrCreateElementWithButtonWidget
 * @see AbstractGetElementNameWidget
 * @since 0.3
 */
public abstract class AbstractGetOrCreateElementWidget<T extends Object>
		extends AbstractWidget
		implements IAbstractGetOrCreateElementWidget {

	/**
	 * The number of columns of this composite.
	 */
	public static final int COLUMN_NUMBER = 3;
	private PropertyElement2<T> propertyElement;
	private Text text;

	/**
	 * Constructor. Initialize the composite with a layout of
	 * {@value #COLUMN_NUMBER} columns.
	 * 
	 * @param parent
	 *            the parent containing this composite.
	 * @param style
	 *            the style of the composite.
	 * @param editingDomain
	 *            the current editing domain.
	 * @param propertyElement
	 *            the property element that this composite will edit.
	 */
	protected AbstractGetOrCreateElementWidget(final Composite parent,
			final PropertyElement2<T> propertyElement) {
		super(parent);
		this.propertyElement = propertyElement;
		this.setLayout(new GridLayout(COLUMN_NUMBER, false));
	}

	/**
	 * @return the property element.
	 */
	public final PropertyElement2<T> getPropertyElement() {
		return this.propertyElement;
	}

	public Text getTextField() {
		return this.text;
	}

	/**
	 * Create the text field in the composite.</p> If the property
	 * {@link #propertyElement} has a value, the field is not editable.
	 * 
	 * @param enabled
	 *            set if the field must be editable or not.
	 */
	protected void createTextField(final boolean enabled) {
		ModifyListener modifyListener = null;
		if (enabled) {
			modifyListener = new ModifyListener() {

				public void modifyText(final ModifyEvent event) {
					onTextModfified();
				}
			};
		}
		String initialText = ""; //$NON-NLS-1$
		if ((this.propertyElement.getValue() != null)
				&& (getTextFieldInitialText() != null)) {
			initialText = getTextFieldInitialText();
		}
		this.text = UIUtils.createTextField(this, initialText, enabled,
				modifyListener);
		fireChanged();
	}

	protected void onTextModfified() {
		this.propertyElement.setValue(this.text.getText().toString());
		onFieldEdited();
	}

	/**
	 * Execute an action when the text field is edited. If not override, nothing
	 * appends. Can be override if needed.
	 */
	protected void onFieldEdited() {
		fireChanged();
	}

	/**
	 * @return the initial text of the textField of this composite. Null or void
	 *         if no text has to be displayed.
	 */
	protected String getTextFieldInitialText() {
		String result = ""; //$NON-NLS-1$
		final Object value = this.getPropertyElement().getValue();
		if (value != null) {
			result = value.toString();
		}
		return result;
	}

	@Override
	public String getError() {
		String error = null;
		final Object value = this.propertyElement.getValue();
		if (value == null && !this.propertyElement.isCanBeNull()) {
			error = getErrorMessage();
		}
		return error;
	}

	/**
	 * @return the error message if the {@link #propertyElement} is not edited.
	 *         Return 'null' if no error can be returned.
	 */
	protected abstract String getErrorMessage();

	/**
	 * @param propertyElement
	 *            the propertyElement to set
	 */
	public void setPropertyElement(final PropertyElement2<T> propertyElement) {
		this.propertyElement = propertyElement;
	}

	public T getElement() {
		return getPropertyElement().getValue2();
	}

	public String getText() {
		return this.text.getText();
	}

	public void setText(final String text) {
		this.text.setText(text);
	}

}

Back to the top