fix bug in extensionForm

This commit is contained in:
sirjonasxx 2018-11-06 15:02:54 +01:00
parent 4bd2798c71
commit 4d29895f5f
3 changed files with 85 additions and 26 deletions

View File

@ -1,11 +1,14 @@
package extensions.blockreplacepackets; package extensions.blockreplacepackets;
import gearth.extensions.Extension;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.ui.GEarthController; import gearth.ui.GEarthController;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.InvalidationListener; import javafx.beans.InvalidationListener;
import javafx.beans.Observable; import javafx.beans.Observable;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Parent; import javafx.scene.Parent;
import javafx.scene.Scene; import javafx.scene.Scene;
@ -15,6 +18,7 @@ import javafx.scene.control.TextField;
import javafx.stage.Stage; import javafx.stage.Stage;
import gearth.extensions.ExtensionForm; import gearth.extensions.ExtensionForm;
import gearth.extensions.ExtensionInfo; import gearth.extensions.ExtensionInfo;
import javafx.stage.WindowEvent;
/** /**
* Created by Jonas on 22/09/18. * Created by Jonas on 22/09/18.
@ -32,7 +36,7 @@ public class BlockAndReplacePackets extends ExtensionForm {
public TextField txt_replacement; public TextField txt_replacement;
public ComboBox<String> cmb_type; public ComboBox<String> cmb_type;
public Button btn_add; public Button btn_add;
public ComboBox<String> cmb_side; public volatile ComboBox<String> cmb_side;
public TextField txt_value; public TextField txt_value;
public static void main(String[] args) { public static void main(String[] args) {
@ -44,7 +48,6 @@ public class BlockAndReplacePackets extends ExtensionForm {
public void initialize() { public void initialize() {
cmb_type.getItems().addAll("Block packet", "Replace packet", "Replace integer", "Replace string", "Replace substring"); cmb_type.getItems().addAll("Block packet", "Replace packet", "Replace integer", "Replace string", "Replace substring");
cmb_type.getSelectionModel().selectFirst(); cmb_type.getSelectionModel().selectFirst();
cmb_type.requestFocus();
cmb_side.getItems().addAll("Incoming", "Outgoing"); cmb_side.getItems().addAll("Incoming", "Outgoing");
cmb_side.getSelectionModel().selectFirst(); cmb_side.getSelectionModel().selectFirst();
@ -55,6 +58,8 @@ public class BlockAndReplacePackets extends ExtensionForm {
txt_value.textProperty().addListener(event -> Platform.runLater(this::refreshOptions)); txt_value.textProperty().addListener(event -> Platform.runLater(this::refreshOptions));
refreshOptions(); refreshOptions();
cmb_type.requestFocus();
} }
private void refreshOptions() { private void refreshOptions() {
@ -118,16 +123,50 @@ public class BlockAndReplacePackets extends ExtensionForm {
btn_add.setDisable(!isValid); 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 @Override
protected void initExtension() { protected void initExtension() {
intercept(HMessage.Side.TOSERVER, new Extension.MessageListener() {
@Override
public void act(HMessage message) {
System.out.println("just testing");
}
});
} }
@Override @Override
public void setStageData(Stage primaryStage) throws Exception { public ExtensionForm launchForm(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(BlockAndReplacePackets.class.getResource("blockreplace.fxml")); FXMLLoader loader = new FXMLLoader(BlockAndReplacePackets.class.getResource("blockreplace.fxml"));
Parent root = loader.load(); Parent root = loader.load();
@ -135,6 +174,13 @@ public class BlockAndReplacePackets extends ExtensionForm {
primaryStage.setScene(new Scene(root)); primaryStage.setScene(new Scene(root));
primaryStage.setResizable(false); primaryStage.setResizable(false);
primaryStage.getScene().getStylesheets().add(GEarthController.class.getResource("/gearth/ui/bootstrap3.css").toExternalForm()); 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) { public void click_btnAddRule(ActionEvent actionEvent) {

View File

@ -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( @ExtensionInfo(

View File

@ -6,6 +6,8 @@ import javafx.stage.Stage;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import java.util.concurrent.Semaphore;
/** /**
* Created by Jonas on 22/09/18. * Created by Jonas on 22/09/18.
*/ */
@ -13,41 +15,36 @@ public abstract class ExtensionForm extends Application {
private Extension extension = null; private Extension extension = null;
protected static String[] args; protected static String[] args;
private volatile Stage primaryStage = null; protected volatile Stage primaryStage;
private ExtensionForm realForm = null;
@Override @Override
public void start(Stage primaryStage) throws Exception { 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); ExtensionInfo extInfo = getClass().getAnnotation(ExtensionInfo.class);
Semaphore semaphore = new Semaphore(1);
semaphore.acquire();
realForm = launchForm(primaryStage);
Thread t = new Thread(() -> { Thread t = new Thread(() -> {
extension = new Extension(args) { realForm.extension = new Extension(args) {
@Override @Override
protected void init() { protected void init() {
thiss.initExtension(); realForm.initExtension();
} }
@Override @Override
protected void onClick() { protected void onClick() {
thiss.onClick(); realForm.onClick();
} }
@Override @Override
protected void onStartConnection() { protected void onStartConnection() {
thiss.onStartConnection(); realForm.onStartConnection();
} }
@Override @Override
protected void onEndConnection() { protected void onEndConnection() {
thiss.onEndConnection(); realForm.onEndConnection();
} }
@Override @Override
@ -55,16 +52,29 @@ public abstract class ExtensionForm extends Application {
return extInfo; return extInfo;
} }
}; };
extension.run(); semaphore.release();
realForm.extension.run();
// Platform.runLater(primaryStage::close); // Platform.runLater(primaryStage::close);
//when the extension has ended, close this process //when the extension has ended, close this process
System.exit(0); System.exit(0);
}); });
t.start(); 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 //wrap extension methods
protected boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback){ protected boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback){
@ -86,6 +96,8 @@ public abstract class ExtensionForm extends Application {
return extension.sendToClient(packet); return extension.sendToClient(packet);
} }
protected void onShow(){};
protected void onHide(){};
/** /**
* Gets called when a connection has been established with G-Earth. * Gets called when a connection has been established with G-Earth.
@ -99,7 +111,9 @@ public abstract class ExtensionForm extends Application {
private void onClick(){ private void onClick(){
Platform.runLater(() -> { Platform.runLater(() -> {
primaryStage.show(); primaryStage.show();
primaryStage.requestFocus();
primaryStage.toFront(); primaryStage.toFront();
onShow();
}); });
} }