Grote aanpassingen gedaan tbv. selector: een worker kan nu active en deactive zijn. Initialise werkt nu anders.

This commit is contained in:
Bram Veenboer
2011-05-16 20:09:19 +00:00
parent a67478e616
commit 33e69c141a
29 changed files with 194 additions and 112 deletions

View File

@@ -24,8 +24,8 @@ final class JNIWinamp {
/** /**
* Verify if Winamp is started * Verify if Winamp is started
* and if not started, starts it * and if not started, starts it
* @return True - if successful run Winamp * @return True - if successful running Winamp
* False - if not successful run Winamp * False - if not successful running Winamp
*/ */
protected static native boolean run() throws UnsatisfiedLinkError; protected static native boolean run() throws UnsatisfiedLinkError;

View File

@@ -76,7 +76,7 @@ public class WinampController {
public static void run() throws Exception{ public static void run() throws Exception{
if (!JNIWinamp.run()) if (!JNIWinamp.run())
throw new Exception("Unable to run Winamp. Verify if it is properly installed"); throw new Exception("Unable to running Winamp. Verify if it is properly installed");
} }

View File

@@ -267,7 +267,7 @@ public class JXInputTestDialog extends javax.swing.JDialog
}//GEN-LAST:event_closeDialog }//GEN-LAST:event_closeDialog
/** /**
* Allow the dialog to run standalone. * Allow the dialog to running standalone.
* @param args the command line arguments * @param args the command line arguments
*/ */
public static void main(String args[]) public static void main(String args[])

View File

@@ -636,7 +636,7 @@ public class BasicPlayer implements BasicController, Runnable
} }
catch (IOException e) catch (IOException e)
{ {
log.error("Thread cannot run()", e); log.error("Thread cannot running()", e);
m_status = STOPPED; m_status = STOPPED;
notifyEvent(BasicPlayerEvent.STOPPED, getEncodedStreamPosition(), -1, null); notifyEvent(BasicPlayerEvent.STOPPED, getEncodedStreamPosition(), -1, null);
} }

View File

