Begin gemaakt met het kijken of een applicatie afgesloten is, deze poging is gebasseerd op timestamps, maar misschien is het toch netter om met events te werken?

This commit is contained in:
Bram Veenboer
2011-05-19 19:53:18 +00:00
parent 153bc2c8d6
commit 585b187c0a
3 changed files with 53 additions and 3 deletions

View File

@@ -19,5 +19,5 @@ public abstract class Application extends EventHandler implements Selectable {
public void exit() {
deactivate();
stop();
}
}
}

View File

@@ -8,12 +8,15 @@ public abstract class Worker implements Runnable {
protected static final boolean THREAD = true;
protected static final int SLEEP = 100;
public static final int CHECK_ALIVE_INTERVAL = 1000;
protected boolean running = false;
protected boolean active = false;
protected boolean connected;
protected Object lock;
public void start(boolean thread) {
running = true;
if (thread) {
@@ -26,6 +29,8 @@ public abstract class Worker implements Runnable {
public void start() {
start(THREAD);
monitorConnection();
connected = false;
}
public void stop() {
@@ -89,4 +94,31 @@ public abstract class Worker implements Runnable {
}
protected abstract void work();
public boolean connected() {
return connected;
}
protected void monitorConnection() {
new Thread() {
protected long timestamp = System.currentTimeMillis();
public void run() {
while (super.isAlive()) {
if (timestamp + (2 * CHECK_ALIVE_INTERVAL) > System.currentTimeMillis()) {
timestamp = System.currentTimeMillis();
connected = true;
log.debug("Het gaat nog helemaal goed");
} else {
log.debug("Het interval is overschreden");
return;
}
try {
Thread.sleep(CHECK_ALIVE_INTERVAL);
} catch (InterruptedException e) {}
}
connected = false;
}
}.start();
}
}

View File

@@ -14,11 +14,14 @@ public class SelectButton<T extends Worker> extends JToggleButton implements Ite
protected static final long serialVersionUID = 1L;
protected T activatable;
public static final int CHECK_ALIVE_INTERVAL = 1000;
public SelectButton(T activatable, String title) {
this.activatable = activatable;
setText(title);
addItemListener(this);
monitorApplication();
}
public void itemStateChanged(ItemEvent itemEvent) {
@@ -29,6 +32,21 @@ public class SelectButton<T extends Worker> extends JToggleButton implements Ite
} else {
System.out.println("Deselected");
activatable.deactivate();
}
}
}
protected void monitorApplication() {
new Thread() {
public void run() {
while (super.isAlive()) {
if (!activatable.connected()) {
//log.debug("Nu moet het knopje uit gaan!");
}
try {
Thread.sleep(CHECK_ALIVE_INTERVAL);
} catch (InterruptedException e) {}
}
}
}.start();
}
}