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

Ported code to use FX properties and housekeeping #162

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 31 additions & 62 deletions G-Earth/src/main/java/gearth/GEarth.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
package gearth;

import gearth.misc.AdminValidator;
import gearth.misc.Cacher;
import gearth.misc.UpdateChecker;
import gearth.misc.listenerpattern.ObservableObject;
import gearth.ui.GEarthController;
import gearth.ui.GEarthProperties;
import gearth.ui.themes.Theme;
import gearth.ui.themes.ThemeFactory;
import gearth.ui.titlebar.TitleBarConfig;
import gearth.ui.titlebar.TitleBarController;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.IOException;

public class GEarth extends Application {

public static GEarth main;
public static String version = "1.5.3";
public static String gitApi = "https://api.github.com/repos/sirjonasxx/G-Earth/releases/latest";
public static ObservableObject<Theme> observableTheme;

private Stage stage;
private GEarthController controller;

static {
observableTheme = new ObservableObject<>(
Cacher.getCacheContents().has("theme") ?
ThemeFactory.themeForTitle(Cacher.getCacheContents().getString("theme")) :
ThemeFactory.getDefaultTheme()
);
}

@Override
public void start(Stage primaryStage) throws Exception{
Expand All @@ -52,9 +44,28 @@ public void start(Stage primaryStage) throws Exception{
}
controller = loader.getController();
controller.setStage(primaryStage);
stage.initStyle(StageStyle.TRANSPARENT);

primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setScene(new Scene(root));
primaryStage.setAlwaysOnTop(GEarthProperties.isAlwaysOnTop());
GEarthProperties.alwaysOnTopProperty
.addListener((observable, oldValue, newValue) -> primaryStage.setAlwaysOnTop(newValue));

initTitleBar(primaryStage);
initTheme();

primaryStage.setResizable(false);
primaryStage.sizeToScene();

primaryStage.show();
primaryStage.setOnCloseRequest(event -> closeGEarth());

AdminValidator.validate();
UpdateChecker.checkForUpdates();

}

private void initTitleBar(Stage primaryStage) throws IOException {
TitleBarController.create(primaryStage, new TitleBarConfig() {
@Override
public boolean displayThemePicker() {
Expand All @@ -66,11 +77,6 @@ public boolean displayMinimizeButton() {
return true;
}

// @Override
// public boolean allowResizing() {
// return false;
// }

@Override
public void onCloseClicked() {
closeGEarth();
Expand All @@ -83,25 +89,20 @@ public void onMinimizeClicked() {

@Override
public void setTheme(Theme theme) {
setGearthTheme(theme);
GEarthProperties.themeProperty.set(theme);
}

@Override
public Theme getCurrentTheme() {
return observableTheme.getObject();
return GEarthProperties.getTheme();
}
});
primaryStage.setResizable(false);
primaryStage.sizeToScene();

setGearthTheme(observableTheme.getObject());

primaryStage.show();
primaryStage.setOnCloseRequest(event -> closeGEarth());

AdminValidator.validate();
UpdateChecker.checkForUpdates();
}

private void initTheme() {
stage.titleProperty().bind(GEarthProperties.themeTitleBinding);
Bindings.bindContent(stage.getScene().getStylesheets(), GEarthProperties.styleSheets);
Bindings.bindContent(stage.getIcons(), GEarthProperties.icons);
}

private void closeGEarth() {
Expand All @@ -110,30 +111,6 @@ private void closeGEarth() {
System.exit(0);
}

private void setGearthTheme(Theme theme) {
Cacher.put("theme", theme.title());
observableTheme.setObject(theme);
Theme defaultTheme = ThemeFactory.getDefaultTheme();

// Platform.runLater(() -> {
stage.getScene().getStylesheets().clear();
stage.getScene().getStylesheets().add(GEarth.class.getResource(String.format("/gearth/ui/themes/%s/styling.css", theme.internalName())).toExternalForm());

stage.getIcons().clear();
stage.getIcons().add(new Image(GEarth.class.getResourceAsStream(String.format("/gearth/ui/themes/%s/logoSmall.png", theme.overridesLogo() ? theme.internalName() : defaultTheme.internalName()))));
stage.setTitle((theme.overridesTitle() ? theme.title() : defaultTheme.title()) + " " + GEarth.version);

controller.infoController.img_logo.setImage(new Image(GEarth.class.getResourceAsStream(
String.format(
"/gearth/ui/themes/%s/logo.png",
theme.overridesLogo() ? theme.internalName() : defaultTheme.internalName()
)
)));
controller.infoController.version.setText(stage.getTitle());
// });

}

public static String[] args;

public static void main(String[] args) {
Expand Down Expand Up @@ -161,14 +138,6 @@ public static String getArgument(String... arg) {
return null;
}

public static ObservableObject<Theme> getThemeObservable() {
return observableTheme;
}

public static Theme getTheme() {
return observableTheme.getObject();
}

public static void setAlertOwner(Alert alert) {
alert.initOwner(main.stage);
}
Expand Down
16 changes: 6 additions & 10 deletions G-Earth/src/main/java/gearth/extensions/ExtensionBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@

import gearth.misc.HostInfo;
import gearth.misc.listenerpattern.Observable;
import gearth.misc.listenerpattern.ObservableObject;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket;
import gearth.services.packet_info.PacketInfo;
import gearth.services.packet_info.PacketInfoManager;
import javafx.beans.property.ObjectProperty;
import org.reactfx.util.Lists;
import javafx.beans.property.SimpleObjectProperty;

import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

public abstract class ExtensionBase extends IExtension {

Expand All @@ -33,10 +28,11 @@ public interface FlagsCheckListener {


volatile PacketInfoManager packetInfoManager = PacketInfoManager.EMPTY;
ObservableObject<HostInfo> observableHostInfo = new ObservableObject<>(null);

ObjectProperty<HostInfo> hostInfoProperty = new SimpleObjectProperty<>();

void updateHostInfo(HostInfo hostInfo) {
observableHostInfo.setObject(hostInfo);
hostInfoProperty.set(hostInfo);
}

/**
Expand Down Expand Up @@ -67,7 +63,7 @@ public void intercept(HMessage.Direction direction, int headerId, MessageListene
* @param messageListener the callback
*/
public void intercept(HMessage.Direction direction, String hashOrName, MessageListener messageListener) {
Map<String, List<MessageListener>> listeners =
final Map<String, List<MessageListener>> listeners =
direction == HMessage.Direction.TOCLIENT ?
hashOrNameIncomingListeners :
hashOrNameOutgoingListeners;
Expand Down Expand Up @@ -197,6 +193,6 @@ public PacketInfoManager getPacketInfoManager() {
}

public HostInfo getHostInfo() {
return observableHostInfo.getObject();
return hostInfoProperty.get();
}
}
19 changes: 14 additions & 5 deletions G-Earth/src/main/java/gearth/extensions/ExtensionForm.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package gearth.extensions;

import com.sun.org.apache.xpath.internal.operations.Bool;
import gearth.misc.HostInfo;
import gearth.misc.listenerpattern.Observable;
import gearth.services.packet_info.PacketInfoManager;
import javafx.application.HostServices;
import javafx.application.Platform;
import javafx.beans.InvalidationListener;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.stage.Stage;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket;

import java.util.function.Consumer;

/**
* Created by Jonas on 22/09/18.
*/
Expand Down Expand Up @@ -99,8 +99,17 @@ public HostServices getHostServices() {
}

public HostInfo getHostInfo() {
return extension.observableHostInfo.getObject();
return extension.hostInfoProperty.get();
}

private final BooleanProperty fieldsInitializedProperty = new SimpleBooleanProperty(false);

public void setFieldsInitialised(boolean value) {
fieldsInitializedProperty.set(value);
}

public BooleanProperty fieldsInitialisedProperty() {
return fieldsInitializedProperty;
}

Observable<Runnable> fieldsInitialized = new Observable<>(Runnable::run);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ protected boolean canDelete() {
extensionForm.extension = extension;

extensionForm.primaryStage = primaryStage;
extensionForm.fieldsInitialized.fireEvent();
extensionForm.setFieldsInitialised(true);

Thread t = new Thread(() -> {
extension.run();
//when the extension has ended, close this process
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected boolean canDelete() {
extensionForm.extension = internalExtension;
extensionForm.primaryStage = stage;

extensionForm.fieldsInitialized.fireEvent();
extensionForm.setFieldsInitialised(true);
GEarthExtension gEarthExtension = new InternalExtensionBuilder(internalExtension);
observer.onExtensionProduced(gEarthExtension);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,47 @@ public abstract class ThemedExtensionFormCreator extends ExtensionFormCreator {

@Override
protected ExtensionForm createForm(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getFormResource());
Parent root = loader.load();
final FXMLLoader loader = new FXMLLoader(getFormResource());
final Parent root = loader.load();

primaryStage.setTitle(getTitle());
primaryStage.setScene(new Scene(root));
initialize(primaryStage);
primaryStage.setResizable(false);
primaryStage.sizeToScene();

Theme defaultTheme = ThemeFactory.getDefaultTheme();
DefaultTitleBarConfig config = new DefaultTitleBarConfig(primaryStage, defaultTheme) {
final Theme defaultTheme = ThemeFactory.getDefaultTheme();
final DefaultTitleBarConfig config = new DefaultTitleBarConfig(primaryStage, defaultTheme) {
@Override
public boolean displayThemePicker() {
return false;
}
};
TitleBarController.create(primaryStage, config);
Platform.runLater(() -> {
primaryStage.getScene().getRoot().getStyleClass().add(defaultTheme.title().replace(" ", "-").toLowerCase());
primaryStage.getScene().getRoot().getStyleClass().add(defaultTheme.isDark() ? "g-dark" : "g-light");
});
Platform.runLater(() -> primaryStage.getScene().getRoot().getStyleClass().addAll(
defaultTheme.title().replace(" ", "-").toLowerCase(),
defaultTheme.isDark() ? "g-dark" : "g-light"
));

ExtensionForm extensionForm = loader.getController();
extensionForm.fieldsInitialized.addListener(() -> extensionForm.extension.observableHostInfo.addListener(hostInfo -> {
if (hostInfo.getAttributes().containsKey("theme")) {
String themeTitle = hostInfo.getAttributes().get("theme");
Theme theme = ThemeFactory.themeForTitle(themeTitle);
final ExtensionForm extensionForm = loader.getController();
extensionForm
.fieldsInitialisedProperty()
.addListener(observable -> listenForThemeChange(primaryStage, config, extensionForm));
return extensionForm;
}

private static void listenForThemeChange(Stage primaryStage, DefaultTitleBarConfig config, ExtensionForm extensionForm) {
extensionForm.extension.hostInfoProperty.addListener((observable, oldValue, newValue) -> {
final String themeTitle = newValue.getAttributes().get("theme");
if (themeTitle != null) {
final Theme theme = ThemeFactory.themeForTitle(themeTitle);
if (config.getCurrentTheme() != theme) {
String styleClassOld = config.getCurrentTheme().title().replace(" ", "-").toLowerCase();
String lightClassOld = config.getCurrentTheme().isDark() ? "g-dark" : "g-light";
String styleClassNew = theme.title().replace(" ", "-").toLowerCase();
String lightClassNew = theme.isDark() ? "g-dark" : "g-light";
final String styleClassOld = config.getCurrentTheme().title().replace(" ", "-").toLowerCase();
final String lightClassOld = config.getCurrentTheme().isDark() ? "g-dark" : "g-light";
final String styleClassNew = theme.title().replace(" ", "-").toLowerCase();
final String lightClassNew = theme.isDark() ? "g-dark" : "g-light";
config.setTheme(theme);
Parent currentRoot = primaryStage.getScene().getRoot();
final Parent currentRoot = primaryStage.getScene().getRoot();
Platform.runLater(() -> {
currentRoot.getStyleClass().remove(styleClassOld);
currentRoot.getStyleClass().add(styleClassNew);
Expand All @@ -62,13 +69,11 @@ public boolean displayThemePicker() {
});
}
}
}));


return extensionForm;
});
}

protected abstract String getTitle();

protected abstract URL getFormResource();

// can be overridden for more settings
Expand Down