[cooja] Reformatted Visualizer and skins code

This commit is contained in:
Enrico Joerns 2014-04-10 19:03:43 +02:00
parent ea979fa027
commit cef3c4079d
4 changed files with 399 additions and 321 deletions

View file

@ -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,6 +128,7 @@ public class MRMVisualizerSkin implements VisualizerSkin {
public Radio getFromRadio() {
return selectedMote.getInterfaces().getRadio();
}
@Override
public Radio getToRadio() {
return dRadio;
@ -144,19 +145,19 @@ public class MRMVisualizerSkin implements VisualizerSkin {
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);
}
}

View file

@ -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<Class<? extends VisualizerSkin>> visualizerSkins =
new ArrayList<>();
private static final ArrayList<Class<? extends VisualizerSkin>> 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<Class<? extends SimulationMenuAction>> simulationMenuActions =
new ArrayList<>();
private ArrayList<Class<? extends MoteMenuAction>> moteMenuActions =
new ArrayList<>();
private ArrayList<Class<? extends SimulationMenuAction>> simulationMenuActions
= new ArrayList<>();
private ArrayList<Class<? extends MoteMenuAction>> moteMenuActions
= new ArrayList<>();
public Visualizer(Simulation simulation, Cooja gui) {
super("Network", gui);
@ -236,7 +244,7 @@ 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<? extends VisualizerSkin> 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) {
}
@ -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()) {
@ -454,7 +468,8 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
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();
@ -537,6 +559,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
handleDropFile(file, dtde.getLocation());
}
private boolean acceptOrRejectDrag(DropTargetDragEvent dtde) {
Transferable transferable = dtde.getTransferable();
@ -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;
}
@ -584,7 +608,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
}
private void generateAndActivateSkin(Class<? extends VisualizerSkin> 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<? extends VisualizerSkin> skinClass =
simulation.getCooja().tryLoadClass(this, VisualizerSkin.class, skin);
Class<? extends VisualizerSkin> 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<? extends MoteMenuAction> menuActionClass: moteMenuActions) {
for (Class<? extends MoteMenuAction> 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);
}
}
@ -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() {
@Override
public void itemStateChanged(ItemEvent e) {
JCheckBoxMenuItem menuItem = ((JCheckBoxMenuItem)e.getItem());
JCheckBoxMenuItem menuItem = ((JCheckBoxMenuItem) e.getItem());
showMoteToMoteRelations = menuItem.isSelected();
repaint();
}
});
if (menu instanceof JMenu) {
((JMenu)menu).add(moteRelationsItem);
((JMenu)menu).add(new JSeparator());
((JMenu) menu).add(moteRelationsItem);
((JMenu) menu).add(new JSeparator());
}
if (menu instanceof JPopupMenu) {
((JPopupMenu)menu).add(moteRelationsItem);
((JPopupMenu)menu).add(new JSeparator());
((JPopupMenu) menu).add(moteRelationsItem);
((JPopupMenu) menu).add(new JSeparator());
}
for (Class<? extends VisualizerSkin> skinClass: visualizerSkins) {
for (Class<? extends VisualizerSkin> 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<VisualizerSkin> skinClass =
(Class<VisualizerSkin>) menuItem.getClientProperty("skinclass");
Class<VisualizerSkin> skinClass
= (Class<VisualizerSkin>) 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<? extends RadioMedium>[] radioMediums = skinClass.getAnnotation(SupportedArguments.class).radioMediums();
for (Class<? extends Object> o: radioMediums) {
for (Class<? extends Object> 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,7 +1058,7 @@ 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) {
@ -1121,7 +1151,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
/* Paint mote relations */
if (showMoteToMoteRelations) {
MoteRelation[] relations = simulation.getCooja().getMoteRelations();
for (MoteRelation r: relations) {
for (MoteRelation r : relations) {
Position sourcePos = r.source.getInterfaces().getPosition();
Position destPos = r.dest.getInterfaces().getPosition();
@ -1133,11 +1163,11 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
}
}
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;
@ -1157,11 +1187,13 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
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)) {
}
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) {
}
else if (moteColors.length >= 2) {
g.setColor(moteColors[0]);
g.fillOval(x - MOTE_RADIUS, y - MOTE_RADIUS, 2 * MOTE_RADIUS,
2 * MOTE_RADIUS);
@ -1170,7 +1202,8 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
g.fillOval(x - MOTE_RADIUS / 2, y - MOTE_RADIUS / 2, 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);
@ -1183,6 +1216,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
}
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,17 +1279,19 @@ 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
@ -1266,8 +1302,8 @@ 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;
@ -1325,6 +1361,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
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(
@ -1336,21 +1373,24 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
}
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) {
@ -1398,7 +1439,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
}
/* Skins */
for (int i=currentSkins.size()-1; i >= 0; i--) {
for (int i = currentSkins.size() - 1; i >= 0; i--) {
VisualizerSkin skin = currentSkins.get(i);
element = new Element("skin");
element.setText(skin.getClass().getName());
@ -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);
}
@ -1443,7 +1484,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
wanted = wanted.replaceFirst("se\\.sics", "org.contikios");
}
for (Class<? extends VisualizerSkin> skinClass: visualizerSkins) {
for (Class<? extends VisualizerSkin> skinClass : visualizerSkins) {
if (wanted.equals(skinClass.getName())
/* Backwards compatibility */
|| wanted.equals(Cooja.getDescriptionOf(skinClass))) {
@ -1461,9 +1502,11 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
if (wanted != null) {
logger.warn("Could not load visualizer: " + element.getText());
}
} else if (element.getName().equals("moterelations")) {
}
else if (element.getName().equals("moterelations")) {
showMoteToMoteRelations = true;
} else if (element.getName().equals("viewport")) {
}
else if (element.getName().equals("viewport")) {
try {
String[] matrix = element.getText().split(" ");
viewportTransform.setTransform(
@ -1475,13 +1518,15 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
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();
}
@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,8 +1606,8 @@ 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 viewer
= (MoteInterfaceViewer) simulation.getCooja().tryStartPlugin(
MoteInterfaceViewer.class,
simulation.getCooja(),
simulation,
@ -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,8 +1655,8 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
/* Extract description (input to plugin) */
String desc = Cooja.getDescriptionOf(serialPort);
MoteInterfaceViewer viewer =
(MoteInterfaceViewer) simulation.getCooja().tryStartPlugin(
MoteInterfaceViewer viewer
= (MoteInterfaceViewer) simulation.getCooja().tryStartPlugin(
MoteInterfaceViewer.class,
simulation.getCooja(),
simulation,
@ -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;

View file

@ -27,7 +27,6 @@
* SUCH DAMAGE.
*
*/
package org.contikios.cooja.plugins.skins;
import java.awt.Color;
@ -53,6 +52,7 @@ 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;
@ -79,7 +79,7 @@ public class DGRMVisualizerSkin 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;
}
@ -124,16 +124,24 @@ public class DGRMVisualizerSkin implements VisualizerSkin {
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 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;
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;
}
@ -148,7 +156,7 @@ public class DGRMVisualizerSkin implements VisualizerSkin {
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);
g.drawString(msg, pixel.x - msgWidth / 2, pixel.y + 2 * Visualizer.MOTE_RADIUS + 3);
}
}
}

View file

@ -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();
}
});
@ -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;
}
@ -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()) {
for (Mote m : simulation.getMotes()) {
if (m == selectedMote) {
continue;
}
double prob =
((UDGM) simulation.getRadioMedium()).getSuccessProbability(selectedRadio, m.getInterfaces().getRadio());
double prob
= ((UDGM) simulation.getRadioMedium()).getSuccessProbability(selectedRadio, m.getInterfaces().getRadio());
if (prob == 0.0d) {
continue;
}
String msg = (((int)(1000*prob))/10.0) + "%";
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);
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);