From 4d29895f5f2f175a0eea639b119279c4071fa930 Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Tue, 6 Nov 2018 15:02:54 +0100 Subject: [PATCH] fix bug in extensionForm --- .../BlockAndReplacePackets.java | 56 +++++++++++++++++-- .../speechcolorizer/SpeechColorizer.java | 3 +- .../java/gearth/extensions/ExtensionForm.java | 52 ++++++++++------- 3 files changed, 85 insertions(+), 26 deletions(-) diff --git a/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/BlockAndReplacePackets.java b/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/BlockAndReplacePackets.java index e11c6c3..8bb771f 100644 --- a/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/BlockAndReplacePackets.java +++ b/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/BlockAndReplacePackets.java @@ -1,11 +1,14 @@ package extensions.blockreplacepackets; +import gearth.extensions.Extension; +import gearth.protocol.HMessage; import gearth.protocol.HPacket; import gearth.ui.GEarthController; import javafx.application.Platform; import javafx.beans.InvalidationListener; import javafx.beans.Observable; import javafx.event.ActionEvent; +import javafx.event.EventHandler; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; @@ -15,6 +18,7 @@ import javafx.scene.control.TextField; import javafx.stage.Stage; import gearth.extensions.ExtensionForm; import gearth.extensions.ExtensionInfo; +import javafx.stage.WindowEvent; /** * Created by Jonas on 22/09/18. @@ -32,7 +36,7 @@ public class BlockAndReplacePackets extends ExtensionForm { public TextField txt_replacement; public ComboBox cmb_type; public Button btn_add; - public ComboBox cmb_side; + public volatile ComboBox cmb_side; public TextField txt_value; public static void main(String[] args) { @@ -44,7 +48,6 @@ public class BlockAndReplacePackets extends ExtensionForm { public void initialize() { cmb_type.getItems().addAll("Block packet", "Replace packet", "Replace integer", "Replace string", "Replace substring"); cmb_type.getSelectionModel().selectFirst(); - cmb_type.requestFocus(); cmb_side.getItems().addAll("Incoming", "Outgoing"); cmb_side.getSelectionModel().selectFirst(); @@ -55,6 +58,8 @@ public class BlockAndReplacePackets extends ExtensionForm { txt_value.textProperty().addListener(event -> Platform.runLater(this::refreshOptions)); refreshOptions(); + cmb_type.requestFocus(); + } private void refreshOptions() { @@ -118,16 +123,50 @@ public class BlockAndReplacePackets extends ExtensionForm { btn_add.setDisable(!isValid); - } + String[] spl = type.split(" "); + if (repl.equals("") && spl[0].equals("Replace")) { + if (spl[1].equals("packet")) { + txt_replacement.setPromptText("Enter a packet here"); + } + else if (spl[1].equals("integer")) { + txt_replacement.setPromptText("Enter an integer here"); + } + else if (spl[1].endsWith("string")) { + txt_replacement.setPromptText("Enter a string here"); + } + } + else { + txt_replacement.setPromptText(""); + } + if (val.equals("")) { + if (spl[1].equals("packet")) { + txt_value.setPromptText("Enter the headerID"); + } + else if (spl[1].equals("integer")) { + txt_value.setPromptText("Enter an integer"); + } + else if (spl[1].endsWith("string")) { + txt_value.setPromptText("Enter a string"); + } + } + else { + txt_value.setPromptText(""); + } + } @Override protected void initExtension() { - + intercept(HMessage.Side.TOSERVER, new Extension.MessageListener() { + @Override + public void act(HMessage message) { + System.out.println("just testing"); + } + }); } @Override - public void setStageData(Stage primaryStage) throws Exception { + public ExtensionForm launchForm(Stage primaryStage) throws Exception { FXMLLoader loader = new FXMLLoader(BlockAndReplacePackets.class.getResource("blockreplace.fxml")); Parent root = loader.load(); @@ -135,6 +174,13 @@ public class BlockAndReplacePackets extends ExtensionForm { primaryStage.setScene(new Scene(root)); primaryStage.setResizable(false); primaryStage.getScene().getStylesheets().add(GEarthController.class.getResource("/gearth/ui/bootstrap3.css").toExternalForm()); + + return loader.getController(); + } + + @Override + protected void onShow() { + Platform.runLater(() -> cmb_type.requestFocus()); } public void click_btnAddRule(ActionEvent actionEvent) { diff --git a/Extensions/SpeechColorizer/src/main/java/extensions/speechcolorizer/SpeechColorizer.java b/Extensions/SpeechColorizer/src/main/java/extensions/speechcolorizer/SpeechColorizer.java index f01feb7..ce9f769 100644 --- a/Extensions/SpeechColorizer/src/main/java/extensions/speechcolorizer/SpeechColorizer.java +++ b/Extensions/SpeechColorizer/src/main/java/extensions/speechcolorizer/SpeechColorizer.java @@ -12,8 +12,7 @@ import java.util.Random; */ /** - * - getTitle(), getDescription(), getVersion() and getAuthor() must be implemented - * + * This is an example extension and is not included in a G-Earth release */ @ExtensionInfo( diff --git a/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java b/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java index bdfd69e..da13115 100644 --- a/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java +++ b/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java @@ -6,6 +6,8 @@ import javafx.stage.Stage; import gearth.protocol.HMessage; import gearth.protocol.HPacket; +import java.util.concurrent.Semaphore; + /** * Created by Jonas on 22/09/18. */ @@ -13,41 +15,36 @@ public abstract class ExtensionForm extends Application { private Extension extension = null; protected static String[] args; - private volatile Stage primaryStage = null; + protected volatile Stage primaryStage; + + private ExtensionForm realForm = null; @Override public void start(Stage primaryStage) throws Exception { - Platform.setImplicitExit(false); - setStageData(primaryStage); - this.primaryStage = primaryStage; - primaryStage.setOnCloseRequest(event -> { - event.consume(); - Platform.runLater(primaryStage::hide); - }); - ExtensionForm thiss = this; - ExtensionInfo extInfo = getClass().getAnnotation(ExtensionInfo.class); - + Semaphore semaphore = new Semaphore(1); + semaphore.acquire(); + realForm = launchForm(primaryStage); Thread t = new Thread(() -> { - extension = new Extension(args) { + realForm.extension = new Extension(args) { @Override protected void init() { - thiss.initExtension(); + realForm.initExtension(); } @Override protected void onClick() { - thiss.onClick(); + realForm.onClick(); } @Override protected void onStartConnection() { - thiss.onStartConnection(); + realForm.onStartConnection(); } @Override protected void onEndConnection() { - thiss.onEndConnection(); + realForm.onEndConnection(); } @Override @@ -55,16 +52,29 @@ public abstract class ExtensionForm extends Application { return extInfo; } }; - extension.run(); + semaphore.release(); + realForm.extension.run(); // Platform.runLater(primaryStage::close); //when the extension has ended, close this process System.exit(0); }); t.start(); + + semaphore.acquire(); + Platform.setImplicitExit(false); + realForm = launchForm(primaryStage); + realForm.primaryStage = primaryStage; + + primaryStage.setOnCloseRequest(event -> { + event.consume(); + Platform.runLater(() -> { + primaryStage.hide(); + realForm.onHide(); + }); + }); } - public abstract void setStageData(Stage primaryStage) throws Exception; - + public abstract ExtensionForm launchForm(Stage primaryStage) throws Exception; //wrap extension methods protected boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback){ @@ -86,6 +96,8 @@ public abstract class ExtensionForm extends Application { return extension.sendToClient(packet); } + protected void onShow(){}; + protected void onHide(){}; /** * Gets called when a connection has been established with G-Earth. @@ -99,7 +111,9 @@ public abstract class ExtensionForm extends Application { private void onClick(){ Platform.runLater(() -> { primaryStage.show(); + primaryStage.requestFocus(); primaryStage.toFront(); + onShow(); }); }