Skip to content

Commit 2e075ec

Browse files
committed
Add annotations
1 parent e181d02 commit 2e075ec

File tree

6 files changed

+94
-45
lines changed

6 files changed

+94
-45
lines changed

.gitignore

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
/target
1+
/target/
22
.classpath
33
.project
4-
.settings/
4+
/.settings/
55
.springBeans
6-
7-
# Mobile Tools for Java (J2ME)
8-
.mtj.tmp/
9-
10-
# Package Files #
11-
*.jar
12-
*.war
13-
*.ear
14-
15-
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
16-
hs_err_pid*

.mvn/wrapper/maven-wrapper.jar

48.3 KB
Binary file not shown.

src/main/java/de/felixroske/jfx/support/AbstractFxmlView.java

+65-29
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@
2424
import javafx.scene.layout.AnchorPane;
2525

2626
/**
27-
* This class is derived from Adam Bien's <a href="http://afterburner.adam-bien.com/">afterburner.fx</a> project.
27+
* This class is derived from Adam Bien's
28+
* <a href="http://afterburner.adam-bien.com/">afterburner.fx</a> project.
2829
* <p>
2930
* {@link AbstractFxmlView} is a stripped down version of <a href=
3031
* "https://github.com/AdamBien/afterburner.fx/blob/02f25fdde9629fcce50ea8ace5dec4f802958c8d/src/main/java/com/airhacks/afterburner/views/FXMLView.java"
3132
* >FXMLView</a> that provides DI for Java FX Controllers via Spring.
3233
*
33-
* Felix Roske <[email protected]> changed this, so it always expects fxml Files by default in FXML_PATH.
34+
* Felix Roske <[email protected]> changed this to use annotation for fxml path.
3435
*
3536
* @author Thomas Darimont
3637
*/
3738
public abstract class AbstractFxmlView implements ApplicationContextAware {
3839

3940
private static String FXML_PATH = "/fxml/";
40-
41+
4142
protected ObjectProperty<Object> presenterProperty;
4243
protected FXMLLoader fxmlLoader;
4344
protected ResourceBundle bundle;
@@ -56,31 +57,45 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
5657

5758
this.applicationContext = applicationContext;
5859
}
59-
60+
6061
public AbstractFxmlView() {
6162
this(FXML_PATH);
6263
}
63-
64+
6465
public AbstractFxmlView(String path) {
6566
setFxmlRootPath(path);
67+
68+
// TODO refactor me!
69+
FXMLView annotation = getFXMLAnnotation();
70+
if (annotation != null && !annotation.value().equals("")) {
71+
this.resource = getClass().getResource(annotation.value());
72+
} else {
73+
this.resource = getClass().getResource(getFxmlPath());
74+
}
75+
6676
this.presenterProperty = new SimpleObjectProperty<>();
67-
this.resource = getClass().getResource(getFxmlPath());
77+
// this.resource = getClass().getResource(getFxmlPath());
6878
this.bundle = getResourceBundle(getBundleName());
6979
}
7080

81+
private FXMLView getFXMLAnnotation() {
82+
Class<? extends AbstractFxmlView> theClass = this.getClass();
83+
FXMLView annotation = theClass.getAnnotation(FXMLView.class);
84+
return annotation;
85+
}
86+
7187
private Object createControllerForType(Class<?> type) {
7288
return this.applicationContext.getBean(type);
7389
}
7490

