Begin gemaakt met herstructureren EventManager.

This commit is contained in:
Bram Veenboer
2011-05-06 08:10:28 +00:00
parent e368a480c2
commit f5a555adbc
16 changed files with 97 additions and 359 deletions

View File

@@ -24,7 +24,7 @@ package com.melloware.jintellitype;
import java.util.Properties;
/**
* Simple executable class that is used as the Main-Class in the JIntellitype
* Simple executable class that is used as the NetworkClient-Class in the JIntellitype
* jar. Outputs version information and other information about the environment
* on which the jar is being executed.
* <p>
@@ -44,7 +44,7 @@ public final class Main {
// private constructor to make singleton.
}
/** Main method that does what the class level javadoc states. */
/** NetworkClient method that does what the class level javadoc states. */
public static void main(String[] argv) {
System.out.println("JIntellitype version \"" + getProjectVersion() + "\"");
System.out.println(" ");

View File

@@ -588,7 +588,7 @@ public class BasicPlayer implements BasicController, Runnable
}
/**
* Main loop.
* NetworkClient loop.
*
* Player Status == STOPPED || SEEKING => End of Thread + Freeing Audio Ressources.<br>
* Player Status == PLAYING => Audio stream data sent to Audio line.<br>
@@ -602,7 +602,7 @@ public class BasicPlayer implements BasicController, Runnable
// Lock stream while playing.
synchronized (m_audioInputStream)
{
// Main play/pause loop.
// NetworkClient play/pause loop.
while ((nBytesRead != -1) && (m_status != STOPPED) && (m_status != SEEKING) && (m_status != UNKNOWN))
{
if (m_status == PLAYING)

View File

@@ -55,7 +55,7 @@ public class Main
/**
* Creates a new <code>Main</code> instance.
* Creates a new <code>NetworkClient</code> instance.
*/
private Main() {
super("CHAINSAW - Log4J Log Viewer");

View File

@@ -49,7 +49,7 @@ public class StartLogFactor5 {
//--------------------------------------------------------------------------
/**
* Main - starts a an instance of the LogFactor5 console and configures
* NetworkClient - starts a an instance of the LogFactor5 console and configures
* the console settings.
*/
public final static void main(String[] args) {

View File

@@ -1,15 +1,10 @@
package pm;
import pm.event.EventListener;
import pm.event.EventManager;
import pm.exception.application.ApplicationExitException;
import pm.exception.application.ApplicationInitialiseException;
public abstract class Application extends EventListener {
public Application() {
super();
}
public void initialise() throws ApplicationInitialiseException {}
public void exit() throws ApplicationExitException {

View File

@@ -6,8 +6,6 @@ import pm.event.task.Continuous;
import pm.event.task.Stopper;
import pm.exception.device.DeviceExitException;
import pm.exception.device.DeviceInitialiseException;
import pm.interrupt.InterruptListener;
import pm.interrupt.Interruptible;
import pm.macro.Sequence;
import pm.macro.SequenceListener;
import pm.macro.State;
@@ -17,12 +15,10 @@ import pm.macro.state.Release;
public abstract class Device extends EventListener {
protected SequenceListener sequenceListener;
//protected InterruptListener interruptListener;
public Device() {
super();
sequenceListener = new SequenceListener(this);
//interruptListener = new InterruptListener(this);
}
/* Register macro's */
@@ -71,11 +67,6 @@ 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);
}*/
/* Recognize events */
protected void add(State state) {

View File

@@ -38,18 +38,15 @@ public class Main extends EventListener {
//protected String[] deviceClassArray;
protected ArrayCycle<Application> applicationCycle;
protected ArrayList<Device> deviceList;
protected EventManager eventManager;
public Main() {
super();
/*deviceClassArray = new String[] {
"pm.device.jintellitype.JIntellitypeDevice",
"pm.device.javainput.rumblepad.RumblepadDevice",
"pm.device.javainput.extreme3d.Extreme3DDevice",
"pm.device.wiimote.WiimoteDevice"};*/
applicationCycle = new ArrayCycle<Application>();
deviceList = new ArrayList<Device>();
EventManager.initialise(applicationCycle);
EventManager.add(this);
eventManager = new EventManager(applicationCycle);
EventListener.initialise(eventManager);
}
public void initialise() throws DeviceInitialiseException {
@@ -59,10 +56,10 @@ public class Main extends EventListener {
//add(new WiimoteDevice());
//add(new GUIDevice());
//add(new TextDevice());
//add(new PanelDevice());
add(new PanelDevice());
//add(new LanTextDevice());
//add(new Extreme3DDevice());
add(new NetworkServer());
//add(new NetworkServer());
startDevices();
//add(new ExampleApplication());
@@ -164,22 +161,22 @@ public class Main extends EventListener {
/* Add / remove methods */
protected void add(Application application) {
EventManager.add(application);
//EventManager.add(application);
applicationCycle.add(application);
}
protected void remove(Application application) {
EventManager.remove(application);
//EventManager.remove(application);
applicationCycle.remove(application);
}
protected void add(Device device) {
EventManager.add(device);
//EventManager.add(device);
deviceList.add(device);
}
protected void remove(Device device) {
EventManager.remove(device);
//EventManager.remove(device);
deviceList.remove(device);
}

View File

@@ -1,11 +0,0 @@
package pm.device.gui;
import pm.Device;
public class GUIDevice extends Device {
protected GUIDeviceUI gui;
public GUIDevice() {
gui = new GUIDeviceUI();
}
}

View File

@@ -1,199 +0,0 @@
package pm.device.gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import pm.event.Task;
import pm.event.EventManager;
import pm.value.Action;
import pm.value.Target;
import layout.TableLayout;
import layout.TableLayoutConstraints;
public class GUIDeviceUI extends JFrame {
protected static final long serialVersionUID = 1L;
protected JButton play;
protected JButton pause;
protected JButton resume;
protected JButton next;
protected JButton previous;
protected JButton forward;
protected JButton rewind;
protected JButton mute;
protected JButton volumeUp;
protected JButton volumeDown;
//TaskGatherer.add
public GUIDeviceUI() {
initComponents();
setSize(30, 300);
setVisible(true);
}
public void initComponents() {
play = new JButton();
pause = new JButton();
resume = new JButton();
next = new JButton();
previous = new JButton();
forward = new JButton();
rewind = new JButton();
mute = new JButton();
volumeUp = new JButton();
volumeDown = new JButton();
setLayout(
new TableLayout(new double[][] {
{TableLayout.PREFERRED},
{TableLayout.PREFERRED,
TableLayout.PREFERRED,
TableLayout.PREFERRED,
TableLayout.PREFERRED,
TableLayout.PREFERRED,
TableLayout.PREFERRED,
TableLayout.PREFERRED,
TableLayout.PREFERRED,
TableLayout.PREFERRED,
TableLayout.PREFERRED,
TableLayout.PREFERRED}
})
);
//---- play ----
play.setText("play");
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
playAction(e);
}
});
add(play, new TableLayoutConstraints(0, 0, 0, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- pause ----
pause.setText("pause");
pause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pauseAction(e);
}
});
add(pause, new TableLayoutConstraints(0, 1, 0, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- resume ----
resume.setText("resume");
resume.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
resumeAction(e);
}
});
add(resume, new TableLayoutConstraints(0, 2, 0, 2, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- next ----
next.setText("next");
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nextAction(e);
}
});
add(next, new TableLayoutConstraints(0, 3, 0, 3, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- previous ----
previous.setText("previous");
previous.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
previousAction(e);
}
});
add(previous, new TableLayoutConstraints(0, 4, 0, 4, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- forward ----
forward.setText("forward");
forward.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
forwardAction(e);
}
});
add(forward, new TableLayoutConstraints(0, 5, 0, 5, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- rewind ----
rewind.setText("rewind");
rewind.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
rewindAction(e);
}
});
add(rewind, new TableLayoutConstraints(0, 6, 0, 6, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- mute ----
mute.setText("mute");
mute.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
muteAction(e);
}
});
add(mute, new TableLayoutConstraints(0, 7, 0, 7, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- volumeUp ----
volumeUp.setText("volume up");
volumeUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
volumeUpAction(e);
}
});
add(volumeUp, new TableLayoutConstraints(0, 8, 0, 8, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- volumeDown ----
volumeDown.setText("volume down");
volumeDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
volumeDownAction(e);
}
});
add(volumeDown, new TableLayoutConstraints(0, 9, 0, 9, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
}
protected void playAction(ActionEvent e) {
EventManager.add(new Task(Action.PLAY, Target.APPLICATION));
}
protected void pauseAction(ActionEvent e) {
EventManager.add(new Task(Action.PAUSE, Target.APPLICATION));
}
protected void resumeAction(ActionEvent e) {
EventManager.add(new Task(Action.RESUME, Target.APPLICATION));
}
protected void nextAction(ActionEvent e) {
EventManager.add(new Task(Action.NEXT, Target.APPLICATION));
}
protected void previousAction(ActionEvent e) {
EventManager.add(new Task(Action.PREVIOUS, Target.APPLICATION));
}
protected void forwardAction(ActionEvent e) {
EventManager.add(new Task(Action.FORWARD, Target.APPLICATION));
}
protected void rewindAction(ActionEvent e) {
EventManager.add(new Task(Action.REWIND, Target.APPLICATION));
}
protected void muteAction(ActionEvent e) {
EventManager.add(new Task(Action.MUTE, Target.APPLICATION));
}
protected void volumeUpAction(ActionEvent e) {
EventManager.add(new Task(Action.VOLUME_UP, Target.APPLICATION));
}
protected void volumeDownAction(ActionEvent e) {
EventManager.add(new Task(Action.VOLUME_DOWN, Target.APPLICATION));
}
}

