Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to FXMLView logging and autoloading of javafx binary css files #4

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
<directory>src/test/java</directory>
<includes>
<include>**/*.fxml</include>
<include>**/*.bss</include>
<include>**/*.css</include>
<include>**/*.properties</include>
</includes>
Expand Down
40 changes: 29 additions & 11 deletions src/main/java/com/airhacks/afterburner/views/FXMLView.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import javafx.application.Platform;
Expand All @@ -53,7 +55,12 @@
*/
public abstract class FXMLView extends StackPane {

public final static String DEFAULT_ENDING = "View";
private static final Logger LOGGER = Logger.getLogger(FXMLView.class.getName());

public static final String DEFAULT_ENDING = "View";
public static final String CSS_FILE_ENDING = ".css";
public static final String BSS_FILE_ENDING = ".bss";

protected ObjectProperty<Object> presenterProperty;
protected FXMLLoader fxmlLoader;
protected String bundleName;
Expand Down Expand Up @@ -114,10 +121,9 @@ PresenterFactory discover() {
if (factories.size() == 1) {
return factories.get(0);
} else {
factories.forEach(System.err::println);
factories.forEach(s->LOGGER.severe(s.toString()));
throw new IllegalStateException("More than one PresenterFactories discovered");
}

}

void initializeFXMLLoader() {
Expand Down Expand Up @@ -165,7 +171,6 @@ public void getViewAsync(Consumer<Parent> consumer) {
CompletableFuture.supplyAsync(supplier, PARENT_CREATION_POOL).
thenAcceptAsync(consumer, FX_PLATFORM_EXECUTOR).
exceptionally(this::exceptionReporter);

}

/**
Expand All @@ -184,16 +189,29 @@ public Node getViewWithoutRootContainer() {
}

void addCSSIfAvailable(Parent parent) {
URL uri = getClass().getResource(getStyleSheetName());
URL uri = getClass().getResource(getBinaryStyleSheetName());
if (uri == null) {
uri = getClass().getResource(getStyleSheetName());
}
if (uri == null) {
return;
}
String uriToCss = uri.toExternalForm();
parent.getStylesheets().add(uriToCss);
if (!parent.getStylesheets().contains(uriToCss)){
parent.getStylesheets().add(uriToCss);
}
}

String getStyleSheetName() {
return getResourceCamelOrLowerCase(false, ".css");
return getResourceCamelOrLowerCase(false, CSS_FILE_ENDING);
}

/**
* .bss files are binary encoded css files which javafx produces.
* @return the conventional name of the bss file expected.
*/
String getBinaryStyleSheetName() {
return getResourceCamelOrLowerCase(false, BSS_FILE_ENDING);
}

/**
Expand All @@ -211,13 +229,13 @@ String getResourceCamelOrLowerCase(boolean mandatory, String ending) {
if (found != null) {
return name;
}
System.err.println("File: " + name + " not found, attempting with camel case");
LOGGER.config("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...");
LOGGER.severe(message);
LOGGER.severe("Stopping initialization phase...");
throw new IllegalStateException(message);
}
return name;
Expand Down Expand Up @@ -320,7 +338,7 @@ static ExecutorService getExecutorService() {
* @return nothing
*/
public Void exceptionReporter(Throwable t) {
System.err.println(t);
LOGGER.log(Level.SEVERE,"Exception thrown in afterburner.fx",t);
return null;
}

Expand Down
34 changes: 34 additions & 0 deletions src/test/java/com/airhacks/afterburner/views/CSSLoadTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.airhacks.afterburner.views;

import com.airhacks.afterburner.topgun.TopgunView;
import com.airhacks.afterburner.views.binary.BinaryView;
import org.junit.Test;

import static org.junit.Assert.assertTrue;

/**
* Tests to check that css files are loaded as required.
* @author Ben Oxley
*/
public class CSSLoadTest {

@Test
public void cssIsLoadedTest(){
TopgunView view = new TopgunView();
assertTrue(view.getView().getStylesheets().stream()
.anyMatch(s->s.contains("topgun.css"))
);
}

@Test
public void bssIsLoadedTest(){
BinaryView view = new BinaryView();
System.out.println(view.getView().getStylesheets());
assertTrue(view.getView().getStylesheets().stream()
.anyMatch(s->s.contains("binary.bss"))
);
assertTrue(view.getView().getStylesheets().stream()
.noneMatch(s->s.contains("binary.css"))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.airhacks.afterburner.views.binary;

/**
* @author Ben Oxley
*/
public class BinaryPresenter {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.airhacks.afterburner.views.binary;

import com.airhacks.afterburner.views.FXMLView;

/**
* @author Ben Oxley
*/
public class BinaryView extends FXMLView {

}
Binary file not shown.
26 changes: 26 additions & 0 deletions src/test/java/com/airhacks/afterburner/views/binary/binary.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* #%L
* afterburner.fx
* %%
* Copyright (C) 2013 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%
*/
/*
* Empty Stylesheet file.
*/

.mainFxmlClass {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="com.airhacks.afterburner.views.binary.BinaryPresenter">
<children><Pane prefHeight="200.0" prefWidth="200.0" />
</children></AnchorPane>