ExtensionForm split launchForm() to different class

This commit is contained in:
sirjonasxx 2021-08-14 05:00:32 +02:00
parent 6621a468f1
commit c968a24e8d
15 changed files with 146 additions and 91 deletions

View File

@ -50,10 +50,9 @@ public abstract class ExtensionBase extends IExtension {
if (!listeners.containsKey(headerId)) { if (!listeners.containsKey(headerId)) {
listeners.put(headerId, new ArrayList<>()); 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)) { if (!listeners.containsKey(hashOrName)) {
listeners.put(hashOrName, new ArrayList<>()); listeners.put(hashOrName, new ArrayList<>());
} }
listeners.get(hashOrName).add(messageListener);
} }
listeners.get(hashOrName).add(messageListener);
} }
/** /**

View File

@ -16,14 +16,6 @@ public abstract class ExtensionForm extends ExtensionBase {
protected volatile Stage primaryStage; protected volatile Stage primaryStage;
volatile HostServices hostServices; volatile HostServices hostServices;
protected static void runExtensionForm(String[] args, Class<? extends ExtensionForm> extension) {
ExtensionFormLauncher launcher = new ExtensionFormLauncher();
launcher.trigger(extension, args);
}
public abstract ExtensionForm launchForm(Stage primaryStage) throws Exception;
//wrap extension methods //wrap extension methods
public boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback){ public boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback){
return extension.requestFlags(flagRequestCallback); return extension.requestFlags(flagRequestCallback);
@ -37,6 +29,9 @@ public abstract class ExtensionForm extends ExtensionBase {
public void intercept(HMessage.Direction direction, Extension.MessageListener messageListener) { public void intercept(HMessage.Direction direction, Extension.MessageListener messageListener) {
extension.intercept(direction, 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){ public void intercept(HMessage.Direction direction, int headerId, Extension.MessageListener messageListener){
extension.intercept(direction, headerId, messageListener); extension.intercept(direction, headerId, messageListener);
} }

View File

@ -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<? extends ExtensionFormCreator> creator) {
ExtensionFormLauncher.trigger(creator, args);
}
}

View File

@ -9,15 +9,16 @@ import javafx.stage.Stage;
*/ */
public class ExtensionFormLauncher extends Application { public class ExtensionFormLauncher extends Application {
private static Class<? extends ExtensionForm> extension; private static Class<? extends ExtensionFormCreator> extensionFormCreator;
private static String[] args; private static String[] args;
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
ExtensionInfo extInfo = extension.getAnnotation(ExtensionInfo.class); // ExtensionInfo extInfo = extensionFormCreator.getAnnotation(ExtensionInfo.class);
ExtensionForm creator = extension.newInstance(); ExtensionFormCreator creator = extensionFormCreator.newInstance();
ExtensionForm extensionForm = creator.launchForm(primaryStage); ExtensionForm extensionForm = creator.createForm(primaryStage);
ExtensionInfo extInfo = extensionForm.getClass().getAnnotation(ExtensionInfo.class);
Extension extension = new Extension(args) { Extension extension = new Extension(args) {
@Override @Override
@ -77,8 +78,8 @@ public class ExtensionFormLauncher extends Application {
}); });
} }
public static void trigger( Class<? extends ExtensionForm> extension, String[] args) { public static void trigger(Class<? extends ExtensionFormCreator> creator, String[] args) {
ExtensionFormLauncher.extension = extension; ExtensionFormLauncher.extensionFormCreator = creator;
ExtensionFormLauncher.args = args; ExtensionFormLauncher.args = args;
launch(args); launch(args);
} }

View File

@ -1,8 +1,6 @@
package gearth.extensions; package gearth.extensions;
import gearth.Main; import gearth.Main;
import gearth.extensions.ExtensionBase;
import gearth.extensions.IExtension;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;

View File

