Actions en targets netter gemaakt.
This commit is contained in:
53
java/src/pm/Action.java
Normal file
53
java/src/pm/Action.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package pm;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import pm.exception.ActionException;
|
||||
import pm.exception.action.InvokeActionException;
|
||||
import pm.exception.action.NotImplementedActionException;
|
||||
import pm.exception.action.TargetNotSetException;
|
||||
|
||||
public enum Action {
|
||||
START ("start"),
|
||||
TEST ("test"),
|
||||
EXIT ("exit");
|
||||
|
||||
protected String action;
|
||||
protected Target target;
|
||||
|
||||
Action(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public Action setTarget(Target target) {
|
||||
this.target = target;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Target getTarget() throws TargetNotSetException {
|
||||
if (target == null) {
|
||||
throw new TargetNotSetException();
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
public Method getMethod(Object object) throws NotImplementedActionException {
|
||||
try {
|
||||
return object.getClass().getMethod(action);
|
||||
} catch (SecurityException e) {
|
||||
} catch (NoSuchMethodException e) {}
|
||||
throw new NotImplementedActionException();
|
||||
}
|
||||
|
||||
public void invoke(Object object) throws ActionException {
|
||||
try {
|
||||
getMethod(object).invoke(object);
|
||||
return;
|
||||
} catch (IllegalArgumentException e) {
|
||||
} catch (IllegalAccessException e) {
|
||||
} catch (InvocationTargetException e) {}
|
||||
throw new InvokeActionException();
|
||||
// Todo: informatie doorgeven over wat er precies is foutgegaan
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import java.util.ArrayList;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
import pm.action.Actions;
|
||||
import pm.application.Application;
|
||||
import pm.application.voorbeeld.VoorbeeldApplication;
|
||||
import pm.device.Device;
|
||||
@@ -12,14 +11,14 @@ import pm.device.javainput.extreme3d.Extreme3DDevice;
|
||||
import pm.exception.ActionException;
|
||||
import pm.exception.action.NotImplementedActionException;
|
||||
import pm.exception.action.UnknownTargetException;
|
||||
import pm.macro.MacroListener;
|
||||
import pm.listener.ActionListener;
|
||||
|
||||
public class Main extends Target {
|
||||
public class Main {
|
||||
protected static final int SLEEP = 100;
|
||||
|
||||
ArrayList<Application> applicationList;
|
||||
ArrayList<Device> deviceList;
|
||||
Queue<Actions> actionQueue;
|
||||
Queue<Action> actionQueue;
|
||||
|
||||
boolean run;
|
||||
Application currentApplication;
|
||||
@@ -28,10 +27,9 @@ public class Main extends Target {
|
||||
applicationList = new ArrayList<Application>();
|
||||
//applicationList.iterator();
|
||||
deviceList = new ArrayList<Device>();
|
||||
actionQueue = new ConcurrentLinkedQueue<Actions>();
|
||||
//JavaInputService.initialize();
|
||||
Device.initialise(actionQueue);
|
||||
MacroListener.initialise(actionQueue);
|
||||
actionQueue = new ConcurrentLinkedQueue<Action>();
|
||||
|
||||
ActionListener.initialise(actionQueue);
|
||||
}
|
||||
|
||||
public void addApplication(Application application) {
|
||||
@@ -62,7 +60,7 @@ public class Main extends Target {
|
||||
for (Device device : deviceList) {
|
||||
device.start();
|
||||
}
|
||||
|
||||
|
||||
run();
|
||||
}
|
||||
|
||||
@@ -75,20 +73,20 @@ public class Main extends Target {
|
||||
Thread.sleep(SLEEP);
|
||||
} catch (InterruptedException e) {}
|
||||
} else {
|
||||
Actions action = actionQueue.poll();
|
||||
Target target;
|
||||
Action action = actionQueue.poll();
|
||||
Object object;
|
||||
switch (action.getTarget()) {
|
||||
case MAIN:
|
||||
target = this;
|
||||
object = this;
|
||||
break;
|
||||
case APPLICATION:
|
||||
target = currentApplication;
|
||||
object = currentApplication;
|
||||
break;
|
||||
default:
|
||||
throw new UnknownTargetException();
|
||||
}
|
||||
try {
|
||||
target.invoke(action);
|
||||
action.invoke(object);
|
||||
} catch (NotImplementedActionException e) {
|
||||
// Todo: log.write(...)
|
||||
}
|
||||
|
||||
@@ -1,20 +1,5 @@
|
||||
package pm;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import pm.action.Actions;
|
||||
import pm.exception.ActionException;
|
||||
import pm.exception.action.InvokeActionException;
|
||||
|
||||
public abstract class Target {
|
||||
public void invoke(Actions action) throws ActionException {
|
||||
try {
|
||||
action.getMethod(this).invoke(this);
|
||||
return;
|
||||
} catch (IllegalArgumentException e) {
|
||||
} catch (IllegalAccessException e) {
|
||||
} catch (InvocationTargetException e) {}
|
||||
throw new InvokeActionException();
|
||||
// Todo: informatie doorgeven over wat er precies is foutgegaan
|
||||
}
|
||||
}
|
||||
public enum Target {
|
||||
MAIN, DEVICE, APPLICATION;
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package pm.action;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import pm.exception.action.NotImplementedActionException;
|
||||
|
||||
public enum Actions {
|
||||
START ("start"),
|
||||
TEST ("test"),
|
||||
EXIT ("exit");
|
||||
|
||||
protected String action;
|
||||
protected Targets target;
|
||||
|
||||
Actions(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public void setTarget(Targets target) {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public Targets getTarget() {
|
||||
// Todo: controle op target echt geset?
|
||||
return target;
|
||||
}
|
||||
|
||||
public Method getMethod(Object object) throws NotImplementedActionException {
|
||||
try {
|
||||
return object.getClass().getMethod(action);
|
||||
} catch (SecurityException e) {
|
||||
} catch (NoSuchMethodException e) {}
|
||||
throw new NotImplementedActionException();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package pm.action;
|
||||
|
||||
public enum Targets {
|
||||
MAIN, DEVICE, APPLICATION;
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
package pm.application;
|
||||
|
||||
import pm.Target;
|
||||
|
||||
public abstract class Application extends Target {}
|
||||
public abstract class Application {}
|
||||
@@ -1,27 +1,19 @@
|
||||
package pm.device;
|
||||
|
||||
import java.util.Queue;
|
||||
import pm.Action;
|
||||
import pm.Macro;
|
||||
import pm.listener.ActionListener;
|
||||
import pm.listener.MacroListener;
|
||||
|
||||
import pm.action.Actions;
|
||||
import pm.action.Targets;
|
||||
import pm.macro.MacroListener;
|
||||
|
||||
public abstract class Device {
|
||||
protected static Queue<Actions> actionQueue;
|
||||
public abstract class Device extends ActionListener {
|
||||
protected MacroListener macroListener;
|
||||
|
||||
public Device() {
|
||||
macroListener = new MacroListener();
|
||||
macroListener.start();
|
||||
}
|
||||
|
||||
public void addAction(Actions action, Targets target) {
|
||||
action.setTarget(target);
|
||||
actionQueue.add(action);
|
||||
}
|
||||
|
||||
public static void initialise(Queue<Actions> actionQueue) {
|
||||
Device.actionQueue = actionQueue;
|
||||
public void add(Macro macro, Action action) {
|
||||
macroListener.add(macro, action);
|
||||
}
|
||||
|
||||
public void start() {}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package pm.device.example;
|
||||
|
||||
import pm.action.Actions;
|
||||
import pm.action.Targets;
|
||||
import pm.Action;
|
||||
import pm.Target;
|
||||
import pm.device.Device;
|
||||
|
||||
public class ExampleDevice extends Device {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package pm.device.javainput.extreme3d;
|
||||
|
||||
import pm.Action;
|
||||
import pm.Macro;
|
||||
import pm.action.Actions;
|
||||
import pm.action.Targets;
|
||||
import pm.Target;
|
||||
import pm.device.javainput.JavaInputDevice;
|
||||
import pm.exception.DeviceException;
|
||||
import pm.exception.MacroException;
|
||||
@@ -11,7 +11,6 @@ import pm.macro.event.Press;
|
||||
import pm.macro.event.Release;
|
||||
|
||||
public class Extreme3DDevice extends JavaInputDevice {
|
||||
|
||||
protected static final String NAME = "Logitech Extreme 3D";
|
||||
|
||||
public Extreme3DDevice() throws DeviceException {
|
||||
@@ -21,14 +20,13 @@ public class Extreme3DDevice extends JavaInputDevice {
|
||||
public void start() {
|
||||
super.start();
|
||||
try {
|
||||
macroListener.add(
|
||||
add(
|
||||
new Macro(
|
||||
new Hold(Extreme3DButton.ONE),
|
||||
new Press(Extreme3DButton.TWO),
|
||||
new Press(Extreme3DButton.ELEVEN),
|
||||
new Release(Extreme3DButton.ONE)),
|
||||
Actions.EXIT,
|
||||
Targets.MAIN);
|
||||
Action.EXIT.setTarget(Target.MAIN));
|
||||
} catch (MacroException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
7
java/src/pm/exception/action/TargetNotSetException.java
Normal file
7
java/src/pm/exception/action/TargetNotSetException.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package pm.exception.action;
|
||||
|
||||
import pm.exception.ActionException;
|
||||
|
||||
public class TargetNotSetException extends ActionException {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
}
|
||||
17
java/src/pm/listener/ActionListener.java
Normal file
17
java/src/pm/listener/ActionListener.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package pm.listener;
|
||||
|
||||
import java.util.Queue;
|
||||
|
||||
import pm.Action;
|
||||
|
||||
public class ActionListener {
|
||||
protected static Queue<Action> actionQueue;
|
||||
|
||||
public static void initialise(Queue<Action> actionQueue) {
|
||||
ActionListener.actionQueue = actionQueue;
|
||||
}
|
||||
|
||||
public void add(Action action) {
|
||||
actionQueue.add(action);
|
||||
}
|
||||
}
|
||||
@@ -1,29 +1,25 @@
|
||||
package pm.macro;
|
||||
package pm.listener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Queue;
|
||||
|
||||
import pm.Action;
|
||||
import pm.Macro;
|
||||
import pm.macro.Active;
|
||||
import pm.macro.Event;
|
||||
|
||||
import pm.action.Actions;
|
||||
import pm.action.Targets;
|
||||
|
||||
public class MacroListener extends Thread {
|
||||
protected static Queue<Actions> actionQueue;
|
||||
|
||||
public class MacroListener extends ActionListener {
|
||||
public ArrayList<Macro> macroList;
|
||||
public HashMap<Macro, Actions> actionMap;
|
||||
public HashMap<Macro, Action> actionMap;
|
||||
public ArrayList<Active> activeList;
|
||||
|
||||
|
||||
public MacroListener() {
|
||||
macroList = new ArrayList<Macro>();
|
||||
actionMap = new HashMap<Macro, Actions>();
|
||||
actionMap = new HashMap<Macro, Action>();
|
||||
activeList = new ArrayList<Active>();
|
||||
}
|
||||
|
||||
public void add(Macro macro, Actions action, Targets target) {
|
||||
action.setTarget(target); // Todo: target en action duidelijker integreren / hernoemen.
|
||||
public void add(Macro macro, Action action) {
|
||||
macroList.add(macro);
|
||||
actionMap.put(macro, action);
|
||||
}
|
||||
@@ -36,7 +32,7 @@ public class MacroListener extends Thread {
|
||||
for (Active active : activeList) {
|
||||
if (active.next(event)) {
|
||||
if (active.last()) {
|
||||
actionQueue.add(actionMap.get(active.getMacro())); // Todo: dit indirect doen?
|
||||
add(actionMap.get(active.getMacro()));
|
||||
removeList.add(active);
|
||||
}
|
||||
} else {
|
||||
@@ -47,8 +43,4 @@ public class MacroListener extends Thread {
|
||||
activeList.remove(active);
|
||||
}
|
||||
}
|
||||
|
||||
public static void initialise(Queue<Actions> actionQueue) {
|
||||
MacroListener.actionQueue = actionQueue;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user