@@ -1344,10 +1344,10 @@ public abstract class LogFactory {
} }
/** /**
* Applets may run in an environment where accessing resources of a loader is * Applets may running in an environment where accessing resources of a loader is
* a secure operation, but where the commons-logging library has explicitly * a secure operation, but where the commons-logging library has explicitly
* been granted permission for that operation. In this case, we need to * been granted permission for that operation. In this case, we need to
* run the operation using an AccessController. * running the operation using an AccessController.
*/ */
private static InputStream getResourceAsStream(final ClassLoader loader, private static InputStream getResourceAsStream(final ClassLoader loader,
final String name) final String name)
@@ -1801,8 +1801,8 @@ public abstract class LogFactory {
// //
// Note that this block must come after any variable declarations used // Note that this block must come after any variable declarations used
// by any methods called from this block, as we want any static initialiser // by any methods called from this block, as we want any static initialiser
// associated with the variable to run first. If static initialisers for // associated with the variable to running first. If static initialisers for
// variables run after this code, then (a) their value might be needed // variables running after this code, then (a) their value might be needed
// by methods called from here, and (b) they might *override* any value // by methods called from here, and (b) they might *override* any value
// computed here! // computed here!
// //

View File

@@ -39,7 +39,7 @@ import org.apache.log4j.Level;
* </ul> * </ul>
* Log4J1.3 is expected to change Level so it no longer extends Priority, which is * Log4J1.3 is expected to change Level so it no longer extends Priority, which is
* a non-binary-compatible change. The class generated by compiling this code against * a non-binary-compatible change. The class generated by compiling this code against
* log4j 1.2 will therefore not run against log4j 1.3. * log4j 1.2 will therefore not running against log4j 1.3.
* *
* @author <a href="mailto:sanders@apache.org">Scott Sanders</a> * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
* @author Rod Waldhoff * @author Rod Waldhoff
@@ -135,7 +135,7 @@ public class Log4JLogger implements Log, Serializable {
// Logger class) in version 1.3 has methods that take both Priority and // Logger class) in version 1.3 has methods that take both Priority and
// Level objects. This means that if we use Level here, and compile // Level objects. This means that if we use Level here, and compile
// against log4j 1.3 then calls would be bound to the versions of // against log4j 1.3 then calls would be bound to the versions of
// methods taking Level objects and then would fail to run against // methods taking Level objects and then would fail to running against
// version 1.2 of log4j. // version 1.2 of log4j.
// --------------------------------------------------------- // ---------------------------------------------------------

View File

@@ -81,7 +81,7 @@ import org.apache.log4j.helpers.LogLog;
request will have a different NDC tag. request will have a different NDC tag.
<p>Heavy duty systems should call the {@link #remove} method when <p>Heavy duty systems should call the {@link #remove} method when
leaving the run method of a thread. This ensures that the memory leaving the running method of a thread. This ensures that the memory
used by the thread can be freed by the Java garbage used by the thread can be freed by the Java garbage
collector. There is a mechanism to lazily remove references to dead collector. There is a mechanism to lazily remove references to dead
threads. In practice, this means that you can be a little sloppy threads. In practice, this means that you can be a little sloppy
@@ -367,7 +367,7 @@ public class NDC {
practice, this means that you can be a little sloppy and practice, this means that you can be a little sloppy and
occasionally forget to call {@link #remove} before exiting a occasionally forget to call {@link #remove} before exiting a
thread. However, you must call <code>remove</code> sometime. If thread. However, you must call <code>remove</code> sometime. If
you never call it, then your application is sure to run out of you never call it, then your application is sure to running out of
memory. memory.
*/ */

View File

@@ -158,7 +158,7 @@ public class LF5Appender extends AppenderSkeleton {
* exits * exits
* the monitor. By default, the <code>LogBrokerMonitor</code> will hide * the monitor. By default, the <code>LogBrokerMonitor</code> will hide
* itself when the log window is exited, and the swing thread will * itself when the log window is exited, and the swing thread will
* continue to run in the background. If this property is * continue to running in the background. If this property is
* set to true, the <code>LogBrokerMonitor</code> will call System.exit(0) * set to true, the <code>LogBrokerMonitor</code> will call System.exit(0)
* and will shut down swing thread and the virtual machine. * and will shut down swing thread and the virtual machine.
* *

View File

@@ -88,7 +88,7 @@ public class LogFileParser implements Runnable {
/** /**
* Starts a new thread to parse the log file and create a LogRecord. * Starts a new thread to parse the log file and create a LogRecord.
* See run(). * See running().
* @param monitor LogBrokerMonitor * @param monitor LogBrokerMonitor
*/ */
public void parse(LogBrokerMonitor monitor) throws RuntimeException { public void parse(LogBrokerMonitor monitor) throws RuntimeException {

View File

@@ -54,7 +54,7 @@ public class LevelRangeFilter extends Filter {
/** /**
Do we return ACCEPT when a match occurs. Default is Do we return ACCEPT when a match occurs. Default is
<code>false</code>, so that later filters get run by default */ <code>false</code>, so that later filters get running by default */
boolean acceptOnMatch = false; boolean acceptOnMatch = false;
Level levelMin; Level levelMin;

View File

@@ -1,9 +1,9 @@
package pm; package pm;
import pm.event.EventHandler; import pm.event.EventHandler;
import pm.selector.Activatable; import pm.selector.Selectable;
public abstract class Application extends EventHandler implements Activatable { public abstract class Application extends EventHandler implements Selectable {
protected String title; protected String title;
protected boolean active; protected boolean active;
@@ -16,20 +16,6 @@ public abstract class Application extends EventHandler implements Activatable {
return title; return title;
} }
public boolean active() {
return active;
}
public void activate() {
start();
active = true;
}
public void deactivate() {
stop();
active = false;
}
public void exit() { public void exit() {
deactivate(); deactivate();
stop(); stop();

View File

@@ -10,9 +10,9 @@ import pm.macro.State;
import pm.macro.state.Hold; import pm.macro.state.Hold;
import pm.macro.state.Press; import pm.macro.state.Press;
import pm.macro.state.Release; import pm.macro.state.Release;
import pm.selector.Activatable; import pm.selector.Selectable;
public abstract class Device extends EventHandler implements Activatable { public abstract class Device extends EventHandler implements Selectable {
protected String title; protected String title;
protected boolean active; protected boolean active;
protected SequenceListener sequenceListener; protected SequenceListener sequenceListener;
@@ -83,20 +83,6 @@ public abstract class Device extends EventHandler implements Activatable {
return title; return title;
} }
public boolean active() {
return active;
}
public void activate() {
start();
active = true;
}
public void deactivate() {
stop();
active = false;
}
public void exit() { public void exit() {
deactivate(); deactivate();
stop(); stop();

View File

@@ -29,15 +29,15 @@ public abstract class Manager extends EventHandler {
public void start() { public void start() {
initialise(); initialise();
Device[] deviceArray = new Device[] { Device[] deviceArray = new Device[] {
new WiimoteDevice(), new WiimoteDevice(),
new PanelDevice(), new PanelDevice(),
new JIntellitypeDevice(), new JIntellitypeDevice(),
new PlayerDevice(), new PlayerDevice(),
new RumblepadDevice(), new RumblepadDevice(),
new Extreme3DDevice(), new Extreme3DDevice(),
new NetworkDevice()}; new NetworkDevice()};
deviceSelector = new DeviceSelector(deviceArray); deviceSelector = new DeviceSelector(deviceArray);
} }
public void exit() { public void exit() {

View File

@@ -1,7 +1,6 @@
package pm; package pm;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
@@ -9,29 +8,29 @@ import javax.swing.JPanel;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import pm.selector.Activatable;
import pm.selector.SelectButton; import pm.selector.SelectButton;
import pm.selector.Selectable;
public class Selector<T extends Activatable> extends JFrame { public class Selector<T extends Worker & Selectable> extends JFrame {
protected Log log = LogFactory.getLog(getClass()); protected Log log = LogFactory.getLog(getClass());
protected static final long serialVersionUID = 1L; protected static final long serialVersionUID = 1L;
protected ArrayList<Activatable> activatableArray; //protected ArrayList<T> activatableArray;
public Selector(Activatable[] activatableArray) { public Selector(T[] activatableArray) {
createPanel(activatableArray); createPanel(activatableArray);
//setUndecorated(true); setUndecorated(true);
pack(); pack();
setResizable(false); setResizable(false);
setVisible(true); setVisible(true);
log.debug("Selector constructed"); log.debug("Selector constructed");
} }
protected void createPanel(Activatable[] activatableArray) { protected void createPanel(T[] activatableArray) {
JPanel panel = new JPanel(new GridLayout(0, 1)); JPanel panel = new JPanel(new GridLayout(0, 1));
for (Activatable activatable : activatableArray) { for (T selectable : activatableArray) {
String title = activatable.title(); String title = selectable.title();
SelectButton<Activatable> button = new SelectButton<Activatable>(activatable); SelectButton<T> button = new SelectButton<T>(selectable);
button.setText(title); button.setText(title);
panel.add(button); panel.add(button);
log.debug(String.format("Item added: %s", title)); log.debug(String.format("Item added: %s", title));

View File

@@ -1,18 +1,27 @@
package pm; package pm;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public abstract class Worker implements Runnable { public abstract class Worker implements Runnable {
protected Log log = LogFactory.getLog(getClass());
protected static final boolean THREAD = true; protected static final boolean THREAD = true;
protected static final int SLEEP = 100; protected static final int SLEEP = 100;
protected boolean run; protected boolean running = false;
protected boolean active = false;
protected Object lock;
public void start(boolean thread) { public void start(boolean thread) {
run = true; running = true;
if (thread) { if (thread) {
new Thread(this).start(); new Thread(this).start();
} else { } else {
run(); run();
} }
activate();
} }
public void start() { public void start() {
@@ -20,7 +29,8 @@ public abstract class Worker implements Runnable {
} }
public void stop() { public void stop() {
run = false; running = false;
deactivate();
} }
protected void sleep(int time) { protected void sleep(int time) {
@@ -28,10 +38,55 @@ public abstract class Worker implements Runnable {
if (time > 0) { if (time > 0) {
Thread.sleep(time); Thread.sleep(time);
} }
} catch (InterruptedException e) {} } catch (InterruptedException e) {
log.info(e);
}
} }
protected void sleep() { protected void sleep() {
sleep(SLEEP); sleep(SLEEP);
} }
public boolean active() {
return active;
}
public void activate() {
if (!running) {
start();
}
synchronized (this) {
notify();
}
active = true;
}
public void deactivate() {
active = false;
}
public void deactivate(boolean stop) {
deactivate();
if (stop && running) {
stop();
}
}
public final void run() {
while (running) {
if (active) {
work();
} else {
try {
synchronized (this) {
wait();
}
} catch (InterruptedException e) {
log.info(e);
}
}
}
}
protected abstract void work();
} }

View File

@@ -18,7 +18,7 @@ public abstract class CMDApplication extends Application {
this.title = title; this.title = title;
} }
public void initialise() { protected void initialise() {
String key = String.format("%s\\%s", REGISTRY, program); String key = String.format("%s\\%s", REGISTRY, program);
// Check of naam is gevonden in register // Check of naam is gevonden in register
String path = Native.getValue(key); String path = Native.getValue(key);
@@ -32,10 +32,17 @@ public abstract class CMDApplication extends Application {
} }
} }
public void exit() { public void activate() {
if (!active) {
initialise();
}
super.activate();
}
public void deactivate() {
if (process != null) { if (process != null) {
process.destroy(); process.destroy();
} }
super.exit(); super.deactivate();
} }
} }

View File

@@ -25,18 +25,22 @@ public class iTunesApplication extends Application implements iTunesEventsInterf
} }
public void activate() { public void activate() {
iTunes.connect(); synchronized (iTunes) {
iTunes.addEventHandler(this); iTunes.connect();
iTunes.addEventHandler(this);
}
super.activate();
} }
public void exit() { public void deactivate() {
System.out.println("Exit iTunesApplication");
super.exit();
try { try {
iTunes.quit(); // Todo: wachten totdat ook daadwerkelijk gestart? Anders wordt iTunes niet afgesloten. synchronized (iTunes) {
iTunes.quit();
}
} catch (Exception e) { } catch (Exception e) {
//throw new ApplicationExitException(); log.info("Unexpected deactivation exception", e);
} }
super.deactivate();
} }
protected void action(Action action) { protected void action(Action action) {

View File

@@ -66,7 +66,7 @@ public class NetworkDevice extends Device {
} }
public void run() { public void run() {
while (run) { while (running) {
System.out.println("Server is waiting for clients"); System.out.println("Server is waiting for clients");
try { try {
Socket socket = serverSocket.accept(); Socket socket = serverSocket.accept();

View File

@@ -34,7 +34,7 @@ public abstract class EventHandler extends EventListener {
action(action); action(action);
continuous.nextIteration(); continuous.nextIteration();
sleep(continuous.getSleep()); sleep(continuous.getSleep());
} while (run && !continuous.getStop()); } while (running && !continuous.getStop());
continuous.reset(); continuous.reset();
} else { } else {
action(action); action(action);
@@ -42,4 +42,9 @@ public abstract class EventHandler extends EventListener {
} }
protected void action(Action action) {} protected void action(Action action) {}
public void activate() {
super.activate();
add(new Feedback());
}
} }

View File

@@ -21,18 +21,16 @@ public abstract class EventListener extends Worker {
} }
} }
public final void run() { public final void work() {
while (run) { while (eventQueue.isEmpty()) {
while (eventQueue.isEmpty()) { synchronized (available) {
synchronized (available) { try {
try { available.wait();
available.wait(); } catch (InterruptedException e) {}
} catch (InterruptedException e) {}
}
} }
event(eventQueue.poll()); }
} event(eventQueue.poll());
} }
public abstract void event(Event event); public abstract void event(Event event);
} }

View File

@@ -0,0 +1,5 @@
package pm.exception;
public class WorkerException extends Exception {
protected static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,7 @@
package pm.exception.worker;
import pm.exception.WorkerException;
public class AlreadyActiveException extends WorkerException {
protected static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,7 @@
package pm.exception.worker;
import pm.exception.WorkerException;
public class AlreadyRunningException extends WorkerException {
protected static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,7 @@
package pm.exception.worker;
import pm.exception.WorkerException;
public class NotActiveException extends WorkerException {
protected static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,7 @@
package pm.exception.worker;
import pm.exception.WorkerException;
public class NotRunningException extends WorkerException {
protected static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,7 @@
package pm.exception.worker;
import pm.exception.WorkerException;
public class StartException extends WorkerException {
protected static final long serialVersionUID = 1L;
}

View File

@@ -1,8 +0,0 @@
package pm.selector;
public interface Activatable {
public String title();
public boolean active();
public void activate();
public void deactivate();
}

View File

@@ -4,9 +4,15 @@ import java.awt.event.ItemEvent;
import java.awt.event.ItemListener; import java.awt.event.ItemListener;
import javax.swing.JToggleButton; import javax.swing.JToggleButton;
public class SelectButton<T extends Activatable> extends JToggleButton implements ItemListener { import org.apache.commons.logging.Log;
protected static final long serialVersionUID = 1L; import org.apache.commons.logging.LogFactory;
import pm.Worker;
public class SelectButton<T extends Worker & Selectable> extends JToggleButton implements ItemListener {
protected Log log = LogFactory.getLog(getClass());
protected static final long serialVersionUID = 1L;
protected T activatable; protected T activatable;
public SelectButton(T activatable) { public SelectButton(T activatable) {
@@ -20,9 +26,8 @@ public class SelectButton<T extends Activatable> extends JToggleButton implement
System.out.println("Selected"); System.out.println("Selected");
activatable.activate(); activatable.activate();
} else { } else {
System.out.println("Deselected"); System.out.println("Deselected");
activatable.deactivate(); activatable.deactivate();
} }
//System.out.println(itemEvent.getSource());
} }
} }

View File

@@ -0,0 +1,5 @@
package pm.selector;
public interface Selectable {
public String title();
}