Beginnetje gemaakt met loop om events van devices te ontvangen en hun action door te sturen naar de huidige applicatie. Onderscheid gemaakt tussen events voor applicaties (start, pauze, enz) en events voor de main (exit, volgend device, enz). Misschien van event een betere uitgebreidere nuttigere wrapper maken voor een action.
This commit is contained in:
@@ -9,12 +9,17 @@ import pm.application.Application;
|
||||
import pm.device.Device;
|
||||
import pm.device.example.Example;
|
||||
import pm.event.Event;
|
||||
import pm.event.ApplicationEvent;
|
||||
|
||||
public class Main {
|
||||
public static final int SLEEP = 100;
|
||||
|
||||
ArrayList<Application> applicationList;
|
||||
ArrayList<Device> deviceList;
|
||||
Queue<Event> eventQueue;
|
||||
|
||||
Application currentApplication;
|
||||
|
||||
public Main() {
|
||||
applicationList = new ArrayList<Application>();
|
||||
deviceList = new ArrayList<Device>();
|
||||
@@ -39,7 +44,28 @@ public class Main {
|
||||
|
||||
public void start() {
|
||||
addDevice(new Example(eventQueue));
|
||||
addApplication(new Voorbeeld());
|
||||
Application application = new Voorbeeld();
|
||||
addApplication(application);
|
||||
currentApplication = application;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
while (true) {
|
||||
if (eventQueue.isEmpty()) {
|
||||
try {
|
||||
Thread.sleep(SLEEP);
|
||||
} catch (InterruptedException e) {}
|
||||
} else {
|
||||
Event event = eventQueue.poll();
|
||||
if (event instanceof ApplicationEvent) {
|
||||
try {
|
||||
currentApplication.invoke(event.getAction());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.util.Queue;
|
||||
|
||||
import pm.action.Action;
|
||||
import pm.device.Device;
|
||||
import pm.event.ApplicationEvent;
|
||||
import pm.event.Event;
|
||||
|
||||
public class Example extends Device {
|
||||
@@ -12,7 +13,7 @@ public class Example extends Device {
|
||||
}
|
||||
|
||||
public void initialise() {
|
||||
Event event = new Event(Action.START);
|
||||
Event event = new ApplicationEvent(Action.START);
|
||||
eventQueue.add(event);
|
||||
}
|
||||
}
|
||||
|
||||
9
java/src/pm/event/ApplicationEvent.java
Normal file
9
java/src/pm/event/ApplicationEvent.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package pm.event;
|
||||
|
||||
import pm.action.Action;
|
||||
|
||||
public class ApplicationEvent extends Event {
|
||||
public ApplicationEvent(Action action) {
|
||||
super(action);
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,14 @@ package pm.event;
|
||||
|
||||
import pm.action.Action;
|
||||
|
||||
public class Event {
|
||||
public abstract class Event {
|
||||
protected Action action;
|
||||
|
||||
|
||||
public Event(Action action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public Action getAction() {
|
||||
return action;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user