From c968a24e8dfbaaecd09507f655d56d2c26e5d9e2 Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Sat, 14 Aug 2021 05:00:32 +0200 Subject: [PATCH] ExtensionForm split launchForm() to different class --- .../java/gearth/extensions/ExtensionBase.java | 10 +++--- .../java/gearth/extensions/ExtensionForm.java | 11 ++---- .../extensions/ExtensionFormCreator.java | 14 ++++++++ .../extensions/ExtensionFormLauncher.java | 13 +++---- .../gearth/extensions/InternalExtension.java | 2 -- .../InternalExtensionFormBuilder.java | 11 +++--- .../InternalExtensionFormLauncher.java | 10 ++++++ .../simple/SimpleExtensionProducer.java | 10 +++--- .../BlockAndReplacePackets.java | 24 +------------ .../BlockAndReplacePacketsLauncher.java | 27 +++++++++++++++ .../PacketInfoExplorer.java | 15 -------- .../PacketInfoExplorerLauncher.java | 28 +++++++++++++++ .../uilogger/UiLogger.java | 23 +++---------- .../uilogger/UiLoggerLauncher.java | 34 +++++++++++++++++++ .../loggerdisplays/PacketLoggerFactory.java | 5 +-- 15 files changed, 146 insertions(+), 91 deletions(-) create mode 100644 G-Earth/src/main/java/gearth/extensions/ExtensionFormCreator.java create mode 100644 G-Earth/src/main/java/gearth/extensions/InternalExtensionFormLauncher.java create mode 100644 G-Earth/src/main/java/gearth/services/internal_extensions/blockreplacepackets/BlockAndReplacePacketsLauncher.java create mode 100644 G-Earth/src/main/java/gearth/services/internal_extensions/packetinfoexplorer/PacketInfoExplorerLauncher.java create mode 100644 G-Earth/src/main/java/gearth/services/internal_extensions/uilogger/UiLoggerLauncher.java diff --git a/G-Earth/src/main/java/gearth/extensions/ExtensionBase.java b/G-Earth/src/main/java/gearth/extensions/ExtensionBase.java index 83b9cc7..542a3c6 100644 --- a/G-Earth/src/main/java/gearth/extensions/ExtensionBase.java +++ b/G-Earth/src/main/java/gearth/extensions/ExtensionBase.java @@ -50,10 +50,9 @@ public abstract class ExtensionBase extends IExtension { if (!listeners.containsKey(headerId)) { listeners.put(headerId, new ArrayList<>()); } + + listeners.get(headerId).add(messageListener); } - - - listeners.get(headerId).add(messageListener); } /** @@ -72,10 +71,9 @@ public abstract class ExtensionBase extends IExtension { if (!listeners.containsKey(hashOrName)) { listeners.put(hashOrName, new ArrayList<>()); } + + listeners.get(hashOrName).add(messageListener); } - - - listeners.get(hashOrName).add(messageListener); } /** diff --git a/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java b/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java index 94b0f09..326eaf0 100644 --- a/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java +++ b/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java @@ -16,14 +16,6 @@ public abstract class ExtensionForm extends ExtensionBase { protected volatile Stage primaryStage; volatile HostServices hostServices; - protected static void runExtensionForm(String[] args, Class extension) { - ExtensionFormLauncher launcher = new ExtensionFormLauncher(); - launcher.trigger(extension, args); - } - - - public abstract ExtensionForm launchForm(Stage primaryStage) throws Exception; - //wrap extension methods public boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback){ return extension.requestFlags(flagRequestCallback); @@ -37,6 +29,9 @@ public abstract class ExtensionForm extends ExtensionBase { public void intercept(HMessage.Direction direction, Extension.MessageListener messageListener) { extension.intercept(direction, messageListener); } + public void intercept(HMessage.Direction direction, String hashOrName, Extension.MessageListener messageListener){ + extension.intercept(direction, hashOrName, messageListener); + } public void intercept(HMessage.Direction direction, int headerId, Extension.MessageListener messageListener){ extension.intercept(direction, headerId, messageListener); } diff --git a/G-Earth/src/main/java/gearth/extensions/ExtensionFormCreator.java b/G-Earth/src/main/java/gearth/extensions/ExtensionFormCreator.java new file mode 100644 index 0000000..9aa33c2 --- /dev/null +++ b/G-Earth/src/main/java/gearth/extensions/ExtensionFormCreator.java @@ -0,0 +1,14 @@ +package gearth.extensions; + +import javafx.stage.Stage; + +public abstract class ExtensionFormCreator { + + // creates an ExtensionForm object and initializes the JavaFX application + protected abstract ExtensionForm createForm(Stage primaryStage) throws Exception; + + public static void runExtensionForm(String[] args, Class creator) { + ExtensionFormLauncher.trigger(creator, args); + } + +} diff --git a/G-Earth/src/main/java/gearth/extensions/ExtensionFormLauncher.java b/G-Earth/src/main/java/gearth/extensions/ExtensionFormLauncher.java index 5b78a82..170b398 100644 --- a/G-Earth/src/main/java/gearth/extensions/ExtensionFormLauncher.java +++ b/G-Earth/src/main/java/gearth/extensions/ExtensionFormLauncher.java @@ -9,15 +9,16 @@ import javafx.stage.Stage; */ public class ExtensionFormLauncher extends Application { - private static Class extension; + private static Class extensionFormCreator; private static String[] args; @Override public void start(Stage primaryStage) throws Exception { - ExtensionInfo extInfo = extension.getAnnotation(ExtensionInfo.class); +// ExtensionInfo extInfo = extensionFormCreator.getAnnotation(ExtensionInfo.class); - ExtensionForm creator = extension.newInstance(); - ExtensionForm extensionForm = creator.launchForm(primaryStage); + ExtensionFormCreator creator = extensionFormCreator.newInstance(); + ExtensionForm extensionForm = creator.createForm(primaryStage); + ExtensionInfo extInfo = extensionForm.getClass().getAnnotation(ExtensionInfo.class); Extension extension = new Extension(args) { @Override @@ -77,8 +78,8 @@ public class ExtensionFormLauncher extends Application { }); } - public static void trigger( Class extension, String[] args) { - ExtensionFormLauncher.extension = extension; + public static void trigger(Class creator, String[] args) { + ExtensionFormLauncher.extensionFormCreator = creator; ExtensionFormLauncher.args = args; launch(args); } diff --git a/G-Earth/src/main/java/gearth/extensions/InternalExtension.java b/G-Earth/src/main/java/gearth/extensions/InternalExtension.java index 5f870d2..6b97d48 100644 --- a/G-Earth/src/main/java/gearth/extensions/InternalExtension.java +++ b/G-Earth/src/main/java/gearth/extensions/InternalExtension.java @@ -1,8 +1,6 @@ package gearth.extensions; import gearth.Main; -import gearth.extensions.ExtensionBase; -import gearth.extensions.IExtension; import gearth.protocol.HPacket; diff --git a/G-Earth/src/main/java/gearth/extensions/InternalExtensionFormBuilder.java b/G-Earth/src/main/java/gearth/extensions/InternalExtensionFormBuilder.java index ee15a33..95bf432 100644 --- a/G-Earth/src/main/java/gearth/extensions/InternalExtensionFormBuilder.java +++ b/G-Earth/src/main/java/gearth/extensions/InternalExtensionFormBuilder.java @@ -6,15 +6,14 @@ import gearth.services.extension_handler.extensions.extensionproducers.Extension import javafx.application.Platform; import javafx.stage.Stage; -public class InternalExtensionFormBuilder { +public class InternalExtensionFormBuilder, T extends ExtensionForm> { - public T launch(Class extensionClass, ExtensionProducerObserver observer) { + public T launch(L launcher, ExtensionProducerObserver observer) { try { - ExtensionInfo extInfo = extensionClass.getAnnotation(ExtensionInfo.class); - T creator = extensionClass.newInstance(); - Stage stage = new Stage(); - T extensionForm = (T)(creator.launchForm(stage)); + T extensionForm = launcher.createForm(stage); + + ExtensionInfo extInfo = extensionForm.getClass().getAnnotation(ExtensionInfo.class); InternalExtension internalExtension = new InternalExtension() { @Override diff --git a/G-Earth/src/main/java/gearth/extensions/InternalExtensionFormLauncher.java b/G-Earth/src/main/java/gearth/extensions/InternalExtensionFormLauncher.java new file mode 100644 index 0000000..a6a2d74 --- /dev/null +++ b/G-Earth/src/main/java/gearth/extensions/InternalExtensionFormLauncher.java @@ -0,0 +1,10 @@ +package gearth.extensions; + +import javafx.stage.Stage; + +public abstract class InternalExtensionFormLauncher { + + // creates an ExtensionForm object and initializes the JavaFX application + public abstract T createForm(Stage primaryStage) throws Exception; + +} diff --git a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/simple/SimpleExtensionProducer.java b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/simple/SimpleExtensionProducer.java index da2cb75..c28e6d0 100644 --- a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/simple/SimpleExtensionProducer.java +++ b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/simple/SimpleExtensionProducer.java @@ -4,7 +4,9 @@ import gearth.extensions.InternalExtensionFormBuilder; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducer; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducerObserver; import gearth.services.internal_extensions.blockreplacepackets.BlockAndReplacePackets; +import gearth.services.internal_extensions.blockreplacepackets.BlockAndReplacePacketsLauncher; import gearth.services.internal_extensions.packetinfoexplorer.PacketInfoExplorer; +import gearth.services.internal_extensions.packetinfoexplorer.PacketInfoExplorerLauncher; public class SimpleExtensionProducer implements ExtensionProducer { @@ -14,10 +16,10 @@ public class SimpleExtensionProducer implements ExtensionProducer { // uncomment the next line if you want to see an embedded example extension in G-Earth // observer.onExtensionProduced(new ExampleExtension()); - new InternalExtensionFormBuilder() - .launch(BlockAndReplacePackets.class, observer); + new InternalExtensionFormBuilder() + .launch(new BlockAndReplacePacketsLauncher(), observer); - new InternalExtensionFormBuilder() - .launch(PacketInfoExplorer.class, observer); + new InternalExtensionFormBuilder() + .launch(new PacketInfoExplorerLauncher(), observer); } } diff --git a/G-Earth/src/main/java/gearth/services/internal_extensions/blockreplacepackets/BlockAndReplacePackets.java b/G-Earth/src/main/java/gearth/services/internal_extensions/blockreplacepackets/BlockAndReplacePackets.java index a30e3fe..be0953b 100644 --- a/G-Earth/src/main/java/gearth/services/internal_extensions/blockreplacepackets/BlockAndReplacePackets.java +++ b/G-Earth/src/main/java/gearth/services/internal_extensions/blockreplacepackets/BlockAndReplacePackets.java @@ -1,28 +1,20 @@ package gearth.services.internal_extensions.blockreplacepackets; -import gearth.Main; import gearth.extensions.ExtensionForm; import gearth.extensions.ExtensionInfo; -import gearth.services.packet_info.PacketInfo; -import gearth.services.packet_info.PacketInfoManager; import gearth.protocol.HMessage; import gearth.protocol.HPacket; import gearth.services.internal_extensions.blockreplacepackets.rules.BlockReplaceRule; import gearth.services.internal_extensions.blockreplacepackets.rules.RuleFactory; -import gearth.ui.GEarthController; +import gearth.services.packet_info.PacketInfo; import javafx.application.Platform; import javafx.event.ActionEvent; -import javafx.fxml.FXMLLoader; -import javafx.scene.Parent; -import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ComboBox; import javafx.scene.control.ScrollPane; import javafx.scene.control.TextField; -import javafx.scene.image.Image; import javafx.scene.layout.GridPane; import javafx.scene.layout.VBox; -import javafx.stage.Stage; import java.util.ArrayList; import java.util.List; @@ -210,20 +202,6 @@ public class BlockAndReplacePackets extends ExtensionForm { refreshOptions(); } - @Override - public ExtensionForm launchForm(Stage primaryStage) throws Exception { - FXMLLoader loader = new FXMLLoader(BlockAndReplacePackets.class.getResource("blockreplace.fxml")); - Parent root = loader.load(); - - primaryStage.setTitle("Packet blocker &/ replacer"); - primaryStage.setScene(new Scene(root)); - primaryStage.setResizable(false); - primaryStage.getScene().getStylesheets().add(GEarthController.class.getResource("/gearth/ui/bootstrap3.css").toExternalForm()); - primaryStage.getIcons().add(new Image(Main.class.getResourceAsStream("G-EarthLogoSmaller.png"))); - - return loader.getController(); - } - @Override protected void onShow() { Platform.runLater(() -> cmb_type.requestFocus()); diff --git a/G-Earth/src/main/java/gearth/services/internal_extensions/blockreplacepackets/BlockAndReplacePacketsLauncher.java b/G-Earth/src/main/java/gearth/services/internal_extensions/blockreplacepackets/BlockAndReplacePacketsLauncher.java new file mode 100644 index 0000000..baea160 --- /dev/null +++ b/G-Earth/src/main/java/gearth/services/internal_extensions/blockreplacepackets/BlockAndReplacePacketsLauncher.java @@ -0,0 +1,27 @@ +package gearth.services.internal_extensions.blockreplacepackets; + +import gearth.Main; +import gearth.extensions.InternalExtensionFormLauncher; +import gearth.ui.GEarthController; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.image.Image; +import javafx.stage.Stage; + +public class BlockAndReplacePacketsLauncher extends InternalExtensionFormLauncher { + + @Override + public BlockAndReplacePackets createForm(Stage primaryStage) throws Exception { + FXMLLoader loader = new FXMLLoader(BlockAndReplacePackets.class.getResource("blockreplace.fxml")); + Parent root = loader.load(); + + primaryStage.setTitle("Packet blocker &/ replacer"); + primaryStage.setScene(new Scene(root)); + primaryStage.setResizable(false); + primaryStage.getScene().getStylesheets().add(GEarthController.class.getResource("/gearth/ui/bootstrap3.css").toExternalForm()); + primaryStage.getIcons().add(new Image(Main.class.getResourceAsStream("G-EarthLogoSmaller.png"))); + + return loader.getController(); + } +} diff --git a/G-Earth/src/main/java/gearth/services/internal_extensions/packetinfoexplorer/PacketInfoExplorer.java b/G-Earth/src/main/java/gearth/services/internal_extensions/packetinfoexplorer/PacketInfoExplorer.java index 5fe5551..2186a28 100644 --- a/G-Earth/src/main/java/gearth/services/internal_extensions/packetinfoexplorer/PacketInfoExplorer.java +++ b/G-Earth/src/main/java/gearth/services/internal_extensions/packetinfoexplorer/PacketInfoExplorer.java @@ -45,21 +45,6 @@ public class PacketInfoExplorer extends ExtensionForm { private TableView tableView; public GridPane grid; - @Override - public ExtensionForm launchForm(Stage primaryStage) throws Exception { - FXMLLoader loader = new FXMLLoader(PacketInfoExplorer.class.getResource("PacketInfoExplorer.fxml")); - Parent root = loader.load(); - - primaryStage.setTitle("Packet info explorer"); - primaryStage.setScene(new Scene(root)); - primaryStage.setMinWidth(430); - primaryStage.setMinHeight(260); - primaryStage.getScene().getStylesheets().add(GEarthController.class.getResource("/gearth/ui/bootstrap3.css").toExternalForm()); - primaryStage.getIcons().add(new Image(Main.class.getResourceAsStream("G-EarthLogoSmaller.png"))); - - return loader.getController(); - } - public void initialize() { Platform.runLater( () -> grid.requestFocus() ); diff --git a/G-Earth/src/main/java/gearth/services/internal_extensions/packetinfoexplorer/PacketInfoExplorerLauncher.java b/G-Earth/src/main/java/gearth/services/internal_extensions/packetinfoexplorer/PacketInfoExplorerLauncher.java new file mode 100644 index 0000000..4a128e3 --- /dev/null +++ b/G-Earth/src/main/java/gearth/services/internal_extensions/packetinfoexplorer/PacketInfoExplorerLauncher.java @@ -0,0 +1,28 @@ +package gearth.services.internal_extensions.packetinfoexplorer; + +import gearth.Main; +import gearth.extensions.InternalExtensionFormLauncher; +import gearth.ui.GEarthController; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.image.Image; +import javafx.stage.Stage; + +public class PacketInfoExplorerLauncher extends InternalExtensionFormLauncher { + + @Override + public PacketInfoExplorer createForm(Stage primaryStage) throws Exception { + FXMLLoader loader = new FXMLLoader(PacketInfoExplorer.class.getResource("PacketInfoExplorer.fxml")); + Parent root = loader.load(); + + primaryStage.setTitle("Packet info explorer"); + primaryStage.setScene(new Scene(root)); + primaryStage.setMinWidth(430); + primaryStage.setMinHeight(260); + primaryStage.getScene().getStylesheets().add(GEarthController.class.getResource("/gearth/ui/bootstrap3.css").toExternalForm()); + primaryStage.getIcons().add(new Image(Main.class.getResourceAsStream("G-EarthLogoSmaller.png"))); + + return loader.getController(); + } +} diff --git a/G-Earth/src/main/java/gearth/services/internal_extensions/uilogger/UiLogger.java b/G-Earth/src/main/java/gearth/services/internal_extensions/uilogger/UiLogger.java index 699adcf..6a75654 100644 --- a/G-Earth/src/main/java/gearth/services/internal_extensions/uilogger/UiLogger.java +++ b/G-Earth/src/main/java/gearth/services/internal_extensions/uilogger/UiLogger.java @@ -57,25 +57,6 @@ public class UiLogger extends ExtensionForm implements PacketLogger { controller.onDisconnect(); } - @Override - public ExtensionForm launchForm(Stage stage) throws Exception { - FXMLLoader loader = new FXMLLoader(UiLogger.class.getResource("UiLogger.fxml")); - - Parent root = loader.load(); - stage.setTitle("G-Earth | Packet Logger"); - stage.initModality(Modality.NONE); - stage.getIcons().add(new Image(getClass().getResourceAsStream("/gearth/G-EarthLogoSmaller.png"))); - - Scene scene = new Scene(root); - scene.getStylesheets().add("/gearth/ui/bootstrap3.css"); - scene.getStylesheets().add("/gearth/services/internal_extensions/uilogger/logger.css"); - controller = loader.getController(); - controller.setStage(stage); - - stage.setScene(scene); - return this; - } - private class Elem { HPacket packet; int types; @@ -104,4 +85,8 @@ public class UiLogger extends ExtensionForm implements PacketLogger { protected boolean canDelete() { return false; } + + public void setController(UiLoggerController controller) { + this.controller = controller; + } } diff --git a/G-Earth/src/main/java/gearth/services/internal_extensions/uilogger/UiLoggerLauncher.java b/G-Earth/src/main/java/gearth/services/internal_extensions/uilogger/UiLoggerLauncher.java new file mode 100644 index 0000000..aa70a10 --- /dev/null +++ b/G-Earth/src/main/java/gearth/services/internal_extensions/uilogger/UiLoggerLauncher.java @@ -0,0 +1,34 @@ +package gearth.services.internal_extensions.uilogger; + +import gearth.extensions.InternalExtensionFormLauncher; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.image.Image; +import javafx.stage.Modality; +import javafx.stage.Stage; + +public class UiLoggerLauncher extends InternalExtensionFormLauncher { + @Override + public UiLogger createForm(Stage stage) throws Exception { + UiLogger uiLogger = new UiLogger(); + + FXMLLoader loader = new FXMLLoader(UiLogger.class.getResource("UiLogger.fxml")); + + Parent root = loader.load(); + stage.setTitle("G-Earth | Packet Logger"); + stage.initModality(Modality.NONE); + stage.getIcons().add(new Image(getClass().getResourceAsStream("/gearth/G-EarthLogoSmaller.png"))); + + Scene scene = new Scene(root); + scene.getStylesheets().add("/gearth/ui/bootstrap3.css"); + scene.getStylesheets().add("/gearth/services/internal_extensions/uilogger/logger.css"); + + UiLoggerController controller = loader.getController(); + uiLogger.setController(controller); + controller.setStage(stage); + + stage.setScene(scene); + return uiLogger; + } +} diff --git a/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/PacketLoggerFactory.java b/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/PacketLoggerFactory.java index ef53e90..1f12f88 100644 --- a/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/PacketLoggerFactory.java +++ b/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/PacketLoggerFactory.java @@ -7,6 +7,7 @@ import gearth.services.extension_handler.ExtensionHandler; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducer; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducerObserver; import gearth.services.internal_extensions.uilogger.UiLogger; +import gearth.services.internal_extensions.uilogger.UiLoggerLauncher; /** * Created by Jonas on 04/04/18. @@ -39,8 +40,8 @@ public class PacketLoggerFactory implements ExtensionProducer { @Override public void startProducing(ExtensionProducerObserver observer) { if (usesUIlogger()) { - uiLogger = new InternalExtensionFormBuilder() - .launch(UiLogger.class, observer); + uiLogger = new InternalExtensionFormBuilder() + .launch(new UiLoggerLauncher(), observer); } } }