From 5d542c2cad8bb999411005d55b9d7b36b9664907 Mon Sep 17 00:00:00 2001 From: nifi Date: Fri, 10 Dec 2010 17:50:48 +0000 Subject: [PATCH] Added optional color for mote relations --- tools/cooja/java/se/sics/cooja/GUI.java | 27 ++++++--- .../cooja/interfaces/Mote2MoteRelations.java | 60 +++++++++++++++---- .../se/sics/cooja/plugins/Visualizer.java | 4 +- 3 files changed, 70 insertions(+), 21 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/GUI.java b/tools/cooja/java/se/sics/cooja/GUI.java index 7936a28eb..1c2a05d9c 100644 --- a/tools/cooja/java/se/sics/cooja/GUI.java +++ b/tools/cooja/java/se/sics/cooja/GUI.java @@ -24,7 +24,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $Id: GUI.java,v 1.173 2010/12/10 15:54:52 fros4943 Exp $ + * $Id: GUI.java,v 1.174 2010/12/10 17:50:48 nifi Exp $ */ package se.sics.cooja; @@ -341,9 +341,11 @@ public class GUI extends Observable { public static class MoteRelation { public Mote source; public Mote dest; - public MoteRelation(Mote source, Mote dest) { + public Color color; + public MoteRelation(Mote source, Mote dest, Color color) { this.source = source; this.dest = dest; + this.color = color; } } private ArrayList moteRelations = new ArrayList(); @@ -3783,11 +3785,22 @@ public class GUI extends Observable { * @param dest Destination mote */ public void addMoteRelation(Mote source, Mote dest) { + addMoteRelation(source, dest, null); + } + + /** + * Adds directed relation between given motes. + * + * @param source Source mote + * @param dest Destination mote + * @param color The color to use when visualizing the mote relation + */ + public void addMoteRelation(Mote source, Mote dest, Color color) { if (source == null || dest == null) { return; } removeMoteRelation(source, dest); /* Unique relations */ - moteRelations.add(new MoteRelation(source, dest)); + moteRelations.add(new MoteRelation(source, dest, color)); moteRelationObservable.setChangedAndNotify(); } @@ -3805,9 +3818,11 @@ public class GUI extends Observable { for (MoteRelation r: arr) { if (r.source == source && r.dest == dest) { moteRelations.remove(r); + /* Relations are unique */ + moteRelationObservable.setChangedAndNotify(); + break; } } - moteRelationObservable.setChangedAndNotify(); } /** @@ -3816,9 +3831,7 @@ public class GUI extends Observable { * @see #addMoteRelationsObserver(Observer) */ public MoteRelation[] getMoteRelations() { - MoteRelation[] arr = new MoteRelation[moteRelations.size()]; - moteRelations.toArray(arr); - return arr; + return moteRelations.toArray(new MoteRelation[moteRelations.size()]); } /** diff --git a/tools/cooja/java/se/sics/cooja/interfaces/Mote2MoteRelations.java b/tools/cooja/java/se/sics/cooja/interfaces/Mote2MoteRelations.java index 412423b12..50943debb 100644 --- a/tools/cooja/java/se/sics/cooja/interfaces/Mote2MoteRelations.java +++ b/tools/cooja/java/se/sics/cooja/interfaces/Mote2MoteRelations.java @@ -26,11 +26,12 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: Mote2MoteRelations.java,v 1.6 2010/02/10 13:29:27 fros4943 Exp $ + * $Id: Mote2MoteRelations.java,v 1.7 2010/12/10 17:50:49 nifi Exp $ */ package se.sics.cooja.interfaces; +import java.awt.Color; import java.util.ArrayList; import java.util.Collection; import java.util.Observable; @@ -142,26 +143,31 @@ public class Mote2MoteRelations extends MoteInterface { return; } + String colorName = null; + int colorIndex = msg.indexOf(';'); + if (colorIndex > 0) { + colorName = msg.substring(colorIndex + 1).trim(); + msg = msg.substring(0, colorIndex).trim(); + } String[] args = msg.split(" "); if (args.length != 3) { return; } - String id = args[1]; + int destID; + try { + destID = Integer.parseInt(args[1]); + } catch (Exception e) { + // Not a mote id + return; + } String state = args[2]; /* Locate destination mote */ /* TODO Use Rime address interface instead of mote ID? */ - Mote destinationMote = null; - Mote[] allMotes = Mote2MoteRelations.this.mote.getSimulation().getMotes(); - for (Mote m: allMotes) { - if (id.equals("" + m.getID())) { - destinationMote = m; - break; - } - } + Mote destinationMote = mote.getSimulation().getMoteWithID(destID); if (destinationMote == null) { - logger.warn("No destination mote with ID: " + id); + logger.warn("No destination mote with ID: " + destID); return; } if (destinationMote == mote) { @@ -175,7 +181,7 @@ public class Mote2MoteRelations extends MoteInterface { return; } relations.add(destinationMote); - gui.addMoteRelation(mote, destinationMote); + gui.addMoteRelation(mote, destinationMote, decodeColor(colorName)); } else { relations.remove(destinationMote); gui.removeMoteRelation(mote, destinationMote); @@ -185,6 +191,36 @@ public class Mote2MoteRelations extends MoteInterface { notifyObservers(); } + private Color decodeColor(String colorString) { + if (colorString == null) { + return null; + } else if (colorString.equalsIgnoreCase("red")) { + return Color.RED; + } else if (colorString.equalsIgnoreCase("green")) { + return Color.GREEN; + } else if (colorString.equalsIgnoreCase("blue")) { + return Color.BLUE; + } else if (colorString.equalsIgnoreCase("orange")) { + return Color.ORANGE; + } else if (colorString.equalsIgnoreCase("pink")) { + return Color.PINK; + } else if (colorString.equalsIgnoreCase("yellow")) { + return Color.YELLOW; + } else if (colorString.equalsIgnoreCase("gray")) { + return Color.GRAY; + } else if (colorString.equalsIgnoreCase("magenta")) { + return Color.MAGENTA; + } else if (colorString.equalsIgnoreCase("black")) { + return Color.BLACK; + } else { + try { + return Color.decode(colorString); + } catch (NumberFormatException e) { + } + return null; + } + } + public JPanel getInterfaceVisualizer() { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); diff --git a/tools/cooja/java/se/sics/cooja/plugins/Visualizer.java b/tools/cooja/java/se/sics/cooja/plugins/Visualizer.java index 3c4ba64aa..77ccf5b61 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/Visualizer.java +++ b/tools/cooja/java/se/sics/cooja/plugins/Visualizer.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: Visualizer.java,v 1.18 2010/12/03 13:54:25 fros4943 Exp $ + * $Id: Visualizer.java,v 1.19 2010/12/10 17:50:49 nifi Exp $ */ package se.sics.cooja.plugins; @@ -847,7 +847,6 @@ public class Visualizer extends VisPlugin { Mote[] allMotes = simulation.getMotes(); /* Paint mote relations */ - g.setColor(Color.BLACK); MoteRelation[] relations = simulation.getGUI().getMoteRelations(); for (MoteRelation r: relations) { Position sourcePos = r.source.getInterfaces().getPosition(); @@ -856,6 +855,7 @@ public class Visualizer extends VisPlugin { Point sourcePoint = transformPositionToPixel(sourcePos); Point destPoint = transformPositionToPixel(destPos); + g.setColor(r.color == null ? Color.black : r.color); drawArrow(g, sourcePoint.x, sourcePoint.y, destPoint.x, destPoint.y, MOTE_RADIUS + 1); }