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:
@@ -19,5 +19,5 @@ public abstract class Application extends EventHandler implements Selectable {
|
||||
public void exit() {
|
||||
deactivate();
|
||||
stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user