diff --git a/java/src/pm/Device.java b/java/src/pm/Device.java index 2205c40..dda4777 100644 --- a/java/src/pm/Device.java +++ b/java/src/pm/Device.java @@ -71,7 +71,7 @@ public abstract class Device extends EventListener { Button button = hold.getButton(); add(new Press(button), new Release(button), continuous); } - + /* Register interruptibles * protected void add(Interruptible interruptible) { interruptListener.add(interruptible); diff --git a/java/src/pm/Main.java b/java/src/pm/Main.java index 596b5a3..9fe6503 100644 --- a/java/src/pm/Main.java +++ b/java/src/pm/Main.java @@ -5,7 +5,6 @@ import java.util.ArrayList; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import pm.application.ApplicationCycle; import pm.application.cmd.windows.gomplayer.GomPlayerApplication; import pm.application.cmd.windows.wmp.WMPApplication; import pm.application.example.ExampleApplication; @@ -29,13 +28,14 @@ import pm.exception.application.ApplicationInitialiseException; import pm.exception.device.DeviceExitException; import pm.exception.device.DeviceInitialiseException; import pm.macro.Active; +import pm.util.ArrayCycle; import pm.value.Action; public class Main extends EventListener { protected Log log = LogFactory.getLog(Main.class); //protected String[] deviceClassArray; - protected ApplicationCycle applicationCycle; + protected ArrayCycle applicationCycle; protected ArrayList deviceList; public Main() { @@ -45,7 +45,7 @@ public class Main extends EventListener { "pm.device.javainput.rumblepad.RumblepadDevice", "pm.device.javainput.extreme3d.Extreme3DDevice", "pm.device.wiimote.WiimoteDevice"};*/ - applicationCycle = new ApplicationCycle(); + applicationCycle = new ArrayCycle(); deviceList = new ArrayList(); EventManager.initialise(applicationCycle); EventManager.add(this); diff --git a/java/src/pm/application/ApplicationCycle.java b/java/src/pm/application/ApplicationCycle.java deleted file mode 100644 index dd3bd3d..0000000 --- a/java/src/pm/application/ApplicationCycle.java +++ /dev/null @@ -1,8 +0,0 @@ -package pm.application; - -import pm.Application; -import pm.util.ArrayCycle; - -public class ApplicationCycle extends ArrayCycle { - protected static final long serialVersionUID = 1L; -} diff --git a/java/src/pm/device/text/InputListener.java b/java/src/pm/device/text/InputListener.java deleted file mode 100644 index ba035ce..0000000 --- a/java/src/pm/device/text/InputListener.java +++ /dev/null @@ -1,41 +0,0 @@ -package pm.device.text; - -import java.io.InputStream; -import java.util.Scanner; - -import pm.Listener; -import pm.event.Task; -import pm.event.EventManager; -import pm.exception.task.TaskNotSupportedException; -import pm.value.Action; -import pm.value.Target; - -public class InputListener extends Listener { - protected Scanner input; - - public InputListener(InputStream inputStream) { - input = new Scanner(inputStream); - } - - public void run() { - run = true; - while (running()) { - String string = input.next().toUpperCase(); - if(string != null) { - try { - EventManager.add( - new Task(Action.valueOf(string), Target.APPLICATION)); - } catch(IllegalArgumentException e) {} - } - try { - Thread.sleep(SLEEP); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - - protected boolean running() { - return run && input.hasNext(); - } -} diff --git a/java/src/pm/device/text/TextDevice.java b/java/src/pm/device/text/TextDevice.java index 60d6dee..f972d22 100644 --- a/java/src/pm/device/text/TextDevice.java +++ b/java/src/pm/device/text/TextDevice.java @@ -1,6 +1,14 @@ package pm.device.text; +import java.io.InputStream; +import java.util.Scanner; + import pm.Device; +import pm.Listener; +import pm.event.EventManager; +import pm.event.Task; +import pm.value.Action; +import pm.value.Target; public class TextDevice extends Device { InputListener inputListener; @@ -16,4 +24,33 @@ public class TextDevice extends Device { public void exit() { inputListener.stop(); } -} + + public void add(String string) { + EventManager.add(new Task(Action.valueOf(string), Target.APPLICATION)); + } + + public class InputListener extends Listener { + protected Scanner input; + + public InputListener(InputStream inputStream) { + input = new Scanner(inputStream); + } + + public void run() { + run = true; + while (run && input.hasNext()) { + String string = input.next().toUpperCase(); + if(string != null) { + try { + add(string); + } catch(IllegalArgumentException e) {} + } + try { + Thread.sleep(SLEEP); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } +} \ No newline at end of file diff --git a/java/src/pm/device/text/lan/SocketListener.java b/java/src/pm/device/text/lan/SocketListener.java index 1ea9faf..0896ad7 100644 --- a/java/src/pm/device/text/lan/SocketListener.java +++ b/java/src/pm/device/text/lan/SocketListener.java @@ -7,15 +7,15 @@ import java.net.Socket; import java.util.ArrayList; import pm.Listener; -import pm.device.text.InputListener; +import pm.device.text.TextDevice; public class SocketListener extends Listener { protected ServerSocket server; - protected ArrayList inputListenerList; + protected ArrayList inputListenerList; public SocketListener(ServerSocket server) { this.server = server; - inputListenerList = new ArrayList(); + inputListenerList = new ArrayList(); } public void run() { @@ -23,7 +23,7 @@ public class SocketListener extends Listener { try { Socket socket = server.accept(); InputStream inputStream = socket.getInputStream(); - InputListener inputListener = new InputListener(inputStream); + TextDevice.InputListener inputListener = new TextDevice().new InputListener(inputStream); inputListenerList.add(inputListener); inputListener.start(); } catch (IOException e) { @@ -34,7 +34,7 @@ public class SocketListener extends Listener { public void stop() { run = false; - for (InputListener inputListener : inputListenerList) { + for (TextDevice.InputListener inputListener : inputListenerList) { inputListener.stop(); } } diff --git a/java/src/pm/event/EventManager.java b/java/src/pm/event/EventManager.java index d731c38..abea509 100644 --- a/java/src/pm/event/EventManager.java +++ b/java/src/pm/event/EventManager.java @@ -5,15 +5,15 @@ import java.util.ArrayList; import pm.Application; import pm.Device; import pm.Main; -import pm.application.ApplicationCycle; import pm.event.task.Stopper; +import pm.util.ArrayCycle; import pm.value.Target; public class EventManager { protected static ArrayList eventListenerList; - protected static ApplicationCycle applicationCycle; + protected static ArrayCycle applicationCycle; - public static void initialise(ApplicationCycle applicationCycle) { + public static void initialise(ArrayCycle applicationCycle) { eventListenerList = new ArrayList(); EventManager.applicationCycle = applicationCycle; } @@ -30,8 +30,7 @@ public class EventManager { public static void add(EventListener self, Task task) { if (task instanceof Stopper) { - Stopper stopper = (Stopper) task; - stopper.stop(); + ((Stopper) task).stop(); } else { Target target = task.getTarget(); switch (target) { @@ -43,7 +42,7 @@ public class EventManager { applicationCycle.current().add(task); } break; - default: + default: { for (EventListener eventListener : eventListenerList) { switch (target) { case ALL: @@ -66,11 +65,16 @@ public class EventManager { break; } } + } } } } - + public static void add(Task task) { + add(null, task); + } + + private static void add(Task task) { if (!task.getTarget().equals(Target.SELF)) { add(null, task); }