From be2c8adecb6dc8b5590356b0386bdab684e8191d Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Sun, 26 Apr 2020 22:00:30 +0200 Subject: [PATCH] change listener<->observable designpattern --- .../java/gearth/extensions/Extension.java | 17 ++-- .../java/gearth/extensions/ExtensionForm.java | 2 +- .../java/gearth/extensions/IExtension.java | 2 +- .../extensions/OnConnectionListener.java | 5 ++ .../extensions/extra/harble/ChatConsole.java | 20 ++--- .../extra/harble/ChatInputListener.java | 5 ++ .../main/java/gearth/misc/StringifyAble.java | 1 + .../misc/listenerpattern/Observable.java | 36 +++++++++ .../SynchronizedObservable.java | 22 +++++ .../java/gearth/protocol/HConnection.java | 36 ++++----- .../gearth/protocol/memory/Rc4Obtainer.java | 4 +- .../packethandler/IncomingPacketHandler.java | 11 +-- .../packethandler/OutgoingPacketHandler.java | 4 +- .../protocol/packethandler/PacketHandler.java | 29 +++---- .../ExtensionConnectedListener.java | 7 ++ .../extensionhandler/ExtensionHandler.java | 54 ++++++------- .../extensions/GEarthExtension.java | 80 +++++-------------- .../ExtensionProducerFactory.java | 4 +- .../ExtensionProducerObserver.java | 2 +- .../network/NetworkExtension.java | 6 +- .../network/NetworkExtensionInfo.java | 2 +- .../network/NetworkExtensionsProducer.java | 6 +- .../network/authentication/Authenticator.java | 4 +- .../network/executer/ExecutionInfo.java | 2 +- .../network/executer/ExtensionRunner.java | 2 +- .../executer/ExtensionRunnerFactory.java | 2 +- .../executer/NormalExtensionRunner.java | 4 +- .../simple/ExampleExtension.java | 2 +- .../{ => implementations}/simple/README.md | 0 .../simple/SimpleExtensionProducer.java | 2 +- .../listeners/OmRemoveClickListener.java | 5 ++ .../extensions/listeners/OnClickListener.java | 5 ++ .../listeners/OnDeleteListener.java | 5 ++ .../java/gearth/ui/connection/Connection.java | 2 +- .../ui/extensions/ExtensionItemContainer.java | 14 ++-- .../java/gearth/ui/extensions/Extensions.java | 10 +-- .../src/main/java/gearth/ui/extra/Extra.java | 2 +- .../java/gearth/ui/injection/Injection.java | 2 +- .../main/java/gearth/ui/logger/Logger.java | 2 +- .../gearth/ui/scheduler/ScheduleItem.java | 55 ++++++------- .../ui/scheduler/ScheduleItemContainer.java | 6 +- .../java/gearth/ui/scheduler/Scheduler.java | 8 +- .../listeners/OnBeingUpdatedListener.java | 5 ++ .../scheduler/listeners/OnDeleteListener.java | 5 ++ .../scheduler/listeners/OnEditListener.java | 5 ++ .../listeners/OnUpdatedListener.java | 5 ++ 46 files changed, 274 insertions(+), 235 deletions(-) create mode 100644 G-Earth/src/main/java/gearth/extensions/OnConnectionListener.java create mode 100644 G-Earth/src/main/java/gearth/extensions/extra/harble/ChatInputListener.java create mode 100644 G-Earth/src/main/java/gearth/misc/listenerpattern/Observable.java create mode 100644 G-Earth/src/main/java/gearth/misc/listenerpattern/SynchronizedObservable.java create mode 100644 G-Earth/src/main/java/gearth/services/extensionhandler/ExtensionConnectedListener.java rename G-Earth/src/main/java/gearth/services/extensionhandler/extensions/{ => implementations}/network/NetworkExtension.java (97%) rename G-Earth/src/main/java/gearth/services/extensionhandler/extensions/{ => implementations}/network/NetworkExtensionInfo.java (99%) rename G-Earth/src/main/java/gearth/services/extensionhandler/extensions/{ => implementations}/network/NetworkExtensionsProducer.java (92%) rename G-Earth/src/main/java/gearth/services/extensionhandler/extensions/{ => implementations}/network/authentication/Authenticator.java (94%) rename G-Earth/src/main/java/gearth/services/extensionhandler/extensions/{ => implementations}/network/executer/ExecutionInfo.java (95%) rename G-Earth/src/main/java/gearth/services/extensionhandler/extensions/{ => implementations}/network/executer/ExtensionRunner.java (78%) rename G-Earth/src/main/java/gearth/services/extensionhandler/extensions/{ => implementations}/network/executer/ExtensionRunnerFactory.java (78%) rename G-Earth/src/main/java/gearth/services/extensionhandler/extensions/{ => implementations}/network/executer/NormalExtensionRunner.java (97%) rename G-Earth/src/main/java/gearth/services/extensionhandler/extensions/{ => implementations}/simple/ExampleExtension.java (97%) rename G-Earth/src/main/java/gearth/services/extensionhandler/extensions/{ => implementations}/simple/README.md (100%) rename G-Earth/src/main/java/gearth/services/extensionhandler/extensions/{ => implementations}/simple/SimpleExtensionProducer.java (87%) create mode 100644 G-Earth/src/main/java/gearth/services/extensionhandler/extensions/listeners/OmRemoveClickListener.java create mode 100644 G-Earth/src/main/java/gearth/services/extensionhandler/extensions/listeners/OnClickListener.java create mode 100644 G-Earth/src/main/java/gearth/services/extensionhandler/extensions/listeners/OnDeleteListener.java create mode 100644 G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnBeingUpdatedListener.java create mode 100644 G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnDeleteListener.java create mode 100644 G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnEditListener.java create mode 100644 G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnUpdatedListener.java diff --git a/G-Earth/src/main/java/gearth/extensions/Extension.java b/G-Earth/src/main/java/gearth/extensions/Extension.java index 4eddf0d..b8ea668 100644 --- a/G-Earth/src/main/java/gearth/extensions/Extension.java +++ b/G-Earth/src/main/java/gearth/extensions/Extension.java @@ -1,8 +1,9 @@ package gearth.extensions; +import gearth.misc.listenerpattern.Observable; import gearth.protocol.HMessage; import gearth.protocol.HPacket; -import gearth.services.extensionhandler.extensions.network.NetworkExtensionInfo; +import gearth.services.extensionhandler.extensions.implementations.network.NetworkExtensionInfo; import java.io.*; import java.net.Socket; @@ -139,7 +140,7 @@ public abstract class Extension implements IExtension{ int connectionPort = packet.readInteger(); String hotelVersion = packet.readString(); String harbleMessagesPath = packet.readString(); - notifyConnectionListeners(host, connectionPort, hotelVersion, harbleMessagesPath); + onConnectionObservable.fireEvent(l -> l.onConnection(host, connectionPort, hotelVersion, harbleMessagesPath)); onStartConnection(); } else if (packet.headerId() == NetworkExtensionInfo.OUTGOING_MESSAGES_IDS.CONNECTIONEND) { @@ -367,17 +368,9 @@ public abstract class Extension implements IExtension{ } - public interface OnConnectionListener { - void act(String host, int port, String hotelversion, String harbleMessagesPath); - } - private List onConnectionListeners = new ArrayList<>(); + private Observable onConnectionObservable = new Observable<>(); public void onConnect(OnConnectionListener listener){ - onConnectionListeners.add(listener); - } - private void notifyConnectionListeners(String host, int port, String hotelversion, String harbleMessagesPath) { - for (OnConnectionListener listener : onConnectionListeners) { - listener.act(host, port, hotelversion, harbleMessagesPath); - } + onConnectionObservable.addListener(listener); } } diff --git a/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java b/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java index 0018b2a..6f246a1 100644 --- a/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java +++ b/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java @@ -40,7 +40,7 @@ public abstract class ExtensionForm implements IExtension{ public boolean sendToClient(HPacket packet){ return extension.sendToClient(packet); } - public void onConnect(Extension.OnConnectionListener listener) { + public void onConnect(OnConnectionListener listener) { extension.onConnect(listener); } diff --git a/G-Earth/src/main/java/gearth/extensions/IExtension.java b/G-Earth/src/main/java/gearth/extensions/IExtension.java index 635249f..74a46eb 100644 --- a/G-Earth/src/main/java/gearth/extensions/IExtension.java +++ b/G-Earth/src/main/java/gearth/extensions/IExtension.java @@ -14,6 +14,6 @@ public interface IExtension { void intercept(HMessage.Direction direction, Extension.MessageListener messageListener); boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback); void writeToConsole(String s); // not implemented in g-earth yet - void onConnect(Extension.OnConnectionListener listener); + void onConnect(OnConnectionListener listener); } diff --git a/G-Earth/src/main/java/gearth/extensions/OnConnectionListener.java b/G-Earth/src/main/java/gearth/extensions/OnConnectionListener.java new file mode 100644 index 0000000..fdd0256 --- /dev/null +++ b/G-Earth/src/main/java/gearth/extensions/OnConnectionListener.java @@ -0,0 +1,5 @@ +package gearth.extensions; + +public interface OnConnectionListener { + void onConnection(String host, int port, String hotelversion, String harbleMessagesPath); +} diff --git a/G-Earth/src/main/java/gearth/extensions/extra/harble/ChatConsole.java b/G-Earth/src/main/java/gearth/extensions/extra/harble/ChatConsole.java index d215296..3f2ed78 100644 --- a/G-Earth/src/main/java/gearth/extensions/extra/harble/ChatConsole.java +++ b/G-Earth/src/main/java/gearth/extensions/extra/harble/ChatConsole.java @@ -2,12 +2,11 @@ package gearth.extensions.extra.harble; import gearth.extensions.ExtensionInfo; import gearth.extensions.IExtension; +import gearth.extensions.OnConnectionListener; +import gearth.misc.listenerpattern.Observable; import gearth.protocol.HMessage; import gearth.protocol.HPacket; -import java.util.ArrayList; -import java.util.List; - /** * Created by Jonas on 3/12/2018. */ @@ -23,6 +22,7 @@ public class ChatConsole { private volatile String infoMessage; private volatile boolean firstTime = true; + private volatile Observable chatInputObservable = new Observable<>(); public ChatConsole(final HashSupport hashSupport, IExtension extension) { @@ -42,7 +42,6 @@ public class ChatConsole { this.infoMessage = infoMessage; final boolean[] doOncePerConnection = {false}; - extension.onConnect((s, i, s1, h1) -> doOncePerConnection[0] = true); extension.intercept(HMessage.Direction.TOSERVER, hMessage -> { @@ -81,7 +80,7 @@ public class ChatConsole { writeOutput(infoMessage, false); } else { - notifyChatInputListeners(str); + chatInputObservable.fireEvent(l -> l.inputEntered(str)); } } }); @@ -106,17 +105,8 @@ public class ChatConsole { } } - public interface ChatInputListener { - void inputEntered(String input); - } - private List chatInputListenerList = new ArrayList(); public void onInput(ChatInputListener listener) { - chatInputListenerList.add(listener); - } - private void notifyChatInputListeners (String s) { - for (ChatInputListener listener : chatInputListenerList) { - listener.inputEntered(s); - } + chatInputObservable.addListener(listener); } diff --git a/G-Earth/src/main/java/gearth/extensions/extra/harble/ChatInputListener.java b/G-Earth/src/main/java/gearth/extensions/extra/harble/ChatInputListener.java new file mode 100644 index 0000000..a9a933a --- /dev/null +++ b/G-Earth/src/main/java/gearth/extensions/extra/harble/ChatInputListener.java @@ -0,0 +1,5 @@ +package gearth.extensions.extra.harble; + +public interface ChatInputListener { + void inputEntered(String input); +} diff --git a/G-Earth/src/main/java/gearth/misc/StringifyAble.java b/G-Earth/src/main/java/gearth/misc/StringifyAble.java index 5039c80..7703a34 100644 --- a/G-Earth/src/main/java/gearth/misc/StringifyAble.java +++ b/G-Earth/src/main/java/gearth/misc/StringifyAble.java @@ -3,6 +3,7 @@ package gearth.misc; /** * Created by Jonas on 14/06/18. * This interface defines an object which can FULLY be represented as a String (all fields). An object must be able to be recreated having the String representation + * So this is basically "Serializable" but for Strings */ public interface StringifyAble { diff --git a/G-Earth/src/main/java/gearth/misc/listenerpattern/Observable.java b/G-Earth/src/main/java/gearth/misc/listenerpattern/Observable.java new file mode 100644 index 0000000..4ae70f4 --- /dev/null +++ b/G-Earth/src/main/java/gearth/misc/listenerpattern/Observable.java @@ -0,0 +1,36 @@ +package gearth.misc.listenerpattern; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +public class Observable { + + private Consumer defaultConsumer = null; + + public Observable(Consumer defaultConsumer) { + this.defaultConsumer = defaultConsumer; + } + + public Observable() { + // no default consumer + this(listener -> {}); + } + + private List listeners = new ArrayList<>(); + public void addListener(Listener listener) { + listeners.add(listener); + } + public void removeListener(Listener listener) { + listeners.remove(listener); + } + public void fireEvent(Consumer consumer) { + for (int i = listeners.size() - 1; i >= 0; i--) { + consumer.accept(listeners.get(i)); + } + } + public void fireEvent() { + fireEvent(defaultConsumer); + } + +} diff --git a/G-Earth/src/main/java/gearth/misc/listenerpattern/SynchronizedObservable.java b/G-Earth/src/main/java/gearth/misc/listenerpattern/SynchronizedObservable.java new file mode 100644 index 0000000..eef7fac --- /dev/null +++ b/G-Earth/src/main/java/gearth/misc/listenerpattern/SynchronizedObservable.java @@ -0,0 +1,22 @@ +package gearth.misc.listenerpattern; + +import java.util.function.Consumer; + +public class SynchronizedObservable extends Observable { + + public SynchronizedObservable(Consumer defaultConsumer) { + super(defaultConsumer); + } + + public SynchronizedObservable() { + super(); + } + + @Override + public void fireEvent(Consumer consumer) { + synchronized (this) { + super.fireEvent(consumer); + } + } + +} diff --git a/G-Earth/src/main/java/gearth/protocol/HConnection.java b/G-Earth/src/main/java/gearth/protocol/HConnection.java index 9ecc63b..6c1050c 100644 --- a/G-Earth/src/main/java/gearth/protocol/HConnection.java +++ b/G-Earth/src/main/java/gearth/protocol/HConnection.java @@ -2,6 +2,7 @@ package gearth.protocol; import gearth.misc.Cacher; import gearth.misc.OSValidator; +import gearth.misc.listenerpattern.Observable; import gearth.protocol.hostreplacer.hostsfile.HostReplacer; import gearth.protocol.hostreplacer.hostsfile.HostReplacerFactory; import gearth.protocol.hostreplacer.ipmapping.IpMapper; @@ -17,9 +18,6 @@ import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import java.util.*; -import java.util.function.IntFunction; -import java.util.function.Predicate; -import java.util.stream.Stream; public class HConnection { @@ -122,8 +120,9 @@ public class HConnection { private static final HostReplacer hostsReplacer = HostReplacerFactory.get(); private volatile boolean hostRedirected = false; - private volatile Object[] trafficListeners = {new ArrayList(), new ArrayList(), new ArrayList()}; - private volatile List stateChangeListeners = new ArrayList<>(); + private volatile Object[] trafficObservables = {new Observable(), new Observable(), new Observable()}; + private volatile Observable stateObservable = new Observable<>(); + private volatile State state = State.NOT_CONNECTED; public static class Proxy { @@ -416,8 +415,8 @@ public class HConnection { final boolean[] aborted = new boolean[1]; Rc4Obtainer rc4Obtainer = new Rc4Obtainer(this); - OutgoingPacketHandler outgoingHandler = new OutgoingPacketHandler(habbo_server_out, trafficListeners); - IncomingPacketHandler incomingHandler = new IncomingPacketHandler(client_out, trafficListeners); + OutgoingPacketHandler outgoingHandler = new OutgoingPacketHandler(habbo_server_out, trafficObservables); + IncomingPacketHandler incomingHandler = new IncomingPacketHandler(client_out, trafficObservables); rc4Obtainer.setPacketHandlers(outgoingHandler, incomingHandler); outgoingHandler.addOnDatastreamConfirmedListener(hotelVersion -> { @@ -593,13 +592,15 @@ public class HConnection { State buffer = this.state; this.state = state; - for (StateChangeListener listener : stateChangeListeners) { - listener.stateChanged(buffer, state); - } + stateObservable.fireEvent(l -> l.stateChanged(buffer, state)); } } + public Observable getStateObservable() { + return stateObservable; + } + /** * 3 orders: * 0 = before modification ¹ @@ -609,19 +610,12 @@ public class HConnection { * ¹don't edit the packet (block, replace) */ public void addTrafficListener(int order, TrafficListener listener) { - ((List)trafficListeners[order]).add(listener); + ((Observable) trafficObservables[order]).addListener(listener); } public void removeTrafficListener(TrafficListener listener) { - ((List)trafficListeners[0]).remove(listener); - ((List)trafficListeners[1]).remove(listener); - ((List)trafficListeners[2]).remove(listener); - } - - public void addStateChangeListener(StateChangeListener listener) { - stateChangeListeners.add(listener); - } - public void removeStateChangeListener(StateChangeListener listener) { - stateChangeListeners.remove(listener); + ((Observable) trafficObservables[0]).removeListener(listener); + ((Observable) trafficObservables[1]).removeListener(listener); + ((Observable) trafficObservables[2]).removeListener(listener); } public int getServerPort() { diff --git a/G-Earth/src/main/java/gearth/protocol/memory/Rc4Obtainer.java b/G-Earth/src/main/java/gearth/protocol/memory/Rc4Obtainer.java index a0abaf8..2f548a6 100644 --- a/G-Earth/src/main/java/gearth/protocol/memory/Rc4Obtainer.java +++ b/G-Earth/src/main/java/gearth/protocol/memory/Rc4Obtainer.java @@ -41,11 +41,11 @@ public class Rc4Obtainer { public void act() { if (handler.isEncryptedStream()) { onSendFirstEncryptedMessage(handler); - handler.removeBufferChangeListener(this); + handler.getBufferChangeObservable().removeListener(this); } } }; - handler.onBufferChanged(bufferChangeListener); + handler.getBufferChangeObservable().addListener(bufferChangeListener); } diff --git a/G-Earth/src/main/java/gearth/protocol/packethandler/IncomingPacketHandler.java b/G-Earth/src/main/java/gearth/protocol/packethandler/IncomingPacketHandler.java index a827ca4..c8ef55f 100644 --- a/G-Earth/src/main/java/gearth/protocol/packethandler/IncomingPacketHandler.java +++ b/G-Earth/src/main/java/gearth/protocol/packethandler/IncomingPacketHandler.java @@ -1,5 +1,6 @@ package gearth.protocol.packethandler; +import gearth.misc.listenerpattern.Observable; import gearth.protocol.HMessage; import gearth.protocol.HPacket; import gearth.protocol.TrafficListener; @@ -10,25 +11,25 @@ import java.util.List; public class IncomingPacketHandler extends PacketHandler { - public IncomingPacketHandler(OutputStream outputStream, Object[] listeners) { - super(outputStream, listeners); + public IncomingPacketHandler(OutputStream outputStream, Object[] trafficObservables) { + super(outputStream, trafficObservables); TrafficListener listener = new TrafficListener() { @Override public void onCapture(HMessage message) { if (isDataStream && message.getPacket().structureEquals("s,b")) { - ((List)listeners[0]).remove(this); + ((Observable)trafficObservables[0]).removeListener(this); HPacket packet = message.getPacket(); packet.readString(); isEncryptedStream = packet.readBoolean(); } else if (message.getIndex() > 3) { - ((List)listeners[0]).remove(this); + ((Observable)trafficObservables[0]).removeListener(this); } } }; - ((List)listeners[0]).add(listener); + ((Observable)trafficObservables[0]).addListener(listener); } @Override diff --git a/G-Earth/src/main/java/gearth/protocol/packethandler/OutgoingPacketHandler.java b/G-Earth/src/main/java/gearth/protocol/packethandler/OutgoingPacketHandler.java index 0fbecf2..38da87c 100644 --- a/G-Earth/src/main/java/gearth/protocol/packethandler/OutgoingPacketHandler.java +++ b/G-Earth/src/main/java/gearth/protocol/packethandler/OutgoingPacketHandler.java @@ -10,8 +10,8 @@ import java.util.List; public class OutgoingPacketHandler extends PacketHandler { - public OutgoingPacketHandler(OutputStream outputStream, Object[] listeners) { - super(outputStream, listeners); + public OutgoingPacketHandler(OutputStream outputStream, Object[] trafficObservables) { + super(outputStream, trafficObservables); } private List onDatastreamConfirmedListeners = new ArrayList<>(); diff --git a/G-Earth/src/main/java/gearth/protocol/packethandler/PacketHandler.java b/G-Earth/src/main/java/gearth/protocol/packethandler/PacketHandler.java index 9ed6547..7cab0ef 100644 --- a/G-Earth/src/main/java/gearth/protocol/packethandler/PacketHandler.java +++ b/G-Earth/src/main/java/gearth/protocol/packethandler/PacketHandler.java @@ -1,5 +1,6 @@ package gearth.protocol.packethandler; +import gearth.misc.listenerpattern.Observable; import gearth.protocol.HConnection; import gearth.protocol.HMessage; import gearth.protocol.HPacket; @@ -17,7 +18,7 @@ public abstract class PacketHandler { volatile PayloadBuffer payloadBuffer = new PayloadBuffer(); volatile OutputStream out; - volatile Object[] listeners = null; //get notified on packet send + volatile Object[] trafficObservables; //get notified on packet send volatile boolean isTempBlocked = false; volatile boolean isDataStream = false; volatile int currentIndex = 0; @@ -31,8 +32,8 @@ public abstract class PacketHandler { protected volatile boolean isEncryptedStream = false; - public PacketHandler(OutputStream outputStream, Object[] listeners) { - this.listeners = listeners; + public PacketHandler(OutputStream outputStream, Object[] trafficObservables) { + this.trafficObservables = trafficObservables; out = outputStream; } @@ -47,7 +48,7 @@ public abstract class PacketHandler { public abstract void act(byte[] buffer) throws IOException; protected void continuedAct(byte[] buffer) throws IOException { - notifyBufferListeners(); + bufferChangeObservable.fireEvent(); if (!isEncryptedStream) { payloadBuffer.push(buffer); @@ -111,10 +112,10 @@ public abstract class PacketHandler { */ void notifyListeners(HMessage message) { for (int x = 0; x < 3; x++) { - for (int i = ((List)listeners[x]).size() - 1; i >= 0; i--) { + ((Observable) trafficObservables[x]).fireEvent(trafficListener -> { message.getPacket().resetReadIndex(); - ((List)listeners[x]).get(i).onCapture(message); - } + trafficListener.onCapture(message); + }); } message.getPacket().resetReadIndex(); } @@ -164,17 +165,9 @@ public abstract class PacketHandler { protected abstract void printForDebugging(byte[] bytes); - private List bufferChangeListeners = new ArrayList<>(); - public void onBufferChanged(BufferChangeListener listener) { - bufferChangeListeners.add(listener); - } - public void removeBufferChangeListener(BufferChangeListener listener) { - bufferChangeListeners.remove(listener); - } - void notifyBufferListeners() { - for (int i = bufferChangeListeners.size() - 1; i >= 0; i -= 1) { - bufferChangeListeners.get(i).act(); - } + private Observable bufferChangeObservable = new Observable<>(BufferChangeListener::act); + public Observable getBufferChangeObservable() { + return bufferChangeObservable; } public int getCurrentIndex() { diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/ExtensionConnectedListener.java b/G-Earth/src/main/java/gearth/services/extensionhandler/ExtensionConnectedListener.java new file mode 100644 index 0000000..05bcb68 --- /dev/null +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/ExtensionConnectedListener.java @@ -0,0 +1,7 @@ +package gearth.services.extensionhandler; + +import gearth.services.extensionhandler.extensions.GEarthExtension; + +public interface ExtensionConnectedListener { + void onExtensionConnect(GEarthExtension e); +} diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/ExtensionHandler.java b/G-Earth/src/main/java/gearth/services/extensionhandler/ExtensionHandler.java index cce13e7..1621ecb 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/ExtensionHandler.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/ExtensionHandler.java @@ -2,6 +2,7 @@ package gearth.services.extensionhandler; import gearth.Main; import gearth.misc.harble_api.HarbleAPIFetcher; +import gearth.misc.listenerpattern.Observable; import gearth.protocol.HConnection; import gearth.protocol.HMessage; import gearth.protocol.HPacket; @@ -12,12 +13,22 @@ import gearth.services.extensionhandler.extensions.extensionproducers.ExtensionP import gearth.services.extensionhandler.extensions.GEarthExtension; import java.util.*; +import java.util.function.Consumer; public class ExtensionHandler { private final List gEarthExtensions = new ArrayList<>(); private final HConnection hConnection; private List extensionProducers; + private Observable observable = new Observable<>() { + @Override + public void addListener(ExtensionConnectedListener extensionConnectedListener) { + super.addListener(extensionConnectedListener); + for (GEarthExtension gEarthExtension : gEarthExtensions) { + extensionConnectedListener.onExtensionConnect(gEarthExtension); + } + } + }; public ExtensionHandler(HConnection hConnection) { @@ -27,7 +38,7 @@ public class ExtensionHandler { private void initialize() { - hConnection.addStateChangeListener((oldState, newState) -> { + hConnection.getStateObservable().addListener((oldState, newState) -> { if (newState == HConnection.State.CONNECTED) { HarbleAPIFetcher.fetch(hConnection.getHotelVersion()); synchronized (gEarthExtensions) { @@ -76,12 +87,12 @@ public class ExtensionHandler { collection.remove(extension); } - extension.removeExtensionListener(this); + extension.getExtensionObservable().removeListener(this); } } } }; - extension.registerExtensionListener(respondCallback); + extension.getExtensionObservable().addListener(respondCallback); } } @@ -134,7 +145,7 @@ public class ExtensionHandler { private void initializeExtensionProducer(ExtensionProducer producer) { producer.startProducing(new ExtensionProducerObserver() { @Override - public void onExtensionConnect(GEarthExtension extension) { + public void onExtensionProduced(GEarthExtension extension) { synchronized (gEarthExtensions) { gEarthExtensions.add(extension); } @@ -167,12 +178,12 @@ public class ExtensionHandler { synchronized (gEarthExtensions) { gEarthExtensions.remove(extension); } - extension.removeExtensionListener(this); - extension.delete(); + extension.getExtensionObservable().removeListener(this); + extension.getDeletedObservable().fireEvent(); } }; - extension.registerExtensionListener(listener); + extension.getExtensionObservable().addListener(listener); extension.init(); if (hConnection.getState() == HConnection.State.CONNECTED) { @@ -184,10 +195,10 @@ public class ExtensionHandler { ); } - extension.onRemoveClick(observable -> extension.close()); - extension.onClick(observable -> extension.doubleclick()); + extension.getRemoveClickObservable().addListener(extension::close); + extension.getClickedObservable().addListener(extension::doubleclick); - notifyExtensionConnectListeners(extension); + observable.fireEvent(l -> l.onExtensionConnect(extension)); } }); } @@ -197,24 +208,9 @@ public class ExtensionHandler { return extensionProducers; } - public interface ExtensionConnectListener { - void extensionConnected(GEarthExtension e); - } - private List listeners = new ArrayList<>(); - public void onExtensionConnected(ExtensionConnectListener extensionConnectListener) { - synchronized (gEarthExtensions) { - for (GEarthExtension gEarthExtension : gEarthExtensions) { - extensionConnectListener.extensionConnected(gEarthExtension); - } - } - listeners.add(extensionConnectListener); - } - public void removeExtensionConnectListener(ExtensionConnectListener extensionConnectListener) { - listeners.remove(extensionConnectListener); - } - private void notifyExtensionConnectListeners(GEarthExtension extension) { - for (int i = listeners.size() - 1; i >= 0; i--) { - listeners.get(i).extensionConnected(extension); - } + public Observable getObservable() { + return observable; } + + } diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/GEarthExtension.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/GEarthExtension.java index 3765a1d..05c2d77 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/GEarthExtension.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/GEarthExtension.java @@ -1,12 +1,12 @@ package gearth.services.extensionhandler.extensions; +import gearth.misc.listenerpattern.Observable; +import gearth.misc.listenerpattern.SynchronizedObservable; import gearth.protocol.HMessage; import gearth.protocol.HPacket; -import javafx.beans.InvalidationListener; - -import java.util.ArrayList; -import java.util.List; -import java.util.function.Consumer; +import gearth.services.extensionhandler.extensions.listeners.OmRemoveClickListener; +import gearth.services.extensionhandler.extensions.listeners.OnClickListener; +import gearth.services.extensionhandler.extensions.listeners.OnDeleteListener; public abstract class GEarthExtension { @@ -47,45 +47,36 @@ public abstract class GEarthExtension { // ----------------- listen to the extension --------------------- - protected final List extensionListeners = new ArrayList<>(); - public void registerExtensionListener(ExtensionListener listener) { - this.extensionListeners.add(listener); - } - public void removeExtensionListener(ExtensionListener listener) { - this.extensionListeners.remove(listener); - } - private void notifyListeners(Consumer consumer) { - for (int i = extensionListeners.size() - 1; i >= 0; i--) { - consumer.accept(extensionListeners.get(i)); - } - extensionListeners.forEach(consumer); + protected final Observable extensionObservable = new Observable<>(); + public Observable getExtensionObservable() { + return extensionObservable; } protected void sendManipulatedPacket(HMessage hMessage) { int orgIndex = hMessage.getPacket().getReadIndex(); - notifyListeners(listener -> { + extensionObservable.fireEvent(listener -> { hMessage.getPacket().setReadIndex(6); listener.manipulatedPacket(hMessage); }); hMessage.getPacket().setReadIndex(orgIndex); } protected void requestFlags() { - notifyListeners(ExtensionListener::flagsRequest); + extensionObservable.fireEvent(ExtensionListener::flagsRequest); } protected void sendMessage(HMessage.Direction direction, HPacket packet) { int orgIndex = packet.getReadIndex(); - notifyListeners(listener -> { + extensionObservable.fireEvent(listener -> { packet.setReadIndex(6); listener.sendMessage(direction, packet); }); packet.setReadIndex(orgIndex); } protected void log(String text) { - notifyListeners(listener -> listener.log(text)); + extensionObservable.fireEvent(listener -> listener.log(text)); } protected void hasClosed() { - notifyListeners(ExtensionListener::hasClosed); + extensionObservable.fireEvent(ExtensionListener::hasClosed); } // -------------------------------------------------------------------- @@ -97,46 +88,19 @@ public abstract class GEarthExtension { // ----------- methods for interaction with G-Earth UI, don't use/change them ---------------- - private final List onRemoveClickListener = new ArrayList<>(); - public void onRemoveClick(InvalidationListener listener) { - synchronized (onRemoveClickListener) { - onRemoveClickListener.add(listener); - } - } - public void isRemoveClickTrigger() { - synchronized (onRemoveClickListener) { - for (int i = onRemoveClickListener.size() - 1; i >= 0; i--) { - onRemoveClickListener.get(i).invalidated(null); - } - } + private final Observable removeClickObservable = new SynchronizedObservable<>(OmRemoveClickListener::onRemove); + public Observable getRemoveClickObservable() { + return removeClickObservable; } - private final List onClickListener = new ArrayList<>(); - public void onClick(InvalidationListener listener) { - synchronized (onClickListener) { - onClickListener.add(listener); - } - } - public void isClickTrigger() { - synchronized (onClickListener) { - for (int i = onClickListener.size() - 1; i >= 0; i--) { - onClickListener.get(i).invalidated(null); - } - } + private final Observable clickedObservable = new SynchronizedObservable<>(OnClickListener::onClick); + public Observable getClickedObservable() { + return clickedObservable; } - private final List onDeleteListeners = new ArrayList<>(); - public void onDelete(InvalidationListener listener) { - synchronized (onDeleteListeners) { - onDeleteListeners.add(listener); - } - } - public void delete() { - synchronized (onDeleteListeners) { - for (int i = onDeleteListeners.size() - 1; i >= 0; i--) { - onDeleteListeners.get(i).invalidated(null); - } - } + private final Observable deletedObservable = new SynchronizedObservable<>(OnDeleteListener::onDelete); + public Observable getDeletedObservable() { + return deletedObservable; } // ---------------------------------------------------------------------------------------- diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/extensionproducers/ExtensionProducerFactory.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/extensionproducers/ExtensionProducerFactory.java index 9b65d02..d02b910 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/extensionproducers/ExtensionProducerFactory.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/extensionproducers/ExtensionProducerFactory.java @@ -1,7 +1,7 @@ package gearth.services.extensionhandler.extensions.extensionproducers; -import gearth.services.extensionhandler.extensions.network.NetworkExtensionsProducer; -import gearth.services.extensionhandler.extensions.simple.SimpleExtensionProducer; +import gearth.services.extensionhandler.extensions.implementations.network.NetworkExtensionsProducer; +import gearth.services.extensionhandler.extensions.implementations.simple.SimpleExtensionProducer; import java.util.Arrays; import java.util.List; diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/extensionproducers/ExtensionProducerObserver.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/extensionproducers/ExtensionProducerObserver.java index 18a6a18..4903166 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/extensionproducers/ExtensionProducerObserver.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/extensionproducers/ExtensionProducerObserver.java @@ -3,5 +3,5 @@ package gearth.services.extensionhandler.extensions.extensionproducers; import gearth.services.extensionhandler.extensions.GEarthExtension; public interface ExtensionProducerObserver { - void onExtensionConnect(GEarthExtension extension); + void onExtensionProduced(GEarthExtension extension); } diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/NetworkExtension.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/NetworkExtension.java similarity index 97% rename from G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/NetworkExtension.java rename to G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/NetworkExtension.java index c5ec4b8..f40b790 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/NetworkExtension.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/NetworkExtension.java @@ -1,4 +1,4 @@ -package gearth.services.extensionhandler.extensions.network; +package gearth.services.extensionhandler.extensions.implementations.network; import gearth.protocol.HMessage; import gearth.services.extensionhandler.extensions.GEarthExtension; @@ -63,7 +63,7 @@ public class NetworkExtension extends GEarthExtension { HPacket message = new HPacket(headerandbody); message.fixLength(); - synchronized (selff.extensionListeners) { + synchronized (selff.extensionObservable) { if (message.headerId() == NetworkExtensionInfo.INCOMING_MESSAGES_IDS.REQUESTFLAGS) { requestFlags(); } @@ -96,7 +96,7 @@ public class NetworkExtension extends GEarthExtension { } catch (IOException e) { // An extension disconnected, which is OK } finally { - synchronized (selff.extensionListeners) { + synchronized (selff.extensionObservable) { hasClosed(); } if (!connection.isClosed()) { diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/NetworkExtensionInfo.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/NetworkExtensionInfo.java similarity index 99% rename from G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/NetworkExtensionInfo.java rename to G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/NetworkExtensionInfo.java index 8f45e81..c2c8e5e 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/NetworkExtensionInfo.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/NetworkExtensionInfo.java @@ -1,4 +1,4 @@ -package gearth.services.extensionhandler.extensions.network; +package gearth.services.extensionhandler.extensions.implementations.network; public class NetworkExtensionInfo { diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/NetworkExtensionsProducer.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/NetworkExtensionsProducer.java similarity index 92% rename from G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/NetworkExtensionsProducer.java rename to G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/NetworkExtensionsProducer.java index 0cddc5a..6433ad1 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/NetworkExtensionsProducer.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/NetworkExtensionsProducer.java @@ -1,9 +1,9 @@ -package gearth.services.extensionhandler.extensions.network; +package gearth.services.extensionhandler.extensions.implementations.network; import gearth.protocol.HPacket; import gearth.services.extensionhandler.extensions.extensionproducers.ExtensionProducer; import gearth.services.extensionhandler.extensions.extensionproducers.ExtensionProducerObserver; -import gearth.services.extensionhandler.extensions.network.authentication.Authenticator; +import gearth.services.extensionhandler.extensions.implementations.network.authentication.Authenticator; import java.io.DataInputStream; import java.io.IOException; @@ -63,7 +63,7 @@ public class NetworkExtensionsProducer implements ExtensionProducer { ); if (Authenticator.evaluate(gEarthExtension)) { - observer.onExtensionConnect(gEarthExtension); + observer.onExtensionProduced(gEarthExtension); } else { gEarthExtension.close(); //you shall not pass... diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/authentication/Authenticator.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/authentication/Authenticator.java similarity index 94% rename from G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/authentication/Authenticator.java rename to G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/authentication/Authenticator.java index 7c2c299..82c5adf 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/authentication/Authenticator.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/authentication/Authenticator.java @@ -1,7 +1,7 @@ -package gearth.services.extensionhandler.extensions.network.authentication; +package gearth.services.extensionhandler.extensions.implementations.network.authentication; import gearth.misc.ConfirmationDialog; -import gearth.services.extensionhandler.extensions.network.NetworkExtension; +import gearth.services.extensionhandler.extensions.implementations.network.NetworkExtension; import javafx.application.Platform; import javafx.scene.control.Alert; import javafx.scene.control.ButtonType; diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/executer/ExecutionInfo.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/executer/ExecutionInfo.java similarity index 95% rename from G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/executer/ExecutionInfo.java rename to G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/executer/ExecutionInfo.java index d77df81..773a425 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/executer/ExecutionInfo.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/executer/ExecutionInfo.java @@ -1,4 +1,4 @@ -package gearth.services.extensionhandler.extensions.network.executer; +package gearth.services.extensionhandler.extensions.implementations.network.executer; import java.util.ArrayList; import java.util.HashMap; diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/executer/ExtensionRunner.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/executer/ExtensionRunner.java similarity index 78% rename from G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/executer/ExtensionRunner.java rename to G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/executer/ExtensionRunner.java index 95f5a7d..ba41dc0 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/executer/ExtensionRunner.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/executer/ExtensionRunner.java @@ -1,4 +1,4 @@ -package gearth.services.extensionhandler.extensions.network.executer; +package gearth.services.extensionhandler.extensions.implementations.network.executer; /** * Created by Jonas on 21/09/18. diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/executer/ExtensionRunnerFactory.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/executer/ExtensionRunnerFactory.java similarity index 78% rename from G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/executer/ExtensionRunnerFactory.java rename to G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/executer/ExtensionRunnerFactory.java index 429e4be..58e04d4 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/executer/ExtensionRunnerFactory.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/executer/ExtensionRunnerFactory.java @@ -1,4 +1,4 @@ -package gearth.services.extensionhandler.extensions.network.executer; +package gearth.services.extensionhandler.extensions.implementations.network.executer; /** * Created by Jonas on 22/09/18. diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/executer/NormalExtensionRunner.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/executer/NormalExtensionRunner.java similarity index 97% rename from G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/executer/NormalExtensionRunner.java rename to G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/executer/NormalExtensionRunner.java index 4dc7740..5ee7f68 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/network/executer/NormalExtensionRunner.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/executer/NormalExtensionRunner.java @@ -1,7 +1,7 @@ -package gearth.services.extensionhandler.extensions.network.executer; +package gearth.services.extensionhandler.extensions.implementations.network.executer; import gearth.Main; -import gearth.services.extensionhandler.extensions.network.authentication.Authenticator; +import gearth.services.extensionhandler.extensions.implementations.network.authentication.Authenticator; import java.io.BufferedReader; import java.io.File; diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/simple/ExampleExtension.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/simple/ExampleExtension.java similarity index 97% rename from G-Earth/src/main/java/gearth/services/extensionhandler/extensions/simple/ExampleExtension.java rename to G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/simple/ExampleExtension.java index 8b01404..98752f2 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/simple/ExampleExtension.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/simple/ExampleExtension.java @@ -1,4 +1,4 @@ -package gearth.services.extensionhandler.extensions.simple; +package gearth.services.extensionhandler.extensions.implementations.simple; import gearth.protocol.HMessage; import gearth.services.extensionhandler.extensions.GEarthExtension; diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/simple/README.md b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/simple/README.md similarity index 100% rename from G-Earth/src/main/java/gearth/services/extensionhandler/extensions/simple/README.md rename to G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/simple/README.md diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/simple/SimpleExtensionProducer.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/simple/SimpleExtensionProducer.java similarity index 87% rename from G-Earth/src/main/java/gearth/services/extensionhandler/extensions/simple/SimpleExtensionProducer.java rename to G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/simple/SimpleExtensionProducer.java index 3317379..105b907 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/simple/SimpleExtensionProducer.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/simple/SimpleExtensionProducer.java @@ -1,4 +1,4 @@ -package gearth.services.extensionhandler.extensions.simple; +package gearth.services.extensionhandler.extensions.implementations.simple; import gearth.services.extensionhandler.extensions.extensionproducers.ExtensionProducer; import gearth.services.extensionhandler.extensions.extensionproducers.ExtensionProducerObserver; diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/listeners/OmRemoveClickListener.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/listeners/OmRemoveClickListener.java new file mode 100644 index 0000000..93057e1 --- /dev/null +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/listeners/OmRemoveClickListener.java @@ -0,0 +1,5 @@ +package gearth.services.extensionhandler.extensions.listeners; + +public interface OmRemoveClickListener { + void onRemove(); +} diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/listeners/OnClickListener.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/listeners/OnClickListener.java new file mode 100644 index 0000000..4717872 --- /dev/null +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/listeners/OnClickListener.java @@ -0,0 +1,5 @@ +package gearth.services.extensionhandler.extensions.listeners; + +public interface OnClickListener { + void onClick(); +} diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/listeners/OnDeleteListener.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/listeners/OnDeleteListener.java new file mode 100644 index 0000000..fd8fbcf --- /dev/null +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/listeners/OnDeleteListener.java @@ -0,0 +1,5 @@ +package gearth.services.extensionhandler.extensions.listeners; + +public interface OnDeleteListener { + void onDelete(); +} diff --git a/G-Earth/src/main/java/gearth/ui/connection/Connection.java b/G-Earth/src/main/java/gearth/ui/connection/Connection.java index d324914..b881500 100644 --- a/G-Earth/src/main/java/gearth/ui/connection/Connection.java +++ b/G-Earth/src/main/java/gearth/ui/connection/Connection.java @@ -73,7 +73,7 @@ public class Connection extends SubForm { } public void onParentSet(){ - getHConnection().addStateChangeListener((oldState, newState) -> Platform.runLater(() -> { + getHConnection().getStateObservable().addListener((oldState, newState) -> Platform.runLater(() -> { txtfield_hotelversion.setText(getHConnection().getHotelVersion()); Platform.runLater(() -> { if (newState == HConnection.State.NOT_CONNECTED) { diff --git a/G-Earth/src/main/java/gearth/ui/extensions/ExtensionItemContainer.java b/G-Earth/src/main/java/gearth/ui/extensions/ExtensionItemContainer.java index ca4fef7..43b1158 100644 --- a/G-Earth/src/main/java/gearth/ui/extensions/ExtensionItemContainer.java +++ b/G-Earth/src/main/java/gearth/ui/extensions/ExtensionItemContainer.java @@ -12,10 +12,10 @@ import javafx.scene.paint.Paint; import javafx.scene.text.Font; import gearth.misc.ConfirmationDialog; import gearth.ui.buttons.*; -import gearth.services.extensionhandler.extensions.network.executer.ExecutionInfo; -import gearth.services.extensionhandler.extensions.network.executer.ExtensionRunner; -import gearth.services.extensionhandler.extensions.network.executer.ExtensionRunnerFactory; -import gearth.services.extensionhandler.extensions.network.executer.NormalExtensionRunner; +import gearth.services.extensionhandler.extensions.implementations.network.executer.ExecutionInfo; +import gearth.services.extensionhandler.extensions.implementations.network.executer.ExtensionRunner; +import gearth.services.extensionhandler.extensions.implementations.network.executer.ExtensionRunnerFactory; +import gearth.services.extensionhandler.extensions.implementations.network.executer.NormalExtensionRunner; import java.nio.file.Paths; @@ -161,14 +161,14 @@ public class ExtensionItemContainer extends GridPane { exitButton.removeEventHandler(MouseEvent.MOUSE_CLICKED, onExit); clickButton.removeEventHandler(MouseEvent.MOUSE_CLICKED, onClick); } - onExit = event -> item.isRemoveClickTrigger(); - onClick = event -> item.isClickTrigger(); + onExit = event -> item.getRemoveClickObservable().fireEvent(); + onClick = event -> item.getClickedObservable().fireEvent(); exitButton.addEventHandler(MouseEvent.MOUSE_CLICKED, onExit); clickButton.addEventHandler(MouseEvent.MOUSE_CLICKED, onClick); ExtensionItemContainer this2 = this; - item.onDelete(observable -> Platform.runLater(() -> { + item.getDeletedObservable().addListener(() -> Platform.runLater(() -> { if (item.isInstalledExtension()) { setBackground(new Background(new BackgroundFill(Paint.valueOf("#cccccc"),null, null))); getChildren().remove(buttonsBox); diff --git a/G-Earth/src/main/java/gearth/ui/extensions/Extensions.java b/G-Earth/src/main/java/gearth/ui/extensions/Extensions.java index 2aabfb0..b837b5e 100644 --- a/G-Earth/src/main/java/gearth/ui/extensions/Extensions.java +++ b/G-Earth/src/main/java/gearth/ui/extensions/Extensions.java @@ -1,10 +1,10 @@ package gearth.ui.extensions; import gearth.services.extensionhandler.ExtensionHandler; -import gearth.services.extensionhandler.extensions.network.NetworkExtensionsProducer; -import gearth.services.extensionhandler.extensions.network.executer.ExecutionInfo; -import gearth.services.extensionhandler.extensions.network.executer.ExtensionRunner; -import gearth.services.extensionhandler.extensions.network.executer.ExtensionRunnerFactory; +import gearth.services.extensionhandler.extensions.implementations.network.NetworkExtensionsProducer; +import gearth.services.extensionhandler.extensions.implementations.network.executer.ExecutionInfo; +import gearth.services.extensionhandler.extensions.implementations.network.executer.ExtensionRunner; +import gearth.services.extensionhandler.extensions.implementations.network.executer.ExtensionRunnerFactory; import gearth.ui.SubForm; import javafx.application.Platform; import javafx.event.ActionEvent; @@ -43,7 +43,7 @@ public class Extensions extends SubForm { protected void onParentSet() { ExtensionItemContainerProducer producer = new ExtensionItemContainerProducer(extensioncontainer, scroller); extensionHandler = new ExtensionHandler(getHConnection()); - extensionHandler.onExtensionConnected((e -> { + extensionHandler.getObservable().addListener((e -> { Platform.runLater(() -> producer.extensionConnected(e)); })); diff --git a/G-Earth/src/main/java/gearth/ui/extra/Extra.java b/G-Earth/src/main/java/gearth/ui/extra/Extra.java index b2c8e33..17ae72e 100644 --- a/G-Earth/src/main/java/gearth/ui/extra/Extra.java +++ b/G-Earth/src/main/java/gearth/ui/extra/Extra.java @@ -59,7 +59,7 @@ public class Extra extends SubForm implements ConnectionInfoOverrider { cbx_alwaysOnTop.selectedProperty().addListener(observable -> parentController.getStage().setAlwaysOnTop(cbx_alwaysOnTop.isSelected())); cbx_advanced.selectedProperty().addListener(observable -> updateAdvancedUI()); - getHConnection().addStateChangeListener((oldState, newState) -> { + getHConnection().getStateObservable().addListener((oldState, newState) -> { if (oldState == HConnection.State.NOT_CONNECTED || newState == HConnection.State.NOT_CONNECTED) { updateAdvancedUI(); } diff --git a/G-Earth/src/main/java/gearth/ui/injection/Injection.java b/G-Earth/src/main/java/gearth/ui/injection/Injection.java index 698f4df..67a2020 100644 --- a/G-Earth/src/main/java/gearth/ui/injection/Injection.java +++ b/G-Earth/src/main/java/gearth/ui/injection/Injection.java @@ -21,7 +21,7 @@ public class Injection extends SubForm { public Button btn_sendToClient; protected void onParentSet() { - getHConnection().addStateChangeListener((oldState, newState) -> Platform.runLater(this::updateUI)); + getHConnection().getStateObservable().addListener((oldState, newState) -> Platform.runLater(this::updateUI)); inputPacket.textProperty().addListener(event -> Platform.runLater(this::updateUI)); } diff --git a/G-Earth/src/main/java/gearth/ui/logger/Logger.java b/G-Earth/src/main/java/gearth/ui/logger/Logger.java index 7a9f745..f52280f 100644 --- a/G-Earth/src/main/java/gearth/ui/logger/Logger.java +++ b/G-Earth/src/main/java/gearth/ui/logger/Logger.java @@ -35,7 +35,7 @@ public class Logger extends SubForm { private PacketLogger packetLogger = PacketLoggerFactory.get(); public void onParentSet(){ - getHConnection().addStateChangeListener((oldState, newState) -> Platform.runLater(() -> { + getHConnection().getStateObservable().addListener((oldState, newState) -> Platform.runLater(() -> { if (newState == HConnection.State.PREPARING) { miniLogText(Color.ORANGE, "Connecting to "+getHConnection().getDomain() + ":" + getHConnection().getServerPort()); } diff --git a/G-Earth/src/main/java/gearth/ui/scheduler/ScheduleItem.java b/G-Earth/src/main/java/gearth/ui/scheduler/ScheduleItem.java index 8abb535..0e23272 100644 --- a/G-Earth/src/main/java/gearth/ui/scheduler/ScheduleItem.java +++ b/G-Earth/src/main/java/gearth/ui/scheduler/ScheduleItem.java @@ -1,5 +1,10 @@ package gearth.ui.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 javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleIntegerProperty; @@ -16,11 +21,11 @@ import java.util.List; */ public class ScheduleItem implements StringifyAble { - private SimpleIntegerProperty indexProperty = null; - private SimpleBooleanProperty pausedProperty = null; - private SimpleObjectProperty delayProperty = null; - private SimpleObjectProperty packetProperty = null; - private SimpleObjectProperty destinationProperty = null; + private SimpleIntegerProperty indexProperty; + private SimpleBooleanProperty pausedProperty; + private SimpleObjectProperty delayProperty; + private SimpleObjectProperty packetProperty; + private SimpleObjectProperty destinationProperty; ScheduleItem (int index, boolean paused, Interval delay, HPacket packet, HMessage.Direction destination) { construct(index, paused, delay, packet, destination); @@ -59,44 +64,36 @@ public class ScheduleItem implements StringifyAble { } - private List onDeleteListeners = new ArrayList<>(); - public void onDelete(InvalidationListener listener) { - onDeleteListeners.add(listener); + private Observable onDeleteObservable = new Observable<>(OnDeleteListener::onDelete); + public void onDelete(OnDeleteListener listener) { + onDeleteObservable.addListener(listener); } public void delete() { - for (int i = onDeleteListeners.size() - 1; i >= 0; i--) { - onDeleteListeners.get(i).invalidated(null); - } + onDeleteObservable.fireEvent(); } - private List onEditListeners = new ArrayList<>(); - public void onEdit(InvalidationListener listener) { - onEditListeners.add(listener); + private Observable onEditObservable = new Observable<>(OnEditListener::onEdit); + public void onEdit(OnEditListener listener) { + onEditObservable.addListener(listener); } public void edit() { - for (int i = onEditListeners.size() - 1; i >= 0; i--) { - onEditListeners.get(i).invalidated(null); - } + onEditObservable.fireEvent(); } - private List onIsupdatedListeners = new ArrayList<>(); - public void onIsupdated(InvalidationListener listener) { - onIsupdatedListeners.add(listener); + private Observable onUpdatedObservable = new Observable<>(OnUpdatedListener::onUpdated); + public void onIsupdated(OnUpdatedListener listener) { + onUpdatedObservable.addListener(listener); } public void isUpdatedTrigger() { - for (int i = onIsupdatedListeners.size() - 1; i >= 0; i--) { - onIsupdatedListeners.get(i).invalidated(null); - } + onUpdatedObservable.fireEvent(); } - private List OnIsBeingUpdatedListeners = new ArrayList<>(); - public void onIsBeingUpdated(InvalidationListener listener) { - OnIsBeingUpdatedListeners.add(listener); + private Observable onBeingUpdatedObservable = new Observable<>(OnBeingUpdatedListener::onBeingUpdated); + public void onIsBeingUpdated(OnBeingUpdatedListener listener) { + onBeingUpdatedObservable.addListener(listener); } public void onIsBeingUpdatedTrigger() { - for (int i = OnIsBeingUpdatedListeners.size() - 1; i >= 0; i--) { - OnIsBeingUpdatedListeners.get(i).invalidated(null); - } + onBeingUpdatedObservable.fireEvent(); } @Override 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 d9f2ceb..b1040c0 100644 --- a/G-Earth/src/main/java/gearth/ui/scheduler/ScheduleItemContainer.java +++ b/G-Earth/src/main/java/gearth/ui/scheduler/ScheduleItemContainer.java @@ -87,9 +87,9 @@ public class ScheduleItemContainer extends GridPane { GridPane this2 = this; - item.onDelete(observable -> parent.getChildren().remove(this2)); - item.onIsBeingUpdated(observable -> setStyle("-fx-background-color: #faebcc;")); - item.onIsupdated(observable -> setStyle("-fx-background-color: #ffffff;")); + item.onDelete(() -> parent.getChildren().remove(this2)); + item.onIsBeingUpdated(() -> setStyle("-fx-background-color: #faebcc;")); + item.onIsupdated(() -> setStyle("-fx-background-color: #ffffff;")); } private Label initNewLabelColumn(String text) { diff --git a/G-Earth/src/main/java/gearth/ui/scheduler/Scheduler.java b/G-Earth/src/main/java/gearth/ui/scheduler/Scheduler.java index 4fe15c3..0a1fb78 100644 --- a/G-Earth/src/main/java/gearth/ui/scheduler/Scheduler.java +++ b/G-Earth/src/main/java/gearth/ui/scheduler/Scheduler.java @@ -173,7 +173,7 @@ public class Scheduler extends SubForm { scheduleItemList.add(newItem); - newItem.onDelete(observable -> { + newItem.onDelete(() -> { if (isBeingEdited == newItem) { setInputDefault(); isBeingEdited = null; @@ -183,7 +183,7 @@ public class Scheduler extends SubForm { scheduleItemList.get(i).getIndexProperty().set(i); } }); - newItem.onEdit(observable -> { + newItem.onEdit(() -> { if (isBeingEdited != null) { isBeingEdited.isUpdatedTrigger(); } @@ -195,7 +195,7 @@ public class Scheduler extends SubForm { rb_outgoing.setSelected(newItem.getDestinationProperty().get() == HMessage.Direction.TOSERVER); isBeingEdited = newItem; - btn_addoredit.setText("Edit schedule item"); //Add to scheduler + btn_addoredit.setText("Edit"); updateUI(); newItem.onIsBeingUpdatedTrigger(); } @@ -213,7 +213,7 @@ public class Scheduler extends SubForm { rb_incoming.setSelected(true); rb_outgoing.setSelected(false); - btn_addoredit.setText("Add to scheduler"); + btn_addoredit.setText("Add"); updateUI(); } diff --git a/G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnBeingUpdatedListener.java b/G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnBeingUpdatedListener.java new file mode 100644 index 0000000..c417dbe --- /dev/null +++ b/G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnBeingUpdatedListener.java @@ -0,0 +1,5 @@ +package gearth.ui.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/ui/scheduler/listeners/OnDeleteListener.java new file mode 100644 index 0000000..a87747c --- /dev/null +++ b/G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnDeleteListener.java @@ -0,0 +1,5 @@ +package gearth.ui.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/ui/scheduler/listeners/OnEditListener.java new file mode 100644 index 0000000..7c95e2a --- /dev/null +++ b/G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnEditListener.java @@ -0,0 +1,5 @@ +package gearth.ui.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/ui/scheduler/listeners/OnUpdatedListener.java new file mode 100644 index 0000000..f3c4e07 --- /dev/null +++ b/G-Earth/src/main/java/gearth/ui/scheduler/listeners/OnUpdatedListener.java @@ -0,0 +1,5 @@ +package gearth.ui.scheduler.listeners; + +public interface OnUpdatedListener { + void onUpdated(); +}