Skip to main content
summaryrefslogtreecommitdiffstats
blob: 870c48a5fe10c9a11a8037362dacf82a379f036f (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
package org.eclipse.fx.ui.controls.form;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.eclipse.fx.core.Status;
import org.eclipse.fx.core.Status.State;
import org.eclipse.fx.core.Util;

import javafx.beans.Observable;
import javafx.beans.property.Property;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Node;

public class DefaultForm implements Form {
	final StatusDecorator decorator;
	private List<BindingImpl<?>> bindingList = new ArrayList<>();
	private ReadOnlyObjectWrapper<Status> validationStatus = new ReadOnlyObjectWrapper<>(this, "validationStatus", Status.ok());

	static Comparator<Status> STATUS_SORTER = (s1, s2) -> s1.getState().compareTo(s2.getState());
	static Predicate<Status> WARNING_ERROR = s -> s.getState() == State.ERROR || s.getState() == State.WARNING;

	public DefaultForm() {
		this(Util.lookupService(DefaultForm.class, StatusDecorator.class));
	}

	public DefaultForm(StatusDecorator decorator) {
		this.decorator = decorator;
	}

	public void registerBinding(BindingImpl<?> binding) {
		binding.validationStatusProperty().addListener(this::handleBindingStatusChange);
		this.bindingList.add(binding);
	}

	public void unregisterBinding(BindingImpl<?> binding) {
		binding.validationStatusProperty().removeListener(this::handleBindingStatusChange);
		this.bindingList.remove(binding);
	}

	private void handleBindingStatusChange(Observable o, Status ol, Status ne) {
		this.validationStatus.set(this.bindingList.stream()
			.map( b -> b.validationStatusProperty().get() )
			.sorted(STATUS_SORTER).filter(WARNING_ERROR)
			.findFirst()
			.orElse(Status.ok()));
	}

	@Override
	public ReadOnlyObjectProperty<Status> validationStatusProperty() {
		return this.validationStatus.getReadOnlyProperty();
	}

	@Override
	public <T> BindingBuilder<T> builder(String id, Property<T> validatedProperty, Property<T> oppositeProperty, BindingConfiguration type ) {
		return new BindingBuilderImpl<>(this, id, validatedProperty, oppositeProperty, type);
	}

	static class MutatorDef {
		final Object data;
		final BiConsumer<Node,Object> mutator;

		public MutatorDef(Object data, BiConsumer<Node,?> mutator) {
			this.data = data;
			this.mutator = (BiConsumer<Node, Object>) mutator;
		}

	}

	static class BindingBuilderImpl<T> implements BindingBuilder<T> {
		private final Property<T> validatedProperty;
		private final Property<T> oppositeProperty;
		private final BindingConfiguration type;
		private final DefaultForm form;
		private final String id;
		private Map<String, Property<?>> dependencyMap;
		private List<BiFunction<T,Map<String,Object>,Status>> validationList;
		private List<MutatorDef> nodeMutatorList;
		private Node decoratedNode;

		public BindingBuilderImpl(DefaultForm form, String id, Property<T> validatedProperty, Property<T> oppositeProperty, BindingConfiguration type) {
			this.form = form;
			this.validatedProperty = validatedProperty;
			this.oppositeProperty = oppositeProperty;
			this.type = type;
			this.id = id;
		}

		@Override
		public BindingBuilder<T> validator(Function<T,Status> validator) {
			return validator(( t, dep ) -> validator.apply(t));
		}

		@Override
		public BindingBuilder<T> decoratedNodeMutator(Consumer<Node> mutator) {
			decoratedNodeMutator(null, (n,o) -> mutator.accept(n));
			return this;
		}

		@Override
		public <O> BindingBuilder<T> decoratedNodeMutator(O data, BiConsumer<Node, O> mutator) {
			if( this.nodeMutatorList == null ) {
				this.nodeMutatorList = new ArrayList<>();
			}
			this.nodeMutatorList.add( new MutatorDef(data, mutator) );
			return this;
		}

		@Override
		public BindingBuilder<T> validator(BiFunction<T,Map<String,Object>,Status> validator) {
			if( this.validationList == null ) {
				this.validationList = new ArrayList<>();
			}
			this.validationList.add(validator);
			return this;
		}

		@Override
		public BindingBuilder<T> dependency(Property<?> property) {
			if( property.getName() == null ) {
				throw new IllegalArgumentException("The provided property is not named. Use dependency(String,Property) instead"); //$NON-NLS-1$
			}
			return dependency(property.getName(), property);
		}

		@Override
		public BindingBuilder<T> dependency(String name, Property<?> property) {
			if( this.dependencyMap != null ) {
				this.dependencyMap = new HashMap<>();
			}
			if( this.dependencyMap.containsKey(name) ) {
				throw new IllegalStateException("You already register a dependency '"+this.dependencyMap.get(name)+"' named '"+name+"'"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			}
			this.dependencyMap.put(name, property);
			return this;
		}

		@Override
		public BindingBuilder<T> decoratedNode(Node decoratedNode) {
			this.decoratedNode = decoratedNode;
			return this;
		}

		@Override
		public Binding build() {
			return new BindingImpl<>(this.form, this.id, this.decoratedNode, this.validatedProperty, this.oppositeProperty, this.dependencyMap, this.validationList, this.nodeMutatorList, this.type);
		}
	}

	static class BindingImpl<T> implements Binding {
		private final Property<T> validatedProperty;
		private final Property<T> oppositeProperty;
		private final Map<String, Property<?>> dependencyMap;
		private final List<BiFunction<T,Map<String,Object>,Status>> validationList;
		private final List<MutatorDef> nodeMutatorList;
		private final String id;
		private ObservableList<Status> validationStatusList = FXCollections.observableArrayList();
		private ReadOnlyObjectWrapper<Status> validationStatus = new ReadOnlyObjectWrapper<>(this, "status", Status.ok()); //$NON-NLS-1$
		private BindingConfiguration type;
		private final DefaultForm form;
		private Node decoratedNode;
		private Optional<Node> cachedNode;

		public BindingImpl(DefaultForm form, String id, Node decoratedNode, Property<T> validatedProperty,
				Property<T> oppositeProperty,
				Map<String, Property<?>> dependencyMap,
				List<BiFunction<T,Map<String,Object>,Status>> validationList,
				List<MutatorDef> nodeMutatorList,
				BindingConfiguration type) {
			this.form = form;
			this.id = id;
			this.form.registerBinding(this);
			this.decoratedNode = decoratedNode;
			this.validatedProperty = validatedProperty;
			this.oppositeProperty = oppositeProperty;
			this.nodeMutatorList = nodeMutatorList == null ? null: new ArrayList<>(nodeMutatorList);
			this.dependencyMap = dependencyMap == null ? null : new HashMap<>(dependencyMap);
			this.validationList = validationList == null ? null : new ArrayList<>(validationList);
			this.type = type;

			if( type == BindingConfiguration.VALIDATE_TO_OPPOSITE ) {
				this.validatedProperty.bindBidirectional(oppositeProperty);
			} else {
				this.oppositeProperty.bindBidirectional(validatedProperty);
			}

			if( dependencyMap != null ) {
				this.dependencyMap.entrySet().forEach( e -> e.getValue().addListener( (o, ol, ne) -> {
					runvalidation();
				} ));
			}
			this.validatedProperty.addListener((o, ol, ne) -> {
				runvalidation();
			} );
		}

		@Override
		public String id() {
			return this.id;
		}

		@Override
		public ReadOnlyObjectProperty<Status> validationStatusProperty() {
			return this.validationStatus.getReadOnlyProperty();
		}

		private void runvalidation() {
			if( this.validationList != null ) {
				Map<String, Object> map = Optional.ofNullable(this.dependencyMap)
					.map(m -> m.entrySet().stream().collect(Collectors.toMap(Entry::getKey, e -> (Object)e.getValue().getValue())))
					.orElse(new HashMap<>());
				this.validationStatusList.setAll(this.validationList
					.stream()
					.map( f -> f.apply(this.validatedProperty.getValue(), map))
					.collect(Collectors.toList()));
				this.validationStatus.setValue(this.validationStatusList
					.stream()
					.sorted( STATUS_SORTER )
					.filter( WARNING_ERROR )
					.findFirst()
					.orElse(Status.ok()));
			}
		}

		@Override
		public Optional<Node> decoratedNode() {
			if( this.cachedNode == null ) {
				this.cachedNode = Optional.ofNullable(this.decoratedNode)
						.map( n -> this.form.decorator.decorate(this.validationStatus, n))
						.map( n -> {
							if( this.nodeMutatorList != null ) {
								this.nodeMutatorList.forEach( m -> m.mutator.accept(n, m.data));
							}
							return n;
						});
			}
			return this.cachedNode;
		}

		@Override
		public void dispose() {
			if( this.type == BindingConfiguration.VALIDATE_TO_OPPOSITE ) {
				this.validatedProperty.unbindBidirectional(this.oppositeProperty);
			} else {
				this.oppositeProperty.unbindBidirectional(this.validatedProperty);
			}
			this.form.unregisterBinding(this);
		}

	}
}

Back to the top