Merge pull request #568 from ejoerns/pull-req/cooja-udgmvis

[Cooja] Made UDGMVisualizerSkin closable
This commit is contained in:
Fredrik Österlind 2014-02-21 13:01:23 +01:00
commit 4fe17ef17f

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2009, Swedish Institute of Computer Science. * Copyright (c) 2009-2013, Swedish Institute of Computer Science, TU Braunscheig
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -30,18 +30,28 @@
package org.contikios.cooja.plugins.skins; package org.contikios.cooja.plugins.skins;
import java.awt.BorderLayout;
import java.awt.Color; import java.awt.Color;
import java.awt.FontMetrics; import java.awt.FontMetrics;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Point; import java.awt.Point;
import java.beans.PropertyVetoException;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.Box; import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JInternalFrame;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JSpinner; import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel; import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import static javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE;
import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener; import javax.swing.event.ChangeListener;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -68,21 +78,24 @@ import org.contikios.cooja.radiomediums.UDGM;
* @see TrafficVisualizerSkin * @see TrafficVisualizerSkin
* @see UDGM * @see UDGM
* @author Fredrik Osterlind * @author Fredrik Osterlind
* @author Enrico Joerns
*/ */
@ClassDescription("Radio environment (UDGM)") @ClassDescription("Radio environment (UDGM)")
@SupportedArguments(radioMediums = {UDGM.class}) @SupportedArguments(radioMediums = {UDGM.class})
public class UDGMVisualizerSkin implements VisualizerSkin { public class UDGMVisualizerSkin implements VisualizerSkin {
private static Logger logger = Logger.getLogger(UDGMVisualizerSkin.class); private static final Logger logger = Logger.getLogger(UDGMVisualizerSkin.class);
private static Color COLOR_TX = new Color(0, 255, 0, 100); private static final Color COLOR_TX = new Color(0, 255, 0, 100);
private static Color COLOR_INT = new Color(50, 50, 50, 100); private static final Color COLOR_INT = new Color(50, 50, 50, 100);
private Simulation simulation = null; private Simulation simulation = null;
private Visualizer visualizer = null; private Visualizer visualizer = null;
private UDGM radioMedium = null; private UDGM radioMedium = null;
private Box top, ratioRX, ratioTX, rangeTX, rangeINT; private JInternalFrame rrFrame;
private Box ratioRX, ratioTX, rangeTX, rangeINT;
@Override
public void setActive(Simulation simulation, Visualizer vis) { public void setActive(Simulation simulation, Visualizer vis) {
if (!(simulation.getRadioMedium() instanceof UDGM)) { if (!(simulation.getRadioMedium() instanceof UDGM)) {
logger.fatal("Cannot activate UDGM skin for unknown radio medium: " + simulation.getRadioMedium()); logger.fatal("Cannot activate UDGM skin for unknown radio medium: " + simulation.getRadioMedium());
@ -139,6 +152,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
successRatioRxSpinner.setToolTipText("Reception success ratio (%)"); successRatioRxSpinner.setToolTipText("Reception success ratio (%)");
txRangeSpinner.addChangeListener(new ChangeListener() { txRangeSpinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
radioMedium.setTxRange(((SpinnerNumberModel) radioMedium.setTxRange(((SpinnerNumberModel)
txRangeSpinner.getModel()).getNumber().doubleValue()); txRangeSpinner.getModel()).getNumber().doubleValue());
@ -147,6 +161,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
}); });
interferenceRangeSpinner.addChangeListener(new ChangeListener() { interferenceRangeSpinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
radioMedium.setInterferenceRange(((SpinnerNumberModel) radioMedium.setInterferenceRange(((SpinnerNumberModel)
interferenceRangeSpinner.getModel()).getNumber().doubleValue()); interferenceRangeSpinner.getModel()).getNumber().doubleValue());
@ -155,6 +170,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
}); });
successRatioTxSpinner.addChangeListener(new ChangeListener() { successRatioTxSpinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
radioMedium.SUCCESS_RATIO_TX = ((SpinnerNumberModel) radioMedium.SUCCESS_RATIO_TX = ((SpinnerNumberModel)
successRatioTxSpinner.getModel()).getNumber().doubleValue(); successRatioTxSpinner.getModel()).getNumber().doubleValue();
@ -163,6 +179,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
}); });
successRatioRxSpinner.addChangeListener(new ChangeListener() { successRatioRxSpinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
radioMedium.SUCCESS_RATIO_RX = ((SpinnerNumberModel) radioMedium.SUCCESS_RATIO_RX = ((SpinnerNumberModel)
successRatioRxSpinner.getModel()).getNumber().doubleValue(); successRatioRxSpinner.getModel()).getNumber().doubleValue();
@ -175,12 +192,9 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
visualizer.registerSimulationMenuAction(SuccessRatioMenuAction.class); visualizer.registerSimulationMenuAction(SuccessRatioMenuAction.class);
/* UI components */ /* UI components */
top = Box.createVerticalBox(); JPanel main = new JPanel();
top.setBorder(BorderFactory.createCompoundBorder( main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
BorderFactory.createLineBorder(Color.BLACK), main.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
BorderFactory.createEmptyBorder(0, 3, 0, 3)));
top.setOpaque(true);
top.setBackground(Color.LIGHT_GRAY);
rangeTX = Box.createHorizontalBox(); rangeTX = Box.createHorizontalBox();
rangeTX.add(new JLabel("TX range:")); rangeTX.add(new JLabel("TX range:"));
@ -203,16 +217,32 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
rangeINT.setVisible(false); rangeINT.setVisible(false);
ratioTX.setVisible(false); ratioTX.setVisible(false);
ratioRX.setVisible(false); ratioRX.setVisible(false);
top.setVisible(false);
top.add(rangeTX); main.add(rangeTX);
top.add(rangeINT); main.add(rangeINT);
top.add(ratioTX); main.add(ratioTX);
top.add(ratioRX); main.add(ratioRX);
visualizer.getCurrentCanvas().add(top); rrFrame = new JInternalFrame("UDGM", false, true);
rrFrame.setVisible(false);
rrFrame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
rrFrame.addInternalFrameListener(new InternalFrameAdapter() {
@Override
public void internalFrameClosing(InternalFrameEvent ife) {
super.internalFrameClosed(ife);
rangeTX.setVisible(false);
rangeINT.setVisible(false);
ratioTX.setVisible(false);
ratioRX.setVisible(false);
rrFrame.setVisible(false);
}
});
rrFrame.getContentPane().add(BorderLayout.CENTER, main);
rrFrame.pack();
} }
@Override
public void setInactive() { public void setInactive() {
if (simulation == null) { if (simulation == null) {
/* Skin was never activated */ /* Skin was never activated */
@ -220,13 +250,14 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
} }
/* Remove spinners etc */ /* Remove spinners etc */
visualizer.getCurrentCanvas().remove(top); visualizer.getCurrentCanvas().remove(rrFrame);
/* Unregister menu actions */ /* Unregister menu actions */
visualizer.unregisterSimulationMenuAction(RangeMenuAction.class); visualizer.unregisterSimulationMenuAction(RangeMenuAction.class);
visualizer.unregisterSimulationMenuAction(SuccessRatioMenuAction.class); visualizer.unregisterSimulationMenuAction(SuccessRatioMenuAction.class);
} }
@Override
public Color[] getColorOf(Mote mote) { public Color[] getColorOf(Mote mote) {
Mote selectedMote = visualizer.getSelectedMote(); Mote selectedMote = visualizer.getSelectedMote();
if (mote == selectedMote) { if (mote == selectedMote) {
@ -235,6 +266,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
return null; return null;
} }
@Override
public void paintBeforeMotes(Graphics g) { public void paintBeforeMotes(Graphics g) {
Mote selectedMote = visualizer.getSelectedMote(); Mote selectedMote = visualizer.getSelectedMote();
if (simulation == null if (simulation == null
@ -334,53 +366,86 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
} }
@Override
public void paintAfterMotes(Graphics g) { public void paintAfterMotes(Graphics g) {
} }
public static class RangeMenuAction implements SimulationMenuAction { public static class RangeMenuAction implements SimulationMenuAction {
@Override
public boolean isEnabled(Visualizer visualizer, Simulation simulation) { public boolean isEnabled(Visualizer visualizer, Simulation simulation) {
return true; return true;
} }
@Override
public String getDescription(Visualizer visualizer, Simulation simulation) { public String getDescription(Visualizer visualizer, Simulation simulation) {
return "Change transmission ranges"; return "Change transmission ranges";
} }
@Override
public void doAction(Visualizer visualizer, Simulation simulation) { public void doAction(Visualizer visualizer, Simulation simulation) {
VisualizerSkin[] skins = visualizer.getCurrentSkins(); VisualizerSkin[] skins = visualizer.getCurrentSkins();
for (VisualizerSkin skin : skins) { for (VisualizerSkin skin : skins) {
if (skin instanceof UDGMVisualizerSkin) { if (skin instanceof UDGMVisualizerSkin) {
((UDGMVisualizerSkin)skin).rangeTX.setVisible(true); UDGMVisualizerSkin vskin = ((UDGMVisualizerSkin) skin);
((UDGMVisualizerSkin)skin).rangeINT.setVisible(true); vskin.rangeTX.setVisible(true);
((UDGMVisualizerSkin)skin).top.setVisible(true); vskin.rangeINT.setVisible(true);
visualizer.repaint(); vskin.updateRatioRangeFrame();
} }
} }
} }
}; };
public static class SuccessRatioMenuAction implements SimulationMenuAction { public static class SuccessRatioMenuAction implements SimulationMenuAction {
@Override
public boolean isEnabled(Visualizer visualizer, Simulation simulation) { public boolean isEnabled(Visualizer visualizer, Simulation simulation) {
return true; return true;
} }
@Override
public String getDescription(Visualizer visualizer, Simulation simulation) { public String getDescription(Visualizer visualizer, Simulation simulation) {
return "Change TX/RX success ratio"; return "Change TX/RX success ratio";
} }
@Override
public void doAction(Visualizer visualizer, Simulation simulation) { public void doAction(Visualizer visualizer, Simulation simulation) {
VisualizerSkin[] skins = visualizer.getCurrentSkins(); VisualizerSkin[] skins = visualizer.getCurrentSkins();
for (VisualizerSkin skin: skins) { for (VisualizerSkin skin: skins) {
if (skin instanceof UDGMVisualizerSkin) { if (skin instanceof UDGMVisualizerSkin) {
((UDGMVisualizerSkin)skin).ratioTX.setVisible(true); UDGMVisualizerSkin vskin = ((UDGMVisualizerSkin) skin);
((UDGMVisualizerSkin)skin).ratioRX.setVisible(true); vskin.ratioTX.setVisible(true);
((UDGMVisualizerSkin)skin).top.setVisible(true); vskin.ratioRX.setVisible(true);
visualizer.repaint(); vskin.updateRatioRangeFrame();
} }
} }
} }
}; };
private void updateRatioRangeFrame() {
if (rrFrame.getDesktopPane() == null) {
visualizer.getDesktopPane().add(rrFrame);
}
rrFrame.pack();
/* Place frame at the upper right corner of the visualizer canvas */
Point visCanvasPos = SwingUtilities.convertPoint(
visualizer.getCurrentCanvas(),
visualizer.getCurrentCanvas().getLocation(),
visualizer.getDesktopPane());
rrFrame.setLocation(
visCanvasPos.x + visualizer.getCurrentCanvas().getWidth() - rrFrame.getWidth(),
visCanvasPos.y);
/* Try to place on top with focus */
rrFrame.setLayer(JLayeredPane.MODAL_LAYER);
rrFrame.setVisible(true);
rrFrame.moveToFront();
try {
rrFrame.setSelected(true);
}
catch (PropertyVetoException ex) {
logger.warn("Failed getting focus");
}
}
@Override
public Visualizer getVisualizer() { public Visualizer getVisualizer() {
return visualizer; return visualizer;
} }