From e141cf0f2c6e3ec71487856a80699b0258f2d398 Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Tue, 6 Nov 2018 20:32:36 +0100 Subject: [PATCH] block & replace packets UI --- .../BlockAndReplacePackets.java | 30 ++++++- .../blockreplacepackets/RuleContainer.java | 86 ++++++++++++++++++- .../blockreplacepackets/blockreplace.fxml | 14 +-- .../rules/BlockPacketRule.java | 2 +- .../rules/BlockReplaceRule.java | 14 +++ .../rules/RuleFactory.java | 2 +- 6 files changed, 137 insertions(+), 11 deletions(-) diff --git a/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/BlockAndReplacePackets.java b/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/BlockAndReplacePackets.java index 1b60e82..db62f0b 100644 --- a/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/BlockAndReplacePackets.java +++ b/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/BlockAndReplacePackets.java @@ -1,27 +1,37 @@ package extensions.blockreplacepackets; +import extensions.blockreplacepackets.rules.BlockReplaceRule; +import extensions.blockreplacepackets.rules.RuleFactory; 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.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.layout.GridPane; +import javafx.scene.layout.VBox; import javafx.stage.Stage; import gearth.extensions.ExtensionForm; import gearth.extensions.ExtensionInfo; +import java.util.ArrayList; +import java.util.List; + /** * Created by Jonas on 22/09/18. */ @ExtensionInfo( - Title = "iManipulate", + Title = "G-Manipulate", Description = "Block &/ replace packets", Version = "0.1", Author = "sirjonasxx" @@ -33,6 +43,11 @@ public class BlockAndReplacePackets extends ExtensionForm { public Button btn_add; public volatile ComboBox cmb_side; public TextField txt_value; + public ScrollPane scrollpane; + public VBox vbox; + public GridPane header; + + List rules = new ArrayList<>(); public static void main(String[] args) { runExtensionForm(args, BlockAndReplacePackets.class); @@ -149,6 +164,13 @@ public class BlockAndReplacePackets extends ExtensionForm { } } + private void clearInput() { + txt_value.clear(); + txt_replacement.clear(); + refreshOptions(); + cmb_type.requestFocus(); + } + @Override protected void initExtension() { intercept(HMessage.Side.TOSERVER, message -> System.out.println("just testing")); @@ -173,6 +195,12 @@ public class BlockAndReplacePackets extends ExtensionForm { } public void click_btnAddRule(ActionEvent actionEvent) { + BlockReplaceRule rule = RuleFactory.getRule(cmb_type.getSelectionModel().getSelectedItem(), cmb_side.getSelectionModel().getSelectedItem(), txt_value.getText(), txt_replacement.getText()); + rules.add(rule); + rule.onDelete(observable -> rules.remove(rule)); + new RuleContainer(rule, vbox); + + clearInput(); } } diff --git a/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/RuleContainer.java b/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/RuleContainer.java index 259e26c..9826502 100644 --- a/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/RuleContainer.java +++ b/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/RuleContainer.java @@ -1,7 +1,91 @@ package extensions.blockreplacepackets; +import extensions.blockreplacepackets.rules.BlockReplaceRule; +import gearth.ui.buttons.DeleteButton; +import gearth.ui.scheduler.ScheduleItem; +import javafx.beans.InvalidationListener; +import javafx.beans.Observable; +import javafx.event.EventHandler; +import javafx.geometry.Insets; +import javafx.geometry.Pos; +import javafx.scene.control.Label; +import javafx.scene.input.MouseEvent; +import javafx.scene.layout.ColumnConstraints; +import javafx.scene.layout.GridPane; +import javafx.scene.layout.RowConstraints; +import javafx.scene.layout.VBox; +import javafx.scene.text.Font; + /** * Created by Jeunez on 6/11/2018. */ -public class RuleContainer { +public class RuleContainer extends GridPane { + + public static final int[] columnWidths = {12, 14, 18, 33, 15, 6}; + + VBox parent; + BlockReplaceRule item; + + RuleContainer(BlockReplaceRule item, VBox parent) { + super(); + + this.parent = parent; + this.item = item; + + setGridLinesVisible(true); + VBox.setMargin(this, new Insets(2, -2, -2, -2)); + + setPrefWidth(parent.getWidth()); + setPrefHeight(23); + initialize(); + } + + private void initialize() { + + RowConstraints rowConstraints = new RowConstraints(23); + getRowConstraints().addAll(rowConstraints); + + for (int i = 0; i < columnWidths.length; i++) { + ColumnConstraints columnConstraints = new ColumnConstraints(20); + columnConstraints.setPercentWidth(columnWidths[i]); + getColumnConstraints().add(columnConstraints); + } + + Label optionLabel = initNewLabelColumn(item.option().name()); + Label typeLabel = initNewLabelColumn(item.type().name()); + Label valueLabel = initNewLabelColumn(item.value()); + Label replacementLabel = initNewLabelColumn(item.replacement()); + Label destinationLabel = initNewLabelColumn(item.side().name()); + + add(optionLabel, 0, 0); + add(typeLabel, 1, 0); + add(valueLabel, 2, 0); + add(replacementLabel, 3, 0); + add(destinationLabel, 4, 0); + + DeleteButton deleteButton = new DeleteButton(); + deleteButton.setAlignment(Pos.CENTER); + deleteButton.show(); + + RuleContainer thiss = this; + item.onDelete(observable -> parent.getChildren().remove(thiss)); + deleteButton.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> item.delete()); + + add(deleteButton, 5, 0); + + + parent.getChildren().add(this); + } + + private Label initNewLabelColumn(String text) { + Label label = new Label(); +// label.setMaxWidth(Double.MAX_VALUE); +// label.setMinHeight(Double.MAX_VALUE); +// label.setAlignment(Pos.CENTER); + label.setFont(new Font(12)); + GridPane.setMargin(label, new Insets(0, 0, 0, 5)); + label.setText(text); + return label; + } + } diff --git a/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/blockreplace.fxml b/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/blockreplace.fxml index eb0532d..6290c54 100644 --- a/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/blockreplace.fxml +++ b/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/blockreplace.fxml @@ -36,10 +36,10 @@ - - - - + + + + @@ -107,16 +107,16 @@ - + - + - + diff --git a/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/rules/BlockPacketRule.java b/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/rules/BlockPacketRule.java index 940942b..3083cbb 100644 --- a/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/rules/BlockPacketRule.java +++ b/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/rules/BlockPacketRule.java @@ -49,6 +49,6 @@ public class BlockPacketRule extends BlockReplaceRule{ @Override public String replacement() { - return ""; + return "/"; } } diff --git a/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/rules/BlockReplaceRule.java b/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/rules/BlockReplaceRule.java index 3c6668c..d1cc521 100644 --- a/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/rules/BlockReplaceRule.java +++ b/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/rules/BlockReplaceRule.java @@ -1,6 +1,10 @@ package extensions.blockreplacepackets.rules; import gearth.protocol.HMessage; +import javafx.beans.InvalidationListener; + +import java.util.ArrayList; +import java.util.List; /** * Created by Jonas on 6/11/2018. @@ -35,4 +39,14 @@ public abstract class BlockReplaceRule { public abstract String value(); public abstract String replacement(); + private List onDeleteListeners = new ArrayList<>(); + public void onDelete(InvalidationListener listener) { + onDeleteListeners.add(listener); + } + public void delete() { + for (int i = onDeleteListeners.size() - 1; i >= 0; i--) { + onDeleteListeners.get(i).invalidated(null); + } + } + } diff --git a/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/rules/RuleFactory.java b/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/rules/RuleFactory.java index d0bb3e8..051bc28 100644 --- a/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/rules/RuleFactory.java +++ b/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/rules/RuleFactory.java @@ -10,7 +10,7 @@ public class RuleFactory { public static BlockReplaceRule getRule(String type, String side, String value, String replacement) { BlockReplaceRule.Option rOption = BlockReplaceRule.Option.valueOf(type.split(" ")[0].toUpperCase()); BlockReplaceRule.Type rType = BlockReplaceRule.Type.valueOf(type.split(" ")[1].toUpperCase()); - BlockReplaceRule.Side rSide = BlockReplaceRule.Side.valueOf(type.toUpperCase()); + BlockReplaceRule.Side rSide = BlockReplaceRule.Side.valueOf(side.toUpperCase()); if (rOption == BlockReplaceRule.Option.BLOCK) { return new BlockPacketRule(rSide, Integer.parseInt(value));