-
Notifications
You must be signed in to change notification settings - Fork 82
/
FXMLView.java
327 lines (295 loc) · 10.9 KB
/
FXMLView.java
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package com.airhacks.afterburner.views;
/*
* #%L
* afterburner.fx
* %%
* Copyright (C) 2015 Adam Bien
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import com.airhacks.afterburner.injection.Injector;
import com.airhacks.afterburner.injection.PresenterFactory;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import static java.util.ResourceBundle.getBundle;
import java.util.concurrent.CompletableFuture;
import static java.util.concurrent.CompletableFuture.supplyAsync;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import javafx.application.Platform;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.layout.StackPane;
import javafx.util.Callback;
/**
* @author adam-bien.com
*/
public abstract class FXMLView extends StackPane {
public final static String DEFAULT_ENDING = "View";
protected ObjectProperty<Object> presenterProperty;
protected FXMLLoader fxmlLoader;
protected String bundleName;
protected ResourceBundle bundle;
protected final Function<String, Object> injectionContext;
protected URL resource;
protected static Executor FX_PLATFORM_EXECUTOR = Platform::runLater;
protected final static ExecutorService PARENT_CREATION_POOL = getExecutorService();
/**
* Constructs the view lazily (fxml is not loaded) with empty injection
* context.
*/
public FXMLView() {
this(f -> null);
}
/**
*
* @param injectionContext the function is used as a injection source.
* Values matching for the keys are going to be used for injection into the
* corresponding presenter.
*/
public FXMLView(Function<String, Object> injectionContext) {
this.injectionContext = injectionContext;
this.init(getFXMLName());
}
private void init(final String conventionalName) {
this.presenterProperty = new SimpleObjectProperty<>();
this.resource = getClass().getResource(conventionalName);
this.bundleName = getBundleName();
this.bundle = getResourceBundle(bundleName);
}
FXMLLoader loadSynchronously(final URL resource, ResourceBundle bundle, final String conventionalName) throws IllegalStateException {
final FXMLLoader loader = new FXMLLoader(resource, bundle);
PresenterFactory factory = discover();
Callback<Class<?>, Object> controllerFactory = (Class<?> p) -> factory.instantiatePresenter(p, this.injectionContext);
loader.setControllerFactory(controllerFactory);
try {
loader.load();
} catch (IOException ex) {
throw new IllegalStateException("Cannot load " + conventionalName, ex);
}
return loader;
}
PresenterFactory discover() {
Iterable<PresenterFactory> discoveredFactories = PresenterFactory.discover();
List<PresenterFactory> factories = StreamSupport.stream(discoveredFactories.spliterator(), false).
collect(Collectors.toList());
if (factories.isEmpty()) {
return Injector::instantiatePresenter;
}
if (factories.size() == 1) {
return factories.get(0);
} else {
factories.forEach(System.err::println);
throw new IllegalStateException("More than one PresenterFactories discovered");
}
}
void initializeFXMLLoader() {
if (this.fxmlLoader == null) {
this.fxmlLoader = this.loadSynchronously(resource, bundle, bundleName);
this.presenterProperty.set(this.fxmlLoader.getController());
}
}
/**
* Initializes the view by loading the FXML (if not happened yet) and
* returns the top Node (parent) specified in
*
* @return the node loaded by FXMLLoader
*/
public Parent getView() {
this.initializeFXMLLoader();
Parent parent = fxmlLoader.getRoot();
addCSSIfAvailable(parent);
return parent;
}
/**
* Initializes the view synchronously and invokes and passes the created
* parent Node to the consumer within the FX UI thread.
*
* @param consumer - an object interested in received the Parent as callback
*/
public void getView(Consumer<Parent> consumer) {
Supplier<Parent> supplier = this::getView;
supplyAsync(supplier, FX_PLATFORM_EXECUTOR).
thenAccept(consumer).
exceptionally(this::exceptionReporter);
}
/**
* Creates the view asynchronously using an internal thread pool and passes
* the parent node within the UI Thread.
*
*
* @param consumer - an object interested in received the Parent as callback
*/
public void getViewAsync(Consumer<Parent> consumer) {
Supplier<Parent> supplier = this::getView;
CompletableFuture.supplyAsync(supplier, PARENT_CREATION_POOL).
thenAcceptAsync(consumer, FX_PLATFORM_EXECUTOR).
exceptionally(this::exceptionReporter);
}
/**
* Scene Builder creates for each FXML document a root container. This
* method omits the root container (e.g. AnchorPane) and gives you the
* access to its first child.
*
* @return the first child of the AnchorPane
*/
public Node getViewWithoutRootContainer() {
final ObservableList<Node> children = getView().getChildrenUnmodifiable();
if (children.isEmpty()) {
return null;
}
return children.listIterator().next();
}
void addCSSIfAvailable(Parent parent) {
URL uri = getClass().getResource(getStyleSheetName());
if (uri == null) {
return;
}
String uriToCss = uri.toExternalForm();
parent.getStylesheets().add(uriToCss);
}
String getStyleSheetName() {
return getResourceCamelOrLowerCase(false, ".css");
}
/**
*
* @return the name of the fxml file derived from the FXML view. e.g. The
* name for the AirhacksView is going to be airhacks.fxml.
*/
final String getFXMLName() {
return getResourceCamelOrLowerCase(true, ".fxml");
}
String getResourceCamelOrLowerCase(boolean mandatory, String ending) {
String name = getConventionalName(true, ending);
URL found = getClass().getResource(name);
if (found != null) {
return name;
}
System.err.println("File: " + name + " not found, attempting with camel case");
name = getConventionalName(false, ending);
found = getClass().getResource(name);
if (mandatory && found == null) {
final String message = "Cannot load file " + name;
System.err.println(message);
System.err.println("Stopping initialization phase...");
throw new IllegalStateException(message);
}
return name;
}
/**
* In case the view was not initialized yet, the conventional fxml
* (airhacks.fxml for the AirhacksView and AirhacksPresenter) are loaded and
* the specified presenter / controller is going to be constructed and
* returned.
*
* @return the corresponding controller / presenter (usually for a
* AirhacksView the AirhacksPresenter)
*/
public Object getPresenter() {
this.initializeFXMLLoader();
return this.presenterProperty.get();
}
/**
* Does not initialize the view. Only registers the Consumer and waits until
* the the view is going to be created / the method FXMLView#getView or
* FXMLView#getViewAsync invoked.
*
* @param presenterConsumer listener for the presenter construction
*/
public void getPresenter(Consumer<Object> presenterConsumer) {
this.presenterProperty.addListener((ObservableValue<? extends Object> o, Object oldValue, Object newValue) -> {
presenterConsumer.accept(newValue);
});
}
/**
*
* @param lowercase indicates whether the simple class name should be
* converted to lowercase of left unchanged
* @param ending the suffix to append
* @return the conventional name with stripped ending
*/
protected String getConventionalName(boolean lowercase, String ending) {
return getConventionalName(lowercase) + ending;
}
/**
*
* @param lowercase indicates whether the simple class name should be
* @return the name of the view without the "View" prefix.
*/
protected String getConventionalName(boolean lowercase) {
final String clazzWithEnding = this.getClass().getSimpleName();
String clazz = stripEnding(clazzWithEnding);
if (lowercase) {
clazz = clazz.toLowerCase();
}
return clazz;
}
String getBundleName() {
String conventionalName = getConventionalName(true);
return this.getClass().getPackage().getName() + "." + conventionalName;
}
static String stripEnding(String clazz) {
if (!clazz.endsWith(DEFAULT_ENDING)) {
return clazz;
}
int viewIndex = clazz.lastIndexOf(DEFAULT_ENDING);
return clazz.substring(0, viewIndex);
}
public static ResourceBundle getResourceBundle(String name) {
try {
return getBundle(name);
} catch (MissingResourceException ex) {
return null;
}
}
/**
*
* @return an existing resource bundle, or null
*/
public ResourceBundle getResourceBundle() {
return this.bundle;
}
static ExecutorService getExecutorService() {
return Executors.newCachedThreadPool((r) -> {
Thread thread = Executors.defaultThreadFactory().newThread(r);
String name = thread.getName();
thread.setName("afterburner.fx-" + name);
thread.setDaemon(true);
return thread;
});
}
/**
*
* @param t exception to report
* @return nothing
*/
public Void exceptionReporter(Throwable t) {
System.err.println(t);
return null;
}
}