From cee7fe5160728246742731df9a1d21c625cd744e Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Sun, 26 Apr 2020 23:04:47 +0200 Subject: [PATCH] refactor UI, split scheduler --- .../blockreplacepackets/RuleContainer.java | 4 - .../{ui => services}/scheduler/Interval.java | 6 +- .../scheduler/ScheduleItem.java | 51 ++-------- .../gearth/services/scheduler/Scheduler.java | 73 +++++++++++++++ .../listeners/OnBeingUpdatedListener.java | 2 +- .../scheduler/listeners/OnDeleteListener.java | 2 +- .../scheduler/listeners/OnEditListener.java | 2 +- .../listeners/OnUpdatedListener.java | 2 +- .../main/java/gearth/ui/GEarthController.java | 33 ++++--- ...nection.java => ConnectionController.java} | 2 +- ...ensions.java => ExtensionsController.java} | 2 +- .../{Extra.java => ExtraController.java} | 8 +- .../info/{Info.java => InfoController.java} | 2 +- ...njection.java => InjectionController.java} | 2 +- .../{Logger.java => LoggerController.java} | 2 +- .../scheduler/InteractableScheduleItem.java | 54 +++++++++++ .../ui/scheduler/ScheduleItemContainer.java | 7 +- ...cheduler.java => SchedulerController.java} | 93 ++++++------------- .../{Tools.java => ToolsController.java} | 2 +- .../gearth/ui/connection/Connection.fxml | 2 +- .../gearth/ui/extensions/Extensions.fxml | 2 +- .../main/resources/gearth/ui/extra/Extra.fxml | 2 +- .../main/resources/gearth/ui/info/Info.fxml | 2 +- .../gearth/ui/injection/Injection.fxml | 2 +- .../resources/gearth/ui/logger/Logger.fxml | 2 +- .../gearth/ui/scheduler/Scheduler.fxml | 2 +- .../main/resources/gearth/ui/tools/Tools.fxml | 2 +- 27 files changed, 207 insertions(+), 158 deletions(-) rename G-Earth/src/main/java/gearth/{ui => services}/scheduler/Interval.java (83%) rename G-Earth/src/main/java/gearth/{ui => services}/scheduler/ScheduleItem.java (62%) create mode 100644 G-Earth/src/main/java/gearth/services/scheduler/Scheduler.java rename G-Earth/src/main/java/gearth/{ui => services}/scheduler/listeners/OnBeingUpdatedListener.java (61%) rename G-Earth/src/main/java/gearth/{ui => services}/scheduler/listeners/OnDeleteListener.java (57%) rename G-Earth/src/main/java/gearth/{ui => services}/scheduler/listeners/OnEditListener.java (55%) rename G-Earth/src/main/java/gearth/{ui => services}/scheduler/listeners/OnUpdatedListener.java (57%) rename G-Earth/src/main/java/gearth/ui/connection/{Connection.java => ConnectionController.java} (98%) rename G-Earth/src/main/java/gearth/ui/extensions/{Extensions.java => ExtensionsController.java} (98%) rename G-Earth/src/main/java/gearth/ui/extra/{Extra.java => ExtraController.java} (93%) rename G-Earth/src/main/java/gearth/ui/info/{Info.java => InfoController.java} (97%) rename G-Earth/src/main/java/gearth/ui/injection/{Injection.java => InjectionController.java} (98%) rename G-Earth/src/main/java/gearth/ui/logger/{Logger.java => LoggerController.java} (99%) create mode 100644 G-Earth/src/main/java/gearth/ui/scheduler/InteractableScheduleItem.java rename G-Earth/src/main/java/gearth/ui/scheduler/{Scheduler.java => SchedulerController.java} (72%) rename G-Earth/src/main/java/gearth/ui/tools/{Tools.java => ToolsController.java} (99%) diff --git a/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/RuleContainer.java b/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/RuleContainer.java index 9826502..c72ee00 100644 --- a/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/RuleContainer.java +++ b/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/RuleContainer.java @@ -2,10 +2,6 @@ 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; diff --git a/G-Earth/src/main/java/gearth/ui/scheduler/Interval.java b/G-Earth/src/main/java/gearth/services/scheduler/Interval.java similarity index 83% rename from G-Earth/src/main/java/gearth/ui/scheduler/Interval.java rename to G-Earth/src/main/java/gearth/services/scheduler/Interval.java index 436065f..5ff49bb 100644 --- a/G-Earth/src/main/java/gearth/ui/scheduler/Interval.java +++ b/G-Earth/src/main/java/gearth/services/scheduler/Interval.java @@ -1,4 +1,6 @@ -package gearth.ui.scheduler; +package gearth.services.scheduler; + +import gearth.ui.scheduler.SchedulerController; /** * Created by Jonas on 11/04/18. @@ -21,7 +23,7 @@ public class Interval { offset = -1; return; } - if (!Scheduler.stringIsNumber(split[0]) || (split.length == 2 && !Scheduler.stringIsNumber(split[1]))) { + if (!SchedulerController.stringIsNumber(split[0]) || (split.length == 2 && !SchedulerController.stringIsNumber(split[1]))) { delay = -1; offset = -1; return; diff --git a/G-Earth/src/main/java/gearth/ui/scheduler/ScheduleItem.java b/G-Earth/src/main/java/gearth/services/scheduler/ScheduleItem.java similarity index 62% rename from G-Earth/src/main/java/gearth/ui/scheduler/ScheduleItem.java rename to G-Earth/src/main/java/gearth/services/scheduler/ScheduleItem.java index 0e23272..633818c 100644 --- a/G-Earth/src/main/java/gearth/ui/scheduler/ScheduleItem.java +++ b/G-Earth/src/main/java/gearth/services/scheduler/ScheduleItem.java @@ -1,11 +1,10 @@ -package gearth.ui.scheduler; +package gearth.services.scheduler; import gearth.misc.listenerpattern.Observable; -import gearth.ui.scheduler.listeners.OnBeingUpdatedListener; -import gearth.ui.scheduler.listeners.OnDeleteListener; -import gearth.ui.scheduler.listeners.OnEditListener; -import gearth.ui.scheduler.listeners.OnUpdatedListener; -import javafx.beans.InvalidationListener; +import gearth.services.scheduler.listeners.OnBeingUpdatedListener; +import gearth.services.scheduler.listeners.OnDeleteListener; +import gearth.services.scheduler.listeners.OnEditListener; +import gearth.services.scheduler.listeners.OnUpdatedListener; import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleObjectProperty; @@ -13,9 +12,6 @@ import gearth.misc.StringifyAble; import gearth.protocol.HMessage; import gearth.protocol.HPacket; -import java.util.ArrayList; -import java.util.List; - /** * Created by Jonas on 07/04/18. */ @@ -27,7 +23,7 @@ public class ScheduleItem implements StringifyAble { private SimpleObjectProperty packetProperty; private SimpleObjectProperty destinationProperty; - ScheduleItem (int index, boolean paused, Interval delay, HPacket packet, HMessage.Direction destination) { + public ScheduleItem (int index, boolean paused, Interval delay, HPacket packet, HMessage.Direction destination) { construct(index, paused, delay, packet, destination); } @@ -39,7 +35,7 @@ public class ScheduleItem implements StringifyAble { this.destinationProperty = new SimpleObjectProperty<>(destination); } - ScheduleItem(String stringifyAbleRepresentation) { + public ScheduleItem(String stringifyAbleRepresentation) { constructFromString(stringifyAbleRepresentation); } @@ -63,39 +59,6 @@ public class ScheduleItem implements StringifyAble { return destinationProperty; } - - private Observable onDeleteObservable = new Observable<>(OnDeleteListener::onDelete); - public void onDelete(OnDeleteListener listener) { - onDeleteObservable.addListener(listener); - } - public void delete() { - onDeleteObservable.fireEvent(); - } - - private Observable onEditObservable = new Observable<>(OnEditListener::onEdit); - public void onEdit(OnEditListener listener) { - onEditObservable.addListener(listener); - } - public void edit() { - onEditObservable.fireEvent(); - } - - private Observable onUpdatedObservable = new Observable<>(OnUpdatedListener::onUpdated); - public void onIsupdated(OnUpdatedListener listener) { - onUpdatedObservable.addListener(listener); - } - public void isUpdatedTrigger() { - onUpdatedObservable.fireEvent(); - } - - private Observable onBeingUpdatedObservable = new Observable<>(OnBeingUpdatedListener::onBeingUpdated); - public void onIsBeingUpdated(OnBeingUpdatedListener listener) { - onBeingUpdatedObservable.addListener(listener); - } - public void onIsBeingUpdatedTrigger() { - onBeingUpdatedObservable.fireEvent(); - } - @Override public String stringify() { StringBuilder b = new StringBuilder(); diff --git a/G-Earth/src/main/java/gearth/services/scheduler/Scheduler.java b/G-Earth/src/main/java/gearth/services/scheduler/Scheduler.java new file mode 100644 index 0000000..7e46b4e --- /dev/null +++ b/G-Earth/src/main/java/gearth/services/scheduler/Scheduler.java @@ -0,0 +1,73 @@ +package gearth.services.scheduler; + +import gearth.protocol.HConnection; +import gearth.protocol.HMessage; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class Scheduler { + + private List scheduleItems = new ArrayList<>(); + + public Scheduler(HConnection connection) { + new Thread(() -> { + long t = System.currentTimeMillis(); + long changed = 1; + + Set set = new HashSet<>(); + + while (true) { + try { + Thread.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + set.clear(); + for (int i = size() - 1; i >= 0; i--) { + set.add(get(i)); + } + + for (ScheduleItem item : set) { + if (!item.getPausedProperty().get()) { + Interval cur = item.getDelayProperty().get(); + for (int i = 0; i < changed; i++) { + if ((t - i) % cur.getDelay() == cur.getOffset()) { + if (item.getDestinationProperty().get() == HMessage.Direction.TOSERVER) { + connection.sendToServerAsync(item.getPacketProperty().get()); + } + else { + connection.sendToClientAsync(item.getPacketProperty().get()); + } + } + } + } + + } + + long newT = System.currentTimeMillis(); + changed = newT - t; + t = newT; + } + }).start(); + } + + public int size() { + return scheduleItems.size(); + } + + public T get(int i) { + return scheduleItems.get(i); + } + + public void add(T item) { + scheduleItems.add(item); + } + + public void remove(T item) { + scheduleItems.remove(item); + } +} diff --git a/G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnBeingUpdatedListener.java b/G-Earth/src/main/java/gearth/services/scheduler/listeners/OnBeingUpdatedListener.java similarity index 61% rename from G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnBeingUpdatedListener.java rename to G-Earth/src/main/java/gearth/services/scheduler/listeners/OnBeingUpdatedListener.java index c417dbe..e9b79ba 100644 --- a/G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnBeingUpdatedListener.java +++ b/G-Earth/src/main/java/gearth/services/scheduler/listeners/OnBeingUpdatedListener.java @@ -1,4 +1,4 @@ -package gearth.ui.scheduler.listeners; +package gearth.services.scheduler.listeners; public interface OnBeingUpdatedListener { void onBeingUpdated(); diff --git a/G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnDeleteListener.java b/G-Earth/src/main/java/gearth/services/scheduler/listeners/OnDeleteListener.java similarity index 57% rename from G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnDeleteListener.java rename to G-Earth/src/main/java/gearth/services/scheduler/listeners/OnDeleteListener.java index a87747c..55e576f 100644 --- a/G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnDeleteListener.java +++ b/G-Earth/src/main/java/gearth/services/scheduler/listeners/OnDeleteListener.java @@ -1,4 +1,4 @@ -package gearth.ui.scheduler.listeners; +package gearth.services.scheduler.listeners; public interface OnDeleteListener { void onDelete(); diff --git a/G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnEditListener.java b/G-Earth/src/main/java/gearth/services/scheduler/listeners/OnEditListener.java similarity index 55% rename from G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnEditListener.java rename to G-Earth/src/main/java/gearth/services/scheduler/listeners/OnEditListener.java index 7c95e2a..0a3b044 100644 --- a/G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnEditListener.java +++ b/G-Earth/src/main/java/gearth/services/scheduler/listeners/OnEditListener.java @@ -1,4 +1,4 @@ -package gearth.ui.scheduler.listeners; +package gearth.services.scheduler.listeners; public interface OnEditListener { void onEdit(); diff --git a/G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnUpdatedListener.java b/G-Earth/src/main/java/gearth/services/scheduler/listeners/OnUpdatedListener.java similarity index 57% rename from G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnUpdatedListener.java rename to G-Earth/src/main/java/gearth/services/scheduler/listeners/OnUpdatedListener.java index f3c4e07..63cf9f6 100644 --- a/G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnUpdatedListener.java +++ b/G-Earth/src/main/java/gearth/services/scheduler/listeners/OnUpdatedListener.java @@ -1,4 +1,4 @@ -package gearth.ui.scheduler.listeners; +package gearth.services.scheduler.listeners; public interface OnUpdatedListener { void onUpdated(); diff --git a/G-Earth/src/main/java/gearth/ui/GEarthController.java b/G-Earth/src/main/java/gearth/ui/GEarthController.java index 431c3ea..3dbbf64 100644 --- a/G-Earth/src/main/java/gearth/ui/GEarthController.java +++ b/G-Earth/src/main/java/gearth/ui/GEarthController.java @@ -6,18 +6,17 @@ import javafx.scene.control.TabPane; import javafx.scene.layout.Pane; import javafx.stage.Stage; import gearth.protocol.HConnection; -import gearth.ui.connection.Connection; -import gearth.ui.extensions.Extensions; -import gearth.ui.info.Info; -import gearth.ui.injection.Injection; -import gearth.ui.logger.Logger; -import gearth.ui.scheduler.Scheduler; -import gearth.ui.extra.Extra; -import gearth.ui.tools.Tools; +import gearth.ui.connection.ConnectionController; +import gearth.ui.extensions.ExtensionsController; +import gearth.ui.info.InfoController; +import gearth.ui.injection.InjectionController; +import gearth.ui.logger.LoggerController; +import gearth.ui.scheduler.SchedulerController; +import gearth.ui.extra.ExtraController; +import gearth.ui.tools.ToolsController; import java.util.ArrayList; import java.util.List; -import java.util.function.Consumer; public class GEarthController { @@ -27,14 +26,14 @@ public class GEarthController { private volatile HConnection hConnection; private volatile int initcount = 0; - public Connection connectionController; - public Injection injectionController; - public Logger loggerController; - public Tools toolsController; - public Scheduler schedulerController; - public Extra extraController; - public Info infoController; - public Extensions extensionsController; + public ConnectionController connectionController; + public InjectionController injectionController; + public LoggerController loggerController; + public ToolsController toolsController; + public SchedulerController schedulerController; + public ExtraController extraController; + public InfoController infoController; + public ExtensionsController extensionsController; private List tabs = null; diff --git a/G-Earth/src/main/java/gearth/ui/connection/Connection.java b/G-Earth/src/main/java/gearth/ui/connection/ConnectionController.java similarity index 98% rename from G-Earth/src/main/java/gearth/ui/connection/Connection.java rename to G-Earth/src/main/java/gearth/ui/connection/ConnectionController.java index b881500..e64b3e3 100644 --- a/G-Earth/src/main/java/gearth/ui/connection/Connection.java +++ b/G-Earth/src/main/java/gearth/ui/connection/ConnectionController.java @@ -12,7 +12,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -public class Connection extends SubForm { +public class ConnectionController extends SubForm { public ComboBox inpPort; public ComboBox inpHost; diff --git a/G-Earth/src/main/java/gearth/ui/extensions/Extensions.java b/G-Earth/src/main/java/gearth/ui/extensions/ExtensionsController.java similarity index 98% rename from G-Earth/src/main/java/gearth/ui/extensions/Extensions.java rename to G-Earth/src/main/java/gearth/ui/extensions/ExtensionsController.java index b837b5e..a6c7ee4 100644 --- a/G-Earth/src/main/java/gearth/ui/extensions/Extensions.java +++ b/G-Earth/src/main/java/gearth/ui/extensions/ExtensionsController.java @@ -21,7 +21,7 @@ import java.io.File; * Created by Jonas on 06/04/18. */ -public class Extensions extends SubForm { +public class ExtensionsController extends SubForm { public Button btn_install; diff --git a/G-Earth/src/main/java/gearth/ui/extra/Extra.java b/G-Earth/src/main/java/gearth/ui/extra/ExtraController.java similarity index 93% rename from G-Earth/src/main/java/gearth/ui/extra/Extra.java rename to G-Earth/src/main/java/gearth/ui/extra/ExtraController.java index 17ae72e..1fc3e91 100644 --- a/G-Earth/src/main/java/gearth/ui/extra/Extra.java +++ b/G-Earth/src/main/java/gearth/ui/extra/ExtraController.java @@ -4,16 +4,14 @@ import gearth.misc.Cacher; import gearth.protocol.HConnection; import gearth.protocol.misc.ConnectionInfoOverrider; import gearth.ui.SubForm; -import gearth.ui.info.Info; -import javafx.beans.InvalidationListener; -import javafx.beans.Observable; +import gearth.ui.info.InfoController; import javafx.scene.control.*; import javafx.scene.layout.GridPane; /** * Created by Jonas on 06/04/18. */ -public class Extra extends SubForm implements ConnectionInfoOverrider { +public class ExtraController extends SubForm implements ConnectionInfoOverrider { public static final String NOTEPAD_CACHE_KEY = "notepad_text"; @@ -40,7 +38,7 @@ public class Extra extends SubForm implements ConnectionInfoOverrider { HConnection.setConnectionInfoOverrider(this); url_troubleshooting.setTooltip(new Tooltip("https://github.com/sirjonasxx/G-Earth/wiki/Troubleshooting")); - Info.activateHyperlink(url_troubleshooting); + InfoController.activateHyperlink(url_troubleshooting); String notepadInitValue = (String)Cacher.get(NOTEPAD_CACHE_KEY); if (notepadInitValue != null) { diff --git a/G-Earth/src/main/java/gearth/ui/info/Info.java b/G-Earth/src/main/java/gearth/ui/info/InfoController.java similarity index 97% rename from G-Earth/src/main/java/gearth/ui/info/Info.java rename to G-Earth/src/main/java/gearth/ui/info/InfoController.java index 6bb16e5..7d06c2b 100644 --- a/G-Earth/src/main/java/gearth/ui/info/Info.java +++ b/G-Earth/src/main/java/gearth/ui/info/InfoController.java @@ -12,7 +12,7 @@ import javafx.scene.image.ImageView; /** * Created by Jonas on 06/04/18. */ -public class Info extends SubForm { +public class InfoController extends SubForm { public ImageView img_logo; public Hyperlink link_sng; public Hyperlink link_darkbox; diff --git a/G-Earth/src/main/java/gearth/ui/injection/Injection.java b/G-Earth/src/main/java/gearth/ui/injection/InjectionController.java similarity index 98% rename from G-Earth/src/main/java/gearth/ui/injection/Injection.java rename to G-Earth/src/main/java/gearth/ui/injection/InjectionController.java index 67a2020..5a87c0d 100644 --- a/G-Earth/src/main/java/gearth/ui/injection/Injection.java +++ b/G-Earth/src/main/java/gearth/ui/injection/InjectionController.java @@ -13,7 +13,7 @@ import gearth.ui.SubForm; import java.util.LinkedList; -public class Injection extends SubForm { +public class InjectionController extends SubForm { public TextArea inputPacket; public Text lbl_corrruption; public Text lbl_pcktInfo; diff --git a/G-Earth/src/main/java/gearth/ui/logger/Logger.java b/G-Earth/src/main/java/gearth/ui/logger/LoggerController.java similarity index 99% rename from G-Earth/src/main/java/gearth/ui/logger/Logger.java rename to G-Earth/src/main/java/gearth/ui/logger/LoggerController.java index f52280f..9a87cc1 100644 --- a/G-Earth/src/main/java/gearth/ui/logger/Logger.java +++ b/G-Earth/src/main/java/gearth/ui/logger/LoggerController.java @@ -17,7 +17,7 @@ import gearth.ui.logger.loggerdisplays.PacketLoggerFactory; import java.util.Calendar; -public class Logger extends SubForm { +public class LoggerController extends SubForm { public TextField txtPacketLimit; diff --git a/G-Earth/src/main/java/gearth/ui/scheduler/InteractableScheduleItem.java b/G-Earth/src/main/java/gearth/ui/scheduler/InteractableScheduleItem.java new file mode 100644 index 0000000..b223af7 --- /dev/null +++ b/G-Earth/src/main/java/gearth/ui/scheduler/InteractableScheduleItem.java @@ -0,0 +1,54 @@ +package gearth.ui.scheduler; + +import gearth.misc.listenerpattern.Observable; +import gearth.protocol.HMessage; +import gearth.protocol.HPacket; +import gearth.services.scheduler.Interval; +import gearth.services.scheduler.ScheduleItem; +import gearth.services.scheduler.listeners.OnBeingUpdatedListener; +import gearth.services.scheduler.listeners.OnDeleteListener; +import gearth.services.scheduler.listeners.OnEditListener; +import gearth.services.scheduler.listeners.OnUpdatedListener; + +public class InteractableScheduleItem extends ScheduleItem { + public InteractableScheduleItem(int index, boolean paused, Interval delay, HPacket packet, HMessage.Direction destination) { + super(index, paused, delay, packet, destination); + } + + public InteractableScheduleItem(String stringifyAbleRepresentation) { + super(stringifyAbleRepresentation); + } + + private Observable onDeleteObservable = new Observable<>(OnDeleteListener::onDelete); + public void onDelete(OnDeleteListener listener) { + onDeleteObservable.addListener(listener); + } + public void delete() { + onDeleteObservable.fireEvent(); + } + + private Observable onEditObservable = new Observable<>(OnEditListener::onEdit); + public void onEdit(OnEditListener listener) { + onEditObservable.addListener(listener); + } + public void edit() { + onEditObservable.fireEvent(); + } + + private Observable onUpdatedObservable = new Observable<>(OnUpdatedListener::onUpdated); + public void onIsupdated(OnUpdatedListener listener) { + onUpdatedObservable.addListener(listener); + } + public void isUpdatedTrigger() { + onUpdatedObservable.fireEvent(); + } + + private Observable onBeingUpdatedObservable = new Observable<>(OnBeingUpdatedListener::onBeingUpdated); + public void onIsBeingUpdated(OnBeingUpdatedListener listener) { + onBeingUpdatedObservable.addListener(listener); + } + public void onIsBeingUpdatedTrigger() { + onBeingUpdatedObservable.fireEvent(); + } + +} diff --git a/G-Earth/src/main/java/gearth/ui/scheduler/ScheduleItemContainer.java b/G-Earth/src/main/java/gearth/ui/scheduler/ScheduleItemContainer.java index b1040c0..b72c8a3 100644 --- a/G-Earth/src/main/java/gearth/ui/scheduler/ScheduleItemContainer.java +++ b/G-Earth/src/main/java/gearth/ui/scheduler/ScheduleItemContainer.java @@ -1,7 +1,6 @@ package gearth.ui.scheduler; -import javafx.beans.InvalidationListener; -import javafx.beans.Observable; +import gearth.services.scheduler.ScheduleItem; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.control.Label; @@ -19,7 +18,7 @@ import gearth.ui.buttons.PauseResumeButton; public class ScheduleItemContainer extends GridPane { public static final int[] columnWidths = {10, 39, 16, 18, 15}; - ScheduleItem item; + InteractableScheduleItem item; Label indexLabel; Label packetLabel; @@ -28,7 +27,7 @@ public class ScheduleItemContainer extends GridPane { VBox parent; - ScheduleItemContainer(ScheduleItem item, VBox parent, ScrollPane scrollPane) { + ScheduleItemContainer(InteractableScheduleItem item, VBox parent, ScrollPane scrollPane) { super(); setGridLinesVisible(true); VBox.setMargin(this, new Insets(2, -2, -2, -2)); diff --git a/G-Earth/src/main/java/gearth/ui/scheduler/Scheduler.java b/G-Earth/src/main/java/gearth/ui/scheduler/SchedulerController.java similarity index 72% rename from G-Earth/src/main/java/gearth/ui/scheduler/Scheduler.java rename to G-Earth/src/main/java/gearth/ui/scheduler/SchedulerController.java index 0a1fb78..9b8038c 100644 --- a/G-Earth/src/main/java/gearth/ui/scheduler/Scheduler.java +++ b/G-Earth/src/main/java/gearth/ui/scheduler/SchedulerController.java @@ -1,6 +1,9 @@ package gearth.ui.scheduler; import com.tulskiy.keymaster.common.Provider; +import gearth.services.scheduler.Interval; +import gearth.services.scheduler.ScheduleItem; +import gearth.services.scheduler.Scheduler; import javafx.application.Platform; import javafx.event.ActionEvent; import javafx.scene.control.*; @@ -21,7 +24,7 @@ import java.util.Set; /** * Created by Jonas on 06/04/18. */ -public class Scheduler extends SubForm { +public class SchedulerController extends SubForm { private static final Interval defaultInterval = new Interval(0, 500); private static final HPacket defaultPacket = new HPacket(0); @@ -44,9 +47,9 @@ public class Scheduler extends SubForm { public CheckBox cbx_hotkeys; - private ScheduleItem isBeingEdited = null; + private InteractableScheduleItem isBeingEdited = null; - private List scheduleItemList = new ArrayList<>(); + private Scheduler scheduler = null; public void initialize() { @@ -62,49 +65,6 @@ public class Scheduler extends SubForm { updateUI(); - new Thread(() -> { - long t = System.currentTimeMillis(); - long changed = 1; - - Set set = new HashSet<>(); - - while (true) { - try { - Thread.sleep(1); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - set.clear(); - for (int i = scheduleItemList.size() - 1; i >= 0; i--) { - set.add(scheduleItemList.get(i)); - } - - for (ScheduleItem item : set) { - if (!item.getPausedProperty().get()) { - Interval cur = item.getDelayProperty().get(); - for (int i = 0; i < changed; i++) { - if ((t - i) % cur.getDelay() == cur.getOffset()) { - if (item.getDestinationProperty().get() == HMessage.Direction.TOSERVER) { - getHConnection().sendToServerAsync(item.getPacketProperty().get()); - } - else { - getHConnection().sendToClientAsync(item.getPacketProperty().get()); - } - } - } - } - - } - - long newT = System.currentTimeMillis(); - changed = newT - t; - t = newT; - } - }).start(); - - - //register hotkeys //disable some output things PrintStream err = System.err; @@ -121,9 +81,14 @@ public class Scheduler extends SubForm { System.setErr(err); } + @Override + protected void onParentSet() { + scheduler = new Scheduler<>(getHConnection()); + } + private void switchPauseHotkey(int index) { - if (cbx_hotkeys.isSelected() && index < scheduleItemList.size()) { - scheduleItemList.get(index).getPausedProperty().set(!scheduleItemList.get(index).getPausedProperty().get()); + if (cbx_hotkeys.isSelected() && index < scheduler.size()) { + scheduler.get(index).getPausedProperty().set(!scheduler.get(index).getPausedProperty().get()); } } @@ -146,8 +111,8 @@ public class Scheduler extends SubForm { HPacket packet = new HPacket(txt_packet.getText()); if (packet.isCorrupted()) return; - ScheduleItem newItem = new ScheduleItem( - scheduleItemList.size(), + InteractableScheduleItem newItem = new InteractableScheduleItem( + scheduler.size(), false, new Interval(txt_delay.getText()), new HPacket(txt_packet.getText()), @@ -168,9 +133,9 @@ public class Scheduler extends SubForm { } - private void addItem(ScheduleItem newItem) { + private void addItem(InteractableScheduleItem newItem) { new ScheduleItemContainer(newItem, schedulecontainer, scrollpane); - scheduleItemList.add(newItem); + scheduler.add(newItem); newItem.onDelete(() -> { @@ -178,9 +143,9 @@ public class Scheduler extends SubForm { setInputDefault(); isBeingEdited = null; } - scheduleItemList.remove(newItem); - for (int i = 0; i < scheduleItemList.size(); i++) { - scheduleItemList.get(i).getIndexProperty().set(i); + scheduler.remove(newItem); + for (int i = 0; i < scheduler.size(); i++) { + scheduler.get(i).getIndexProperty().set(i); } }); newItem.onEdit(() -> { @@ -219,14 +184,14 @@ public class Scheduler extends SubForm { private void clear() { - for (int i = scheduleItemList.size() - 1; i >= 0; i--) { - scheduleItemList.get(i).delete(); + for (int i = scheduler.size() - 1; i >= 0; i--) { + scheduler.get(i).delete(); } } - private void load(List list) { + private void load(List list) { clear(); - for (ScheduleItem item : list) { + for (InteractableScheduleItem item : list) { addItem(item); } } @@ -254,9 +219,9 @@ public class Scheduler extends SubForm { FileWriter fileWriter = new FileWriter(file); BufferedWriter out = new BufferedWriter(fileWriter); - for (int i = 0; i < scheduleItemList.size(); i++) { - out.write(scheduleItemList.get(i).stringify()); - if (i != scheduleItemList.size() - 1) out.write("\n"); + for (int i = 0; i < scheduler.size(); i++) { + out.write(scheduler.get(i).stringify()); + if (i != scheduler.size() - 1) out.write("\n"); } out.flush(); @@ -270,7 +235,7 @@ public class Scheduler extends SubForm { } public void loadBtnClicked(ActionEvent actionEvent) { - List list = new ArrayList<>(); + List list = new ArrayList<>(); FileChooser fileChooser = new FileChooser(); fileChooser.setTitle("Load Schedule File"); @@ -287,7 +252,7 @@ public class Scheduler extends SubForm { while ((line = br.readLine()) != null) { - list.add(new ScheduleItem(line)); + list.add(new InteractableScheduleItem(line)); } fr.close(); diff --git a/G-Earth/src/main/java/gearth/ui/tools/Tools.java b/G-Earth/src/main/java/gearth/ui/tools/ToolsController.java similarity index 99% rename from G-Earth/src/main/java/gearth/ui/tools/Tools.java rename to G-Earth/src/main/java/gearth/ui/tools/ToolsController.java index d08543e..ef62772 100644 --- a/G-Earth/src/main/java/gearth/ui/tools/Tools.java +++ b/G-Earth/src/main/java/gearth/ui/tools/ToolsController.java @@ -11,7 +11,7 @@ import gearth.ui.SubForm; import java.nio.ByteBuffer; -public class Tools extends SubForm { +public class ToolsController extends SubForm { public TextField txt_intDecoded; public TextField txt_intEncoded; public TextField txt_ushortDecoded; diff --git a/G-Earth/src/main/resources/gearth/ui/connection/Connection.fxml b/G-Earth/src/main/resources/gearth/ui/connection/Connection.fxml index 8f5303d..1a931ba 100644 --- a/G-Earth/src/main/resources/gearth/ui/connection/Connection.fxml +++ b/G-Earth/src/main/resources/gearth/ui/connection/Connection.fxml @@ -11,7 +11,7 @@ - + diff --git a/G-Earth/src/main/resources/gearth/ui/extensions/Extensions.fxml b/G-Earth/src/main/resources/gearth/ui/extensions/Extensions.fxml index 67b21a0..11784c1 100644 --- a/G-Earth/src/main/resources/gearth/ui/extensions/Extensions.fxml +++ b/G-Earth/src/main/resources/gearth/ui/extensions/Extensions.fxml @@ -12,7 +12,7 @@ + fx:controller="gearth.ui.extensions.ExtensionsController"> diff --git a/G-Earth/src/main/resources/gearth/ui/extra/Extra.fxml b/G-Earth/src/main/resources/gearth/ui/extra/Extra.fxml index 715d0dc..4a7346b 100644 --- a/G-Earth/src/main/resources/gearth/ui/extra/Extra.fxml +++ b/G-Earth/src/main/resources/gearth/ui/extra/Extra.fxml @@ -11,7 +11,7 @@ - + diff --git a/G-Earth/src/main/resources/gearth/ui/info/Info.fxml b/G-Earth/src/main/resources/gearth/ui/info/Info.fxml index e1b9540..c639834 100644 --- a/G-Earth/src/main/resources/gearth/ui/info/Info.fxml +++ b/G-Earth/src/main/resources/gearth/ui/info/Info.fxml @@ -5,7 +5,7 @@ - + diff --git a/G-Earth/src/main/resources/gearth/ui/injection/Injection.fxml b/G-Earth/src/main/resources/gearth/ui/injection/Injection.fxml index cd58ec6..7012cfc 100644 --- a/G-Earth/src/main/resources/gearth/ui/injection/Injection.fxml +++ b/G-Earth/src/main/resources/gearth/ui/injection/Injection.fxml @@ -12,7 +12,7 @@ + fx:controller="gearth.ui.injection.InjectionController"> diff --git a/G-Earth/src/main/resources/gearth/ui/logger/Logger.fxml b/G-Earth/src/main/resources/gearth/ui/logger/Logger.fxml index fb7846e..1d8d62d 100644 --- a/G-Earth/src/main/resources/gearth/ui/logger/Logger.fxml +++ b/G-Earth/src/main/resources/gearth/ui/logger/Logger.fxml @@ -14,7 +14,7 @@ + fx:controller="gearth.ui.logger.LoggerController"> diff --git a/G-Earth/src/main/resources/gearth/ui/scheduler/Scheduler.fxml b/G-Earth/src/main/resources/gearth/ui/scheduler/Scheduler.fxml index 1fec3e4..4955214 100644 --- a/G-Earth/src/main/resources/gearth/ui/scheduler/Scheduler.fxml +++ b/G-Earth/src/main/resources/gearth/ui/scheduler/Scheduler.fxml @@ -4,7 +4,7 @@ - + diff --git a/G-Earth/src/main/resources/gearth/ui/tools/Tools.fxml b/G-Earth/src/main/resources/gearth/ui/tools/Tools.fxml index e933629..95ff97e 100644 --- a/G-Earth/src/main/resources/gearth/ui/tools/Tools.fxml +++ b/G-Earth/src/main/resources/gearth/ui/tools/Tools.fxml @@ -12,7 +12,7 @@ + fx:controller="gearth.ui.tools.ToolsController">