7591
private void setFxmlRootPath(String path) {
76-
if(path.endsWith("/")) {
92+
if (path.endsWith("/")) {
7793
this.fxmlRoot = path;
78-
}
79-
else {
94+
} else {
8095
this.fxmlRoot = path + "/";
8196
}
8297
}
83-
98+
8499
FXMLLoader loadSynchronously(URL resource, ResourceBundle bundle) throws IllegalStateException {
85100

86101
FXMLLoader loader = new FXMLLoader(resource, bundle);
@@ -106,8 +121,8 @@ void ensureFxmlLoaderInitialized() {
106121
}
107122

108123
/**
109-
* Initializes the view by loading the FXML (if not happened yet) and returns the top Node (parent) specified in the
110-
* FXML file.
124+
* Initializes the view by loading the FXML (if not happened yet) and
125+
* returns the top Node (parent) specified in the FXML file.
111126
*
112127
* @return
113128
*/
@@ -121,17 +136,21 @@ public Parent getView() {
121136
}
122137

123138
/**
124-
* Initializes the view synchronously and invokes the consumer with the created parent Node within the FX UI thread.
139+
* Initializes the view synchronously and invokes the consumer with the
140+
* created parent Node within the FX UI thread.
125141
*
126-
* @param consumer - an object interested in received the {@link Parent} as callback
142+
* @param consumer
143+
* - an object interested in received the {@link Parent} as
144+
* callback
127145
*/
128146
public void getView(Consumer<Parent> consumer) {
129147
CompletableFuture.supplyAsync(this::getView, Platform::runLater).thenAccept(consumer);
130148
}
131149

132150
/**
133-
* Scene Builder creates for each FXML document a root container. This method omits the root container (e.g.
134-
* {@link AnchorPane}) and gives you the access to its first child.
151+
* Scene Builder creates for each FXML document a root container. This
152+
* method omits the root container (e.g. {@link AnchorPane}) and gives you
153+
* the access to its first child.
135154
*
136155
* @return the first child of the {@link AnchorPane}
137156
*/
@@ -146,7 +165,18 @@ public Node getViewWithoutRootContainer() {
146165
}
147166

148167
void addCSSIfAvailable(Parent parent) {
149-
168+
169+
// TODO refactor me!
170+
FXMLView annotation = getFXMLAnnotation();
171+
if(annotation != null && annotation.css().length > 0) {
172+
for (String cssFile : annotation.css()) {
173+
URL uri = getClass().getResource(cssFile);
174+
String uriToCss = uri.toExternalForm();
175+
parent.getStylesheets().add(uriToCss);
176+
}
177+
return;
178+
}
179+
150180
URL uri = getClass().getResource(getStyleSheetName());
151181
if (uri == null) {
152182
return;
@@ -161,10 +191,13 @@ String getStyleSheetName() {
161191
}
162192

163193
/**
164-
* In case the view was not initialized yet, the conventional fxml (airhacks.fxml for the AirhacksView and
165-
* AirhacksPresenter) are loaded and the specified presenter / controller is going to be constructed and returned.
194+
* In case the view was not initialized yet, the conventional fxml
195+
* (airhacks.fxml for the AirhacksView and AirhacksPresenter) are loaded and
196+
* the specified presenter / controller is going to be constructed and
197+
* returned.
166198
*
167-
* @return the corresponding controller / presenter (usually for a AirhacksView the AirhacksPresenter)
199+
* @return the corresponding controller / presenter (usually for a
200+
* AirhacksView the AirhacksPresenter)
168201
*/
169202
public Object getPresenter() {
170203

@@ -174,10 +207,12 @@ public Object getPresenter() {
174207
}
175208

176209
/**
177-
* Does not initialize the view. Only registers the Consumer and waits until the the view is going to be created / the
178-
* method FXMLView#getView or FXMLView#getViewAsync invoked.
210+
* Does not initialize the view. Only registers the Consumer and waits until
211+
* the the view is going to be created / the method FXMLView#getView or
212+
* FXMLView#getViewAsync invoked.
179213
*
180-
* @param presenterConsumer listener for the presenter construction
214+
* @param presenterConsumer
215+
* listener for the presenter construction
181216
*/
182217
public void getPresenter(Consumer<Object> presenterConsumer) {
183218

@@ -187,16 +222,17 @@ public void getPresenter(Consumer<Object> presenterConsumer) {
187222
}
188223

189224
/**
190-
* @param ending the suffix to append
225+
* @param ending
226+
* the suffix to append
191227
* @return the conventional name with stripped ending
192228
*/
193229
protected String getConventionalName(String ending) {
194230
return getConventionalName() + ending;
195231
}
196232

197233
/**
198-
* @return the name of the view without the "View" prefix in lowerCase. For AirhacksView just airhacks is going to be
199-
* returned.
234+
* @return the name of the view without the "View" prefix in lowerCase. For
235+
* AirhacksView just airhacks is going to be returned.
200236
*/
201237
protected String getConventionalName() {
202238
return stripEnding(getClass().getSimpleName().toLowerCase());
@@ -216,14 +252,15 @@ static String stripEnding(String clazz) {
216252
}
217253

218254
/**
219-
* @return the relative path to the fxml file derived from the FXML view. e.g. The name for the AirhacksView is going to be
255+
* @return the relative path to the fxml file derived from the FXML view.
256+
* e.g. The name for the AirhacksView is going to be
220257
* <PATH>/airhacks.fxml.
221258
*/
222259

223260
final String getFxmlPath() {
224261
return fxmlRoot + getConventionalName(".fxml");
225262
}
226-
263+
227264
private ResourceBundle getResourceBundle(String name) {
228265
try {
229266
return getBundle(name);
@@ -239,4 +276,3 @@ public ResourceBundle getResourceBundle() {
239276
return this.bundle;
240277
}
241278
}
242-

src/main/java/de/felixroske/jfx/support/AbstractJavaFxApplicationSupport.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.springframework.context.ConfigurableApplicationContext;
55

66
import javafx.application.Application;
7-
import javafx.scene.Parent;
87
import javafx.scene.Scene;
98
import javafx.stage.Stage;
109

@@ -47,8 +46,8 @@ public void showView(Class<? extends AbstractFxmlView> newView) {
4746

4847
// stage.setTitle(windowTitle);
4948
stage.setScene(scene);
50-
stage.setResizable(true);
51-
stage.centerOnScreen();
49+
// stage.setResizable(true);
50+
// stage.centerOnScreen();
5251
stage.show();
5352
}
5453

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package de.felixroske.jfx.support;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.RetentionPolicy;
5+
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
@Retention(RetentionPolicy.RUNTIME)
10+
public @interface FXMLController {
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package de.felixroske.jfx.support;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.RetentionPolicy;
5+
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
@Retention(RetentionPolicy.RUNTIME)
10+
public @interface FXMLView {
11+
String value() default "";
12+
String[] css() default {};
13+
}

0 commit comments

Comments
 (0)