From 585b187c0a67168afed6b4b1d53315ce62dbf860 Mon Sep 17 00:00:00 2001 From: Bram Veenboer Date: Thu, 19 May 2011 19:53:18 +0000 Subject: [PATCH] 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? --- java/src/pm/Application.java | 2 +- java/src/pm/Worker.java | 34 +++++++++++++++++++++++++- java/src/pm/selector/SelectButton.java | 20 ++++++++++++++- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/java/src/pm/Application.java b/java/src/pm/Application.java index 4a39985..40ca543 100644 --- a/java/src/pm/Application.java +++ b/java/src/pm/Application.java @@ -19,5 +19,5 @@ public abstract class Application extends EventHandler implements Selectable { public void exit() { deactivate(); stop(); - } + } } \ No newline at end of file diff --git a/java/src/pm/Worker.java b/java/src/pm/Worker.java index 9de782c..e02c4b4 100644 --- a/java/src/pm/Worker.java +++ b/java/src/pm/Worker.java @@ -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(); + } } diff --git a/java/src/pm/selector/SelectButton.java b/java/src/pm/selector/SelectButton.java index 813f821..3a39780 100644 --- a/java/src/pm/selector/SelectButton.java +++ b/java/src/pm/selector/SelectButton.java @@ -14,11 +14,14 @@ public class SelectButton 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 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(); } }