Skip to content

Commit

Permalink
Custom resource locations per @FxView
Browse files Browse the repository at this point in the history
  • Loading branch information
Eng-Fouad committed Oct 10, 2024
1 parent 85e6e00 commit 456b5c6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.function.Consumer;

import io.quarkiverse.fx.views.FxViewLocation;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
Expand Down Expand Up @@ -126,19 +127,26 @@ void fxViews(
final FxViewRecorder recorder,
final BeanContainerBuildItem beanContainerBuildItem) {

List<String> views = new ArrayList<>();
List<FxViewLocation> viewLocations = new ArrayList<>();

// Look for all @FxView annotations
Collection<AnnotationInstance> annotations = combinedIndex.getComputingIndex().getAnnotations(FxView.class);
for (AnnotationInstance annotation : annotations) {

ClassInfo target = annotation.target().asClass();
AnnotationValue value = annotation.value();
AnnotationValue fxmlLocationValue = annotation.value("fxmlLocation");
AnnotationValue styleLocationValue = annotation.value("styleLocation");
AnnotationValue bundleLocationValue = annotation.value("bundleLocation");

String fxmlLocation = fxmlLocationValue != null ? fxmlLocationValue.asString() : "";
String styleLocation = styleLocationValue != null ? styleLocationValue.asString() : "";
String bundleLocation = bundleLocationValue != null ? bundleLocationValue.asString() : "";

if (value != null) {
// Custom value is set in annotation : use it
String customName = value.asString();
views.add(customName);
viewLocations.add(new FxViewLocation(customName, fxmlLocation, styleLocation, bundleLocation));
} else {
// Use convention
// If controller is named "MySampleController", expected fxml would be MySample.fxml
Expand All @@ -153,21 +161,21 @@ void fxViews(

// Remove the controller suffix
String baseName = name.substring(0, name.length() - CONTROLLER_SUFFIX.length());
views.add(baseName);
viewLocations.add(new FxViewLocation(baseName, fxmlLocation, styleLocation, bundleLocation));
} else {
LOGGER.warnf(
"Type %s is annotated with %s but does not comply with naming convention (shall end with %s)",
name,
FxView.class.getName(),
CONTROLLER_SUFFIX);

views.add(name);
viewLocations.add(new FxViewLocation(name, fxmlLocation, styleLocation, bundleLocation));
}
}
}

LOGGER.infof("Fx views : {}", views);
LOGGER.infof("Fx views : {}", viewLocations);

recorder.process(views, beanContainerBuildItem.getValue());
recorder.process(viewLocations, beanContainerBuildItem.getValue());
}
}
18 changes: 18 additions & 0 deletions runtime/src/main/java/io/quarkiverse/fx/views/FxView.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,22 @@
* If omitted, name will use convention (controller's name without the "Controller" suffix)
*/
String value() default "";

/**
* Defines location for fx resource (.fxml)
* If omitted, "${quarkus.fx.fxml-root}/${baseName}.fxml" will be used.
*/
String fxmlLocation() default "";

/**
* Root location for style resources (.css)
* If omitted, "${quarkus.fx.style-root}/${baseName}.css" will be used.
*/
String styleLocation() default "";

/**
* Root location for bundle resources (.properties)
* If omitted, "${quarkus.fx.bundle-root}/${baseName}.properties" will be used.
*/
String bundleLocation() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.quarkiverse.fx.views;

public record FxViewLocation(String baseName, String fxmlLocation, String styleLocation, String bundleLocation){}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
@Recorder
public class FxViewRecorder {

public void process(final List<String> viewNames, final BeanContainer beanContainer) {
public void process(final List<FxViewLocation> viewLocations, final BeanContainer beanContainer) {
FxViewRepository fxViewRepository = beanContainer.beanInstance(FxViewRepository.class);
fxViewRepository.setViewNames(viewNames);
fxViewRepository.setViewLocations(viewLocations);
}
}
23 changes: 13 additions & 10 deletions runtime/src/main/java/io/quarkiverse/fx/views/FxViewRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public class FxViewRepository {
@Inject
FxViewConfig config;

private List<String> viewNames;
private List<FxViewLocation> viewLocations;

public void setViewNames(final List<String> views) {
this.viewNames = views;
public void setViewLocations(final List<FxViewLocation> viewLocations) {
this.viewLocations = viewLocations;
}

/**
Expand All @@ -54,22 +54,25 @@ public void setViewNames(final List<String> views) {
void setupViews(@Observes final FxViewLoadEvent event) {

// Skip processing if no FX view is set
if (this.viewNames.isEmpty()) {
if (this.viewLocations.isEmpty()) {
return;
}

boolean stylesheetReload = LaunchMode.current() == LaunchMode.DEVELOPMENT && this.config.stylesheetReload();

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

for (String name : this.viewNames) {
for (var viewLocation : this.viewLocations) {

FXMLLoader loader = this.fxmlLoader.get();

// Append path and extensions
String fxml = this.config.fxmlRoot() + name + FXML_EXT;
String css = this.config.styleRoot() + name + STYLE_EXT;
String resources = this.config.bundleRoot() + name;
String fxml = viewLocation.fxmlLocation().isBlank() ?
this.config.fxmlRoot() + viewLocation.baseName() + FXML_EXT : viewLocation.fxmlLocation();
String css = viewLocation.styleLocation().isBlank() ?
this.config.styleRoot() + viewLocation.baseName() + STYLE_EXT : viewLocation.styleLocation();
String resources = viewLocation.bundleLocation().isBlank() ?
this.config.bundleRoot() + viewLocation.baseName() : viewLocation.bundleLocation();

// Resources
ResourceBundle bundle = null;
Expand Down Expand Up @@ -129,10 +132,10 @@ void setupViews(@Observes final FxViewLoadEvent event) {

// Register view
FxViewData viewData = FxViewData.of(rootNode, controller);
this.viewDataMap.put(name, viewData);
this.viewDataMap.put(viewLocation.baseName(), viewData);

} catch (IOException e) {
throw new IllegalStateException("Failed to load FX view " + name, e);
throw new IllegalStateException("Failed to load FX view " + viewLocation.baseName(), e);
}
}
}
Expand Down

0 comments on commit 456b5c6

Please sign in to comment.