View File

@@ -34,12 +34,6 @@ public class Extreme3DDevice extends JavaInputDevice {
new Press(Extreme3DButton.ELEVEN),
new Release(Extreme3DButton.ONE)),
new Task(Action.EXIT, Target.MAIN));
add(
new Interruptible(
new Press(Extreme3DButton.NINE),
new Task(Action.FORWARD),
new Press(Extreme3DButton.TEN),
new Task(Action.PLAY)));
} catch (MacroException e) {
e.printStackTrace();
}

View File

@@ -1,34 +1,23 @@
package pm.event;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import pm.Event;
import pm.Listener;
import pm.event.task.Continuous;
import pm.value.Action;
public abstract class EventListener extends Listener implements Runnable {
protected Queue<Event> eventQueue;
protected static EventManager eventManager;
public EventListener() {
eventQueue = new ConcurrentLinkedQueue<Event>();
public static void initialise(EventManager eventManager) {
EventListener.eventManager = eventManager;
}
public final void run() {
while (run) {
if (eventQueue.isEmpty()) {
sleep();
} else {
event(eventQueue.poll());
}
event(eventManager.get());
}
}
public void add(Event event) {
eventQueue.add(event);
}
protected void event(Event event) {
if (event instanceof Feedback) {
event((Feedback) event);

View File

@@ -1,34 +1,78 @@
package pm.event;
import java.util.ArrayList;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import pm.Application;
import pm.Device;
import pm.Main;
import pm.event.task.Stopper;
import pm.Event;
import pm.util.ArrayCycle;
import pm.value.Target;
public class EventManager {
protected static ArrayList<EventListener> eventListenerList;
protected static ArrayCycle<Application> applicationCycle;
protected Queue<Event> eventQueue;
protected Lock lock;
protected Condition available;
public static void initialise(ArrayCycle<Application> applicationCycle) {
eventListenerList = new ArrayList<EventListener>();
EventManager.applicationCycle = applicationCycle;
public EventManager(ArrayCycle<Application> applicationCycle) {
eventQueue = new ConcurrentLinkedQueue<Event>();
lock = new ReentrantLock();
available = lock.newCondition();
}
public static void add(EventListener eventListener) {
eventListenerList.add(eventListener);
}
public static void add(Feedback feedback) {
for (EventListener eventListener : eventListenerList) {
eventListener.add(feedback);
public void add(Event event) {
eventQueue.add(event);
synchronized (available) {
lock.notifyAll();
}
}
public static void add(EventListener self, Task task) {
public Event get(Target target) {
while (eventQueue.isEmpty()) {
synchronized (available) {
try {
available.await();
} catch (InterruptedException e) {}
}
}
Event event = eventQueue.peek();
if (event instanceof Task) {
Task task = (Task) event;
if (task.getTarget() == target) {
return eventQueue.poll();
} else {
return null;
}
} else {
return eventQueue.poll();
}
}
public Event gett(Target target) {
while (true) {
Event event = eventQueue.peek();
if (event instanceof Task) {
Task task = (Task) event;
if (task.getTarget() == target) {
return get();
}
}
}
}
public Event get() {
return get(Target.ALL);
}
/*public static void add(Feedback feedback) {
for (EventListener eventListener : eventListenerList) {
eventListener.add(feedback);
}
}*/
/*public static void add(EventListener self, Task task) {
if (task instanceof Stopper) {
((Stopper) task).stop();
} else {
@@ -68,19 +112,5 @@ public class EventManager {
}
}
}
}
/* public static void add(Task task) {
add(null, task);
}
private static void add(Task task) {
if (!task.getTarget().equals(Target.SELF)) {
add(null, task);
}
}
*/
public static void remove(EventListener eventListener) {
eventListenerList.remove(eventListener);
}
}*/
}

View File

@@ -1,34 +0,0 @@
package pm.interrupt;
import java.util.ArrayList;
import pm.event.EventListener;
import pm.event.EventManager;
import pm.macro.State;
public class InterruptListener {
protected EventListener eventListener;
protected ArrayList<Interruptible> interruptibleList;
public InterruptListener(EventListener eventListener) {
this.eventListener = eventListener;
interruptibleList = new ArrayList<Interruptible>();
}
public void add(Interruptible interruptible) {
interruptibleList.add(interruptible);
}
public void add(State state) {
for (Interruptible interruptible : interruptibleList) {
if(interruptible.triggerPress.equals(state)) {
EventManager.add(interruptible.triggerTask);
interruptible.triggered = true;
} else if(interruptible.interruptPress.equals(state) && interruptible.triggered) {
EventManager.add(interruptible.interruptTask);
interruptible.interrupted = true;
interruptibleList.remove(interruptible);
}
}
}
}

View File

@@ -1,26 +0,0 @@
package pm.interrupt;
import pm.event.Task;
import pm.macro.state.Press;
public class Interruptible extends Task {
protected static final int SLEEP = 0;
protected boolean triggered;
protected boolean interrupted;
protected Press triggerPress;
protected Task triggerTask;
protected Press interruptPress;
protected Task interruptTask;
public Interruptible(Press triggerPress, Task triggerTask, Press interruptPress, Task interruptTask) {
super(null, null);
triggered = false;
interrupted = false;
this.triggerPress = triggerPress;
this.triggerTask = triggerTask;
this.interruptPress = interruptPress;
this.interruptTask = interruptTask;
}
}

View File

@@ -6,12 +6,15 @@ import java.util.HashMap;
import pm.event.Task;
import pm.event.EventListener;
import pm.event.EventManager;
import pm.value.Target;
public class SequenceListener {
protected EventListener eventListener;
protected ArrayList<Sequence> sequenceList;
protected HashMap<Sequence, Task> taskMap;
protected ArrayList<Active> activeList;
protected static EventManager eventManager;
public SequenceListener(EventListener eventListener) {
this.eventListener = eventListener;
@@ -20,6 +23,10 @@ public class SequenceListener {
activeList = new ArrayList<Active>();
}
public static void initialise(EventManager eventManager) {
SequenceListener.eventManager = eventManager;
}
public int add(Sequence sequence, Task task) {
int id = sequenceList.size();
sequenceList.add(sequence);
@@ -36,7 +43,12 @@ public class SequenceListener {
if (active.next(state)) {
if (active.last()) {
Task task = taskMap.get(active.getSequence());
EventManager.add(eventListener, task);
if (task.getTarget().equals(Target.SELF)) {
//eventListener.event() protected in event listener
//.add(eventListener, task);
} else {
eventManager.add(task);
}
removeList.add(active);
}
} else {

View File

@@ -20,7 +20,7 @@ import wiiusej.WiiUseApiManager;
import wiiusej.Wiimote;
/**
* Main Class to launch WiiuseJ GUI Test.
* NetworkClient Class to launch WiiuseJ GUI Test.
*
* @author guiguito
*/