Skip to main content
summaryrefslogtreecommitdiffstats
blob: 06863a489b7d175169f2d5b8a41244b2c7f6bac7 (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
/*******************************************************************************
 * Copyright (c) 2012 BestSolution.at and others.
 * 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:
 *     Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
 *******************************************************************************/
package org.eclipse.fx.ui.workbench.renderers.fx.widget;

import java.util.ArrayList;
import java.util.List;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.eclipse.e4.ui.model.application.ui.MUIElement;
import org.eclipse.fx.ui.workbench.renderers.base.widget.WCallback;
import org.eclipse.fx.ui.workbench.renderers.base.widget.WPropertyChangeHandler;
import org.eclipse.fx.ui.workbench.renderers.base.widget.WPropertyChangeHandler.WPropertyChangeEvent;
import org.eclipse.fx.ui.workbench.renderers.base.widget.WWidget;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

/**
 * Base class for all widgets
 * 
 * @param <N>
 *            the native type
 * @param <M>
 *            the model type
 */
public abstract class WWidgetImpl<N, M extends MUIElement> implements WWidget<M> {
	private N nativeWidget;

	@Nullable
	private M domElement;
	private List<WCallback<Boolean, Void>> activationCallbacks = new ArrayList<WCallback<Boolean, Void>>();
	private boolean active;
	/**
	 * The current widget state
	 */
	@NonNull
	protected WidgetState state = WidgetState.IN_SETUP;

	private WPropertyChangeHandler<? extends WWidget<M>> propertyChangeHandler;

	/**
	 * @return the widget
	 */
	@NonNull
	protected abstract N createWidget();

	@Override
	public void setWidgetState(WidgetState state) {
		this.state = state;
	}

	@Override
	public WidgetState getWidgetState() {
		return this.state;
	}

	@Override
	public void activate() {
		this.active = true;
		if (this.activationCallbacks != null) {
			for (WCallback<Boolean, Void> c : this.activationCallbacks) {
				c.call(Boolean.TRUE);
			}
		}
	}

	@Override
	public void deactivate() {
		this.active = false;
		if (this.activationCallbacks != null) {
			for (WCallback<Boolean, Void> c : this.activationCallbacks) {
				c.call(Boolean.FALSE);
			}
		}
	}

	@Override
	public boolean isActive() {
		return this.active;
	}

	@Override
	public void registerActivationCallback(WCallback<Boolean, Void> callback) {
		// Could be that we are already disposed at this point
		if (this.activationCallbacks != null) {
			this.activationCallbacks.add(callback);
		}
	}

	/**
	 * Initialize the widget (called by DI)
	 */
	@PostConstruct
	protected void init() {
		getWidget(); // ensure that the widget is created
	}

	@PreDestroy
	void destroy() {
		if (this.nativeWidget != null) {
			setUserData(null);
		}
		this.propertyChangeHandler = null;
		this.domElement = null;
		this.activationCallbacks.clear();
		this.activationCallbacks = null;
		doCleanup();
	}

	/**
	 * Cleanup code to run
	 */
	protected void doCleanup() {
		// empty by default
	}

	@Override
	public final void setDomElement(M domElement) {
		this.domElement = domElement;
	}

	@Override
	public M getDomElement() {
		return this.domElement;
	}

	@Override
	public N getWidget() {
		N widget = this.nativeWidget;
		if (widget == null) {
			widget = this.nativeWidget = createWidget();
			bindProperties(this.nativeWidget);
			setUserData(this);
		}
		return widget;
	}

	/**
	 * Setup property bindings
	 * 
	 * @param widget
	 *            the widget
	 */
	protected void bindProperties(N widget) {
		// empty by default
	}

	/**
	 * Bind a property and fire change events
	 * 
	 * @param propertyName
	 *            the property to bound to
	 * @param value
	 *            the property to attach a listener
	 */
	protected void bindProperty(@NonNull final String propertyName, @NonNull ObservableValue<? extends Object> value) {
		value.addListener(new ChangeListener<Object>() {

			@Override
			public void changed(ObservableValue<? extends Object> observable, Object oldValue, Object newValue) {
				fireChange(propertyName, newValue);
			}
		});
	}

	/**
	 * Associate user data with the widget
	 * 
	 * @param widget
	 *            the widget
	 */
	protected abstract void setUserData(WWidgetImpl<N, M> widget);

	@Override
	public void setPropertyChangeHandler(WPropertyChangeHandler<? extends WWidget<M>> propertyChangeHandler) {
		this.propertyChangeHandler = propertyChangeHandler;
	}

	/**
	 * Fire a change event
	 * 
	 * @param propertyName
	 *            the property modified
	 * @param newValue
	 *            the new value
	 */
	protected final void fireChange(@NonNull String propertyName, @Nullable Object newValue) {
		if (this.propertyChangeHandler != null) {
			WPropertyChangeEvent<WWidget<M>> e = new WPropertyChangeEvent<WWidget<M>>(this, propertyName, newValue);
			this.propertyChangeHandler.propertyObjectChanged(e);
		}
	}
}

Back to the top