From 11314f9ba98525b6f5719c76ba13e603d03b0445 Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 30 May 2020 15:28:11 +0200 Subject: [PATCH] Added ability to load CSS files --- src/main/java/envoy/client/ui/Startup.java | 70 ++++++++----------- src/main/resources/client.properties | 4 +- src/main/resources/css/base.css | 4 ++ .../resources/{fxml/themes => css}/custom.css | 0 .../resources/{fxml/themes => css}/dark.css | 0 .../resources/{fxml/themes => css}/light.css | 0 src/main/resources/fxml/themes/base.css | 5 -- 7 files changed, 37 insertions(+), 46 deletions(-) create mode 100644 src/main/resources/css/base.css rename src/main/resources/{fxml/themes => css}/custom.css (100%) rename src/main/resources/{fxml/themes => css}/dark.css (100%) rename src/main/resources/{fxml/themes => css}/light.css (100%) delete mode 100644 src/main/resources/fxml/themes/base.css diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 12d54e4..8e367a6 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -3,7 +3,6 @@ package envoy.client.ui; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.nio.file.Paths; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; @@ -20,11 +19,13 @@ import javafx.scene.layout.Pane; import javafx.stage.Stage; import envoy.client.data.*; +import envoy.client.event.ThemeChangeEvent; import envoy.client.net.Client; import envoy.client.net.WriteProxy; import envoy.data.Message; import envoy.data.User; import envoy.data.User.UserStatus; +import envoy.event.EventBus; import envoy.exception.EnvoyException; import envoy.util.EnvoyLog; @@ -47,9 +48,6 @@ public final class Startup extends Application { private Stage stage; private Scene previousScene; - private final String[] CSSPaths = { "file://./src/main/resources/fxml/themes/base.css", - "file://./src/main/resources/fxml/themes/" + (settings.isUsingDefaultTheme() ? settings.getCurrentThemeName() : "custom") + ".css" }; - private static final Settings settings = Settings.getInstance(); private static final ClientConfig config = ClientConfig.getInstance(); private static final Logger logger = EnvoyLog.getLogger(Startup.class); @@ -62,17 +60,17 @@ public final class Startup extends Application { this.stage = stage; try { // Load the configuration from client.properties first - Properties properties = new Properties(); + final Properties properties = new Properties(); properties.load(Startup.class.getClassLoader().getResourceAsStream("client.properties")); config.load(properties); // Override configuration values with command line arguments - String[] args = getParameters().getRaw().toArray(new String[0]); + final String[] args = getParameters().getRaw().toArray(new String[0]); if (args.length > 0) config.load(args); // Check if all mandatory configuration values have been initialized if (!config.isInitialized()) throw new EnvoyException("Configuration is not fully initialized"); - } catch (Exception e) { + } catch (final Exception e) { new Alert(AlertType.ERROR, "Error loading configuration values:\n" + e); e.printStackTrace(); System.exit(1); @@ -90,7 +88,7 @@ public final class Startup extends Application { new Alert(AlertType.WARNING, "Ignoring local database.\nMessages will not be saved!").showAndWait(); } else try { localDB = new PersistentLocalDB(new File(config.getHomeDirectory(), config.getLocalDB().getPath())); - } catch (IOException e3) { + } catch (final IOException e3) { logger.log(Level.SEVERE, "Could not initialize local database", e3); new Alert(AlertType.ERROR, "Could not initialize local database!\n" + e3).showAndWait(); System.exit(1); @@ -111,9 +109,9 @@ public final class Startup extends Application { try { localDB.initializeUserStorage(); localDB.loadUserData(); - } catch (FileNotFoundException e) { + } catch (final FileNotFoundException e) { // The local database file has not yet been created, probably first login - } catch (Exception e) { + } catch (final Exception e) { e.printStackTrace(); new Alert(AlertType.ERROR, "Error while loading local database: " + e + "\nChats will not be stored locally.").showAndWait(); } @@ -140,8 +138,10 @@ public final class Startup extends Application { Platform.runLater(() -> { ((ChatSceneController) loader.getController()).initializeData(this, localDB, client, writeProxy); }); stage.setTitle("Envoy"); stage.getIcons().add(new Image(getClass().getResourceAsStream("/icons/envoy_logo.png"))); - stage.show(); + // TODO: Add capability to change custom CSS. In case of switching to a default + // theme, no further action is required + EventBus.getInstance().register(ThemeChangeEvent.class, theme -> applyCSS()); // Relay unread messages from cache if (cache != null && client.isOnline()) cache.relay(); @@ -154,8 +154,7 @@ public final class Startup extends Application { * @param fxmlLocation the location of the fxml file * @param layout the layout to use * @param savePrevious if true, the previous stage will be stored in this - * instance of Startup, else the variable storing it will - * be + * instance of Startup, else the variable storing it will be * set to null * @since Envoy Client v0.1-beta */ @@ -164,18 +163,16 @@ public final class Startup extends Application { try { // Clearing the loader so that a new Scene can be initialised loader = new FXMLLoader(); - var rootNode = loader.load(getClass().getResourceAsStream(fxmlLocation)); - var scene = new Scene(rootNode); + final var rootNode = loader.load(getClass().getResourceAsStream(fxmlLocation)); + final var scene = new Scene(rootNode); previousScene = savePrevious ? stage.getScene() : null; // Setting the visual appearance - scene.getStylesheets().addAll(CSSPaths); - System.out.println(Paths.get(".").toAbsolutePath().normalize().toString()); stage.setScene(scene); + applyCSS(); stage.show(); - // return loader.getController(); - } catch (IOException e) { + } catch (final IOException e) { new Alert(AlertType.ERROR, "The screen could not be updated due to reasons. (...bad programming...)"); - System.err.println("input: FXMLLocation: " + fxmlLocation + ", CSS paths: " + CSSPaths); + System.err.println("input: FXMLLocation: " + fxmlLocation); e.printStackTrace(); logger.severe("Something happened (while loading the new scene from " + fxmlLocation + ")"); } @@ -194,8 +191,9 @@ public final class Startup extends Application { if (previousScene == null) throw new IllegalStateException("Someone tried restoring a null scene. (Something happened)"); else { // switching previous and current - var temp = storeCurrent ? stage.getScene() : null; + final var temp = storeCurrent ? stage.getScene() : null; stage.setScene(previousScene); + applyCSS(); previousScene = temp; stage.show(); } @@ -208,7 +206,6 @@ public final class Startup extends Application { @Override public void stop() throws Exception { try { - // Save Settings and PersistentLocalDB on shutdown logger.info("Closing connection..."); client.close(); @@ -216,14 +213,11 @@ public final class Startup extends Application { logger.info("Saving local database and settings..."); localDB.save(); Settings.getInstance().save(); - } catch (Exception e) { + } catch (final Exception e) { logger.log(Level.SEVERE, "Unable to save local files", e); } } - @SuppressWarnings("javadoc") - public static void main(String[] args) { launch(args); } - /** * @return the controller of the current scene or a {@link NullPointerException} * if there is none @@ -235,21 +229,19 @@ public final class Startup extends Application { } /** - * @return the CSSPaths - * @since Envoy Client v0.1-beta - */ - public String[] getCSSPaths() { return CSSPaths; } - - /** - * Changes the currently displayed theme + * Sets the CSS files used for each scene. Should be called when the theme + * changes. * * @since Envoy Client v0.1-beta */ - public void changeTheme() { - // the base.css file should never be changed during runtime - CSSPaths[1] = "file://.fxml/themes/" + (settings.isUsingDefaultTheme() ? settings.getCurrentThemeName() : "custom") + ".css"; - var styleSheets = stage.getScene().getStylesheets(); - styleSheets.remove(styleSheets.size() - 1); - styleSheets.add(CSSPaths[1]); + public void applyCSS() { + final var styleSheets = stage.getScene().getStylesheets(); + styleSheets.clear(); + styleSheets.add(getClass().getResource("/css/base.css").toExternalForm()); + styleSheets.add(getClass().getResource("/css/" + (settings.isUsingDefaultTheme() ? settings.getCurrentThemeName() : "custom") + ".css") + .toExternalForm()); } + + @SuppressWarnings("javadoc") + public static void main(String[] args) { launch(args); } } diff --git a/src/main/resources/client.properties b/src/main/resources/client.properties index 2641927..609d61a 100644 --- a/src/main/resources/client.properties +++ b/src/main/resources/client.properties @@ -1,3 +1,3 @@ -server=http://kske.feste-ip.net -port=43315 +server=localhost +port=8080 localDB=.\\localDB diff --git a/src/main/resources/css/base.css b/src/main/resources/css/base.css new file mode 100644 index 0000000..059fefb --- /dev/null +++ b/src/main/resources/css/base.css @@ -0,0 +1,4 @@ +*{ + -fx-font: 14pt "Serif"; + -fx-background-color:#000000; + } diff --git a/src/main/resources/fxml/themes/custom.css b/src/main/resources/css/custom.css similarity index 100% rename from src/main/resources/fxml/themes/custom.css rename to src/main/resources/css/custom.css diff --git a/src/main/resources/fxml/themes/dark.css b/src/main/resources/css/dark.css similarity index 100% rename from src/main/resources/fxml/themes/dark.css rename to src/main/resources/css/dark.css diff --git a/src/main/resources/fxml/themes/light.css b/src/main/resources/css/light.css similarity index 100% rename from src/main/resources/fxml/themes/light.css rename to src/main/resources/css/light.css diff --git a/src/main/resources/fxml/themes/base.css b/src/main/resources/fxml/themes/base.css deleted file mode 100644 index 8cfc8ca..0000000 --- a/src/main/resources/fxml/themes/base.css +++ /dev/null @@ -1,5 +0,0 @@ -.root{ - -fx-font: 200pt "Serif"; - --background=#00FF00; - background: var(--background); -} \ No newline at end of file