From 22ac769781c454c085457267e3648a8ebb1b236d Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Thu, 10 Apr 2014 17:23:28 +0200 Subject: [PATCH 1/9] [cooja] plugins/Visualizer: Multi-mote selection functionality This adds multi-mote selection capabilities to the visualizer plugin: - [Ctrl + Mouse Drag]: Rectangular selection of multiple motes - [Ctrl + Mouse Click]: Add/Remove motes from current selection - Mouse Drag on any selected Mote: Move all currently selected motes Note: This changes previous behaviour of using Ctrl key. --- .../org/contikios/mrm/MRMVisualizerSkin.java | 16 +- .../contikios/cooja/plugins/Visualizer.java | 425 +++++++++++------- .../plugins/skins/DGRMVisualizerSkin.java | 15 +- .../plugins/skins/UDGMVisualizerSkin.java | 15 +- 4 files changed, 298 insertions(+), 173 deletions(-) diff --git a/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java b/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java index d18f1c66b..22714dc6d 100644 --- a/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java +++ b/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java @@ -33,6 +33,7 @@ import java.awt.Color; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Point; +import java.util.Set; import org.apache.log4j.Logger; @@ -72,20 +73,23 @@ public class MRMVisualizerSkin implements VisualizerSkin { } public Color[] getColorOf(Mote mote) { - Mote selectedMote = visualizer.getSelectedMote(); - if (mote == selectedMote) { + if (visualizer.getSelectedMotes().contains(mote)) { return new Color[] { Color.CYAN }; } return null; } public void paintBeforeMotes(Graphics g) { - final Mote selectedMote = visualizer.getSelectedMote(); - if (simulation == null - || selectedMote == null - || selectedMote.getInterfaces().getRadio() == null) { + Set selectedMotes = visualizer.getSelectedMotes(); + if (simulation == null || selectedMotes == null || selectedMotes.isEmpty()) { return; } + + final Mote selectedMote = visualizer.getSelectedMotes().iterator().next(); + if (selectedMote.getInterfaces().getRadio() == null) { + return; + } + final Position sPos = selectedMote.getInterfaces().getPosition(); /* Paint transmission and interference range for selected mote */ diff --git a/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java b/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java index 8926e9af7..14ef496e2 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java @@ -30,10 +30,12 @@ package org.contikios.cooja.plugins; +import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; +import java.awt.Event; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; @@ -55,7 +57,7 @@ import java.awt.event.ItemListener; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; -import java.awt.event.MouseMotionListener; +import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import java.awt.geom.AffineTransform; @@ -64,9 +66,14 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Observable; import java.util.Observer; +import java.util.Set; import javax.swing.AbstractAction; import javax.swing.Action; @@ -74,7 +81,6 @@ import javax.swing.JCheckBoxMenuItem; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; -import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JSeparator; @@ -128,6 +134,7 @@ import org.contikios.cooja.plugins.skins.UDGMVisualizerSkin; * @see #registerVisualizerSkin(Class) * @see UDGMVisualizerSkin * @author Fredrik Osterlind + * @author Enrico Jorns */ @ClassDescription("Network") @PluginType(PluginType.SIM_STANDARD_PLUGIN) @@ -149,20 +156,34 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { private AffineTransform viewportTransform; public int resetViewport = 0; - /* Actions: move motes, pan view, and zoom view */ - private boolean panning = false; - private Position panningPosition = null; /* Panning start position */ - private boolean zooming = false; - private double zoomStart = 0; - private Position zoomingPosition = null; /* Zooming center position */ - private Point zoomingPixel = null; /* Zooming center pixel */ - private boolean moving = false; - private Point mouseDownPixel = null; /* Records position of mouse down to differentiate a click from a move */ - private Mote movedMote = null; - public Mote clickedMote = null; + private static final int SELECT_MASK = Event.CTRL_MASK; + private static final int MOVE_MASK = Event.SHIFT_MASK; + + enum MotesActionState { + + UNKNWON, + SELECT_PRESS, + DEFAULT_PRESS, + PAN_PRESS, + PANNING, + MOVING, + // rectangular select + SELECTING + } + + /* All selected motes */ + public Set selectedMotes = new HashSet<>(); + /* Mote that was under curser while mouse press */ + Mote cursorMote; + + MotesActionState mouseActionState = MotesActionState.UNKNWON; + /* Position where mouse button was pressed */ + Position pressedPos; + + private Set movedMotes = null; private long moveStartTime = -1; - private boolean moveConfirm; - private Cursor moveCursor = new Cursor(Cursor.MOVE_CURSOR); + private static final Cursor MOVE_CURSOR = new Cursor(Cursor.MOVE_CURSOR); + private Selection selection; /* Visualizers */ private static ArrayList> visualizerSkins = @@ -277,6 +298,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }); zoomMenu.add(resetViewportItem); + selection = new Selection(); /* Main canvas */ canvas = new JPanel() { private static final long serialVersionUID = 1L; @@ -296,6 +318,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { for (VisualizerSkin skin: currentSkins) { skin.paintAfterMotes(g); } + selection.drawSelection(g); } }; canvas.setBackground(Color.WHITE); @@ -378,18 +401,16 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }); /* Popup menu */ - canvas.addMouseMotionListener(new MouseMotionListener() { - public void mouseMoved(MouseEvent e) { - handleMouseMove(e, false); - } + canvas.addMouseMotionListener(new MouseMotionAdapter() { + @Override public void mouseDragged(MouseEvent e) { - handleMouseMove(e, false); + handleMouseDrag(e, false); } }); canvas.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if (e.isPopupTrigger()) { - handlePopupRequest(e.getPoint().x, e.getPoint().y); + handlePopupRequest(e.getPoint()); return; } @@ -399,19 +420,13 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { - handlePopupRequest(e.getPoint().x, e.getPoint().y); + handlePopupRequest(e.getPoint()); return; } - handleMouseMove(e, true); - } - public void mouseClicked(MouseEvent e) { - if (e.isPopupTrigger()) { - handlePopupRequest(e.getPoint().x, e.getPoint().y); + if (SwingUtilities.isLeftMouseButton(e)) { + handleMouseRelease(e); } - - handleMouseMove(e, true); - repaint(); } }); canvas.addMouseWheelListener(new MouseWheelListener() { @@ -645,11 +660,11 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { visualizerSkins.remove(skin); } - private void handlePopupRequest(final int x, final int y) { + private void handlePopupRequest(Point point) { JPopupMenu menu = new JPopupMenu(); /* Mote specific actions */ - final Mote[] motes = findMotesAtPosition(x, y); + final Mote[] motes = findMotesAtPosition(point.x, point.y); if (motes != null && motes.length > 0) { menu.add(new JSeparator()); @@ -679,7 +694,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Simulation specific actions */ menu.add(new JSeparator()); - for (Class menuActionClass: simulationMenuActions) { + for (Class menuActionClass : simulationMenuActions) { try { final SimulationMenuAction menuAction = menuActionClass.newInstance(); if (menuAction.isEnabled(this, simulation)) { @@ -709,8 +724,8 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Show menu */ menu.setLocation(new Point( - canvas.getLocationOnScreen().x + x, - canvas.getLocationOnScreen().y + y)); + canvas.getLocationOnScreen().x + point.x, + canvas.getLocationOnScreen().y + point.y)); menu.setInvoker(canvas); menu.setVisible(true); } @@ -826,43 +841,169 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { private void handleMousePress(MouseEvent mouseEvent) { int x = mouseEvent.getX(); int y = mouseEvent.getY(); - clickedMote = null; - if (mouseEvent.isControlDown()) { - /* Zoom */ - zooming = true; - zoomingPixel = new Point(x, y); - zoomingPosition = transformPixelToPosition(zoomingPixel); - zoomStart = viewportTransform.getScaleX(); - return; + pressedPos = transformPixelToPosition(mouseEvent.getPoint()); + + // this is the state we have from pressing button + final Mote[] foundMotes = findMotesAtPosition(x, y); + if (foundMotes == null) { + cursorMote = null; + } + else { + // select top mote + cursorMote = foundMotes[foundMotes.length - 1]; } - final Mote[] motes = findMotesAtPosition(x, y); - if (mouseEvent.isShiftDown() || - (!mouseEvent.isAltDown() && (motes == null || motes.length == 0))) { - /* No motes clicked or shift pressed: We should pan */ - panning = true; - panningPosition = transformPixelToPosition(x, y); - return; - } + int modifiers = mouseEvent.getModifiers(); - if (motes != null && motes.length > 0) { - /* One of the clicked motes should be moved */ - mouseDownPixel = new Point(x, y); - clickedMote = motes[0]; - beginMoveRequest(motes[0], false, false); + /* translate input */ + if ((modifiers & SELECT_MASK) != 0) { + mouseActionState = MotesActionState.SELECT_PRESS; + } + else if ((modifiers & MOVE_MASK) != 0) { + // only move viewport + mouseActionState = MotesActionState.PAN_PRESS; + } + else { + if (foundMotes == null) { + // move viewport + selectedMotes.clear(); + } + else { + // if this mote was not selected before, assume a new selection + if (!selectedMotes.contains(cursorMote)) { + selectedMotes.clear(); + selectedMotes.add(cursorMote); + } + } + mouseActionState = MotesActionState.DEFAULT_PRESS; + } + repaint(); + } + + Map moveStartPositions = new HashMap<>(); + + private void handleMouseDrag(MouseEvent e, boolean stop) { + Position currPos = transformPixelToPosition(e.getPoint()); + + switch (mouseActionState) { + case DEFAULT_PRESS: + if (cursorMote == null) { + mouseActionState = MotesActionState.PANNING; + } + else { + mouseActionState = MotesActionState.MOVING; + // save start position + for (Mote m : selectedMotes) { + Position pos = m.getInterfaces().getPosition(); + moveStartPositions.put(m, new double[]{ + pos.getXCoordinate(), + pos.getYCoordinate(), + pos.getZCoordinate()}); + } + } + break; + case MOVING: + canvas.setCursor(MOVE_CURSOR); + for (Mote moveMote : selectedMotes) { + moveMote.getInterfaces().getPosition().setCoordinates( + moveStartPositions.get(moveMote)[0] + + (currPos.getXCoordinate() - pressedPos.getXCoordinate()), + moveStartPositions.get(moveMote)[1] + + (currPos.getYCoordinate() - pressedPos.getYCoordinate()), + moveStartPositions.get(moveMote)[2] + ); + repaint(); + } + break; + case PAN_PRESS: + mouseActionState = MotesActionState.PANNING; + break; + case PANNING: + /* The current mouse position should correspond to where panning started */ + viewportTransform.translate( + currPos.getXCoordinate() - pressedPos.getXCoordinate(), + currPos.getYCoordinate() - pressedPos.getYCoordinate() + ); + repaint(); + break; + case SELECT_PRESS: + mouseActionState = MotesActionState.SELECTING; + selection.setEnabled(true); + break; + case SELECTING: + int pressedX = transformToPixelX(pressedPos.getXCoordinate()); + int pressedY = transformToPixelY(pressedPos.getYCoordinate()); + int currX = transformToPixelX(currPos.getXCoordinate()); + int currY = transformToPixelY(currPos.getYCoordinate()); + int startX = pressedX < currX ? pressedX : currX; + int startY = pressedY < currY ? pressedY : currY; + int width = Math.abs(pressedX - currX); + int height = Math.abs(pressedY - currY); + + selection.setSelection(startX, startY, width, height); + selectedMotes.clear(); + selectedMotes.addAll(Arrays.asList(findMotesInRange(startX, startY, width, height))); + + repaint(); + break; } } - private void beginMoveRequest(Mote moteToMove, boolean withTiming, boolean confirm) { + private void handleMouseRelease(MouseEvent mouseEvent) { + + switch (mouseActionState) { + case PAN_PRESS: + // ignore + break; + case SELECT_PRESS: + if (cursorMote == null) { + /* Click on free canvas deselects all mote */ + selectedMotes.clear(); + } + else { + /* toggle selection for mote */ + if (selectedMotes.contains(cursorMote)) { + selectedMotes.remove(cursorMote); + } + else { + selectedMotes.add(cursorMote); + } + } + break; + case DEFAULT_PRESS: + if (cursorMote == null) { + /* Click on free canvas deselects all mote */ + selectedMotes.clear(); + } + else { + /* Click on mote selects single mote */ + selectedMotes.clear(); + selectedMotes.add(cursorMote); + } + break; + case MOVING: + /* Release stops moving */ + canvas.setCursor(Cursor.getDefaultCursor()); + break; + case SELECTING: + /* Release stops moving */ + selection.setEnabled(false); + repaint(); + break; + } + repaint(); + } + + private void beginMoveRequest(Mote motesToMove, boolean withTiming, boolean confirm) { if (withTiming) { moveStartTime = System.currentTimeMillis(); } else { moveStartTime = -1; } - moving = true; - moveConfirm = confirm; - movedMote = moteToMove; + mouseActionState = MotesActionState.DEFAULT_PRESS; + selectedMotes.clear(); + selectedMotes.add(motesToMove); repaint(); } @@ -888,107 +1029,37 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { repaint(); } - private void handleMouseMove(MouseEvent e, boolean stop) { - int x = e.getX(); - int y = e.getY(); - - /* Panning */ - if (panning) { - if (panningPosition == null || stop) { - panning = false; - return; + /** + * Returns all motes in rectangular range + * + * @param startX + * @param startY + * @param width + * @param height + * @return All motes in range + */ + public Mote[] findMotesInRange(int startX, int startY, int width, int height) { + List motes = new LinkedList<>(); + for (Mote m : simulation.getMotes()) { + Position pos = m.getInterfaces().getPosition(); + int moteX = transformToPixelX(pos.getXCoordinate()); + int moteY = transformToPixelY(pos.getYCoordinate()); + if (moteX > startX && moteX < startX + width + && moteY > startY && moteY < startY + height) { + motes.add(m); } - - /* The current mouse position should correspond to where panning started */ - Position moved = transformPixelToPosition(x,y); - viewportTransform.translate( - moved.getXCoordinate() - panningPosition.getXCoordinate(), - moved.getYCoordinate() - panningPosition.getYCoordinate() - ); - repaint(); - return; - } - - /* Zooming */ - if (zooming) { - if (zoomingPosition == null || zoomingPixel == null || stop) { - zooming = false; - return; - } - - /* The zooming start pixel should correspond to the zooming center position */ - /* The current mouse position should correspond to where panning started */ - double zoomFactor = 1.0 + Math.abs((double) zoomingPixel.y - y)/100.0; - double newZoom = (zoomingPixel.y - y)>0?zoomStart*zoomFactor: zoomStart/zoomFactor; - if (newZoom < 0.00001) { - newZoom = 0.00001; - } - viewportTransform.setToScale( - newZoom, - newZoom - ); - Position moved = transformPixelToPosition(zoomingPixel); - viewportTransform.translate( - moved.getXCoordinate() - zoomingPosition.getXCoordinate(), - moved.getYCoordinate() - zoomingPosition.getYCoordinate() - ); - repaint(); - return; - } - - /* Moving */ - if (moving) { - if(x != mouseDownPixel.x || y != mouseDownPixel.y) { - Position newPos = transformPixelToPosition(x, y); - - if (!stop) { - canvas.setCursor(moveCursor); - movedMote.getInterfaces().getPosition().setCoordinates( - newPos.getXCoordinate(), - newPos.getYCoordinate(), - movedMote.getInterfaces().getPosition().getZCoordinate() - ); - repaint(); - return; - } - /* Restore cursor */ - canvas.setCursor(Cursor.getDefaultCursor()); - - - /* Move mote */ - if (moveStartTime < 0 || System.currentTimeMillis() - moveStartTime > 300) { - if (moveConfirm) { - String options[] = {"Yes", "Cancel"}; - int returnValue = JOptionPane.showOptionDialog(Visualizer.this, - "Move mote to" + - "\nX=" + newPos.getXCoordinate() + - "\nY=" + newPos.getYCoordinate() + - "\nZ=" + movedMote.getInterfaces().getPosition().getZCoordinate(), - "Move mote?", - JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, - null, options, options[0]); - moving = returnValue == JOptionPane.YES_OPTION; - } - if (moving) { - movedMote.getInterfaces().getPosition().setCoordinates( - newPos.getXCoordinate(), - newPos.getYCoordinate(), - movedMote.getInterfaces().getPosition().getZCoordinate() - ); - repaint(); - } - } - } - - moving = false; - movedMote = null; - repaint(); } + Mote[] motesArr = new Mote[motes.size()]; + return motes.toArray(motesArr); } /** * Returns all motes at given position. * + * If multiple motes were found on at a position the motes are returned + * in the order they are painted on screen. + * First mote in array is the bottom mote, last mote is the top mote. + * * @param clickedX * X coordinate * @param clickedY @@ -1066,7 +1137,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { int x = pixelCoord.x; int y = pixelCoord.y; - if (mote == movedMote) { + if (mote == movedMotes) { g.setColor(MOVE_COLOR); g.fillOval(x - MOTE_RADIUS, y - MOTE_RADIUS, 2 * MOTE_RADIUS, 2 * MOTE_RADIUS); @@ -1292,8 +1363,8 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /** * @return Selected mote */ - public Mote getSelectedMote() { - return clickedMote; + public Set getSelectedMotes() { + return selectedMotes; } public Collection getConfigXML() { @@ -1585,4 +1656,44 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { "Multiple views can be active at the same time. " + "Use the View menu to select views. "; }; + + private class Selection { + + private int x; + private int y; + private int width; + private int height; + private boolean enable; + + public void setSelection(int x, int y, int width, int height) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + + public void setEnabled(boolean enable) { + this.enable = enable; + } + + public void drawSelection(Graphics g) { + /* only draw if enabled */ + if (!enable) { + return; + } + Graphics2D g2d = (Graphics2D) g; + g2d.setColor(new Color(64, 64, 64, 10)); + g2d.fillRect(x, y, width, height); + + BasicStroke dashed + = new BasicStroke(1.0f, + BasicStroke.CAP_BUTT, + BasicStroke.JOIN_MITER, + 10.0f, new float[]{5.0f}, 0.0f); + g2d.setColor(Color.BLACK); + g2d.setStroke(dashed); + g2d.drawRect(x, y, width, height); + } + } + } diff --git a/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java b/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java index 3a3176a91..ccd3481d1 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java @@ -34,6 +34,7 @@ import java.awt.Color; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Point; +import java.util.Set; import org.apache.log4j.Logger; @@ -74,21 +75,25 @@ public class DGRMVisualizerSkin implements VisualizerSkin { } public Color[] getColorOf(Mote mote) { - Mote selectedMote = visualizer.getSelectedMote(); - if (mote == selectedMote) { + if (visualizer.getSelectedMotes().contains(mote)) { return new Color[] { Color.CYAN }; } return null; } public void paintBeforeMotes(Graphics g) { - Mote selectedMote = visualizer.getSelectedMote(); + Set selectedMotes = visualizer.getSelectedMotes(); if (simulation == null - || selectedMote == null - || selectedMote.getInterfaces().getRadio() == null) { + || selectedMotes == null + || selectedMotes.isEmpty()) { return; } + final Mote selectedMote = visualizer.getSelectedMotes().iterator().next(); + if (selectedMote.getInterfaces().getRadio() == null) { + return; + } + /* Paint transmission and interference range for selected mote */ Position motePos = selectedMote.getInterfaces().getPosition(); diff --git a/tools/cooja/java/org/contikios/cooja/plugins/skins/UDGMVisualizerSkin.java b/tools/cooja/java/org/contikios/cooja/plugins/skins/UDGMVisualizerSkin.java index 26f66e3e7..bcffa0eb9 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/skins/UDGMVisualizerSkin.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/skins/UDGMVisualizerSkin.java @@ -36,6 +36,7 @@ import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Point; import java.beans.PropertyVetoException; +import java.util.Set; import javax.swing.BorderFactory; import javax.swing.Box; @@ -259,8 +260,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin { @Override public Color[] getColorOf(Mote mote) { - Mote selectedMote = visualizer.getSelectedMote(); - if (mote == selectedMote) { + if (visualizer.getSelectedMotes().contains(mote)) { return new Color[] { Color.CYAN }; } return null; @@ -268,10 +268,15 @@ public class UDGMVisualizerSkin implements VisualizerSkin { @Override public void paintBeforeMotes(Graphics g) { - Mote selectedMote = visualizer.getSelectedMote(); + Set selectedMotes = visualizer.getSelectedMotes(); if (simulation == null - || selectedMote == null - || selectedMote.getInterfaces().getRadio() == null) { + || selectedMotes == null + || selectedMotes.isEmpty()) { + return; + } + + final Mote selectedMote = visualizer.getSelectedMotes().iterator().next(); + if (selectedMote.getInterfaces().getRadio() == null) { return; } From ac61c459503eec208877d1d51cdbd2088c226417 Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Thu, 10 Apr 2014 18:24:14 +0200 Subject: [PATCH 2/9] [cooja] plugins/Visualizer: Updated quick help to match new control capabilities --- .../contikios/cooja/plugins/Visualizer.java | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java b/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java index 14ef496e2..6b36a0ebf 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java @@ -1646,16 +1646,20 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } public String getQuickHelp() { - return - "Network " + - "

The network window shows the positions of simulated motes. " + - "It is possible to zoom (CRTL+Mouse drag) and pan (Shift+Mouse drag) the current view. Motes can be moved by dragging them. " + - "Mouse right-click motes for options. " + - "

The network window supports different views. " + - "Each view provides some specific information, such as the IP addresses of motes. " + - "Multiple views can be active at the same time. " + - "Use the View menu to select views. "; - }; + return "Network " + + "

The network window shows the positions of simulated motes. " + + "

" + + "It is possible to zoom (Mouse wheel) and pan (Shift+Mouse drag) the current view. " + + "Motes can be moved by dragging them. " + + "You can add/remove motes to/from selection (CTRL+Left click) " + + "or use the rectangular selection tool (CTRL+Mouse drag). " + + "Mouse right-click motes for options menu. " + + "

" + + "The network window supports different views. " + + "Each view provides some specific information, such as the IP addresses of motes. " + + "Multiple views can be active at the same time. " + + "Use the View menu to select views. "; + } private class Selection { From e69b08f5fde5adcffb921553a22bafed16c51ac4 Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Thu, 10 Apr 2014 18:32:23 +0200 Subject: [PATCH 3/9] [cooja] plugins/skins: Adapted DGRM and UDGM visualizer skins to show adequate information for multi-selections --- .../plugins/skins/DGRMVisualizerSkin.java | 112 +++++----- .../plugins/skins/UDGMVisualizerSkin.java | 197 ++++++++++-------- 2 files changed, 166 insertions(+), 143 deletions(-) diff --git a/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java b/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java index ccd3481d1..76ed04ba7 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java @@ -83,66 +83,70 @@ public class DGRMVisualizerSkin implements VisualizerSkin { public void paintBeforeMotes(Graphics g) { Set selectedMotes = visualizer.getSelectedMotes(); - if (simulation == null - || selectedMotes == null - || selectedMotes.isEmpty()) { + if (simulation == null || selectedMotes == null) { return; } - final Mote selectedMote = visualizer.getSelectedMotes().iterator().next(); - if (selectedMote.getInterfaces().getRadio() == null) { - return; - } - - /* Paint transmission and interference range for selected mote */ - Position motePos = selectedMote.getInterfaces().getPosition(); - - Point pixelCoord = visualizer.transformPositionToPixel(motePos); - int x = pixelCoord.x; - int y = pixelCoord.y; - Radio selectedRadio = selectedMote.getInterfaces().getRadio(); - - FontMetrics fm = g.getFontMetrics(); - g.setColor(Color.BLACK); - - DirectedGraphMedium radioMedium = (DirectedGraphMedium) simulation.getRadioMedium(); - - /* Print transmission success probabilities */ - DestinationRadio[] dests = radioMedium.getPotentialDestinations(selectedRadio); - if (dests == null || dests.length == 0) { - String msg = "No edges"; - int msgWidth = fm.stringWidth(msg); - g.setColor(Color.BLACK); - g.drawString(msg, x - msgWidth/2, y + 2*Visualizer.MOTE_RADIUS + 3); - return; - } - String msg = dests.length + " edges"; - int msgWidth = fm.stringWidth(msg); - g.setColor(Color.BLACK); - g.drawString(msg, x - msgWidth/2, y + 2*Visualizer.MOTE_RADIUS + 3); - for (DestinationRadio r: dests) { - double prob = ((DGRMDestinationRadio)r).ratio; - double rssi = ((DGRMDestinationRadio)r).signal; - double pos_rssi = rssi + 100; - int lqi = ((DGRMDestinationRadio)r).lqi; - float red = (float)(1 - prob*pos_rssi/90*lqi/100); - if(red > 1) red = 1; - if(red < 0) red = 0; - float green = (float)(prob*pos_rssi/90*lqi/100); - if(green > 1) green = 1; - if(green < 0) green = 0; - if (prob == 0.0d) { + for (final Mote selectedMote : selectedMotes) { + if (selectedMote.getInterfaces().getRadio() == null) { continue; } - msg = String.format("%1.1f%%", 100.0*prob); - Position pos = r.radio.getPosition(); - Point pixel = visualizer.transformPositionToPixel(pos); - msgWidth = fm.stringWidth(msg); - g.setColor(new Color(red, green, 0.0f)); - g.drawString("LQI: " + lqi + " RSSI: " + rssi,(x + pixel.x)/2,(y + pixel.y)/2); - g.drawLine(x, y, pixel.x, pixel.y); + + /* Paint transmission and interference range for selected mote */ + Position motePos = selectedMote.getInterfaces().getPosition(); + + Point pixelCoord = visualizer.transformPositionToPixel(motePos); + int x = pixelCoord.x; + int y = pixelCoord.y; + Radio selectedRadio = selectedMote.getInterfaces().getRadio(); + + FontMetrics fm = g.getFontMetrics(); g.setColor(Color.BLACK); - g.drawString(msg, pixel.x - msgWidth/2, pixel.y + 2*Visualizer.MOTE_RADIUS + 3); + + DirectedGraphMedium radioMedium = (DirectedGraphMedium) simulation.getRadioMedium(); + + /* Print transmission success probabilities */ + DestinationRadio[] dests = radioMedium.getPotentialDestinations(selectedRadio); + if (dests == null || dests.length == 0) { + String msg = "No edges"; + int msgWidth = fm.stringWidth(msg); + g.setColor(Color.BLACK); + g.drawString(msg, x - msgWidth / 2, y + 2 * Visualizer.MOTE_RADIUS + 3); + continue; + } + String msg = dests.length + " edges"; + int msgWidth = fm.stringWidth(msg); + g.setColor(Color.BLACK); + g.drawString(msg, x - msgWidth / 2, y + 2 * Visualizer.MOTE_RADIUS + 3); + /* Draw LQI/RSSI edges */ + for (DestinationRadio r : dests) { + double prob = ((DGRMDestinationRadio)r).ratio; + double rssi = ((DGRMDestinationRadio)r).signal; + double pos_rssi = rssi + 100; + int lqi = ((DGRMDestinationRadio)r).lqi; + float red = (float)(1 - prob*pos_rssi/90*lqi/100); + if(red > 1) red = 1; + if(red < 0) red = 0; + float green = (float)(prob*pos_rssi/90*lqi/100); + if(green > 1) green = 1; + if(green < 0) green = 0; + if (prob == 0.0d) { + continue; + } + Position pos = r.radio.getPosition(); + Point pixel = visualizer.transformPositionToPixel(pos); + g.setColor(new Color(red, green, 0.0f)); + g.drawString("LQI: " + lqi, (x + pixel.x) / 2, (y + pixel.y) / 2); + g.drawString("RSSI: " + rssi, (x + pixel.x) / 2, (y + pixel.y) / 2 + g.getFontMetrics().getHeight()); + g.drawLine(x, y, pixel.x, pixel.y); + /* Draw success ratio only if single mote selected */ + if (selectedMotes.size() == 1) { + g.setColor(Color.BLACK); + msg = String.format("%1.1f%%", 100.0 * prob); + msgWidth = fm.stringWidth(msg); + g.drawString(msg, pixel.x - msgWidth/2, pixel.y + 2*Visualizer.MOTE_RADIUS + 3); + } + } } } diff --git a/tools/cooja/java/org/contikios/cooja/plugins/skins/UDGMVisualizerSkin.java b/tools/cooja/java/org/contikios/cooja/plugins/skins/UDGMVisualizerSkin.java index bcffa0eb9..4c0a18b0f 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/skins/UDGMVisualizerSkin.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/skins/UDGMVisualizerSkin.java @@ -34,7 +34,10 @@ import java.awt.BorderLayout; import java.awt.Color; import java.awt.FontMetrics; import java.awt.Graphics; +import java.awt.Graphics2D; import java.awt.Point; +import java.awt.geom.Area; +import java.awt.geom.Ellipse2D; import java.beans.PropertyVetoException; import java.util.Set; @@ -269,104 +272,120 @@ public class UDGMVisualizerSkin implements VisualizerSkin { @Override public void paintBeforeMotes(Graphics g) { Set selectedMotes = visualizer.getSelectedMotes(); - if (simulation == null - || selectedMotes == null - || selectedMotes.isEmpty()) { + if (simulation == null || selectedMotes == null) { return; } - final Mote selectedMote = visualizer.getSelectedMotes().iterator().next(); - if (selectedMote.getInterfaces().getRadio() == null) { - return; + Area intRangeArea = new Area(); + Area intRangeMaxArea = new Area(); + Area trxRangeArea = new Area(); + Area trxRangeMaxArea = new Area(); + + for (Mote selectedMote : selectedMotes) { + if (selectedMote.getInterfaces().getRadio() == null) { + continue; + } + + /* Paint transmission and interference range for selected mote */ + Position motePos = selectedMote.getInterfaces().getPosition(); + + Point pixelCoord = visualizer.transformPositionToPixel(motePos); + int x = pixelCoord.x; + int y = pixelCoord.y; + + // Fetch current output power indicator (scale with as percent) + Radio selectedRadio = selectedMote.getInterfaces().getRadio(); + double moteInterferenceRange + = radioMedium.INTERFERENCE_RANGE + * ((double) selectedRadio.getCurrentOutputPowerIndicator() + / (double) selectedRadio.getOutputPowerIndicatorMax()); + double moteTransmissionRange + = radioMedium.TRANSMITTING_RANGE + * ((double) selectedRadio.getCurrentOutputPowerIndicator() + / (double) selectedRadio.getOutputPowerIndicatorMax()); + + Point translatedZero = visualizer.transformPositionToPixel(0.0, 0.0, 0.0); + Point translatedInterference + = visualizer.transformPositionToPixel(moteInterferenceRange, moteInterferenceRange, 0.0); + Point translatedTransmission + = visualizer.transformPositionToPixel(moteTransmissionRange, moteTransmissionRange, 0.0); + Point translatedInterferenceMax + = visualizer.transformPositionToPixel(radioMedium.INTERFERENCE_RANGE, radioMedium.INTERFERENCE_RANGE, 0.0); + Point translatedTransmissionMax + = visualizer.transformPositionToPixel(radioMedium.TRANSMITTING_RANGE, radioMedium.TRANSMITTING_RANGE, 0.0); + + translatedInterference.x = Math.abs(translatedInterference.x - translatedZero.x); + translatedInterference.y = Math.abs(translatedInterference.y - translatedZero.y); + translatedTransmission.x = Math.abs(translatedTransmission.x - translatedZero.x); + translatedTransmission.y = Math.abs(translatedTransmission.y - translatedZero.y); + translatedInterferenceMax.x = Math.abs(translatedInterferenceMax.x - translatedZero.x); + translatedInterferenceMax.y = Math.abs(translatedInterferenceMax.y - translatedZero.y); + translatedTransmissionMax.x = Math.abs(translatedTransmissionMax.x - translatedZero.x); + translatedTransmissionMax.y = Math.abs(translatedTransmissionMax.y - translatedZero.y); + + /* Interference range */ + intRangeArea.add(new Area(new Ellipse2D.Double( + x - translatedInterference.x, + y - translatedInterference.y, + 2 * translatedInterference.x, + 2 * translatedInterference.y))); + + /* Interference range (MAX) */ + trxRangeArea.add(new Area(new Ellipse2D.Double( + x - translatedTransmission.x, + y - translatedTransmission.y, + 2 * translatedTransmission.x, + 2 * translatedTransmission.y))); + + intRangeMaxArea.add(new Area(new Ellipse2D.Double( + x - translatedInterferenceMax.x, + y - translatedInterferenceMax.y, + 2 * translatedInterferenceMax.x, + 2 * translatedInterferenceMax.y))); + + /* Transmission range (MAX) */ + trxRangeMaxArea.add(new Area(new Ellipse2D.Double( + x - translatedTransmissionMax.x, + y - translatedTransmissionMax.y, + 2 * translatedTransmissionMax.x, + 2 * translatedTransmissionMax.y))); + } - - /* Paint transmission and interference range for selected mote */ - Position motePos = selectedMote.getInterfaces().getPosition(); - - Point pixelCoord = visualizer.transformPositionToPixel(motePos); - int x = pixelCoord.x; - int y = pixelCoord.y; - - // Fetch current output power indicator (scale with as percent) - Radio selectedRadio = selectedMote.getInterfaces().getRadio(); - double moteInterferenceRange = - radioMedium.INTERFERENCE_RANGE - * ((double) selectedRadio.getCurrentOutputPowerIndicator() - / (double) selectedRadio.getOutputPowerIndicatorMax()); - double moteTransmissionRange = - radioMedium.TRANSMITTING_RANGE - * ((double) selectedRadio.getCurrentOutputPowerIndicator() - / (double) selectedRadio.getOutputPowerIndicatorMax()); - - Point translatedZero = visualizer.transformPositionToPixel(0.0, 0.0, 0.0); - Point translatedInterference = - visualizer.transformPositionToPixel(moteInterferenceRange, moteInterferenceRange, 0.0); - Point translatedTransmission = - visualizer.transformPositionToPixel(moteTransmissionRange, moteTransmissionRange, 0.0); - Point translatedInterferenceMax = - visualizer.transformPositionToPixel(radioMedium.INTERFERENCE_RANGE, radioMedium.INTERFERENCE_RANGE, 0.0); - Point translatedTransmissionMax = - visualizer.transformPositionToPixel(radioMedium.TRANSMITTING_RANGE, radioMedium.TRANSMITTING_RANGE, 0.0); - - translatedInterference.x = Math.abs(translatedInterference.x - translatedZero.x); - translatedInterference.y = Math.abs(translatedInterference.y - translatedZero.y); - translatedTransmission.x = Math.abs(translatedTransmission.x - translatedZero.x); - translatedTransmission.y = Math.abs(translatedTransmission.y - translatedZero.y); - translatedInterferenceMax.x = Math.abs(translatedInterferenceMax.x - translatedZero.x); - translatedInterferenceMax.y = Math.abs(translatedInterferenceMax.y - translatedZero.y); - translatedTransmissionMax.x = Math.abs(translatedTransmissionMax.x - translatedZero.x); - translatedTransmissionMax.y = Math.abs(translatedTransmissionMax.y - translatedZero.y); - - /* Interference range */ - g.setColor(COLOR_INT); - g.fillOval( - x - translatedInterference.x, - y - translatedInterference.y, - 2 * translatedInterference.x, - 2 * translatedInterference.y); - - /* Transmission range */ - g.setColor(COLOR_TX); - g.fillOval( - x - translatedTransmission.x, - y - translatedTransmission.y, - 2 * translatedTransmission.x, - 2 * translatedTransmission.y); - - /* Interference range (MAX) */ + + Graphics2D g2d = (Graphics2D) g; + + g2d.setColor(COLOR_INT); + g2d.fill(intRangeArea); g.setColor(Color.GRAY); - g.drawOval( - x - translatedInterferenceMax.x, - y - translatedInterferenceMax.y, - 2 * translatedInterferenceMax.x, - 2 * translatedInterferenceMax.y); - - /* Transmission range (MAX) */ - g.drawOval( - x - translatedTransmissionMax.x, - y - translatedTransmissionMax.y, - 2 * translatedTransmissionMax.x, - 2 * translatedTransmissionMax.y); - + g2d.draw(intRangeMaxArea); + + g.setColor(COLOR_TX); + g2d.fill(trxRangeArea); + g.setColor(Color.GRAY); + g2d.draw(trxRangeMaxArea); FontMetrics fm = g.getFontMetrics(); g.setColor(Color.BLACK); - /* Print transmission success probabilities */ - for (Mote m: simulation.getMotes()) { - if (m == selectedMote) { - continue; - } - double prob = - ((UDGM) simulation.getRadioMedium()).getSuccessProbability(selectedRadio, m.getInterfaces().getRadio()); - if (prob == 0.0d) { - continue; - } - String msg = (((int)(1000*prob))/10.0) + "%"; - Position pos = m.getInterfaces().getPosition(); - Point pixel = visualizer.transformPositionToPixel(pos); - int msgWidth = fm.stringWidth(msg); - g.drawString(msg, pixel.x - msgWidth/2, pixel.y + 2*Visualizer.MOTE_RADIUS + 3); + /* Print transmission success probabilities only if single mote is selected */ + if (selectedMotes.size() == 1) { + Mote selectedMote = selectedMotes.toArray(new Mote[0])[0]; + Radio selectedRadio = selectedMote.getInterfaces().getRadio(); + for (Mote m: simulation.getMotes()) { + if (m == selectedMote) { + continue; + } + double prob = + ((UDGM) simulation.getRadioMedium()).getSuccessProbability(selectedRadio, m.getInterfaces().getRadio()); + if (prob == 0.0d) { + continue; + } + String msg = (((int)(1000*prob))/10.0) + "%"; + Position pos = m.getInterfaces().getPosition(); + Point pixel = visualizer.transformPositionToPixel(pos); + int msgWidth = fm.stringWidth(msg); + g.drawString(msg, pixel.x - msgWidth/2, pixel.y + 2*Visualizer.MOTE_RADIUS + 3); + } } } From 8e648bcecee474e73c9cbd6aa6082c2b0622175b Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Thu, 10 Apr 2014 18:33:26 +0200 Subject: [PATCH 4/9] [cooja] apps/mrm: Adapted MRM visualizer skin to show adequate information for multi-selections --- .../org/contikios/mrm/MRMVisualizerSkin.java | 120 +++++++++--------- 1 file changed, 60 insertions(+), 60 deletions(-) diff --git a/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java b/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java index 22714dc6d..58f1fab1a 100644 --- a/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java +++ b/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java @@ -81,77 +81,77 @@ public class MRMVisualizerSkin implements VisualizerSkin { public void paintBeforeMotes(Graphics g) { Set selectedMotes = visualizer.getSelectedMotes(); - if (simulation == null || selectedMotes == null || selectedMotes.isEmpty()) { + if (simulation == null || selectedMotes == null) { return; } - - final Mote selectedMote = visualizer.getSelectedMotes().iterator().next(); - if (selectedMote.getInterfaces().getRadio() == null) { - return; - } - - final Position sPos = selectedMote.getInterfaces().getPosition(); - /* Paint transmission and interference range for selected mote */ - Position motePos = selectedMote.getInterfaces().getPosition(); + for (final Mote selectedMote : selectedMotes) { + if (selectedMote.getInterfaces().getRadio() == null) { + continue; + } + final Position sPos = selectedMote.getInterfaces().getPosition(); - Point pixelCoord = visualizer.transformPositionToPixel(motePos); - int x = pixelCoord.x; - int y = pixelCoord.y; + /* Paint transmission and interference range for selected mote */ + Position motePos = selectedMote.getInterfaces().getPosition(); - FontMetrics fm = g.getFontMetrics(); - g.setColor(Color.BLACK); + Point pixelCoord = visualizer.transformPositionToPixel(motePos); + int x = pixelCoord.x; + int y = pixelCoord.y; - MRM radioMedium = (MRM) simulation.getRadioMedium(); + FontMetrics fm = g.getFontMetrics(); + g.setColor(Color.BLACK); - /* Print transmission success probabilities */ - Mote[] dests = simulation.getMotes(); - if (dests == null || dests.length == 0) { - String msg = "No edges"; + MRM radioMedium = (MRM) simulation.getRadioMedium(); + + /* Print transmission success probabilities */ + Mote[] dests = simulation.getMotes(); + if (dests == null || dests.length == 0) { + String msg = "No edges"; + int msgWidth = fm.stringWidth(msg); + g.setColor(Color.BLACK); + g.drawString(msg, x - msgWidth / 2, y + 2 * Visualizer.MOTE_RADIUS + 3); + return; + } + g.setColor(Color.BLACK); + int edges = 0; + for (Mote d : dests) { + if (d == selectedMote) { + continue; + } + final Radio dRadio = d.getInterfaces().getRadio(); + TxPair txPair = new RadioPair() { + public Radio getFromRadio() { + return selectedMote.getInterfaces().getRadio(); + } + public Radio getToRadio() { + return dRadio; + } + }; + double probArr[] = radioMedium.getChannelModel().getProbability( + txPair, + Double.NEGATIVE_INFINITY + ); + double prob = probArr[0]; + double ss = probArr[1]; + + if (prob == 0.0d) { + continue; + } + edges++; + String msg = String.format("%1.1f%%, %1.2fdB", 100.0*prob, ss); + Point pixel = visualizer.transformPositionToPixel(d.getInterfaces().getPosition()); + int msgWidth = fm.stringWidth(msg); + g.setColor(new Color(1-(float)prob, (float)prob, 0.0f)); + g.drawLine(x, y, pixel.x, pixel.y); + g.setColor(Color.BLACK); + g.drawString(msg, pixel.x - msgWidth/2, pixel.y + 2*Visualizer.MOTE_RADIUS + 3); + } + + String msg = dests.length + " edges"; int msgWidth = fm.stringWidth(msg); g.setColor(Color.BLACK); g.drawString(msg, x - msgWidth/2, y + 2*Visualizer.MOTE_RADIUS + 3); - return; } - g.setColor(Color.BLACK); - int edges = 0; - for (Mote d: dests) { - if (d == selectedMote) { - continue; - } - final Radio dRadio = d.getInterfaces().getRadio(); - TxPair txPair = new RadioPair() { - public Radio getFromRadio() { - return selectedMote.getInterfaces().getRadio(); - } - public Radio getToRadio() { - return dRadio; - } - }; - double probArr[] = radioMedium.getChannelModel().getProbability( - txPair, - Double.NEGATIVE_INFINITY - ); - double prob = probArr[0]; - double ss = probArr[1]; - - if (prob == 0.0d) { - continue; - } - edges++; - String msg = String.format("%1.1f%%, %1.2fdB", 100.0*prob, ss); - Point pixel = visualizer.transformPositionToPixel(d.getInterfaces().getPosition()); - int msgWidth = fm.stringWidth(msg); - g.setColor(new Color(1-(float)prob, (float)prob, 0.0f)); - g.drawLine(x, y, pixel.x, pixel.y); - g.setColor(Color.BLACK); - g.drawString(msg, pixel.x - msgWidth/2, pixel.y + 2*Visualizer.MOTE_RADIUS + 3); - } - - String msg = dests.length + " edges"; - int msgWidth = fm.stringWidth(msg); - g.setColor(Color.BLACK); - g.drawString(msg, x - msgWidth/2, y + 2*Visualizer.MOTE_RADIUS + 3); } public void paintAfterMotes(Graphics g) { From 6e9b0e1497f9ee43a466e65144d1f020846deea6 Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Thu, 10 Apr 2014 18:39:13 +0200 Subject: [PATCH 5/9] [cooja] Added @Override annotations for Visualizer and skins --- .../org/contikios/mrm/MRMVisualizerSkin.java | 8 +++ .../contikios/cooja/plugins/Visualizer.java | 56 +++++++++++++++++++ .../plugins/skins/DGRMVisualizerSkin.java | 6 ++ 3 files changed, 70 insertions(+) diff --git a/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java b/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java index 58f1fab1a..12c27187d 100644 --- a/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java +++ b/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java @@ -56,6 +56,7 @@ public class MRMVisualizerSkin implements VisualizerSkin { private Simulation simulation = null; private Visualizer visualizer = null; + @Override public void setActive(Simulation simulation, Visualizer vis) { if (!(simulation.getRadioMedium() instanceof MRM)) { logger.fatal("Cannot activate MRM skin for unknown radio medium: " + simulation.getRadioMedium()); @@ -65,6 +66,7 @@ public class MRMVisualizerSkin implements VisualizerSkin { this.visualizer = vis; } + @Override public void setInactive() { if (simulation == null) { /* Skin was never activated */ @@ -72,6 +74,7 @@ public class MRMVisualizerSkin implements VisualizerSkin { } } + @Override public Color[] getColorOf(Mote mote) { if (visualizer.getSelectedMotes().contains(mote)) { return new Color[] { Color.CYAN }; @@ -79,6 +82,7 @@ public class MRMVisualizerSkin implements VisualizerSkin { return null; } + @Override public void paintBeforeMotes(Graphics g) { Set selectedMotes = visualizer.getSelectedMotes(); if (simulation == null || selectedMotes == null) { @@ -120,9 +124,11 @@ public class MRMVisualizerSkin implements VisualizerSkin { } final Radio dRadio = d.getInterfaces().getRadio(); TxPair txPair = new RadioPair() { + @Override public Radio getFromRadio() { return selectedMote.getInterfaces().getRadio(); } + @Override public Radio getToRadio() { return dRadio; } @@ -154,9 +160,11 @@ public class MRMVisualizerSkin implements VisualizerSkin { } } + @Override public void paintAfterMotes(Graphics g) { } + @Override public Visualizer getVisualizer() { return visualizer; } diff --git a/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java b/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java index 6b36a0ebf..86df645d6 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java @@ -249,12 +249,15 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { viewMenu = new JMenu("View"); viewMenu.addMenuListener(new MenuListener() { + @Override public void menuSelected(MenuEvent e) { viewMenu.removeAll(); populateSkinMenu(viewMenu); } + @Override public void menuDeselected(MenuEvent e) { } + @Override public void menuCanceled(MenuEvent e) { } }); @@ -266,6 +269,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { this.setJMenuBar(menuBar); Action zoomInAction = new AbstractAction("Zoom in") { + @Override public void actionPerformed(ActionEvent e) { zoomToFactor(zoomFactor() * 1.2); } @@ -278,6 +282,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { zoomMenu.add(zoomInItem); Action zoomOutAction = new AbstractAction("Zoom out") { + @Override public void actionPerformed(ActionEvent e) { zoomToFactor(zoomFactor() / 1.2); } @@ -291,6 +296,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { JMenuItem resetViewportItem = new JMenuItem("Reset viewport"); resetViewportItem.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { resetViewport = 1; repaint(); @@ -302,6 +308,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Main canvas */ canvas = new JPanel() { private static final long serialVersionUID = 1L; + @Override public void paintComponent(Graphics g) { super.paintComponent(g); @@ -328,16 +335,19 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Observe simulation and mote positions */ posObserver = new Observer() { + @Override public void update(Observable obs, Object obj) { repaint(); } }; simulation.getEventCentral().addMoteCountListener(newMotesListener = new MoteCountListener() { + @Override public void moteWasAdded(Mote mote) { Position pos = mote.getInterfaces().getPosition(); if (pos != null) { pos.addObserver(posObserver); SwingUtilities.invokeLater(new Runnable() { + @Override public void run() { resetViewport = 1; repaint(); @@ -345,6 +355,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }); } } + @Override public void moteWasRemoved(Mote mote) { Position pos = mote.getInterfaces().getPosition(); if (pos != null) { @@ -362,6 +373,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Observe mote highlights */ gui.addMoteHighlightObserver(moteHighligtObserver = new Observer() { + @Override public void update(Observable obs, Object obj) { if (!(obj instanceof Mote)) { return; @@ -370,6 +382,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { final Timer timer = new Timer(100, null); final Mote mote = (Mote) obj; timer.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { /* Count down */ if (timer.getDelay() < 90) { @@ -395,6 +408,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Observe mote relations */ gui.addMoteRelationsObserver(moteRelationsObserver = new Observer() { + @Override public void update(Observable obs, Object obj) { repaint(); } @@ -408,6 +422,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } }); canvas.addMouseListener(new MouseAdapter() { + @Override public void mousePressed(MouseEvent e) { if (e.isPopupTrigger()) { handlePopupRequest(e.getPoint()); @@ -418,6 +433,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { handleMousePress(e); } } + @Override public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { handlePopupRequest(e.getPoint()); @@ -430,6 +446,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } }); canvas.addMouseWheelListener(new MouseWheelListener() { + @Override public void mouseWheelMoved(MouseWheelEvent mwe) { int x = mwe.getX(); int y = mwe.getY(); @@ -457,6 +474,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Drag and drop files to motes */ DropTargetListener dTargetListener = new DropTargetListener() { + @Override public void dragEnter(DropTargetDragEvent dtde) { if (acceptOrRejectDrag(dtde)) { dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); @@ -464,8 +482,10 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { dtde.rejectDrag(); } } + @Override public void dragExit(DropTargetEvent dte) { } + @Override public void dropActionChanged(DropTargetDragEvent dtde) { if (acceptOrRejectDrag(dtde)) { dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); @@ -473,6 +493,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { dtde.rejectDrag(); } } + @Override public void dragOver(DropTargetDragEvent dtde) { if (acceptOrRejectDrag(dtde)) { dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); @@ -480,6 +501,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { dtde.rejectDrag(); } } + @Override public void drop(DropTargetDropEvent dtde) { Transferable transferable = dtde.getTransferable(); @@ -589,6 +611,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { repaint(); } + @Override public void startPlugin() { super.startPlugin(); if (loadedConfig) { @@ -677,6 +700,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { if (menuAction.isEnabled(this, mote)) { JMenuItem menuItem = new JMenuItem(menuAction.getDescription(this, mote)); menuItem.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { menuAction.doAction(Visualizer.this, mote); } @@ -700,6 +724,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { if (menuAction.isEnabled(this, simulation)) { JMenuItem menuItem = new JMenuItem(menuAction.getDescription(this, simulation)); menuItem.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { menuAction.doAction(Visualizer.this, simulation); } @@ -735,6 +760,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Mote-to-mote relations */ JCheckBoxMenuItem moteRelationsItem = new JCheckBoxMenuItem("Mote relations", showMoteToMoteRelations); moteRelationsItem.addItemListener(new ItemListener() { + @Override public void itemStateChanged(ItemEvent e) { JCheckBoxMenuItem menuItem = ((JCheckBoxMenuItem)e.getItem()); showMoteToMoteRelations = menuItem.isSelected(); @@ -769,6 +795,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } item.addItemListener(new ItemListener() { + @Override public void itemStateChanged(ItemEvent e) { JCheckBoxMenuItem menuItem = ((JCheckBoxMenuItem)e.getItem()); if (menuItem == null) { @@ -1248,6 +1275,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Center visible motes */ final double smallXfinal = smallX, bigXfinal = bigX, smallYfinal = smallY, bigYfinal = bigY; SwingUtilities.invokeLater(new Runnable() { + @Override public void run() { Position viewMid = transformPixelToPosition(canvas.getWidth()/2, canvas.getHeight()/2); @@ -1331,6 +1359,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { return (y - viewportTransform.getTranslateY())/viewportTransform.getScaleY() ; } + @Override public void closePlugin() { for (VisualizerSkin skin: currentSkins) { skin.setInactive(); @@ -1367,6 +1396,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { return selectedMotes; } + @Override public Collection getConfigXML() { ArrayList config = new ArrayList(); Element element; @@ -1411,6 +1441,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { return config; } + @Override public boolean setConfigXML(Collection configXML, boolean visAvailable) { loadedConfig = true; showMoteToMoteRelations = false; @@ -1429,6 +1460,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { || wanted.equals(Cooja.getDescriptionOf(skinClass))) { final Class skin = skinClass; SwingUtilities.invokeLater(new Runnable() { + @Override public void run() { generateAndActivateSkin(skin); } @@ -1467,6 +1499,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } private AbstractAction makeSkinsDefaultAction = new AbstractAction() { + @Override public void actionPerformed(ActionEvent e) { StringBuilder sb = new StringBuilder(); for (VisualizerSkin skin: currentSkins) { @@ -1480,37 +1513,46 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }; protected static class ButtonClickMoteMenuAction implements MoteMenuAction { + @Override public boolean isEnabled(Visualizer visualizer, Mote mote) { return mote.getInterfaces().getButton() != null && !mote.getInterfaces().getButton().isPressed(); } + @Override public String getDescription(Visualizer visualizer, Mote mote) { return "Click button on " + mote; } + @Override public void doAction(Visualizer visualizer, Mote mote) { mote.getInterfaces().getButton().clickButton(); } }; protected static class DeleteMoteMenuAction implements MoteMenuAction { + @Override public boolean isEnabled(Visualizer visualizer, Mote mote) { return true; } + @Override public String getDescription(Visualizer visualizer, Mote mote) { return "Delete " + mote; } + @Override public void doAction(Visualizer visualizer, Mote mote) { mote.getSimulation().removeMote(mote); } }; protected static class ShowLEDMoteMenuAction implements MoteMenuAction { + @Override public boolean isEnabled(Visualizer visualizer, Mote mote) { return mote.getInterfaces().getLED() != null; } + @Override public String getDescription(Visualizer visualizer, Mote mote) { return "Show LEDs on " + mote; } + @Override public void doAction(Visualizer visualizer, Mote mote) { Simulation simulation = mote.getSimulation(); LED led = mote.getInterfaces().getLED(); @@ -1536,6 +1578,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }; protected static class ShowSerialMoteMenuAction implements MoteMenuAction { + @Override public boolean isEnabled(Visualizer visualizer, Mote mote) { for (MoteInterface intf: mote.getInterfaces().getInterfaces()) { if (intf instanceof SerialPort) { @@ -1544,9 +1587,11 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } return false; } + @Override public String getDescription(Visualizer visualizer, Mote mote) { return "Show serial port on " + mote; } + @Override public void doAction(Visualizer visualizer, Mote mote) { Simulation simulation = mote.getSimulation(); SerialPort serialPort = null; @@ -1579,31 +1624,38 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }; protected static class MoveMoteMenuAction implements MoteMenuAction { + @Override public boolean isEnabled(Visualizer visualizer, Mote mote) { return true; } + @Override public String getDescription(Visualizer visualizer, Mote mote) { return "Move " + mote; } + @Override public void doAction(Visualizer visualizer, Mote mote) { visualizer.beginMoveRequest(mote, false, false); } }; protected static class ResetViewportAction implements SimulationMenuAction { + @Override public void doAction(Visualizer visualizer, Simulation simulation) { visualizer.resetViewport = 1; visualizer.repaint(); } + @Override public String getDescription(Visualizer visualizer, Simulation simulation) { return "Reset viewport"; } + @Override public boolean isEnabled(Visualizer visualizer, Simulation simulation) { return true; } }; protected static class ToggleDecorationsMenuAction implements SimulationMenuAction { + @Override public void doAction(final Visualizer visualizer, Simulation simulation) { if (!(visualizer.getUI() instanceof BasicInternalFrameUI)) { return; @@ -1620,11 +1672,13 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } visualizer.revalidate(); SwingUtilities.invokeLater(new Runnable() { + @Override public void run() { visualizer.repaint(); } }); } + @Override public String getDescription(Visualizer visualizer, Simulation simulation) { if (!(visualizer.getUI() instanceof BasicInternalFrameUI)) { return "Hide window decorations"; @@ -1637,6 +1691,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } return "Hide window decorations"; } + @Override public boolean isEnabled(Visualizer visualizer, Simulation simulation) { if (!(visualizer.getUI() instanceof BasicInternalFrameUI)) { return false; @@ -1645,6 +1700,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } } + @Override public String getQuickHelp() { return "Network " + "

The network window shows the positions of simulated motes. " diff --git a/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java b/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java index 76ed04ba7..ef8c19435 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java @@ -58,6 +58,7 @@ public class DGRMVisualizerSkin implements VisualizerSkin { private Simulation simulation = null; private Visualizer visualizer = null; + @Override public void setActive(Simulation simulation, Visualizer vis) { if (!(simulation.getRadioMedium() instanceof DirectedGraphMedium)) { logger.fatal("Cannot activate DGRM skin for unknown radio medium: " + simulation.getRadioMedium()); @@ -67,6 +68,7 @@ public class DGRMVisualizerSkin implements VisualizerSkin { this.visualizer = vis; } + @Override public void setInactive() { if (simulation == null) { /* Skin was never activated */ @@ -74,6 +76,7 @@ public class DGRMVisualizerSkin implements VisualizerSkin { } } + @Override public Color[] getColorOf(Mote mote) { if (visualizer.getSelectedMotes().contains(mote)) { return new Color[] { Color.CYAN }; @@ -81,6 +84,7 @@ public class DGRMVisualizerSkin implements VisualizerSkin { return null; } + @Override public void paintBeforeMotes(Graphics g) { Set selectedMotes = visualizer.getSelectedMotes(); if (simulation == null || selectedMotes == null) { @@ -150,9 +154,11 @@ public class DGRMVisualizerSkin implements VisualizerSkin { } } + @Override public void paintAfterMotes(Graphics g) { } + @Override public Visualizer getVisualizer() { return visualizer; } From 84b687efeb7a9756cd6ba53039a1db15ae0203a9 Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Thu, 10 Apr 2014 18:50:04 +0200 Subject: [PATCH 6/9] [cooja] Automated code updates that do not make any functional changes - made loggers final - added some diamond interface cosmetic - shortened if-return statements - replaced size == 0 check with .isEmpty() --- .../org/contikios/mrm/MRMVisualizerSkin.java | 2 +- .../contikios/cooja/plugins/Visualizer.java | 30 ++++++++----------- .../plugins/skins/DGRMVisualizerSkin.java | 2 +- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java b/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java index 12c27187d..3a9ab4ab2 100644 --- a/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java +++ b/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java @@ -51,7 +51,7 @@ import org.contikios.mrm.ChannelModel.TxPair; @ClassDescription("Radio environment (MRM)") @SupportedArguments(radioMediums = {MRM.class}) public class MRMVisualizerSkin implements VisualizerSkin { - private static Logger logger = Logger.getLogger(MRMVisualizerSkin.class); + private static final Logger logger = Logger.getLogger(MRMVisualizerSkin.class); private Simulation simulation = null; private Visualizer visualizer = null; diff --git a/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java b/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java index 86df645d6..b49d12d79 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java @@ -140,7 +140,7 @@ import org.contikios.cooja.plugins.skins.UDGMVisualizerSkin; @PluginType(PluginType.SIM_STANDARD_PLUGIN) public class Visualizer extends VisPlugin implements HasQuickHelp { private static final long serialVersionUID = 1L; - private static Logger logger = Logger.getLogger(Visualizer.class); + private static final Logger logger = Logger.getLogger(Visualizer.class); public static final int MOTE_RADIUS = 8; private static final Color[] DEFAULT_MOTE_COLORS = { Color.WHITE }; @@ -186,8 +186,8 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { private Selection selection; /* Visualizers */ - private static ArrayList> visualizerSkins = - new ArrayList>(); + private static final ArrayList> visualizerSkins = + new ArrayList<>(); static { /* Register default visualizer skins */ registerVisualizerSkin(IDVisualizerSkin.class); @@ -200,13 +200,13 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { registerVisualizerSkin(MoteTypeVisualizerSkin.class); registerVisualizerSkin(AttributeVisualizerSkin.class); } - private ArrayList currentSkins = new ArrayList(); + private ArrayList currentSkins = new ArrayList<>(); /* Generic visualization */ private MoteCountListener newMotesListener; private Observer posObserver = null; private Observer moteHighligtObserver = null; - private ArrayList highlightedMotes = new ArrayList(); + private ArrayList highlightedMotes = new ArrayList<>(); private final static Color HIGHLIGHT_COLOR = Color.CYAN; private final static Color MOVE_COLOR = Color.WHITE; private Observer moteRelationsObserver = null; @@ -224,9 +224,9 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } private ArrayList> simulationMenuActions = - new ArrayList>(); + new ArrayList<>(); private ArrayList> moteMenuActions = - new ArrayList>(); + new ArrayList<>(); public Visualizer(Simulation simulation, Cooja gui) { super("Network", gui); @@ -859,10 +859,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } } } - if (!showMenuItem) { - return false; - } - return true; + return showMenuItem; } private void handleMousePress(MouseEvent mouseEvent) { @@ -1097,7 +1094,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { double xCoord = transformToPositionX(clickedX); double yCoord = transformToPositionY(clickedY); - ArrayList motes = new ArrayList(); + ArrayList motes = new ArrayList<>(); // Calculate painted mote radius in coordinates double paintedMoteWidth = transformToPositionX(MOTE_RADIUS) @@ -1118,7 +1115,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { motes.add(simulation.getMote(i)); } } - if (motes.size() == 0) { + if (motes.isEmpty()) { return null; } @@ -1398,7 +1395,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { @Override public Collection getConfigXML() { - ArrayList config = new ArrayList(); + ArrayList config = new ArrayList<>(); Element element; /* Show mote-to-mote relations */ @@ -1693,10 +1690,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } @Override public boolean isEnabled(Visualizer visualizer, Simulation simulation) { - if (!(visualizer.getUI() instanceof BasicInternalFrameUI)) { - return false; - } - return true; + return visualizer.getUI() instanceof BasicInternalFrameUI; } } diff --git a/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java b/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java index ef8c19435..7d8bf882b 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java @@ -53,7 +53,7 @@ import org.contikios.cooja.radiomediums.DirectedGraphMedium; @ClassDescription("Radio environment (DGRM)") @SupportedArguments(radioMediums = {DirectedGraphMedium.class}) public class DGRMVisualizerSkin implements VisualizerSkin { - private static Logger logger = Logger.getLogger(DGRMVisualizerSkin.class); + private static final Logger logger = Logger.getLogger(DGRMVisualizerSkin.class); private Simulation simulation = null; private Visualizer visualizer = null; From ea979fa027becaae562bb10282be2161db09463a Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Thu, 10 Apr 2014 19:00:24 +0200 Subject: [PATCH 7/9] [cooja] plugins/Visualizer: Use more specific catches and multi-catches if possible --- .../contikios/cooja/plugins/Visualizer.java | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java b/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java index b49d12d79..45654955f 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java @@ -527,7 +527,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } file = list.get(0); } - catch (Exception e) { + catch (UnsupportedFlavorException | IOException e) { return; } @@ -564,9 +564,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { return false; } file = list.get(0); - } catch (UnsupportedFlavorException e) { - return false; - } catch (IOException e) { + } catch (UnsupportedFlavorException | IOException e) { return false; } @@ -603,9 +601,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { VisualizerSkin newSkin = skinClass.newInstance(); newSkin.setActive(Visualizer.this.simulation, Visualizer.this); currentSkins.add(0, newSkin); - } catch (InstantiationException e1) { - e1.printStackTrace(); - } catch (IllegalAccessException e1) { + } catch (InstantiationException | IllegalAccessException e1) { e1.printStackTrace(); } repaint(); @@ -707,9 +703,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }); menu.add(menuItem); } - } catch (InstantiationException e1) { - logger.fatal("Error: " + e1.getMessage(), e1); - } catch (IllegalAccessException e1) { + } catch (InstantiationException | IllegalAccessException e1) { logger.fatal("Error: " + e1.getMessage(), e1); } } @@ -731,9 +725,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }); menu.add(menuItem); } - } catch (InstantiationException e1) { - logger.fatal("Error: " + e1.getMessage(), e1); - } catch (IllegalAccessException e1) { + } catch (InstantiationException | IllegalAccessException e1) { logger.fatal("Error: " + e1.getMessage(), e1); } } @@ -1483,7 +1475,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { Double.parseDouble(matrix[5]) ); resetViewport = 0; - } catch (Exception e) { + } catch (NumberFormatException e) { logger.warn("Bad viewport: " + e.getMessage()); resetViewport(); } From cef3c4079d79f6c070893cacdc451653254cef3f Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Thu, 10 Apr 2014 19:03:43 +0200 Subject: [PATCH 8/9] [cooja] Reformatted Visualizer and skins code --- .../org/contikios/mrm/MRMVisualizerSkin.java | 21 +- .../contikios/cooja/plugins/Visualizer.java | 439 ++++++++++-------- .../plugins/skins/DGRMVisualizerSkin.java | 190 ++++---- .../plugins/skins/UDGMVisualizerSkin.java | 70 +-- 4 files changed, 399 insertions(+), 321 deletions(-) diff --git a/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java b/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java index 3a9ab4ab2..54bbf247a 100644 --- a/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java +++ b/tools/cooja/apps/mrm/java/org/contikios/mrm/MRMVisualizerSkin.java @@ -26,7 +26,6 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ - package org.contikios.mrm; import java.awt.Color; @@ -51,6 +50,7 @@ import org.contikios.mrm.ChannelModel.TxPair; @ClassDescription("Radio environment (MRM)") @SupportedArguments(radioMediums = {MRM.class}) public class MRMVisualizerSkin implements VisualizerSkin { + private static final Logger logger = Logger.getLogger(MRMVisualizerSkin.class); private Simulation simulation = null; @@ -77,7 +77,7 @@ public class MRMVisualizerSkin implements VisualizerSkin { @Override public Color[] getColorOf(Mote mote) { if (visualizer.getSelectedMotes().contains(mote)) { - return new Color[] { Color.CYAN }; + return new Color[]{Color.CYAN}; } return null; } @@ -128,35 +128,36 @@ public class MRMVisualizerSkin implements VisualizerSkin { public Radio getFromRadio() { return selectedMote.getInterfaces().getRadio(); } + @Override public Radio getToRadio() { return dRadio; } }; double probArr[] = radioMedium.getChannelModel().getProbability( - txPair, - Double.NEGATIVE_INFINITY + txPair, + Double.NEGATIVE_INFINITY ); double prob = probArr[0]; double ss = probArr[1]; - + if (prob == 0.0d) { continue; } edges++; - String msg = String.format("%1.1f%%, %1.2fdB", 100.0*prob, ss); + String msg = String.format("%1.1f%%, %1.2fdB", 100.0 * prob, ss); Point pixel = visualizer.transformPositionToPixel(d.getInterfaces().getPosition()); int msgWidth = fm.stringWidth(msg); - g.setColor(new Color(1-(float)prob, (float)prob, 0.0f)); + g.setColor(new Color(1 - (float) prob, (float) prob, 0.0f)); g.drawLine(x, y, pixel.x, pixel.y); g.setColor(Color.BLACK); - g.drawString(msg, pixel.x - msgWidth/2, pixel.y + 2*Visualizer.MOTE_RADIUS + 3); + g.drawString(msg, pixel.x - msgWidth / 2, pixel.y + 2 * Visualizer.MOTE_RADIUS + 3); } - + String msg = dests.length + " edges"; int msgWidth = fm.stringWidth(msg); g.setColor(Color.BLACK); - g.drawString(msg, x - msgWidth/2, y + 2*Visualizer.MOTE_RADIUS + 3); + g.drawString(msg, x - msgWidth / 2, y + 2 * Visualizer.MOTE_RADIUS + 3); } } diff --git a/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java b/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java index 45654955f..6e6e5c2e9 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java @@ -27,7 +27,6 @@ * SUCH DAMAGE. * */ - package org.contikios.cooja.plugins; import java.awt.BasicStroke; @@ -139,11 +138,12 @@ import org.contikios.cooja.plugins.skins.UDGMVisualizerSkin; @ClassDescription("Network") @PluginType(PluginType.SIM_STANDARD_PLUGIN) public class Visualizer extends VisPlugin implements HasQuickHelp { + private static final long serialVersionUID = 1L; private static final Logger logger = Logger.getLogger(Visualizer.class); public static final int MOTE_RADIUS = 8; - private static final Color[] DEFAULT_MOTE_COLORS = { Color.WHITE }; + private static final Color[] DEFAULT_MOTE_COLORS = {Color.WHITE}; private Cooja gui = null; private Simulation simulation = null; @@ -186,8 +186,9 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { private Selection selection; /* Visualizers */ - private static final ArrayList> visualizerSkins = - new ArrayList<>(); + private static final ArrayList> visualizerSkins + = new ArrayList<>(); + static { /* Register default visualizer skins */ registerVisualizerSkin(IDVisualizerSkin.class); @@ -213,20 +214,27 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Popup menu */ public static interface SimulationMenuAction { + public boolean isEnabled(Visualizer visualizer, Simulation simulation); + public String getDescription(Visualizer visualizer, Simulation simulation); + public void doAction(Visualizer visualizer, Simulation simulation); } + public static interface MoteMenuAction { + public boolean isEnabled(Visualizer visualizer, Mote mote); + public String getDescription(Visualizer visualizer, Mote mote); + public void doAction(Visualizer visualizer, Mote mote); } - private ArrayList> simulationMenuActions = - new ArrayList<>(); - private ArrayList> moteMenuActions = - new ArrayList<>(); + private ArrayList> simulationMenuActions + = new ArrayList<>(); + private ArrayList> moteMenuActions + = new ArrayList<>(); public Visualizer(Simulation simulation, Cooja gui) { super("Network", gui); @@ -235,8 +243,8 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Register external visualizers */ String[] skins = gui.getProjectConfig().getStringArrayValue(Visualizer.class, "SKINS"); - - for (String skinClass: skins) { + + for (String skinClass : skins) { Class skin = gui.tryLoadClass(this, VisualizerSkin.class, skinClass); if (registerVisualizerSkin(skin)) { logger.info("Registered external visualizer: " + skinClass); @@ -254,9 +262,11 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { viewMenu.removeAll(); populateSkinMenu(viewMenu); } + @Override public void menuDeselected(MenuEvent e) { } + @Override public void menuCanceled(MenuEvent e) { } @@ -275,8 +285,8 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } }; zoomInAction.putValue( - Action.ACCELERATOR_KEY, - KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, ActionEvent.CTRL_MASK) + Action.ACCELERATOR_KEY, + KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, ActionEvent.CTRL_MASK) ); JMenuItem zoomInItem = new JMenuItem(zoomInAction); zoomMenu.add(zoomInItem); @@ -288,8 +298,8 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } }; zoomOutAction.putValue( - Action.ACCELERATOR_KEY, - KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, ActionEvent.CTRL_MASK) + Action.ACCELERATOR_KEY, + KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, ActionEvent.CTRL_MASK) ); JMenuItem zoomOutItem = new JMenuItem(zoomOutAction); zoomMenu.add(zoomOutItem); @@ -308,6 +318,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Main canvas */ canvas = new JPanel() { private static final long serialVersionUID = 1L; + @Override public void paintComponent(Graphics g) { super.paintComponent(g); @@ -317,12 +328,12 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { resetViewport--; } - ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - for (VisualizerSkin skin: currentSkins) { + ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + for (VisualizerSkin skin : currentSkins) { skin.paintBeforeMotes(g); } paintMotes(g); - for (VisualizerSkin skin: currentSkins) { + for (VisualizerSkin skin : currentSkins) { skin.paintAfterMotes(g); } selection.drawSelection(g); @@ -355,6 +366,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }); } } + @Override public void moteWasRemoved(Mote mote) { Position pos = mote.getInterfaces().getPosition(); @@ -364,7 +376,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } } }); - for (Mote mote: simulation.getMotes()) { + for (Mote mote : simulation.getMotes()) { Position pos = mote.getInterfaces().getPosition(); if (pos != null) { pos.addObserver(posObserver); @@ -395,10 +407,11 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Toggle highlight state */ if (highlightedMotes.contains(mote)) { highlightedMotes.remove(mote); - } else { + } + else { highlightedMotes.add(mote); } - timer.setDelay(timer.getDelay()-1); + timer.setDelay(timer.getDelay() - 1); repaint(); } }); @@ -429,10 +442,11 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { return; } - if (SwingUtilities.isLeftMouseButton(e)){ + if (SwingUtilities.isLeftMouseButton(e)) { handleMousePress(e); } } + @Override public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { @@ -451,10 +465,11 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { int x = mwe.getX(); int y = mwe.getY(); int rot = mwe.getWheelRotation(); - - if (rot > 0) { + + if (rot > 0) { zoomToFactor(zoomFactor() / 1.2, new Point(x, y)); - } else { + } + else { zoomToFactor(zoomFactor() * 1.2, new Point(x, y)); } @@ -478,29 +493,36 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { public void dragEnter(DropTargetDragEvent dtde) { if (acceptOrRejectDrag(dtde)) { dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); - } else { + } + else { dtde.rejectDrag(); } } + @Override public void dragExit(DropTargetEvent dte) { } + @Override public void dropActionChanged(DropTargetDragEvent dtde) { if (acceptOrRejectDrag(dtde)) { dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); - } else { + } + else { dtde.rejectDrag(); } } + @Override public void dragOver(DropTargetDragEvent dtde) { if (acceptOrRejectDrag(dtde)) { dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); - } else { + } + else { dtde.rejectDrag(); } } + @Override public void drop(DropTargetDropEvent dtde) { Transferable transferable = dtde.getTransferable(); @@ -516,7 +538,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { try { List transferList = Arrays.asList( - transferable.getTransferData(DataFlavor.javaFileListFlavor) + transferable.getTransferData(DataFlavor.javaFileListFlavor) ); if (transferList.size() != 1) { return; @@ -537,6 +559,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { handleDropFile(file, dtde.getLocation()); } + private boolean acceptOrRejectDrag(DropTargetDragEvent dtde) { Transferable transferable = dtde.getTransferable(); @@ -554,7 +577,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } try { List transferList = Arrays.asList( - transferable.getTransferData(DataFlavor.javaFileListFlavor) + transferable.getTransferData(DataFlavor.javaFileListFlavor) ); if (transferList.size() != 1) { return false; @@ -564,7 +587,8 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { return false; } file = list.get(0); - } catch (UnsupportedFlavorException | IOException e) { + } + catch (UnsupportedFlavorException | IOException e) { return false; } @@ -573,7 +597,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } }; canvas.setDropTarget( - new DropTarget(canvas, DnDConstants.ACTION_COPY_OR_MOVE, dTargetListener, true, null) + new DropTarget(canvas, DnDConstants.ACTION_COPY_OR_MOVE, dTargetListener, true, null) ); resetViewport = 3; /* XXX Quick-fix */ @@ -584,7 +608,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } private void generateAndActivateSkin(Class skinClass) { - for (VisualizerSkin skin: currentSkins) { + for (VisualizerSkin skin : currentSkins) { if (skinClass == skin.getClass()) { logger.warn("Selected visualizer already active: " + skinClass); return; @@ -601,7 +625,8 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { VisualizerSkin newSkin = skinClass.newInstance(); newSkin.setActive(Visualizer.this.simulation, Visualizer.this); currentSkins.add(0, newSkin); - } catch (InstantiationException | IllegalAccessException e1) { + } + catch (InstantiationException | IllegalAccessException e1) { e1.printStackTrace(); } repaint(); @@ -616,12 +641,12 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Activate default skins */ String[] defaultSkins = Cooja.getExternalToolsSetting("VISUALIZER_DEFAULT_SKINS", "").split(";"); - for (String skin: defaultSkins) { + for (String skin : defaultSkins) { if (skin.isEmpty()) { continue; } - Class skinClass = - simulation.getCooja().tryLoadClass(this, VisualizerSkin.class, skin); + Class skinClass + = simulation.getCooja().tryLoadClass(this, VisualizerSkin.class, skin); generateAndActivateSkin(skinClass); } } @@ -690,7 +715,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Add registered mote actions */ for (final Mote mote : motes) { menu.add(simulation.getCooja().createMotePluginsSubmenu(mote)); - for (Class menuActionClass: moteMenuActions) { + for (Class menuActionClass : moteMenuActions) { try { final MoteMenuAction menuAction = menuActionClass.newInstance(); if (menuAction.isEnabled(this, mote)) { @@ -703,7 +728,8 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }); menu.add(menuItem); } - } catch (InstantiationException | IllegalAccessException e1) { + } + catch (InstantiationException | IllegalAccessException e1) { logger.fatal("Error: " + e1.getMessage(), e1); } } @@ -725,7 +751,8 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }); menu.add(menuItem); } - } catch (InstantiationException | IllegalAccessException e1) { + } + catch (InstantiationException | IllegalAccessException e1) { logger.fatal("Error: " + e1.getMessage(), e1); } } @@ -733,11 +760,11 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Visualizer skin actions */ menu.add(new JSeparator()); /*JMenu skinMenu = new JMenu("Visualizers"); - populateSkinMenu(skinMenu); - menu.add(skinMenu); - makeSkinsDefaultAction.putValue(Action.NAME, "Set default visualizers"); - JMenuItem skinDefaultItem = new JMenuItem(makeSkinsDefaultAction); - menu.add(skinDefaultItem);*/ + populateSkinMenu(skinMenu); + menu.add(skinMenu); + makeSkinsDefaultAction.putValue(Action.NAME, "Set default visualizers"); + JMenuItem skinDefaultItem = new JMenuItem(makeSkinsDefaultAction); + menu.add(skinDefaultItem);*/ /* Show menu */ menu.setLocation(new Point( @@ -748,27 +775,28 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } private boolean showMoteToMoteRelations = true; + private void populateSkinMenu(MenuElement menu) { - /* Mote-to-mote relations */ - JCheckBoxMenuItem moteRelationsItem = new JCheckBoxMenuItem("Mote relations", showMoteToMoteRelations); - moteRelationsItem.addItemListener(new ItemListener() { + /* Mote-to-mote relations */ + JCheckBoxMenuItem moteRelationsItem = new JCheckBoxMenuItem("Mote relations", showMoteToMoteRelations); + moteRelationsItem.addItemListener(new ItemListener() { @Override - public void itemStateChanged(ItemEvent e) { - JCheckBoxMenuItem menuItem = ((JCheckBoxMenuItem)e.getItem()); - showMoteToMoteRelations = menuItem.isSelected(); - repaint(); - } - }); - if (menu instanceof JMenu) { - ((JMenu)menu).add(moteRelationsItem); - ((JMenu)menu).add(new JSeparator()); - } - if (menu instanceof JPopupMenu) { - ((JPopupMenu)menu).add(moteRelationsItem); - ((JPopupMenu)menu).add(new JSeparator()); - } - - for (Class skinClass: visualizerSkins) { + public void itemStateChanged(ItemEvent e) { + JCheckBoxMenuItem menuItem = ((JCheckBoxMenuItem) e.getItem()); + showMoteToMoteRelations = menuItem.isSelected(); + repaint(); + } + }); + if (menu instanceof JMenu) { + ((JMenu) menu).add(moteRelationsItem); + ((JMenu) menu).add(new JSeparator()); + } + if (menu instanceof JPopupMenu) { + ((JPopupMenu) menu).add(moteRelationsItem); + ((JPopupMenu) menu).add(new JSeparator()); + } + + for (Class skinClass : visualizerSkins) { /* Should skin be enabled in this simulation? */ if (!isSkinCompatible(skinClass)) { continue; @@ -779,7 +807,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { item.putClientProperty("skinclass", skinClass); /* Select skin if active */ - for (VisualizerSkin skin: currentSkins) { + for (VisualizerSkin skin : currentSkins) { if (skin.getClass() == skinClass) { item.setSelected(true); break; @@ -789,14 +817,14 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { item.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { - JCheckBoxMenuItem menuItem = ((JCheckBoxMenuItem)e.getItem()); + JCheckBoxMenuItem menuItem = ((JCheckBoxMenuItem) e.getItem()); if (menuItem == null) { logger.fatal("No menu item"); return; } - Class skinClass = - (Class) menuItem.getClientProperty("skinclass"); + Class skinClass + = (Class) menuItem.getClientProperty("skinclass"); if (skinClass == null) { logger.fatal("Unknown visualizer skin class: " + skinClass); return; @@ -805,10 +833,11 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { if (menuItem.isSelected()) { /* Create and activate new skin */ generateAndActivateSkin(skinClass); - } else { + } + else { /* Deactivate skin */ VisualizerSkin skinToDeactivate = null; - for (VisualizerSkin skin: currentSkins) { + for (VisualizerSkin skin : currentSkins) { if (skin.getClass() == skinClass) { skinToDeactivate = skin; break; @@ -826,10 +855,10 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }); if (menu instanceof JMenu) { - ((JMenu)menu).add(item); + ((JMenu) menu).add(item); } if (menu instanceof JPopupMenu) { - ((JPopupMenu)menu).add(item); + ((JPopupMenu) menu).add(item); } } } @@ -844,7 +873,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { if (skinClass.getAnnotation(SupportedArguments.class) != null) { showMenuItem = false; Class[] radioMediums = skinClass.getAnnotation(SupportedArguments.class).radioMediums(); - for (Class o: radioMediums) { + for (Class o : radioMediums) { if (o.isAssignableFrom(simulation.getRadioMedium().getClass())) { showMenuItem = true; break; @@ -1014,7 +1043,8 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { private void beginMoveRequest(Mote motesToMove, boolean withTiming, boolean confirm) { if (withTiming) { moveStartTime = System.currentTimeMillis(); - } else { + } + else { moveStartTime = -1; } mouseActionState = MotesActionState.DEFAULT_PRESS; @@ -1028,19 +1058,19 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } private void zoomToFactor(double newZoom) { - zoomToFactor(newZoom, new Point(canvas.getWidth()/2, canvas.getHeight()/2)); + zoomToFactor(newZoom, new Point(canvas.getWidth() / 2, canvas.getHeight() / 2)); } - + private void zoomToFactor(double newZoom, Point zoomCenter) { Position center = transformPixelToPosition(zoomCenter); viewportTransform.setToScale( - newZoom, - newZoom + newZoom, + newZoom ); Position newCenter = transformPixelToPosition(zoomCenter); viewportTransform.translate( - newCenter.getXCoordinate() - center.getXCoordinate(), - newCenter.getYCoordinate() - center.getYCoordinate() + newCenter.getXCoordinate() - center.getXCoordinate(), + newCenter.getYCoordinate() - center.getYCoordinate() ); repaint(); } @@ -1077,9 +1107,9 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { * First mote in array is the bottom mote, last mote is the top mote. * * @param clickedX - * X coordinate + * X coordinate * @param clickedY - * Y coordinate + * Y coordinate * @return All motes at given position */ public Mote[] findMotesAtPosition(int clickedX, int clickedY) { @@ -1090,18 +1120,18 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { // Calculate painted mote radius in coordinates double paintedMoteWidth = transformToPositionX(MOTE_RADIUS) - - transformToPositionX(0); + - transformToPositionX(0); double paintedMoteHeight = transformToPositionY(MOTE_RADIUS) - - transformToPositionY(0); + - transformToPositionY(0); for (int i = 0; i < simulation.getMotesCount(); i++) { Position pos = simulation.getMote(i).getInterfaces().getPosition(); // Transform to unit circle before checking if mouse hit this mote double distanceX = Math.abs(xCoord - pos.getXCoordinate()) - / paintedMoteWidth; + / paintedMoteWidth; double distanceY = Math.abs(yCoord - pos.getYCoordinate()) - / paintedMoteHeight; + / paintedMoteHeight; if (distanceX * distanceX + distanceY * distanceY <= 1) { motes.add(simulation.getMote(i)); @@ -1120,24 +1150,24 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Paint mote relations */ if (showMoteToMoteRelations) { - MoteRelation[] relations = simulation.getCooja().getMoteRelations(); - for (MoteRelation r: relations) { - Position sourcePos = r.source.getInterfaces().getPosition(); - Position destPos = r.dest.getInterfaces().getPosition(); + MoteRelation[] relations = simulation.getCooja().getMoteRelations(); + for (MoteRelation r : relations) { + Position sourcePos = r.source.getInterfaces().getPosition(); + Position destPos = r.dest.getInterfaces().getPosition(); - Point sourcePoint = transformPositionToPixel(sourcePos); - Point destPoint = transformPositionToPixel(destPos); + 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); - } + g.setColor(r.color == null ? Color.black : r.color); + drawArrow(g, sourcePoint.x, sourcePoint.y, destPoint.x, destPoint.y, MOTE_RADIUS + 1); + } } - for (Mote mote: allMotes) { + for (Mote mote : allMotes) { /* Use the first skin's non-null mote colors */ Color moteColors[] = null; - for (VisualizerSkin skin: currentSkins) { + for (VisualizerSkin skin : currentSkins) { moteColors = skin.getColorOf(mote); if (moteColors != null) { break; @@ -1156,33 +1186,37 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { if (mote == movedMotes) { g.setColor(MOVE_COLOR); g.fillOval(x - MOTE_RADIUS, y - MOTE_RADIUS, 2 * MOTE_RADIUS, - 2 * MOTE_RADIUS); - } else if (!highlightedMotes.isEmpty() && highlightedMotes.contains(mote)) { + 2 * MOTE_RADIUS); + } + else if (!highlightedMotes.isEmpty() && highlightedMotes.contains(mote)) { g.setColor(HIGHLIGHT_COLOR); g.fillOval(x - MOTE_RADIUS, y - MOTE_RADIUS, 2 * MOTE_RADIUS, - 2 * MOTE_RADIUS); - } else if (moteColors.length >= 2) { + 2 * MOTE_RADIUS); + } + else if (moteColors.length >= 2) { g.setColor(moteColors[0]); g.fillOval(x - MOTE_RADIUS, y - MOTE_RADIUS, 2 * MOTE_RADIUS, - 2 * MOTE_RADIUS); + 2 * MOTE_RADIUS); g.setColor(moteColors[1]); g.fillOval(x - MOTE_RADIUS / 2, y - MOTE_RADIUS / 2, MOTE_RADIUS, - MOTE_RADIUS); + MOTE_RADIUS); - } else if (moteColors.length >= 1) { + } + else if (moteColors.length >= 1) { g.setColor(moteColors[0]); g.fillOval(x - MOTE_RADIUS, y - MOTE_RADIUS, 2 * MOTE_RADIUS, - 2 * MOTE_RADIUS); + 2 * MOTE_RADIUS); } g.setColor(Color.BLACK); g.drawOval(x - MOTE_RADIUS, y - MOTE_RADIUS, 2 * MOTE_RADIUS, - 2 * MOTE_RADIUS); + 2 * MOTE_RADIUS); } } private Polygon arrowPoly = new Polygon(); + private void drawArrow(Graphics g, int xSource, int ySource, int xDest, int yDest, int delta) { double dx = xSource - xDest; double dy = ySource - yDest; @@ -1205,11 +1239,11 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } private int yCor(int len, double dir) { - return (int)(0.5 + len * Math.cos(dir)); + return (int) (0.5 + len * Math.cos(dir)); } private int xCor(int len, double dir) { - return (int)(0.5 + len * Math.sin(dir)); + return (int) (0.5 + len * Math.sin(dir)); } /** @@ -1234,7 +1268,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } /* Extremes */ - for (Mote mote: motes) { + for (Mote mote : motes) { Position pos = mote.getInterfaces().getPosition(); smallX = Math.min(smallX, pos.getXCoordinate()); bigX = Math.max(bigX, pos.getXCoordinate()); @@ -1245,20 +1279,22 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Scale viewport */ if (smallX == bigX) { scaleX = 1; - } else { + } + else { scaleX = (bigX - smallX) / (canvas.getWidth()); } if (smallY == bigY) { scaleY = 1; - } else { + } + else { scaleY = (bigY - smallY) / (canvas.getHeight()); } viewportTransform.setToIdentity(); - double newZoom = (1.0/(BORDER_SCALE_FACTOR*Math.max(scaleX, scaleY))); + double newZoom = (1.0 / (BORDER_SCALE_FACTOR * Math.max(scaleX, scaleY))); viewportTransform.setToScale( - newZoom, - newZoom + newZoom, + newZoom ); /* Center visible motes */ @@ -1266,14 +1302,14 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { - Position viewMid = - transformPixelToPosition(canvas.getWidth()/2, canvas.getHeight()/2); + Position viewMid + = transformPixelToPosition(canvas.getWidth() / 2, canvas.getHeight() / 2); double motesMidX = (smallXfinal + bigXfinal) / 2.0; double motesMidY = (smallYfinal + bigYfinal) / 2.0; viewportTransform.translate( - viewMid.getXCoordinate() - motesMidX, - viewMid.getYCoordinate() - motesMidY); + viewMid.getXCoordinate() - motesMidX, + viewMid.getYCoordinate() - motesMidY); canvas.repaint(); } }); @@ -1284,14 +1320,14 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { * current sized canvas. * * @param pos - * Real-world position + * Real-world position * @return Pixel coordinates */ public Point transformPositionToPixel(Position pos) { return transformPositionToPixel( - pos.getXCoordinate(), - pos.getYCoordinate(), - pos.getZCoordinate() + pos.getXCoordinate(), + pos.getYCoordinate(), + pos.getZCoordinate() ); } @@ -1319,38 +1355,42 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { * Transforms a pixel coordinate to a real-world. Z-value will always be 0. * * @param pixelPos - * On-screen pixel coordinate + * On-screen pixel coordinate * @return Real world coordinate (z=0). */ public Position transformPixelToPosition(Point pixelPos) { return transformPixelToPosition(pixelPos.x, pixelPos.y); } + public Position transformPixelToPosition(int x, int y) { Position position = new Position(null); position.setCoordinates( - transformToPositionX(x), - transformToPositionY(y), - 0.0 + transformToPositionX(x), + transformToPositionY(y), + 0.0 ); return position; } private int transformToPixelX(double x) { - return (int) (viewportTransform.getScaleX()*x + viewportTransform.getTranslateX()); + return (int) (viewportTransform.getScaleX() * x + viewportTransform.getTranslateX()); } + private int transformToPixelY(double y) { - return (int) (viewportTransform.getScaleY()*y + viewportTransform.getTranslateY()); + return (int) (viewportTransform.getScaleY() * y + viewportTransform.getTranslateY()); } + private double transformToPositionX(int x) { - return (x - viewportTransform.getTranslateX())/viewportTransform.getScaleX() ; + return (x - viewportTransform.getTranslateX()) / viewportTransform.getScaleX(); } + private double transformToPositionY(int y) { - return (y - viewportTransform.getTranslateY())/viewportTransform.getScaleY() ; + return (y - viewportTransform.getTranslateY()) / viewportTransform.getScaleY(); } @Override public void closePlugin() { - for (VisualizerSkin skin: currentSkins) { + for (VisualizerSkin skin : currentSkins) { skin.setInactive(); } currentSkins.clear(); @@ -1362,7 +1402,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } simulation.getEventCentral().removeMoteCountListener(newMotesListener); - for (Mote mote: simulation.getMotes()) { + for (Mote mote : simulation.getMotes()) { Position pos = mote.getInterfaces().getPosition(); if (pos != null) { pos.deleteObserver(posObserver); @@ -1372,6 +1412,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { protected boolean isDropFileAccepted(File file) { return true; /* TODO */ + } protected void handleDropFile(File file, Point point) { @@ -1392,17 +1433,17 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Show mote-to-mote relations */ if (showMoteToMoteRelations) { - element = new Element("moterelations"); - element.setText("" + true); - config.add(element); + element = new Element("moterelations"); + element.setText("" + true); + config.add(element); } - + /* Skins */ - for (int i=currentSkins.size()-1; i >= 0; i--) { - VisualizerSkin skin = currentSkins.get(i); - element = new Element("skin"); - element.setText(skin.getClass().getName()); - config.add(element); + for (int i = currentSkins.size() - 1; i >= 0; i--) { + VisualizerSkin skin = currentSkins.get(i); + element = new Element("skin"); + element.setText(skin.getClass().getName()); + config.add(element); } /* Viewport */ @@ -1410,19 +1451,19 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { double[] matrix = new double[6]; viewportTransform.getMatrix(matrix); element.setText( - matrix[0] + " " + - matrix[1] + " " + - matrix[2] + " " + - matrix[3] + " " + - matrix[4] + " " + - matrix[5] + matrix[0] + " " + + matrix[1] + " " + + matrix[2] + " " + + matrix[3] + " " + + matrix[4] + " " + + matrix[5] ); config.add(element); /* Hide decorations */ BasicInternalFrameUI ui = (BasicInternalFrameUI) getUI(); - if (ui.getNorthPane().getPreferredSize() == null || - ui.getNorthPane().getPreferredSize().height == 0) { + if (ui.getNorthPane().getPreferredSize() == null + || ui.getNorthPane().getPreferredSize().height == 0) { element = new Element("hidden"); config.add(element); } @@ -1440,13 +1481,13 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { String wanted = element.getText(); /* Backwards compatibility: se.sics -> org.contikios */ if (wanted.startsWith("se.sics")) { - wanted = wanted.replaceFirst("se\\.sics", "org.contikios"); + wanted = wanted.replaceFirst("se\\.sics", "org.contikios"); } - for (Class skinClass: visualizerSkins) { + for (Class skinClass : visualizerSkins) { if (wanted.equals(skinClass.getName()) - /* Backwards compatibility */ - || wanted.equals(Cooja.getDescriptionOf(skinClass))) { + /* Backwards compatibility */ + || wanted.equals(Cooja.getDescriptionOf(skinClass))) { final Class skin = skinClass; SwingUtilities.invokeLater(new Runnable() { @Override @@ -1461,27 +1502,31 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { if (wanted != null) { logger.warn("Could not load visualizer: " + element.getText()); } - } else if (element.getName().equals("moterelations")) { - showMoteToMoteRelations = true; - } else if (element.getName().equals("viewport")) { + } + else if (element.getName().equals("moterelations")) { + showMoteToMoteRelations = true; + } + else if (element.getName().equals("viewport")) { try { String[] matrix = element.getText().split(" "); viewportTransform.setTransform( - Double.parseDouble(matrix[0]), - Double.parseDouble(matrix[1]), - Double.parseDouble(matrix[2]), - Double.parseDouble(matrix[3]), - Double.parseDouble(matrix[4]), - Double.parseDouble(matrix[5]) + Double.parseDouble(matrix[0]), + Double.parseDouble(matrix[1]), + Double.parseDouble(matrix[2]), + Double.parseDouble(matrix[3]), + Double.parseDouble(matrix[4]), + Double.parseDouble(matrix[5]) ); resetViewport = 0; - } catch (NumberFormatException e) { + } + catch (NumberFormatException e) { logger.warn("Bad viewport: " + e.getMessage()); resetViewport(); } - } else if (element.getName().equals("hidden")) { + } + else if (element.getName().equals("hidden")) { BasicInternalFrameUI ui = (BasicInternalFrameUI) getUI(); - ui.getNorthPane().setPreferredSize(new Dimension(0,0)); + ui.getNorthPane().setPreferredSize(new Dimension(0, 0)); } } return true; @@ -1491,7 +1536,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { @Override public void actionPerformed(ActionEvent e) { StringBuilder sb = new StringBuilder(); - for (VisualizerSkin skin: currentSkins) { + for (VisualizerSkin skin : currentSkins) { if (sb.length() > 0) { sb.append(';'); } @@ -1502,15 +1547,18 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }; protected static class ButtonClickMoteMenuAction implements MoteMenuAction { + @Override public boolean isEnabled(Visualizer visualizer, Mote mote) { return mote.getInterfaces().getButton() != null - && !mote.getInterfaces().getButton().isPressed(); + && !mote.getInterfaces().getButton().isPressed(); } + @Override public String getDescription(Visualizer visualizer, Mote mote) { return "Click button on " + mote; } + @Override public void doAction(Visualizer visualizer, Mote mote) { mote.getInterfaces().getButton().clickButton(); @@ -1518,14 +1566,17 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }; protected static class DeleteMoteMenuAction implements MoteMenuAction { + @Override public boolean isEnabled(Visualizer visualizer, Mote mote) { return true; } + @Override public String getDescription(Visualizer visualizer, Mote mote) { return "Delete " + mote; } + @Override public void doAction(Visualizer visualizer, Mote mote) { mote.getSimulation().removeMote(mote); @@ -1533,14 +1584,17 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }; protected static class ShowLEDMoteMenuAction implements MoteMenuAction { + @Override public boolean isEnabled(Visualizer visualizer, Mote mote) { return mote.getInterfaces().getLED() != null; } + @Override public String getDescription(Visualizer visualizer, Mote mote) { return "Show LEDs on " + mote; } + @Override public void doAction(Visualizer visualizer, Mote mote) { Simulation simulation = mote.getSimulation(); @@ -1552,12 +1606,12 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Extract description (input to plugin) */ String desc = Cooja.getDescriptionOf(mote.getInterfaces().getLED()); - MoteInterfaceViewer viewer = - (MoteInterfaceViewer) simulation.getCooja().tryStartPlugin( - MoteInterfaceViewer.class, - simulation.getCooja(), - simulation, - mote); + MoteInterfaceViewer viewer + = (MoteInterfaceViewer) simulation.getCooja().tryStartPlugin( + MoteInterfaceViewer.class, + simulation.getCooja(), + simulation, + mote); if (viewer == null) { return; } @@ -1567,24 +1621,27 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }; protected static class ShowSerialMoteMenuAction implements MoteMenuAction { + @Override public boolean isEnabled(Visualizer visualizer, Mote mote) { - for (MoteInterface intf: mote.getInterfaces().getInterfaces()) { + for (MoteInterface intf : mote.getInterfaces().getInterfaces()) { if (intf instanceof SerialPort) { return true; } } return false; } + @Override public String getDescription(Visualizer visualizer, Mote mote) { return "Show serial port on " + mote; } + @Override public void doAction(Visualizer visualizer, Mote mote) { Simulation simulation = mote.getSimulation(); SerialPort serialPort = null; - for (MoteInterface intf: mote.getInterfaces().getInterfaces()) { + for (MoteInterface intf : mote.getInterfaces().getInterfaces()) { if (intf instanceof SerialPort) { serialPort = (SerialPort) intf; break; @@ -1598,12 +1655,12 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { /* Extract description (input to plugin) */ String desc = Cooja.getDescriptionOf(serialPort); - MoteInterfaceViewer viewer = - (MoteInterfaceViewer) simulation.getCooja().tryStartPlugin( - MoteInterfaceViewer.class, - simulation.getCooja(), - simulation, - mote); + MoteInterfaceViewer viewer + = (MoteInterfaceViewer) simulation.getCooja().tryStartPlugin( + MoteInterfaceViewer.class, + simulation.getCooja(), + simulation, + mote); if (viewer == null) { return; } @@ -1613,14 +1670,17 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }; protected static class MoveMoteMenuAction implements MoteMenuAction { + @Override public boolean isEnabled(Visualizer visualizer, Mote mote) { return true; } + @Override public String getDescription(Visualizer visualizer, Mote mote) { return "Move " + mote; } + @Override public void doAction(Visualizer visualizer, Mote mote) { visualizer.beginMoveRequest(mote, false, false); @@ -1628,15 +1688,18 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }; protected static class ResetViewportAction implements SimulationMenuAction { + @Override public void doAction(Visualizer visualizer, Simulation simulation) { visualizer.resetViewport = 1; visualizer.repaint(); } + @Override public String getDescription(Visualizer visualizer, Simulation simulation) { return "Reset viewport"; } + @Override public boolean isEnabled(Visualizer visualizer, Simulation simulation) { return true; @@ -1644,6 +1707,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { }; protected static class ToggleDecorationsMenuAction implements SimulationMenuAction { + @Override public void doAction(final Visualizer visualizer, Simulation simulation) { if (!(visualizer.getUI() instanceof BasicInternalFrameUI)) { @@ -1651,13 +1715,14 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } BasicInternalFrameUI ui = (BasicInternalFrameUI) visualizer.getUI(); - if (ui.getNorthPane().getPreferredSize() == null || - ui.getNorthPane().getPreferredSize().height == 0) { + if (ui.getNorthPane().getPreferredSize() == null + || ui.getNorthPane().getPreferredSize().height == 0) { /* Restore window decorations */ ui.getNorthPane().setPreferredSize(null); - } else { + } + else { /* Hide window decorations */ - ui.getNorthPane().setPreferredSize(new Dimension(0,0)); + ui.getNorthPane().setPreferredSize(new Dimension(0, 0)); } visualizer.revalidate(); SwingUtilities.invokeLater(new Runnable() { @@ -1667,6 +1732,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } }); } + @Override public String getDescription(Visualizer visualizer, Simulation simulation) { if (!(visualizer.getUI() instanceof BasicInternalFrameUI)) { @@ -1674,12 +1740,13 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } BasicInternalFrameUI ui = (BasicInternalFrameUI) visualizer.getUI(); - if (ui.getNorthPane().getPreferredSize() == null || - ui.getNorthPane().getPreferredSize().height == 0) { + if (ui.getNorthPane().getPreferredSize() == null + || ui.getNorthPane().getPreferredSize().height == 0) { return "Restore window decorations"; } return "Hide window decorations"; } + @Override public boolean isEnabled(Visualizer visualizer, Simulation simulation) { return visualizer.getUI() instanceof BasicInternalFrameUI; diff --git a/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java b/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java index 7d8bf882b..cc991be25 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/skins/DGRMVisualizerSkin.java @@ -27,7 +27,6 @@ * SUCH DAMAGE. * */ - package org.contikios.cooja.plugins.skins; import java.awt.Color; @@ -53,113 +52,122 @@ import org.contikios.cooja.radiomediums.DirectedGraphMedium; @ClassDescription("Radio environment (DGRM)") @SupportedArguments(radioMediums = {DirectedGraphMedium.class}) public class DGRMVisualizerSkin implements VisualizerSkin { - private static final Logger logger = Logger.getLogger(DGRMVisualizerSkin.class); - private Simulation simulation = null; - private Visualizer visualizer = null; + private static final Logger logger = Logger.getLogger(DGRMVisualizerSkin.class); + + private Simulation simulation = null; + private Visualizer visualizer = null; @Override - public void setActive(Simulation simulation, Visualizer vis) { - if (!(simulation.getRadioMedium() instanceof DirectedGraphMedium)) { - logger.fatal("Cannot activate DGRM skin for unknown radio medium: " + simulation.getRadioMedium()); - return; - } - this.simulation = simulation; - this.visualizer = vis; - } + public void setActive(Simulation simulation, Visualizer vis) { + if (!(simulation.getRadioMedium() instanceof DirectedGraphMedium)) { + logger.fatal("Cannot activate DGRM skin for unknown radio medium: " + simulation.getRadioMedium()); + return; + } + this.simulation = simulation; + this.visualizer = vis; + } @Override - public void setInactive() { - if (simulation == null) { - /* Skin was never activated */ - return; - } - } + public void setInactive() { + if (simulation == null) { + /* Skin was never activated */ + return; + } + } @Override - public Color[] getColorOf(Mote mote) { - if (visualizer.getSelectedMotes().contains(mote)) { - return new Color[] { Color.CYAN }; - } - return null; - } + public Color[] getColorOf(Mote mote) { + if (visualizer.getSelectedMotes().contains(mote)) { + return new Color[]{Color.CYAN}; + } + return null; + } @Override - public void paintBeforeMotes(Graphics g) { - Set selectedMotes = visualizer.getSelectedMotes(); - if (simulation == null || selectedMotes == null) { - return; - } + public void paintBeforeMotes(Graphics g) { + Set selectedMotes = visualizer.getSelectedMotes(); + if (simulation == null || selectedMotes == null) { + return; + } - for (final Mote selectedMote : selectedMotes) { - if (selectedMote.getInterfaces().getRadio() == null) { - continue; - } + for (final Mote selectedMote : selectedMotes) { + if (selectedMote.getInterfaces().getRadio() == null) { + continue; + } - /* Paint transmission and interference range for selected mote */ - Position motePos = selectedMote.getInterfaces().getPosition(); + /* Paint transmission and interference range for selected mote */ + Position motePos = selectedMote.getInterfaces().getPosition(); - Point pixelCoord = visualizer.transformPositionToPixel(motePos); - int x = pixelCoord.x; - int y = pixelCoord.y; - Radio selectedRadio = selectedMote.getInterfaces().getRadio(); + Point pixelCoord = visualizer.transformPositionToPixel(motePos); + int x = pixelCoord.x; + int y = pixelCoord.y; + Radio selectedRadio = selectedMote.getInterfaces().getRadio(); - FontMetrics fm = g.getFontMetrics(); - g.setColor(Color.BLACK); + FontMetrics fm = g.getFontMetrics(); + g.setColor(Color.BLACK); - DirectedGraphMedium radioMedium = (DirectedGraphMedium) simulation.getRadioMedium(); + DirectedGraphMedium radioMedium = (DirectedGraphMedium) simulation.getRadioMedium(); - /* Print transmission success probabilities */ - DestinationRadio[] dests = radioMedium.getPotentialDestinations(selectedRadio); - if (dests == null || dests.length == 0) { - String msg = "No edges"; - int msgWidth = fm.stringWidth(msg); - g.setColor(Color.BLACK); - g.drawString(msg, x - msgWidth / 2, y + 2 * Visualizer.MOTE_RADIUS + 3); - continue; - } - String msg = dests.length + " edges"; - int msgWidth = fm.stringWidth(msg); - g.setColor(Color.BLACK); - g.drawString(msg, x - msgWidth / 2, y + 2 * Visualizer.MOTE_RADIUS + 3); - /* Draw LQI/RSSI edges */ - for (DestinationRadio r : dests) { - double prob = ((DGRMDestinationRadio)r).ratio; - double rssi = ((DGRMDestinationRadio)r).signal; - double pos_rssi = rssi + 100; - int lqi = ((DGRMDestinationRadio)r).lqi; - float red = (float)(1 - prob*pos_rssi/90*lqi/100); - if(red > 1) red = 1; - if(red < 0) red = 0; - float green = (float)(prob*pos_rssi/90*lqi/100); - if(green > 1) green = 1; - if(green < 0) green = 0; - if (prob == 0.0d) { - continue; - } - Position pos = r.radio.getPosition(); - Point pixel = visualizer.transformPositionToPixel(pos); - g.setColor(new Color(red, green, 0.0f)); - g.drawString("LQI: " + lqi, (x + pixel.x) / 2, (y + pixel.y) / 2); - g.drawString("RSSI: " + rssi, (x + pixel.x) / 2, (y + pixel.y) / 2 + g.getFontMetrics().getHeight()); - g.drawLine(x, y, pixel.x, pixel.y); - /* Draw success ratio only if single mote selected */ - if (selectedMotes.size() == 1) { - g.setColor(Color.BLACK); - msg = String.format("%1.1f%%", 100.0 * prob); - msgWidth = fm.stringWidth(msg); - g.drawString(msg, pixel.x - msgWidth/2, pixel.y + 2*Visualizer.MOTE_RADIUS + 3); - } - } - } - } + /* Print transmission success probabilities */ + DestinationRadio[] dests = radioMedium.getPotentialDestinations(selectedRadio); + if (dests == null || dests.length == 0) { + String msg = "No edges"; + int msgWidth = fm.stringWidth(msg); + g.setColor(Color.BLACK); + g.drawString(msg, x - msgWidth / 2, y + 2 * Visualizer.MOTE_RADIUS + 3); + continue; + } + String msg = dests.length + " edges"; + int msgWidth = fm.stringWidth(msg); + g.setColor(Color.BLACK); + g.drawString(msg, x - msgWidth / 2, y + 2 * Visualizer.MOTE_RADIUS + 3); + /* Draw LQI/RSSI edges */ + for (DestinationRadio r : dests) { + double prob = ((DGRMDestinationRadio) r).ratio; + double rssi = ((DGRMDestinationRadio) r).signal; + double pos_rssi = rssi + 100; + int lqi = ((DGRMDestinationRadio) r).lqi; + float red = (float) (1 - prob * pos_rssi / 90 * lqi / 100); + if (red > 1) { + red = 1; + } + if (red < 0) { + red = 0; + } + float green = (float) (prob * pos_rssi / 90 * lqi / 100); + if (green > 1) { + green = 1; + } + if (green < 0) { + green = 0; + } + if (prob == 0.0d) { + continue; + } + Position pos = r.radio.getPosition(); + Point pixel = visualizer.transformPositionToPixel(pos); + g.setColor(new Color(red, green, 0.0f)); + g.drawString("LQI: " + lqi, (x + pixel.x) / 2, (y + pixel.y) / 2); + g.drawString("RSSI: " + rssi, (x + pixel.x) / 2, (y + pixel.y) / 2 + g.getFontMetrics().getHeight()); + g.drawLine(x, y, pixel.x, pixel.y); + /* Draw success ratio only if single mote selected */ + if (selectedMotes.size() == 1) { + g.setColor(Color.BLACK); + msg = String.format("%1.1f%%", 100.0 * prob); + msgWidth = fm.stringWidth(msg); + g.drawString(msg, pixel.x - msgWidth / 2, pixel.y + 2 * Visualizer.MOTE_RADIUS + 3); + } + } + } + } @Override - public void paintAfterMotes(Graphics g) { - } + public void paintAfterMotes(Graphics g) { + } @Override - public Visualizer getVisualizer() { - return visualizer; - } + public Visualizer getVisualizer() { + return visualizer; + } } diff --git a/tools/cooja/java/org/contikios/cooja/plugins/skins/UDGMVisualizerSkin.java b/tools/cooja/java/org/contikios/cooja/plugins/skins/UDGMVisualizerSkin.java index 4c0a18b0f..45f7a826a 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/skins/UDGMVisualizerSkin.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/skins/UDGMVisualizerSkin.java @@ -27,7 +27,6 @@ * SUCH DAMAGE. * */ - package org.contikios.cooja.plugins.skins; import java.awt.BorderLayout; @@ -87,6 +86,7 @@ import org.contikios.cooja.radiomediums.UDGM; @ClassDescription("Radio environment (UDGM)") @SupportedArguments(radioMediums = {UDGM.class}) public class UDGMVisualizerSkin implements VisualizerSkin { + private static final Logger logger = Logger.getLogger(UDGMVisualizerSkin.class); private static final Color COLOR_TX = new Color(0, 255, 0, 100); @@ -158,8 +158,8 @@ public class UDGMVisualizerSkin implements VisualizerSkin { txRangeSpinner.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { - radioMedium.setTxRange(((SpinnerNumberModel) - txRangeSpinner.getModel()).getNumber().doubleValue()); + radioMedium.setTxRange(((SpinnerNumberModel) txRangeSpinner.getModel()) + .getNumber().doubleValue()); visualizer.repaint(); } }); @@ -167,8 +167,8 @@ public class UDGMVisualizerSkin implements VisualizerSkin { interferenceRangeSpinner.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { - radioMedium.setInterferenceRange(((SpinnerNumberModel) - interferenceRangeSpinner.getModel()).getNumber().doubleValue()); + radioMedium.setInterferenceRange(((SpinnerNumberModel) interferenceRangeSpinner.getModel()) + .getNumber().doubleValue()); visualizer.repaint(); } }); @@ -176,8 +176,8 @@ public class UDGMVisualizerSkin implements VisualizerSkin { successRatioTxSpinner.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { - radioMedium.SUCCESS_RATIO_TX = ((SpinnerNumberModel) - successRatioTxSpinner.getModel()).getNumber().doubleValue(); + radioMedium.SUCCESS_RATIO_TX = ((SpinnerNumberModel) successRatioTxSpinner.getModel()) + .getNumber().doubleValue(); visualizer.repaint(); } }); @@ -185,8 +185,8 @@ public class UDGMVisualizerSkin implements VisualizerSkin { successRatioRxSpinner.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { - radioMedium.SUCCESS_RATIO_RX = ((SpinnerNumberModel) - successRatioRxSpinner.getModel()).getNumber().doubleValue(); + radioMedium.SUCCESS_RATIO_RX = ((SpinnerNumberModel) successRatioRxSpinner.getModel()) + .getNumber().doubleValue(); visualizer.repaint(); } }); @@ -199,7 +199,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin { JPanel main = new JPanel(); main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS)); main.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); - + rangeTX = Box.createHorizontalBox(); rangeTX.add(new JLabel("TX range:")); rangeTX.add(Box.createHorizontalStrut(5)); @@ -221,12 +221,12 @@ public class UDGMVisualizerSkin implements VisualizerSkin { rangeINT.setVisible(false); ratioTX.setVisible(false); ratioRX.setVisible(false); - + main.add(rangeTX); main.add(rangeINT); main.add(ratioTX); main.add(ratioRX); - + rrFrame = new JInternalFrame("UDGM", false, true); rrFrame.setVisible(false); rrFrame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); @@ -264,7 +264,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin { @Override public Color[] getColorOf(Mote mote) { if (visualizer.getSelectedMotes().contains(mote)) { - return new Color[] { Color.CYAN }; + return new Color[]{Color.CYAN}; } return null; } @@ -280,7 +280,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin { Area intRangeMaxArea = new Area(); Area trxRangeArea = new Area(); Area trxRangeMaxArea = new Area(); - + for (Mote selectedMote : selectedMotes) { if (selectedMote.getInterfaces().getRadio() == null) { continue; @@ -336,7 +336,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin { y - translatedTransmission.y, 2 * translatedTransmission.x, 2 * translatedTransmission.y))); - + intRangeMaxArea.add(new Area(new Ellipse2D.Double( x - translatedInterferenceMax.x, y - translatedInterferenceMax.y, @@ -349,16 +349,16 @@ public class UDGMVisualizerSkin implements VisualizerSkin { y - translatedTransmissionMax.y, 2 * translatedTransmissionMax.x, 2 * translatedTransmissionMax.y))); - + } - + Graphics2D g2d = (Graphics2D) g; - + g2d.setColor(COLOR_INT); g2d.fill(intRangeArea); g.setColor(Color.GRAY); g2d.draw(intRangeMaxArea); - + g.setColor(COLOR_TX); g2d.fill(trxRangeArea); g.setColor(Color.GRAY); @@ -371,20 +371,20 @@ public class UDGMVisualizerSkin implements VisualizerSkin { if (selectedMotes.size() == 1) { Mote selectedMote = selectedMotes.toArray(new Mote[0])[0]; Radio selectedRadio = selectedMote.getInterfaces().getRadio(); - for (Mote m: simulation.getMotes()) { - if (m == selectedMote) { - continue; - } - double prob = - ((UDGM) simulation.getRadioMedium()).getSuccessProbability(selectedRadio, m.getInterfaces().getRadio()); - if (prob == 0.0d) { - continue; - } - String msg = (((int)(1000*prob))/10.0) + "%"; - Position pos = m.getInterfaces().getPosition(); - Point pixel = visualizer.transformPositionToPixel(pos); - int msgWidth = fm.stringWidth(msg); - g.drawString(msg, pixel.x - msgWidth/2, pixel.y + 2*Visualizer.MOTE_RADIUS + 3); + for (Mote m : simulation.getMotes()) { + if (m == selectedMote) { + continue; + } + double prob + = ((UDGM) simulation.getRadioMedium()).getSuccessProbability(selectedRadio, m.getInterfaces().getRadio()); + if (prob == 0.0d) { + continue; + } + String msg = (((int) (1000 * prob)) / 10.0) + "%"; + Position pos = m.getInterfaces().getPosition(); + Point pixel = visualizer.transformPositionToPixel(pos); + int msgWidth = fm.stringWidth(msg); + g.drawString(msg, pixel.x - msgWidth / 2, pixel.y + 2 * Visualizer.MOTE_RADIUS + 3); } } @@ -395,6 +395,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin { } public static class RangeMenuAction implements SimulationMenuAction { + @Override public boolean isEnabled(Visualizer visualizer, Simulation simulation) { return true; @@ -420,6 +421,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin { }; public static class SuccessRatioMenuAction implements SimulationMenuAction { + @Override public boolean isEnabled(Visualizer visualizer, Simulation simulation) { return true; @@ -433,7 +435,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin { @Override public void doAction(Visualizer visualizer, Simulation simulation) { VisualizerSkin[] skins = visualizer.getCurrentSkins(); - for (VisualizerSkin skin: skins) { + for (VisualizerSkin skin : skins) { if (skin instanceof UDGMVisualizerSkin) { UDGMVisualizerSkin vskin = ((UDGMVisualizerSkin) skin); vskin.ratioTX.setVisible(true); From ad0e192d8490621881c3222ecdb00a2359096096 Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Thu, 10 Apr 2014 19:10:21 +0200 Subject: [PATCH 9/9] [cooja] plugins/Visualizer: Replaced multiple if-string with switch over strings --- .../contikios/cooja/plugins/Visualizer.java | 59 +++++++++---------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java b/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java index 6e6e5c2e9..733bafa6f 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java @@ -1477,14 +1477,13 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { showMoteToMoteRelations = false; for (Element element : configXML) { - if (element.getName().equals("skin")) { - String wanted = element.getText(); - /* Backwards compatibility: se.sics -> org.contikios */ - if (wanted.startsWith("se.sics")) { - wanted = wanted.replaceFirst("se\\.sics", "org.contikios"); - } - - for (Class skinClass : visualizerSkins) { + switch (element.getName()) { + case "skin": + String wanted = element.getText(); + /* Backwards compatibility: se.sics -> org.contikios */ + if (wanted.startsWith("se.sics")) { + wanted = wanted.replaceFirst("se\\.sics", "org.contikios"); + } for (Class skinClass : visualizerSkins) { if (wanted.equals(skinClass.getName()) /* Backwards compatibility */ || wanted.equals(Cooja.getDescriptionOf(skinClass))) { @@ -1498,35 +1497,33 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { wanted = null; break; } - } - if (wanted != null) { + } if (wanted != null) { logger.warn("Could not load visualizer: " + element.getText()); - } - } - else if (element.getName().equals("moterelations")) { - showMoteToMoteRelations = true; - } - else if (element.getName().equals("viewport")) { - try { - String[] matrix = element.getText().split(" "); - viewportTransform.setTransform( - Double.parseDouble(matrix[0]), + } break; + case "moterelations": + showMoteToMoteRelations = true; + break; + case "viewport": + try { + String[] matrix = element.getText().split(" "); + viewportTransform.setTransform( + Double.parseDouble(matrix[0]), Double.parseDouble(matrix[1]), Double.parseDouble(matrix[2]), Double.parseDouble(matrix[3]), Double.parseDouble(matrix[4]), Double.parseDouble(matrix[5]) - ); - resetViewport = 0; - } - catch (NumberFormatException e) { - logger.warn("Bad viewport: " + e.getMessage()); - resetViewport(); - } - } - else if (element.getName().equals("hidden")) { - BasicInternalFrameUI ui = (BasicInternalFrameUI) getUI(); - ui.getNorthPane().setPreferredSize(new Dimension(0, 0)); + ); + resetViewport = 0; + } + catch (NumberFormatException e) { + logger.warn("Bad viewport: " + e.getMessage()); + resetViewport(); + } break; + case "hidden": + BasicInternalFrameUI ui = (BasicInternalFrameUI) getUI(); + ui.getNorthPane().setPreferredSize(new Dimension(0, 0)); + break; } } return true;