From d99708ba080b067f81a8127f5fbf1a02290ca3f6 Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Fri, 31 Oct 2014 00:30:57 +0100 Subject: [PATCH 1/9] Button: Move common interface visualization to Button class The implementation of a simple JPanel with a JButton was only code duplication accross the several button implementations. --- .../cooja/mspmote/interfaces/ESBButton.java | 22 ----------------- .../cooja/mspmote/interfaces/MspButton.java | 24 ------------------- .../cooja/mspmote/interfaces/SkyButton.java | 21 ---------------- .../contikimote/interfaces/ContikiButton.java | 20 ---------------- .../contikios/cooja/interfaces/Button.java | 23 ++++++++++++++++++ 5 files changed, 23 insertions(+), 87 deletions(-) diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java index c722e74eb..def3d207e 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java @@ -30,11 +30,7 @@ package org.contikios.cooja.mspmote.interfaces; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; import java.util.Collection; -import javax.swing.JButton; -import javax.swing.JPanel; import org.apache.log4j.Logger; import org.jdom.Element; @@ -76,24 +72,6 @@ public class ESBButton extends Button { return false; } - public JPanel getInterfaceVisualizer() { - JPanel panel = new JPanel(); - final JButton clickButton = new JButton("Click button"); - - panel.add(clickButton); - - clickButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - clickButton(); - } - }); - - return panel; - } - - public void releaseInterfaceVisualizer(JPanel panel) { - } - public Collection getConfigXML() { return null; } diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java index 5136485e7..28d4f32b2 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java @@ -28,11 +28,7 @@ */ package org.contikios.cooja.mspmote.interfaces; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; import java.util.Collection; -import javax.swing.JButton; -import javax.swing.JPanel; import org.jdom.Element; import org.contikios.cooja.ClassDescription; import org.contikios.cooja.Mote; @@ -87,26 +83,6 @@ public class MspButton extends Button { return button.isPressed(); } - @Override - public JPanel getInterfaceVisualizer() { - final JPanel panel = new JPanel(); - final JButton clickButton = new JButton("Click button"); - - panel.add(clickButton); - - clickButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - clickButton(); - } - }); - - return panel; - } - - @Override - public void releaseInterfaceVisualizer(JPanel panel) { - } - @Override public Collection getConfigXML() { return null; diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java index 6b1711b63..04cd767b5 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java @@ -30,12 +30,8 @@ package org.contikios.cooja.mspmote.interfaces; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; import java.util.Collection; -import javax.swing.JButton; -import javax.swing.JPanel; import org.apache.log4j.Logger; import org.jdom.Element; @@ -103,23 +99,6 @@ public class SkyButton extends Button { return false; } - public JPanel getInterfaceVisualizer() { - JPanel panel = new JPanel(); - final JButton clickButton = new JButton("Click button"); - - panel.add(clickButton); - - clickButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - clickButton(); - } - }); - - return panel; - } - - public void releaseInterfaceVisualizer(JPanel panel) { - } public Collection getConfigXML() { return null; diff --git a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java index 852383a09..f0e9fa359 100644 --- a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java +++ b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java @@ -30,9 +30,7 @@ package org.contikios.cooja.contikimote.interfaces; -import java.awt.event.*; import java.util.Collection; -import javax.swing.*; import org.apache.log4j.Logger; import org.jdom.Element; @@ -164,24 +162,6 @@ public class ContikiButton extends Button implements ContikiMoteInterface { return moteMem.getByteValueOf("simButtonIsDown") == 1; } - public JPanel getInterfaceVisualizer() { - JPanel panel = new JPanel(); - final JButton clickButton = new JButton("Click button"); - - panel.add(clickButton); - - clickButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - clickButton(); - } - }); - - return panel; - } - - public void releaseInterfaceVisualizer(JPanel panel) { - } - public Collection getConfigXML() { return null; } diff --git a/tools/cooja/java/org/contikios/cooja/interfaces/Button.java b/tools/cooja/java/org/contikios/cooja/interfaces/Button.java index 549c5ba65..cb50a16e4 100644 --- a/tools/cooja/java/org/contikios/cooja/interfaces/Button.java +++ b/tools/cooja/java/org/contikios/cooja/interfaces/Button.java @@ -30,6 +30,10 @@ package org.contikios.cooja.interfaces; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.JButton; +import javax.swing.JPanel; import org.contikios.cooja.*; /** @@ -62,4 +66,23 @@ public abstract class Button extends MoteInterface { * @return True if button is pressed */ public abstract boolean isPressed(); + + public JPanel getInterfaceVisualizer() { + JPanel panel = new JPanel(); + final JButton clickButton = new JButton("Click button"); + + panel.add(clickButton); + + clickButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + clickButton(); + } + }); + + return panel; + } + + public void releaseInterfaceVisualizer(JPanel panel) { + } + } From 82d30ef2e3de333e2fd081be11da379cc808a109 Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Fri, 31 Oct 2014 00:39:43 +0100 Subject: [PATCH 2/9] SkyButton: Reduce dependency on SkyMote Placed call to setButton() in implementation-specific functions doPressButton() and doReleaseButton() as in ContikiButton. --- .../cooja/mspmote/interfaces/SkyButton.java | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java index 04cd767b5..5dd5dedb1 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java @@ -31,17 +31,14 @@ package org.contikios.cooja.mspmote.interfaces; import java.util.Collection; - - import org.apache.log4j.Logger; -import org.jdom.Element; - import org.contikios.cooja.ClassDescription; import org.contikios.cooja.Mote; +import org.contikios.cooja.MoteTimeEvent; import org.contikios.cooja.Simulation; import org.contikios.cooja.interfaces.Button; -import org.contikios.cooja.mspmote.MspMoteTimeEvent; import org.contikios.cooja.mspmote.SkyMote; +import org.jdom.Element; @ClassDescription("Button") public class SkyButton extends Button { @@ -50,21 +47,21 @@ public class SkyButton extends Button { private SkyMote skyMote; private Simulation sim; - private MspMoteTimeEvent pressButtonEvent; - private MspMoteTimeEvent releaseButtonEvent; + private MoteTimeEvent pressButtonEvent; + private MoteTimeEvent releaseButtonEvent; public SkyButton(Mote mote) { skyMote = (SkyMote) mote; sim = mote.getSimulation(); - pressButtonEvent = new MspMoteTimeEvent((SkyMote)mote, 0) { + pressButtonEvent = new MoteTimeEvent(mote, 0) { public void execute(long t) { - skyMote.skyNode.setButton(true); + doPressButton(); } }; - releaseButtonEvent = new MspMoteTimeEvent((SkyMote)mote, 0) { + releaseButtonEvent = new MoteTimeEvent(mote, 0) { public void execute(long t) { - skyMote.skyNode.setButton(false); + doReleaseButton(); } }; } @@ -86,6 +83,10 @@ public class SkyButton extends Button { }); } + public void doPressButton() { + skyMote.skyNode.setButton(true); + } + public void releaseButton() { sim.invokeSimulationThread(new Runnable() { public void run() { @@ -94,6 +95,10 @@ public class SkyButton extends Button { }); } + public void doReleaseButton() { + skyMote.skyNode.setButton(false); + } + public boolean isPressed() { /* Not implemented */ return false; From 378ca2629d778196ae1735ad5c16986aebc24322 Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Fri, 31 Oct 2014 00:49:43 +0100 Subject: [PATCH 3/9] Button: None of the buttons will saves its state Saving a buttons state in simulation file is not required as its state is much too volatile. --- .../cooja/mspmote/interfaces/ESBButton.java | 9 --------- .../cooja/mspmote/interfaces/MspButton.java | 11 ----------- .../cooja/mspmote/interfaces/SkyButton.java | 10 ---------- .../cooja/contikimote/interfaces/ContikiButton.java | 9 --------- .../java/org/contikios/cooja/interfaces/Button.java | 12 ++++++++++++ 5 files changed, 12 insertions(+), 39 deletions(-) diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java index def3d207e..c2ac16c39 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java @@ -30,9 +30,7 @@ package org.contikios.cooja.mspmote.interfaces; -import java.util.Collection; import org.apache.log4j.Logger; -import org.jdom.Element; import org.contikios.cooja.*; import org.contikios.cooja.interfaces.Button; @@ -72,11 +70,4 @@ public class ESBButton extends Button { return false; } - public Collection getConfigXML() { - return null; - } - - public void setConfigXML(Collection configXML, boolean visAvailable) { - } - } diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java index 28d4f32b2..a261b2863 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java @@ -28,8 +28,6 @@ */ package org.contikios.cooja.mspmote.interfaces; -import java.util.Collection; -import org.jdom.Element; import org.contikios.cooja.ClassDescription; import org.contikios.cooja.Mote; import org.contikios.cooja.Simulation; @@ -83,15 +81,6 @@ public class MspButton extends Button { return button.isPressed(); } - @Override - public Collection getConfigXML() { - return null; - } - - @Override - public void setConfigXML(Collection configXML, boolean visAvailable) { - } - private class ButtonClick extends TimeEvent implements Runnable { public ButtonClick() { diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java index 5dd5dedb1..b4f4c0fe7 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java @@ -30,7 +30,6 @@ package org.contikios.cooja.mspmote.interfaces; -import java.util.Collection; import org.apache.log4j.Logger; import org.contikios.cooja.ClassDescription; import org.contikios.cooja.Mote; @@ -38,7 +37,6 @@ import org.contikios.cooja.MoteTimeEvent; import org.contikios.cooja.Simulation; import org.contikios.cooja.interfaces.Button; import org.contikios.cooja.mspmote.SkyMote; -import org.jdom.Element; @ClassDescription("Button") public class SkyButton extends Button { @@ -104,12 +102,4 @@ public class SkyButton extends Button { return false; } - - public Collection getConfigXML() { - return null; - } - - public void setConfigXML(Collection configXML, boolean visAvailable) { - } - } diff --git a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java index f0e9fa359..0cade0caf 100644 --- a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java +++ b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java @@ -30,9 +30,7 @@ package org.contikios.cooja.contikimote.interfaces; -import java.util.Collection; import org.apache.log4j.Logger; -import org.jdom.Element; import org.contikios.cooja.*; import org.contikios.cooja.contikimote.ContikiMote; @@ -162,11 +160,4 @@ public class ContikiButton extends Button implements ContikiMoteInterface { return moteMem.getByteValueOf("simButtonIsDown") == 1; } - public Collection getConfigXML() { - return null; - } - - public void setConfigXML(Collection configXML, boolean visAvailable) { - } - } diff --git a/tools/cooja/java/org/contikios/cooja/interfaces/Button.java b/tools/cooja/java/org/contikios/cooja/interfaces/Button.java index cb50a16e4..b5f0bf371 100644 --- a/tools/cooja/java/org/contikios/cooja/interfaces/Button.java +++ b/tools/cooja/java/org/contikios/cooja/interfaces/Button.java @@ -32,9 +32,11 @@ package org.contikios.cooja.interfaces; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.Collection; import javax.swing.JButton; import javax.swing.JPanel; import org.contikios.cooja.*; +import org.jdom.Element; /** * A Button represents a mote button. An implementation should notify all @@ -85,4 +87,14 @@ public abstract class Button extends MoteInterface { public void releaseInterfaceVisualizer(JPanel panel) { } + @Override + public Collection getConfigXML() { + // The button state will not be saved! + return null; + } + + @Override + public void setConfigXML(Collection configXML, boolean visAvailable) { + // The button state will not be saved! + } } From be88a4fc524bfd9bdd38c733dd267d121a4bc915 Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Fri, 31 Oct 2014 01:04:07 +0100 Subject: [PATCH 4/9] Button: Add doPressButton() and doReleaseButton() to Button class As every Button has a node-specific implementation part, this should be the minimal interface to the backend node emulator for pressing and releasing a button. --- .../cooja/mspmote/interfaces/ESBButton.java | 14 ++++++++++++-- .../cooja/mspmote/interfaces/MspButton.java | 14 ++++++++++++-- .../cooja/mspmote/interfaces/SkyButton.java | 6 ++++-- .../contikimote/interfaces/ContikiButton.java | 6 ++++-- .../org/contikios/cooja/interfaces/Button.java | 10 ++++++++++ 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java index c2ac16c39..6e03921f3 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java @@ -55,17 +55,27 @@ public class ESBButton extends Button { } public void releaseButton() { - mote.esbNode.setButton(false); + doReleaseButton(); setChanged(); notifyObservers(); } + @Override + protected void doReleaseButton() { + mote.esbNode.setButton(false); + } + public void pressButton() { - mote.esbNode.setButton(true); + doPressButton(); setChanged(); notifyObservers(); } + @Override + protected void doPressButton() { + mote.esbNode.setButton(true); + } + public boolean isPressed() { return false; } diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java index a261b2863..9762430fc 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java @@ -62,20 +62,30 @@ public class MspButton extends Button { public void pressButton() { sim.invokeSimulationThread(new Runnable() { public void run() { - button.setPressed(true); + doPressButton(); } }); } + @Override + protected void doPressButton() { + button.setPressed(true); + } + @Override public void releaseButton() { sim.invokeSimulationThread(new Runnable() { public void run() { - button.setPressed(false); + doReleaseButton(); } }); } + @Override + protected void doReleaseButton() { + button.setPressed(false); + } + @Override public boolean isPressed() { return button.isPressed(); diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java index b4f4c0fe7..c785c5695 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java @@ -81,7 +81,8 @@ public class SkyButton extends Button { }); } - public void doPressButton() { + @Override + protected void doPressButton() { skyMote.skyNode.setButton(true); } @@ -93,7 +94,8 @@ public class SkyButton extends Button { }); } - public void doReleaseButton() { + @Override + protected void doReleaseButton() { skyMote.skyNode.setButton(false); } diff --git a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java index 0cade0caf..8190968bd 100644 --- a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java +++ b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java @@ -128,7 +128,8 @@ public class ContikiButton extends Button implements ContikiMoteInterface { }); } - private void doReleaseButton() { + @Override + protected void doReleaseButton() { moteMem.setByteValueOf("simButtonIsDown", (byte) 0); if (moteMem.getByteValueOf("simButtonIsActive") == 1) { @@ -142,7 +143,8 @@ public class ContikiButton extends Button implements ContikiMoteInterface { } } - private void doPressButton() { + @Override + protected void doPressButton() { moteMem.setByteValueOf("simButtonIsDown", (byte) 1); if (moteMem.getByteValueOf("simButtonIsActive") == 1) { diff --git a/tools/cooja/java/org/contikios/cooja/interfaces/Button.java b/tools/cooja/java/org/contikios/cooja/interfaces/Button.java index b5f0bf371..6c402c0e7 100644 --- a/tools/cooja/java/org/contikios/cooja/interfaces/Button.java +++ b/tools/cooja/java/org/contikios/cooja/interfaces/Button.java @@ -59,11 +59,21 @@ public abstract class Button extends MoteInterface { */ public abstract void releaseButton(); + /** + * Node-type dependent implementation of pressing a button. + */ + protected abstract void doPressButton(); + /** * Presses button (if not already pressed). */ public abstract void pressButton(); + /** + * Node-type dependent implementation of releasing a button. + */ + protected abstract void doReleaseButton(); + /** * @return True if button is pressed */ From 0a63922fa247127946d62c06d19921696903e217 Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Fri, 31 Oct 2014 01:20:54 +0100 Subject: [PATCH 5/9] Buttons: Move implementation of button routines to Button class The implementation of clickButton(), pressButton(), and releaseButton() can be shared accross the several node-dependent implementations as they use the node-dependent doPressButton() doReleaseButton() routines. --- .../cooja/mspmote/interfaces/ESBButton.java | 18 +------ .../cooja/mspmote/interfaces/MspButton.java | 41 +-------------- .../cooja/mspmote/interfaces/SkyButton.java | 46 +---------------- .../contikimote/interfaces/ContikiButton.java | 49 +----------------- .../contikios/cooja/interfaces/Button.java | 50 +++++++++++++++++-- 5 files changed, 50 insertions(+), 154 deletions(-) diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java index 6e03921f3..cb6b6689f 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java @@ -46,31 +46,15 @@ public class ESBButton extends Button { private ESBMote mote; public ESBButton(Mote mote) { + super(mote); this.mote = (ESBMote) mote; } - public void clickButton() { - pressButton(); - releaseButton(); - } - - public void releaseButton() { - doReleaseButton(); - setChanged(); - notifyObservers(); - } - @Override protected void doReleaseButton() { mote.esbNode.setButton(false); } - public void pressButton() { - doPressButton(); - setChanged(); - notifyObservers(); - } - @Override protected void doPressButton() { mote.esbNode.setButton(true); diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java index 9762430fc..30214802a 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java @@ -45,6 +45,7 @@ public class MspButton extends Button { private final se.sics.mspsim.chip.Button button; public MspButton(Mote mote) { + super(mote); final MspMote mspMote = (MspMote) mote; sim = mote.getSimulation(); button = mspMote.getCPU().getChip(se.sics.mspsim.chip.Button.class); @@ -53,34 +54,11 @@ public class MspButton extends Button { } } - @Override - public void clickButton() { - sim.invokeSimulationThread(new ButtonClick()); - } - - @Override - public void pressButton() { - sim.invokeSimulationThread(new Runnable() { - public void run() { - doPressButton(); - } - }); - } - @Override protected void doPressButton() { button.setPressed(true); } - @Override - public void releaseButton() { - sim.invokeSimulationThread(new Runnable() { - public void run() { - doReleaseButton(); - } - }); - } - @Override protected void doReleaseButton() { button.setPressed(false); @@ -91,21 +69,4 @@ public class MspButton extends Button { return button.isPressed(); } - private class ButtonClick extends TimeEvent implements Runnable { - - public ButtonClick() { - super(0); - } - - @Override - public void run() { - button.setPressed(true); - sim.scheduleEvent(this, sim.getSimulationTime() + Simulation.MILLISECOND); - } - - @Override - public void execute(long t) { - button.setPressed(false); - } - } } diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java index c785c5695..b11030ccf 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java @@ -33,8 +33,6 @@ package org.contikios.cooja.mspmote.interfaces; import org.apache.log4j.Logger; import org.contikios.cooja.ClassDescription; import org.contikios.cooja.Mote; -import org.contikios.cooja.MoteTimeEvent; -import org.contikios.cooja.Simulation; import org.contikios.cooja.interfaces.Button; import org.contikios.cooja.mspmote.SkyMote; @@ -43,42 +41,10 @@ public class SkyButton extends Button { private static Logger logger = Logger.getLogger(SkyButton.class); private SkyMote skyMote; - private Simulation sim; - - private MoteTimeEvent pressButtonEvent; - private MoteTimeEvent releaseButtonEvent; - + public SkyButton(Mote mote) { + super(mote); skyMote = (SkyMote) mote; - sim = mote.getSimulation(); - - pressButtonEvent = new MoteTimeEvent(mote, 0) { - public void execute(long t) { - doPressButton(); - } - }; - releaseButtonEvent = new MoteTimeEvent(mote, 0) { - public void execute(long t) { - doReleaseButton(); - } - }; - } - - public void clickButton() { - sim.invokeSimulationThread(new Runnable() { - public void run() { - sim.scheduleEvent(pressButtonEvent, sim.getSimulationTime()); - sim.scheduleEvent(releaseButtonEvent, sim.getSimulationTime() + Simulation.MILLISECOND); - } - }); - } - - public void pressButton() { - sim.invokeSimulationThread(new Runnable() { - public void run() { - sim.scheduleEvent(pressButtonEvent, sim.getSimulationTime()); - } - }); } @Override @@ -86,14 +52,6 @@ public class SkyButton extends Button { skyMote.skyNode.setButton(true); } - public void releaseButton() { - sim.invokeSimulationThread(new Runnable() { - public void run() { - sim.scheduleEvent(releaseButtonEvent, sim.getSimulationTime()); - } - }); - } - @Override protected void doReleaseButton() { skyMote.skyNode.setButton(false); diff --git a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java index 8190968bd..67a16c97f 100644 --- a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java +++ b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java @@ -72,6 +72,7 @@ public class ContikiButton extends Button implements ContikiMoteInterface { * @see org.contikios.cooja.MoteInterfaceHandler */ public ContikiButton(Mote mote) { + super(mote); this.mote = (ContikiMote) mote; this.moteMem = new VarMemory(mote.getMemory()); } @@ -80,54 +81,6 @@ public class ContikiButton extends Button implements ContikiMoteInterface { return new String[]{"button_interface"}; } - private TimeEvent pressButtonEvent = new MoteTimeEvent(mote, 0) { - public void execute(long t) { - doPressButton(); - } - }; - - private TimeEvent releaseButtonEvent = new MoteTimeEvent(mote, 0) { - public void execute(long t) { - /* Wait until button change is handled by Contiki */ - if (moteMem.getByteValueOf("simButtonChanged") != 0) { - /* Postpone button release */ - mote.getSimulation().scheduleEvent(releaseButtonEvent, t + Simulation.MILLISECOND); - return; - } - - /*logger.info("Releasing button at: " + t);*/ - doReleaseButton(); - } - }; - - /** - * Clicks button: Presses and immediately releases button. - */ - public void clickButton() { - mote.getSimulation().invokeSimulationThread(new Runnable() { - public void run() { - mote.getSimulation().scheduleEvent(pressButtonEvent, mote.getSimulation().getSimulationTime()); - mote.getSimulation().scheduleEvent(releaseButtonEvent, mote.getSimulation().getSimulationTime() + Simulation.MILLISECOND); - } - }); - } - - public void pressButton() { - mote.getSimulation().invokeSimulationThread(new Runnable() { - public void run() { - mote.getSimulation().scheduleEvent(pressButtonEvent, mote.getSimulation().getSimulationTime()); - } - }); - } - - public void releaseButton() { - mote.getSimulation().invokeSimulationThread(new Runnable() { - public void run() { - mote.getSimulation().scheduleEvent(releaseButtonEvent, mote.getSimulation().getSimulationTime()); - } - }); - } - @Override protected void doReleaseButton() { moteMem.setByteValueOf("simButtonIsDown", (byte) 0); diff --git a/tools/cooja/java/org/contikios/cooja/interfaces/Button.java b/tools/cooja/java/org/contikios/cooja/interfaces/Button.java index 6c402c0e7..02985a8e8 100644 --- a/tools/cooja/java/org/contikios/cooja/interfaces/Button.java +++ b/tools/cooja/java/org/contikios/cooja/interfaces/Button.java @@ -36,6 +36,7 @@ import java.util.Collection; import javax.swing.JButton; import javax.swing.JPanel; import org.contikios.cooja.*; +import org.contikios.cooja.mspmote.SkyMote; import org.jdom.Element; /** @@ -48,16 +49,49 @@ import org.jdom.Element; @ClassDescription("Button") public abstract class Button extends MoteInterface { + private final Simulation sim; + + private final MoteTimeEvent pressButtonEvent; + private final MoteTimeEvent releaseButtonEvent; + + public Button(Mote mote) { + sim = mote.getSimulation(); + + pressButtonEvent = new MoteTimeEvent(mote, 0) { + public void execute(long t) { + doPressButton(); + } + }; + releaseButtonEvent = new MoteTimeEvent(mote, 0) { + public void execute(long t) { + doReleaseButton(); + } + }; + } + /** * Clicks button. Button will be pressed for some time and then automatically * released. */ - public abstract void clickButton(); + public void clickButton() { + sim.invokeSimulationThread(new Runnable() { + public void run() { + sim.scheduleEvent(pressButtonEvent, sim.getSimulationTime()); + sim.scheduleEvent(releaseButtonEvent, sim.getSimulationTime() + Simulation.MILLISECOND); + } + }); + } /** - * Releases button (if pressed). + * Presses button. */ - public abstract void releaseButton(); + public void pressButton() { + sim.invokeSimulationThread(new Runnable() { + public void run() { + sim.scheduleEvent(pressButtonEvent, sim.getSimulationTime()); + } + }); + } /** * Node-type dependent implementation of pressing a button. @@ -65,9 +99,15 @@ public abstract class Button extends MoteInterface { protected abstract void doPressButton(); /** - * Presses button (if not already pressed). + * Releases button. */ - public abstract void pressButton(); + public void releaseButton() { + sim.invokeSimulationThread(new Runnable() { + public void run() { + sim.scheduleEvent(releaseButtonEvent, sim.getSimulationTime()); + } + }); + } /** * Node-type dependent implementation of releasing a button. From b28c593776155a8d8e62d2e28f69e6dbd6088ea5 Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Fri, 31 Oct 2014 01:23:50 +0100 Subject: [PATCH 6/9] Buttons: Added missing @Override annotations --- .../org/contikios/cooja/mspmote/interfaces/ESBButton.java | 1 + .../org/contikios/cooja/mspmote/interfaces/SkyButton.java | 1 + .../cooja/contikimote/interfaces/ContikiButton.java | 1 + .../cooja/java/org/contikios/cooja/interfaces/Button.java | 8 ++++++++ 4 files changed, 11 insertions(+) diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java index cb6b6689f..2214dce69 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java @@ -60,6 +60,7 @@ public class ESBButton extends Button { mote.esbNode.setButton(true); } + @Override public boolean isPressed() { return false; } diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java index b11030ccf..502e75570 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java @@ -57,6 +57,7 @@ public class SkyButton extends Button { skyMote.skyNode.setButton(false); } + @Override public boolean isPressed() { /* Not implemented */ return false; diff --git a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java index 67a16c97f..dea5728a3 100644 --- a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java +++ b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java @@ -111,6 +111,7 @@ public class ContikiButton extends Button implements ContikiMoteInterface { } } + @Override public boolean isPressed() { return moteMem.getByteValueOf("simButtonIsDown") == 1; } diff --git a/tools/cooja/java/org/contikios/cooja/interfaces/Button.java b/tools/cooja/java/org/contikios/cooja/interfaces/Button.java index 02985a8e8..62e7a5eb0 100644 --- a/tools/cooja/java/org/contikios/cooja/interfaces/Button.java +++ b/tools/cooja/java/org/contikios/cooja/interfaces/Button.java @@ -58,11 +58,13 @@ public abstract class Button extends MoteInterface { sim = mote.getSimulation(); pressButtonEvent = new MoteTimeEvent(mote, 0) { + @Override public void execute(long t) { doPressButton(); } }; releaseButtonEvent = new MoteTimeEvent(mote, 0) { + @Override public void execute(long t) { doReleaseButton(); } @@ -75,6 +77,7 @@ public abstract class Button extends MoteInterface { */ public void clickButton() { sim.invokeSimulationThread(new Runnable() { + @Override public void run() { sim.scheduleEvent(pressButtonEvent, sim.getSimulationTime()); sim.scheduleEvent(releaseButtonEvent, sim.getSimulationTime() + Simulation.MILLISECOND); @@ -87,6 +90,7 @@ public abstract class Button extends MoteInterface { */ public void pressButton() { sim.invokeSimulationThread(new Runnable() { + @Override public void run() { sim.scheduleEvent(pressButtonEvent, sim.getSimulationTime()); } @@ -103,6 +107,7 @@ public abstract class Button extends MoteInterface { */ public void releaseButton() { sim.invokeSimulationThread(new Runnable() { + @Override public void run() { sim.scheduleEvent(releaseButtonEvent, sim.getSimulationTime()); } @@ -119,6 +124,7 @@ public abstract class Button extends MoteInterface { */ public abstract boolean isPressed(); + @Override public JPanel getInterfaceVisualizer() { JPanel panel = new JPanel(); final JButton clickButton = new JButton("Click button"); @@ -126,6 +132,7 @@ public abstract class Button extends MoteInterface { panel.add(clickButton); clickButton.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { clickButton(); } @@ -134,6 +141,7 @@ public abstract class Button extends MoteInterface { return panel; } + @Override public void releaseInterfaceVisualizer(JPanel panel) { } From b5d119babd22510d680fb6b6e595032bd94ecabd Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Fri, 31 Oct 2014 01:25:10 +0100 Subject: [PATCH 7/9] Buttons: Some minor cleanups Removed imports not required anymore and made some class members final --- .../org/contikios/cooja/mspmote/interfaces/ESBButton.java | 4 ++-- .../org/contikios/cooja/mspmote/interfaces/MspButton.java | 1 - .../org/contikios/cooja/mspmote/interfaces/SkyButton.java | 4 ++-- .../cooja/contikimote/interfaces/ContikiButton.java | 6 +++--- tools/cooja/java/org/contikios/cooja/interfaces/Button.java | 1 - 5 files changed, 7 insertions(+), 9 deletions(-) diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java index 2214dce69..a16207c72 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java @@ -41,9 +41,9 @@ import org.contikios.cooja.mspmote.ESBMote; */ @ClassDescription("Button") public class ESBButton extends Button { - private static Logger logger = Logger.getLogger(ESBButton.class); + private static final Logger logger = Logger.getLogger(ESBButton.class); - private ESBMote mote; + private final ESBMote mote; public ESBButton(Mote mote) { super(mote); diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java index 30214802a..459205153 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java @@ -31,7 +31,6 @@ package org.contikios.cooja.mspmote.interfaces; import org.contikios.cooja.ClassDescription; import org.contikios.cooja.Mote; import org.contikios.cooja.Simulation; -import org.contikios.cooja.TimeEvent; import org.contikios.cooja.interfaces.Button; import org.contikios.cooja.mspmote.MspMote; diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java index 502e75570..e20376a69 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java @@ -38,9 +38,9 @@ import org.contikios.cooja.mspmote.SkyMote; @ClassDescription("Button") public class SkyButton extends Button { - private static Logger logger = Logger.getLogger(SkyButton.class); + private static final Logger logger = Logger.getLogger(SkyButton.class); - private SkyMote skyMote; + private final SkyMote skyMote; public SkyButton(Mote mote) { super(mote); diff --git a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java index dea5728a3..f4cc75acd 100644 --- a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java +++ b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java @@ -59,10 +59,10 @@ import org.contikios.cooja.mote.memory.VarMemory; * @author Fredrik Osterlind */ public class ContikiButton extends Button implements ContikiMoteInterface { - private VarMemory moteMem; - private ContikiMote mote; + private final VarMemory moteMem; + private final ContikiMote mote; - private static Logger logger = Logger.getLogger(ContikiButton.class); + private static final Logger logger = Logger.getLogger(ContikiButton.class); /** * Creates an interface to the button at mote. diff --git a/tools/cooja/java/org/contikios/cooja/interfaces/Button.java b/tools/cooja/java/org/contikios/cooja/interfaces/Button.java index 62e7a5eb0..f04b81e8b 100644 --- a/tools/cooja/java/org/contikios/cooja/interfaces/Button.java +++ b/tools/cooja/java/org/contikios/cooja/interfaces/Button.java @@ -36,7 +36,6 @@ import java.util.Collection; import javax.swing.JButton; import javax.swing.JPanel; import org.contikios.cooja.*; -import org.contikios.cooja.mspmote.SkyMote; import org.jdom.Element; /** From bc6b7535d0c22af897d1a275a81b88005227ee2c Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Tue, 4 Nov 2014 01:41:32 +0100 Subject: [PATCH 8/9] Buttons: use non-deprecated MSPSim button api --- .../org/contikios/cooja/mspmote/interfaces/SkyButton.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java index e20376a69..faaec655a 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java @@ -49,18 +49,17 @@ public class SkyButton extends Button { @Override protected void doPressButton() { - skyMote.skyNode.setButton(true); + skyMote.skyNode.getButton().setPressed(true); } @Override protected void doReleaseButton() { - skyMote.skyNode.setButton(false); + skyMote.skyNode.getButton().setPressed(false); } @Override public boolean isPressed() { - /* Not implemented */ - return false; + return skyMote.skyNode.getButton().isPressed(); } } From f49e1b8f5fc491ad63f8a640a404299d57dd0e1a Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Tue, 4 Nov 2014 01:42:46 +0100 Subject: [PATCH 9/9] Button: Allow to press and release button by mouse and key In the previous implementation a click event was triggered when the button was pressed. This implementation allows to set and release buttons independently both by mouse clicking and by key typing. --- .../contikios/cooja/interfaces/Button.java | 56 +++++++++++++++++-- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/tools/cooja/java/org/contikios/cooja/interfaces/Button.java b/tools/cooja/java/org/contikios/cooja/interfaces/Button.java index f04b81e8b..b32cfe315 100644 --- a/tools/cooja/java/org/contikios/cooja/interfaces/Button.java +++ b/tools/cooja/java/org/contikios/cooja/interfaces/Button.java @@ -1,4 +1,5 @@ /* + * Copyright (c) 2014, TU Braunschweig. * Copyright (c) 2006, Swedish Institute of Computer Science. * All rights reserved. * @@ -30,8 +31,10 @@ package org.contikios.cooja.interfaces; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import java.util.Collection; import javax.swing.JButton; import javax.swing.JPanel; @@ -129,11 +132,52 @@ public abstract class Button extends MoteInterface { final JButton clickButton = new JButton("Click button"); panel.add(clickButton); - - clickButton.addActionListener(new ActionListener() { + + clickButton.addMouseListener(new MouseAdapter() { @Override - public void actionPerformed(ActionEvent e) { - clickButton(); + public void mousePressed(MouseEvent e) { + sim.invokeSimulationThread(new Runnable() { + + @Override + public void run() { + doPressButton(); + } + }); + } + + @Override + public void mouseReleased(MouseEvent e) { + sim.invokeSimulationThread(new Runnable() { + + @Override + public void run() { + doReleaseButton(); + } + }); + } + }); + + clickButton.addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + sim.invokeSimulationThread(new Runnable() { + + @Override + public void run() { + doPressButton(); + } + }); + } + + @Override + public void keyReleased(KeyEvent e) { + sim.invokeSimulationThread(new Runnable() { + + @Override + public void run() { + doReleaseButton(); + } + }); } });