@ -6,15 +6,14 @@ import gearth.services.extension_handler.extensions.extensionproducers.Extension
import javafx.application.Platform; import javafx.application.Platform;
import javafx.stage.Stage; import javafx.stage.Stage;
public class InternalExtensionFormBuilder<T extends ExtensionForm> { public class InternalExtensionFormBuilder<L extends InternalExtensionFormLauncher<T>, T extends ExtensionForm> {
public T launch(Class<T> extensionClass, ExtensionProducerObserver observer) { public T launch(L launcher, ExtensionProducerObserver observer) {
try { try {
ExtensionInfo extInfo = extensionClass.getAnnotation(ExtensionInfo.class);
T creator = extensionClass.newInstance();
Stage stage = new Stage(); 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() { InternalExtension internalExtension = new InternalExtension() {
@Override @Override

View File

@ -0,0 +1,10 @@
package gearth.extensions;
import javafx.stage.Stage;
public abstract class InternalExtensionFormLauncher<T extends ExtensionForm> {
// creates an ExtensionForm object and initializes the JavaFX application
public abstract T createForm(Stage primaryStage) throws Exception;
}

View File

@ -4,7 +4,9 @@ import gearth.extensions.InternalExtensionFormBuilder;
import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducer; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducer;
import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducerObserver; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducerObserver;
import gearth.services.internal_extensions.blockreplacepackets.BlockAndReplacePackets; 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.PacketInfoExplorer;
import gearth.services.internal_extensions.packetinfoexplorer.PacketInfoExplorerLauncher;
public class SimpleExtensionProducer implements ExtensionProducer { 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 // uncomment the next line if you want to see an embedded example extension in G-Earth
// observer.onExtensionProduced(new ExampleExtension()); // observer.onExtensionProduced(new ExampleExtension());
new InternalExtensionFormBuilder<BlockAndReplacePackets>() new InternalExtensionFormBuilder<BlockAndReplacePacketsLauncher, BlockAndReplacePackets>()
.launch(BlockAndReplacePackets.class, observer); .launch(new BlockAndReplacePacketsLauncher(), observer);
new InternalExtensionFormBuilder<PacketInfoExplorer>() new InternalExtensionFormBuilder<PacketInfoExplorerLauncher, PacketInfoExplorer>()
.launch(PacketInfoExplorer.class, observer); .launch(new PacketInfoExplorerLauncher(), observer);
} }
} }

View File

@ -1,28 +1,20 @@
package gearth.services.internal_extensions.blockreplacepackets; package gearth.services.internal_extensions.blockreplacepackets;
import gearth.Main;
import gearth.extensions.ExtensionForm; import gearth.extensions.ExtensionForm;
import gearth.extensions.ExtensionInfo; import gearth.extensions.ExtensionInfo;
import gearth.services.packet_info.PacketInfo;
import gearth.services.packet_info.PacketInfoManager;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.services.internal_extensions.blockreplacepackets.rules.BlockReplaceRule; import gearth.services.internal_extensions.blockreplacepackets.rules.BlockReplaceRule;
import gearth.services.internal_extensions.blockreplacepackets.rules.RuleFactory; import gearth.services.internal_extensions.blockreplacepackets.rules.RuleFactory;
import gearth.ui.GEarthController; import gearth.services.packet_info.PacketInfo;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.event.ActionEvent; 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.Button;
import javafx.scene.control.ComboBox; import javafx.scene.control.ComboBox;
import javafx.scene.control.ScrollPane; import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -210,20 +202,6 @@ public class BlockAndReplacePackets extends ExtensionForm {
refreshOptions(); 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 @Override
protected void onShow() { protected void onShow() {
Platform.runLater(() -> cmb_type.requestFocus()); Platform.runLater(() -> cmb_type.requestFocus());

View File

@ -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<BlockAndReplacePackets> {
@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();
}
}

View File

@ -45,21 +45,6 @@ public class PacketInfoExplorer extends ExtensionForm {
private TableView<PacketInfo> tableView; private TableView<PacketInfo> tableView;
public GridPane grid; 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() { public void initialize() {
Platform.runLater( () -> grid.requestFocus() ); Platform.runLater( () -> grid.requestFocus() );

View File

@ -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<PacketInfoExplorer> {
@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();
}
}

View File

@ -57,25 +57,6 @@ public class UiLogger extends ExtensionForm implements PacketLogger {
controller.onDisconnect(); 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 { private class Elem {
HPacket packet; HPacket packet;
int types; int types;
@ -104,4 +85,8 @@ public class UiLogger extends ExtensionForm implements PacketLogger {
protected boolean canDelete() { protected boolean canDelete() {
return false; return false;
} }
public void setController(UiLoggerController controller) {
this.controller = controller;
}
} }

View File

@ -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<UiLogger> {
@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;
}
}

View File

@ -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.ExtensionProducer;
import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducerObserver; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducerObserver;
import gearth.services.internal_extensions.uilogger.UiLogger; import gearth.services.internal_extensions.uilogger.UiLogger;
import gearth.services.internal_extensions.uilogger.UiLoggerLauncher;
/** /**
* Created by Jonas on 04/04/18. * Created by Jonas on 04/04/18.
@ -39,8 +40,8 @@ public class PacketLoggerFactory implements ExtensionProducer {
@Override @Override
public void startProducing(ExtensionProducerObserver observer) { public void startProducing(ExtensionProducerObserver observer) {
if (usesUIlogger()) { if (usesUIlogger()) {
uiLogger = new InternalExtensionFormBuilder<UiLogger>() uiLogger = new InternalExtensionFormBuilder<UiLoggerLauncher, UiLogger>()
.launch(UiLogger.class, observer); .launch(new UiLoggerLauncher(), observer);
} }
} }
} }