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..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 @@ -30,13 +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; import org.contikios.cooja.*; import org.contikios.cooja.interfaces.Button; @@ -47,58 +41,28 @@ 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); this.mote = (ESBMote) mote; } - public void clickButton() { - pressButton(); - releaseButton(); - } - - public void releaseButton() { + @Override + protected void doReleaseButton() { mote.esbNode.setButton(false); - setChanged(); - notifyObservers(); } - public void pressButton() { + @Override + protected void doPressButton() { mote.esbNode.setButton(true); - setChanged(); - notifyObservers(); } + @Override public boolean isPressed() { 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; - } - - 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 5136485e7..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 @@ -28,16 +28,9 @@ */ 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; import org.contikios.cooja.Simulation; -import org.contikios.cooja.TimeEvent; import org.contikios.cooja.interfaces.Button; import org.contikios.cooja.mspmote.MspMote; @@ -51,6 +44,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); @@ -60,26 +54,13 @@ public class MspButton extends Button { } @Override - public void clickButton() { - sim.invokeSimulationThread(new ButtonClick()); + protected void doPressButton() { + button.setPressed(true); } @Override - public void pressButton() { - sim.invokeSimulationThread(new Runnable() { - public void run() { - button.setPressed(true); - } - }); - } - - @Override - public void releaseButton() { - sim.invokeSimulationThread(new Runnable() { - public void run() { - button.setPressed(false); - } - }); + protected void doReleaseButton() { + button.setPressed(false); } @Override @@ -87,50 +68,4 @@ 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; - } - - @Override - public void setConfigXML(Collection configXML, boolean visAvailable) { - } - - 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 6b1711b63..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 @@ -30,102 +30,36 @@ 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; - import org.contikios.cooja.ClassDescription; import org.contikios.cooja.Mote; -import org.contikios.cooja.Simulation; import org.contikios.cooja.interfaces.Button; -import org.contikios.cooja.mspmote.MspMoteTimeEvent; 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 final SkyMote skyMote; - private SkyMote skyMote; - private Simulation sim; - - private MspMoteTimeEvent pressButtonEvent; - private MspMoteTimeEvent releaseButtonEvent; - public SkyButton(Mote mote) { + super(mote); skyMote = (SkyMote) mote; - sim = mote.getSimulation(); - - pressButtonEvent = new MspMoteTimeEvent((SkyMote)mote, 0) { - public void execute(long t) { - skyMote.skyNode.setButton(true); - } - }; - releaseButtonEvent = new MspMoteTimeEvent((SkyMote)mote, 0) { - public void execute(long t) { - skyMote.skyNode.setButton(false); - } - }; } - public void clickButton() { - sim.invokeSimulationThread(new Runnable() { - public void run() { - sim.scheduleEvent(pressButtonEvent, sim.getSimulationTime()); - sim.scheduleEvent(releaseButtonEvent, sim.getSimulationTime() + Simulation.MILLISECOND); - } - }); + @Override + protected void doPressButton() { + skyMote.skyNode.getButton().setPressed(true); } - public void pressButton() { - sim.invokeSimulationThread(new Runnable() { - public void run() { - sim.scheduleEvent(pressButtonEvent, sim.getSimulationTime()); - } - }); - } - - public void releaseButton() { - sim.invokeSimulationThread(new Runnable() { - public void run() { - sim.scheduleEvent(releaseButtonEvent, sim.getSimulationTime()); - } - }); + @Override + protected void doReleaseButton() { + skyMote.skyNode.getButton().setPressed(false); } + @Override public boolean isPressed() { - /* Not implemented */ - 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; - } - - public void setConfigXML(Collection configXML, boolean visAvailable) { + return skyMote.skyNode.getButton().isPressed(); } } 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..f4cc75acd 100644 --- a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java +++ b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java @@ -30,11 +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; import org.contikios.cooja.*; import org.contikios.cooja.contikimote.ContikiMote; @@ -63,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. @@ -76,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()); } @@ -84,55 +81,8 @@ 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()); - } - }); - } - - private void doReleaseButton() { + @Override + protected void doReleaseButton() { moteMem.setByteValueOf("simButtonIsDown", (byte) 0); if (moteMem.getByteValueOf("simButtonIsActive") == 1) { @@ -146,7 +96,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) { @@ -160,33 +111,9 @@ public class ContikiButton extends Button implements ContikiMoteInterface { } } + @Override public boolean isPressed() { 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; - } - - 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 549c5ba65..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,7 +31,15 @@ package org.contikios.cooja.interfaces; +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; import org.contikios.cooja.*; +import org.jdom.Element; /** * A Button represents a mote button. An implementation should notify all @@ -42,24 +51,151 @@ import org.contikios.cooja.*; @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) { + @Override + public void execute(long t) { + doPressButton(); + } + }; + releaseButtonEvent = new MoteTimeEvent(mote, 0) { + @Override + 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() { + @Override + 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() { + @Override + public void run() { + sim.scheduleEvent(pressButtonEvent, sim.getSimulationTime()); + } + }); + } /** - * Presses button (if not already pressed). + * Node-type dependent implementation of pressing a button. */ - public abstract void pressButton(); + protected abstract void doPressButton(); + + /** + * Releases button. + */ + public void releaseButton() { + sim.invokeSimulationThread(new Runnable() { + @Override + public void run() { + sim.scheduleEvent(releaseButtonEvent, sim.getSimulationTime()); + } + }); + } + + /** + * Node-type dependent implementation of releasing a button. + */ + protected abstract void doReleaseButton(); /** * @return True if button is pressed */ public abstract boolean isPressed(); + + @Override + public JPanel getInterfaceVisualizer() { + JPanel panel = new JPanel(); + final JButton clickButton = new JButton("Click button"); + + panel.add(clickButton); + + clickButton.addMouseListener(new MouseAdapter() { + @Override + 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(); + } + }); + } + }); + + return panel; + } + + @Override + 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! + } }