Added simple spring layout that attracts connected nodes in node visualizer

This commit is contained in:
nifi 2010-10-10 22:39:09 +00:00
parent 5988b06bd1
commit febb07a71b
3 changed files with 386 additions and 265 deletions

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: CollectServer.java,v 1.24 2010/10/07 21:13:00 nifi Exp $ * $Id: CollectServer.java,v 1.25 2010/10/10 22:39:09 nifi Exp $
* *
* ----------------------------------------------------------------- * -----------------------------------------------------------------
* *
@ -34,8 +34,8 @@
* *
* Authors : Joakim Eriksson, Niclas Finne * Authors : Joakim Eriksson, Niclas Finne
* Created : 3 jul 2008 * Created : 3 jul 2008
* Updated : $Date: 2010/10/07 21:13:00 $ * Updated : $Date: 2010/10/10 22:39:09 $
* $Revision: 1.24 $ * $Revision: 1.25 $
*/ */
package se.sics.contiki.collect; package se.sics.contiki.collect;
@ -229,7 +229,7 @@ public class CollectServer implements SerialConnectionListener {
categoryTable.put(MAIN, mainPanel); categoryTable.put(MAIN, mainPanel);
serialConsole = new SerialConsole(this, MAIN); serialConsole = new SerialConsole(this, MAIN);
mapPanel = new MapPanel(this, MAIN); mapPanel = new MapPanel(this, "Sensor Map", MAIN, true);
String image = getConfig("collect.mapimage"); String image = getConfig("collect.mapimage");
if (image != null) { if (image != null) {
mapPanel.setMapBackground(image); mapPanel.setMapBackground(image);
@ -237,6 +237,7 @@ public class CollectServer implements SerialConnectionListener {
final int defaultMaxItemCount = 250; final int defaultMaxItemCount = 250;
visualizers = new Visualizer[] { visualizers = new Visualizer[] {
mapPanel, mapPanel,
new MapPanel(this, "Network Graph", MAIN, false),
new BarChartPanel(this, SENSORS, "Average Temperature", "Temperature", "Nodes", "Celsius", new BarChartPanel(this, SENSORS, "Average Temperature", "Temperature", "Nodes", "Celsius",
new String[] { "Celsius" }) { new String[] { "Celsius" }) {
{ {
@ -867,7 +868,6 @@ public class CollectServer implements SerialConnectionListener {
if (node == null) { if (node == null) {
node = new Node(nodeID); node = new Node(nodeID);
nodeTable.put(nodeID, node); nodeTable.put(nodeID, node);
updateNodeLocation(node);
synchronized (this) { synchronized (this) {
nodeCache = null; nodeCache = null;
@ -940,31 +940,6 @@ public class CollectServer implements SerialConnectionListener {
// Node location handling // Node location handling
// ------------------------------------------------------------------- // -------------------------------------------------------------------
public boolean updateNodeLocation(Node node) {
String id = node.getID();
if (node.hasLocation()) {
String location = "" + node.getX() + ',' + node.getY();
if (!location.equals(configTable.get(id))) {
configTable.put(id, location);
}
return false;
}
String location = configTable.getProperty(id);
if (location != null) {
try {
String[] pos = location.split(",");
node.setLocation(Integer.parseInt(pos[0].trim()),
Integer.parseInt(pos[1].trim()));
return true;
} catch (Exception e) {
System.err.println("could not parse node location: " + location);
e.printStackTrace();
}
}
return false;
}
private boolean loadConfig(Properties properties, String configFile) { private boolean loadConfig(Properties properties, String configFile) {
try { try {
BufferedInputStream input = BufferedInputStream input =

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: Node.java,v 1.7 2010/10/07 21:13:00 nifi Exp $ * $Id: Node.java,v 1.8 2010/10/10 22:39:09 nifi Exp $
* *
* ----------------------------------------------------------------- * -----------------------------------------------------------------
* *
@ -34,8 +34,8 @@
* *
* Authors : Joakim Eriksson, Niclas Finne * Authors : Joakim Eriksson, Niclas Finne
* Created : 3 jul 2008 * Created : 3 jul 2008
* Updated : $Date: 2010/10/07 21:13:00 $ * Updated : $Date: 2010/10/10 22:39:09 $
* $Revision: 1.7 $ * $Revision: 1.8 $
*/ */
package se.sics.contiki.collect; package se.sics.contiki.collect;
@ -56,9 +56,6 @@ public class Node implements Comparable<Node> {
private final String id; private final String id;
private final String name; private final String name;
public int x = -1;
public int y = -1;
private Hashtable<String,Object> objectTable; private Hashtable<String,Object> objectTable;
private long lastActive; private long lastActive;
@ -77,23 +74,6 @@ public class Node implements Comparable<Node> {
return name; return name;
} }
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setLocation(int x, int y) {
this.x = x;
this.y = y;
}
public boolean hasLocation() {
return x >= 0 && y >= 0;
}
public long getLastActive() { public long getLastActive() {
return lastActive; return lastActive;
} }

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: MapPanel.java,v 1.3 2010/09/15 16:15:10 nifi Exp $ * $Id: MapPanel.java,v 1.4 2010/10/10 22:39:09 nifi Exp $
* *
* ----------------------------------------------------------------- * -----------------------------------------------------------------
* *
@ -34,12 +34,11 @@
* *
* Authors : Joakim Eriksson, Niclas Finne * Authors : Joakim Eriksson, Niclas Finne
* Created : 3 jul 2008 * Created : 3 jul 2008
* Updated : $Date: 2010/09/15 16:15:10 $ * Updated : $Date: 2010/10/10 22:39:09 $
* $Revision: 1.3 $ * $Revision: 1.4 $
*/ */
package se.sics.contiki.collect.gui; package se.sics.contiki.collect.gui;
import java.awt.BorderLayout;
import java.awt.Color; import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.Cursor; import java.awt.Cursor;
@ -47,22 +46,35 @@ import java.awt.Dimension;
import java.awt.FontMetrics; import java.awt.FontMetrics;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener; import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Properties;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JPopupMenu; import javax.swing.JPopupMenu;
import javax.swing.JSlider;
import javax.swing.Timer; import javax.swing.Timer;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.basic.BasicGraphicsUtils; import javax.swing.plaf.basic.BasicGraphicsUtils;
import se.sics.contiki.collect.CollectServer; import se.sics.contiki.collect.CollectServer;
import se.sics.contiki.collect.Configurable;
import se.sics.contiki.collect.Link; import se.sics.contiki.collect.Link;
import se.sics.contiki.collect.Node; import se.sics.contiki.collect.Node;
import se.sics.contiki.collect.SensorData; import se.sics.contiki.collect.SensorData;
@ -71,48 +83,40 @@ import se.sics.contiki.collect.Visualizer;
/** /**
* *
*/ */
public class MapPanel extends JPanel implements Visualizer, ActionListener, MouseListener, MouseMotionListener { public class MapPanel extends JPanel implements Configurable, Visualizer, ActionListener, MouseListener, MouseMotionListener {
private static final long serialVersionUID = -8256619482599309425L; private static final long serialVersionUID = -8256619482599309425L;
private static final Logger log = private static final Logger log =
Logger.getLogger(MapPanel.class.getName()); Logger.getLogger(MapPanel.class.getName());
private static final boolean VISUAL_DRAG = false; private static final boolean VISUAL_DRAG = true;
private static final int FADE_COUNT = 20; private static final Color LINK_COLOR = new Color(0x40, 0x40, 0xf0, 0xff);
private static final int AGE_COUNT = 200;
private static final Color[] OTHER_COLOR = new Color[FADE_COUNT];
private static final Color[] LINK_COLOR = new Color[AGE_COUNT];
static {
for (int i = 0; i < FADE_COUNT; i++) {
OTHER_COLOR[i] = new Color(0xe0,0xe0,0x00,0xFF
- ((i * 255) / (FADE_COUNT - 1)));
}
for (int i = 0, n = AGE_COUNT; i < n; i++) {
LINK_COLOR[i] = new Color(0x40 + i / 2, 0x40 + i / 2, 0xf0, 0xff - i);
}
}
private static final int delta = 7; private static final int delta = 7;
public static final int SHOW_BLINK = 40; private final CollectServer server;
public static final int TOTAL_SHOW = 600; private final String category;
private final boolean isMap;
private String title;
private Timer timer = new Timer(400, this); private Timer timer;
private boolean hasPendingEvents = false;
private int ticker = 0;
private JPopupMenu popupMenu; private JPopupMenu popupMenu;
// private MapNode popupNode; private JCheckBoxMenuItem layoutItem;
private JMenuItem hideItem; private JCheckBoxMenuItem lockedItem;
private JMenuItem shakeItem;
// private JCheckBoxMenuItem dragItem;
private JCheckBoxMenuItem backgroundItem;
private JCheckBoxMenuItem showNetworkItem;
private JCheckBoxMenuItem configItem;
private JMenuItem resetNetworkItem; private JMenuItem resetNetworkItem;
private MapNode popupNode;
private Hashtable<String,MapNode> nodeTable = new Hashtable<String,MapNode>(); private Hashtable<String,MapNode> nodeTable = new Hashtable<String,MapNode>();
private MapNode[] nodeList; private MapNode[] nodeList = new MapNode[0];
private boolean updateNodeList;
private MapNode selectedNode; private MapNode selectedNode;
private ArrayList<MapNode> selectedMapNodes = new ArrayList<MapNode>(); private ArrayList<MapNode> selectedMapNodes = new ArrayList<MapNode>();
@ -122,36 +126,120 @@ public class MapPanel extends JPanel implements Visualizer, ActionListener, Mous
private ImageIcon mapImage; private ImageIcon mapImage;
private String mapName; private String mapName;
private boolean showBackground;
private final CollectServer server; private int layoutRepel = 100;
private final String category; private int layoutAttract = 50;
private int layoutGravity = 1;
private boolean isLayoutActive = true;
private boolean hideNetwork = false; private boolean hideNetwork = false;
public MapPanel(CollectServer server, String category) { protected JPanel configPanel;
super(new BorderLayout());
public MapPanel(CollectServer server, String title, String category, boolean isMap) {
super(null);
this.server = server; this.server = server;
this.title = title;
this.category = category; this.category = category;
this.isMap = isMap;
setPreferredSize(new Dimension(300, 200)); setPreferredSize(new Dimension(300, 200));
popupMenu = new JPopupMenu(getTitle()); popupMenu = new JPopupMenu(getTitle());
// popupMenu.addSeparator(); if (!isMap) {
hideItem = createMenuItem(popupMenu, "Hide Network"); layoutItem = createCheckBoxMenuItem(popupMenu, "Update Layout", isLayoutActive);
resetNetworkItem = createMenuItem(popupMenu, "Reset Network"); popupMenu.add(layoutItem);
lockedItem = createCheckBoxMenuItem(popupMenu, "Fixed Node Position", false);
shakeItem = createMenuItem(popupMenu, "Shake Nodes");
popupMenu.addSeparator();
}
showNetworkItem = createCheckBoxMenuItem(popupMenu, "Show Network Info", true);
resetNetworkItem = createMenuItem(popupMenu, "Reset Network");
popupMenu.addSeparator();
if (isMap) {
backgroundItem = createCheckBoxMenuItem(popupMenu, "Show Background", false);
backgroundItem.setEnabled(false);
} else {
configItem = createCheckBoxMenuItem(popupMenu, "Show Layout Settings", false);
}
// popupMenu.addSeparator();
// dragItem = new JCheckBoxMenuItem("Visible Drag", true);
// popupMenu.add(dragItem);
setBackground(Color.white);
addMouseListener(this); addMouseListener(this);
addMouseMotionListener(this); addMouseMotionListener(this);
setBackground(Color.white);
if (!isMap) {
timer = new Timer(100, this);
configPanel = new JPanel(new GridLayout(0, 1));
configPanel.setBorder(LineBorder.createBlackLineBorder());
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 1000, 1000 - layoutAttract);
slider.setBorder(new TitledBorder("Attract Factor: " + (1000 - layoutAttract)));
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSlider slider = (JSlider)e.getSource();
layoutAttract = 1000 - slider.getValue();
((TitledBorder)slider.getBorder()).setTitle("Attract Factor: " + slider.getValue());
}
});
configPanel.add(slider);
slider = new JSlider(JSlider.HORIZONTAL, 0, 1000, layoutRepel);
slider.setBorder(new TitledBorder("Repel Range: " + layoutRepel));
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSlider slider = (JSlider)e.getSource();
layoutRepel = slider.getValue();
((TitledBorder)slider.getBorder()).setTitle("Repel Range: " + layoutRepel);
}
});
configPanel.add(slider);
slider = new JSlider(JSlider.HORIZONTAL, 0, 100, layoutGravity);
slider.setBorder(new TitledBorder("Gravity: " + layoutGravity));
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSlider slider = (JSlider)e.getSource();
layoutGravity = slider.getValue();
((TitledBorder)slider.getBorder()).setTitle("Gravity: " + layoutGravity);
}
});
configPanel.add(slider);
add(configPanel);
configPanel.setVisible(false);
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ev) {
if (configPanel.isVisible()) {
updateConfigLayout();
}
}
});
}
} }
public String getMapBackground() { public String getMapBackground() {
return mapName; return isMap ? mapName : null;
} }
public boolean setMapBackground(String image) { public boolean setMapBackground(String image) {
if (!isMap) {
return false;
}
if (image == null) { if (image == null) {
mapImage = null; mapImage = null;
mapName = null; mapName = null;
backgroundItem.setEnabled(false);
backgroundItem.setSelected(false);
showBackground = false;
repaint();
return true; return true;
} }
@ -163,10 +251,20 @@ public class MapPanel extends JPanel implements Visualizer, ActionListener, Mous
mapImage = ii; mapImage = ii;
mapName = image; mapName = image;
setPreferredSize(new Dimension(ii.getIconWidth(), ii.getIconHeight())); setPreferredSize(new Dimension(ii.getIconWidth(), ii.getIconHeight()));
showBackground = true;
backgroundItem.setEnabled(true);
backgroundItem.setSelected(true);
repaint(); repaint();
return true; return true;
} }
private JCheckBoxMenuItem createCheckBoxMenuItem(JPopupMenu menu, String title, boolean isSelected) {
JCheckBoxMenuItem item = new JCheckBoxMenuItem(title, isSelected);
item.addActionListener(this);
menu.add(item);
return item;
}
private JMenuItem createMenuItem(JPopupMenu menu, String title) { private JMenuItem createMenuItem(JPopupMenu menu, String title) {
JMenuItem item = new JMenuItem(title); JMenuItem item = new JMenuItem(title);
item.addActionListener(this); item.addActionListener(this);
@ -177,9 +275,13 @@ public class MapPanel extends JPanel implements Visualizer, ActionListener, Mous
public void setVisible(boolean visible) { public void setVisible(boolean visible) {
if (visible) { if (visible) {
clear(); clear();
timer.start(); if (timer != null) {
timer.start();
}
} else { } else {
timer.stop(); if (timer != null) {
timer.stop();
}
} }
super.setVisible(visible); super.setVisible(visible);
} }
@ -187,7 +289,6 @@ public class MapPanel extends JPanel implements Visualizer, ActionListener, Mous
public void clear() { public void clear() {
setCursor(Cursor.getDefaultCursor()); setCursor(Cursor.getDefaultCursor());
draggedNode = null; draggedNode = null;
hasPendingEvents = false;
updateSelected(); updateSelected();
} }
@ -206,15 +307,40 @@ public class MapPanel extends JPanel implements Visualizer, ActionListener, Mous
} }
private MapNode addMapNode(Node nd) { private MapNode addMapNode(Node nd) {
MapNode node = nodeTable.get(nd.getID()); String id = nd.getID();
MapNode node = nodeTable.get(id);
if (node == null) { if (node == null) {
node = new MapNode(this, nd); node = new MapNode(this, nd);
synchronized (this) { node.y = 10 + (int) (Math.random() * (Math.max(100, getHeight()) - 20));
nodeTable.put("" + nd.getID(), node); node.x = 10 + (int) (Math.random() * (Math.max(100, getWidth()) - 30));
String location = server.getConfig(isMap ? id : ("collect.map." + id));
if (location != null) {
try {
String[] pos = location.split(",");
node.x = Integer.parseInt(pos[0].trim());
node.y = Integer.parseInt(pos[1].trim());
node.hasFixedLocation = !isMap;
} catch (Exception e) {
System.err.println("could not parse node location: " + location);
e.printStackTrace();
}
}
nodeTable.put(id, node);
updateNodeList = true;
}
return node;
}
private MapNode[] getNodeList() {
if (updateNodeList) {
synchronized (nodeTable) {
updateNodeList = false;
nodeList = nodeTable.values().toArray(new MapNode[nodeTable.size()]); nodeList = nodeTable.values().toArray(new MapNode[nodeTable.size()]);
} }
} }
return node; return nodeList;
} }
@ -229,7 +355,7 @@ public class MapPanel extends JPanel implements Visualizer, ActionListener, Mous
@Override @Override
public String getTitle() { public String getTitle() {
return "Sensor Map"; return title;
} }
@Override @Override
@ -286,7 +412,12 @@ public class MapPanel extends JPanel implements Visualizer, ActionListener, Mous
@Override @Override
public void clearNodeData() { public void clearNodeData() {
// Ignore nodeTable.clear();
updateNodeList = true;
nodesSelected(null);
if (isVisible()) {
repaint();
}
} }
@ -297,80 +428,53 @@ public class MapPanel extends JPanel implements Visualizer, ActionListener, Mous
@Override @Override
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g; Graphics2D g2d = (Graphics2D) g;
int lx = 10;
long time = System.currentTimeMillis();
g.setColor(getBackground()); g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight()); g.fillRect(0, 0, getWidth(), getHeight());
if (mapImage != null) { if (showBackground && isMap) {
mapImage.paintIcon(this, g, 0, 0); mapImage.paintIcon(this, g, 0, 0);
} }
MapNode[] nodes = nodeList; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
if (nodes != null) { RenderingHints.VALUE_ANTIALIAS_ON);
Line2D line = new Line2D.Double(); for (MapNode n : getNodeList()) {
for (int i = 0, m = nodes.length; i < m; i++) { if (!isMap || !hideNetwork) {
MapNode n = nodes[i]; FontMetrics fm = g.getFontMetrics();
int x, y; int fnHeight = fm.getHeight();
if (n.node.hasLocation()) { int fnDescent = fm.getDescent();
x = n.node.getX(); g.setColor(LINK_COLOR);
y = n.node.getY(); for (int j = 0, mu = n.node.getLinkCount(); j < mu; j++) {
} else { Link link = n.node.getLink(j);
x = lx; MapNode linkNode = addMapNode(link.node);
y = 10; int x2 = linkNode.x;
lx += 30; int y2 = linkNode.y;
} g2d.drawLine(n.x, n.y, x2, y2);
if (!hideNetwork) {
if (!hideNetwork) { int xn1, xn2, yn1, yn2;
FontMetrics fm = g.getFontMetrics(); if (n.x <= x2) {
int fnHeight = fm.getHeight(); xn1 = n.x; xn2 = x2;
int fnDescent = fm.getDescent(); yn1 = n.y; yn2 = y2;
for (int j = 0, mu = n.node.getLinkCount(); j < mu; j++) { } else {
Link link = n.node.getLink(j); xn1 = x2; xn2 = n.x;
if (link.node.hasLocation()) { yn1 = y2; yn2 = n.y;
long age = (time - link.getLastActive()) / 100;
int x2 = link.node.getX();
int y2 = link.node.getY();
if (n.isSelected) {
if (age > LINK_COLOR.length) {
age = 100;
} else {
age -= 50;
}
}
line.setLine(x, y, x2, y2);
if (age < LINK_COLOR.length) {
g.setColor(age < 0 ? LINK_COLOR[0] : LINK_COLOR[(int) age]);
} else {
g.setColor(LINK_COLOR[LINK_COLOR.length - 1]);
}
g2d.draw(line);
// g.setColor(Color.lightGray);
int xn1, xn2, yn1, yn2;
if (x <= x2) {
xn1 = x; xn2 = x2;
yn1 = y; yn2 = y2;
} else {
xn1 = x2; xn2 = x;
yn1 = y2; yn2 = y;
}
int dx = xn1 + (xn2 - xn1) / 2 + 4;
int dy = yn1 + (yn2 - yn1) / 2 - fnDescent;
if (yn2 < yn1) {
dy += fnHeight - fnDescent;
}
g.drawString("ETX:" + (((int)(link.getETX() * 100 + 0.5)) / 100.0), dx, dy);
} }
int dx = xn1 + (xn2 - xn1) / 2 + 4;
int dy = yn1 + (yn2 - yn1) / 2 - fnDescent;
if (yn2 < yn1) {
dy += fnHeight - fnDescent;
}
g.drawString("ETX:" + (((int)(link.getETX() * 100 + 0.5)) / 100.0), dx, dy);
} }
} }
}
n.paint(g, x, y); n.paint(g, n.x, n.y);
g.setColor(Color.black); g.setColor(Color.black);
if (n.isSelected) { if (n.isSelected) {
BasicGraphicsUtils.drawDashedRect(g, x - delta, y - delta, 2 * delta, 2 * delta); BasicGraphicsUtils.drawDashedRect(g, n.x - delta, n.y - delta,
} 2 * delta, 2 * delta);
if (selectedNode != null && selectedNode.message != null) { }
g.drawString(selectedNode.message, 10, 10); if (selectedNode != null && selectedNode.message != null) {
} g.drawString(selectedNode.message, 10, 10);
} }
} }
} }
@ -382,71 +486,145 @@ public class MapPanel extends JPanel implements Visualizer, ActionListener, Mous
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
Object source = e.getSource(); Object source = e.getSource();
if (source == timer) { if (!isMap && source == timer) {
ticker++; if (isLayoutActive) {
if (hasPendingEvents) { updateNodeLayout();
boolean repaint = false;
hasPendingEvents = false;
MapNode[] nodes = nodeList;
if (nodes != null) {
long time = System.currentTimeMillis();
for (int i = 0, n = nodes.length; i < n; i++) {
if (nodes[i].tick(time)) {
repaint = true;
}
}
}
if (repaint) {
hasPendingEvents = true;
repaint();
}
} else if ((ticker % 10) == 0) {
repaint(); repaint();
} }
} else if (source == hideItem) {
hideNetwork = !hideNetwork; } else if (!isMap && source == lockedItem) {
if (!hideNetwork) hideItem.setText("Hide Network"); if (popupNode != null) {
else hideItem.setText("Show Network"); popupNode.hasFixedLocation = lockedItem.isSelected();
}
} else if (!isMap && source == layoutItem) {
isLayoutActive = layoutItem.isSelected();
} else if (!isMap && source == shakeItem) {
for(MapNode n : getNodeList()) {
if (!n.hasFixedLocation) {
n.x += Math.random() * 100 - 50;
n.y += Math.random() * 100 - 50;
}
}
} else if (!isMap && source == configItem) {
if (configItem.isSelected()) {
configPanel.setSize(getPreferredSize());
configPanel.validate();
updateConfigLayout();
configPanel.setVisible(true);
} else {
configPanel.setVisible(false);
}
repaint();
} else if (source == showNetworkItem) {
hideNetwork = !showNetworkItem.isSelected();
repaint(); repaint();
} else if (source == resetNetworkItem) { } else if (source == resetNetworkItem) {
MapNode[] nodes = nodeList; for(MapNode n : getNodeList()) {
if (nodes != null) { n.node.clearLinks();
for (int i = 0, m = nodes.length; i < m; i++) {
MapNode n = nodes[i];
n.node.clearLinks();
}
repaint();
} }
repaint();
} else if (isMap && source == backgroundItem) {
showBackground = mapImage != null && backgroundItem.isSelected();
repaint();
} }
} }
private void updateNodeLayout() {
MapNode[] nodes = getNodeList();
for (MapNode n : nodes) {
// Attract connected nodes
for(int i = 0, jn = n.node.getLinkCount(); i < jn; i++) {
Link link = n.node.getLink(i);
MapNode n2 = addMapNode(link.node);
double vx = n2.x - n.x;
double vy = n2.y - n.y;
double dist = Math.sqrt(vx * vx + vy * vy);
dist = dist == 0 ? 0.00001 : dist;
double etx = link.getETX();
if (etx > 5) etx = 5;
double factor = (etx * layoutAttract - dist) / (dist * 3);
double dx = factor * vx;
double dy = factor * vy;
n2.dx += dx;
n2.dy += dy;
n.dx -= dx;
n.dy -= dy;
}
// Repel nodes that are too close
double dx = 0, dy = 0;
for (MapNode n2 : nodes) {
if (n == n2) {
continue;
}
double vx = n.x - n2.x;
double vy = n.y - n2.y;
double dist = vx * vx + vy * vy;
if (dist == 0) {
dx += Math.random() * 5;
dy += Math.random() * 5;
} else if (dist < layoutRepel * layoutRepel) {
dx += vx / dist;
dy += vy / dist;
}
}
double dist = dx * dx + dy * dy;
if (dist > 0) {
dist = Math.sqrt(dist) / 2;
n.dx += dx / dist;
n.dy += dy / dist;
}
n.dy += layoutGravity;
}
// Update the node positions
int width = getWidth();
int height = getHeight();
for(MapNode n : nodes) {
if (!n.hasFixedLocation && n != draggedNode) {
n.x += Math.max(-5, Math.min(5, n.dx));
n.y += Math.max(-5, Math.min(5, n.dy));
if (n.x < 0) {
n.x = 0;
} else if (n.x > width) {
n.x = width;
}
if (n.y < 0) {
n.y = 0;
} else if (n.y > height) {
n.y = height;
}
}
n.dx /= 2;
n.dy /= 2;
}
}
private void updateConfigLayout() {
configPanel.setLocation(getWidth() - configPanel.getWidth() - 10,
getHeight() - configPanel.getHeight() - 10);
}
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Mouselistener // Mouselistener
// ------------------------------------------------------------------- // -------------------------------------------------------------------
private MapNode getNodeAt(int mx, int my) { private MapNode getNodeAt(int mx, int my) {
int lx = 10; for(MapNode n : getNodeList()) {
MapNode[] nodes = nodeList; if (mx >= (n.x - delta)
if (nodes != null) { && mx <= (n.x + delta)
for (int i = 0, m = nodes.length; i < m; i++) { && my >= (n.y - delta)
MapNode n = nodes[i]; && my <= (n.y + delta)) {
int x, y; return n;
if (n.node.hasLocation()) {
x = n.node.getX();
y = n.node.getY();
} else {
x = lx;
y = 10;
lx += 30;
}
if (mx >= (x - delta)
&& mx <= (x + delta)
&& my >= (y - delta)
&& my <= (y + delta)) {
return n;
}
} }
} }
return null; return null;
@ -491,8 +669,8 @@ public class MapPanel extends JPanel implements Visualizer, ActionListener, Mous
// Do not drag if mouse is only moved during click // Do not drag if mouse is only moved during click
} else { } else {
draggedNode.node.setLocation(e.getX(), e.getY()); draggedNode.x = e.getX();
server.updateNodeLocation(draggedNode.node); draggedNode.y = e.getY();
setCursor(Cursor.getDefaultCursor()); setCursor(Cursor.getDefaultCursor());
draggedTime = 0; draggedTime = 0;
draggedNode = null; draggedNode = null;
@ -506,8 +684,11 @@ public class MapPanel extends JPanel implements Visualizer, ActionListener, Mous
private void showPopup(MouseEvent e) { private void showPopup(MouseEvent e) {
if (e.isPopupTrigger() if (e.isPopupTrigger()
&& (e.getModifiers() & (MouseEvent.SHIFT_MASK|MouseEvent.CTRL_MASK)) == 0) { && (e.getModifiers() & (MouseEvent.SHIFT_MASK|MouseEvent.CTRL_MASK)) == 0) {
// popupNode = getNodeAt(e.getX(), e.getY()); popupNode = getNodeAt(e.getX(), e.getY());
// nodeItem.setEnabled(popupNode != null); if (!isMap) {
lockedItem.setEnabled(popupNode != null);
lockedItem.setSelected(popupNode != null ? popupNode.hasFixedLocation : false);
}
popupMenu.show(this, e.getX(), e.getY()); popupMenu.show(this, e.getX(), e.getY());
} }
} }
@ -534,8 +715,9 @@ public class MapPanel extends JPanel implements Visualizer, ActionListener, Mous
setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
} }
} else if (VISUAL_DRAG) { } else if (VISUAL_DRAG /* && dragItem.isSelected() */) {
draggedNode.node.setLocation(e.getX(), e.getY()); draggedNode.x = e.getX();
draggedNode.y = e.getY();
repaint(); repaint();
} }
} }
@ -551,60 +733,44 @@ public class MapPanel extends JPanel implements Visualizer, ActionListener, Mous
private static class MapNode { private static class MapNode {
public final Node node; public final Node node;
public int x;
public int y;
public double dx;
public double dy;
public boolean hasFixedLocation;
public boolean isSelected; public boolean isSelected;
public String message = null; public String message;
private int tick = 0;
MapNode(MapPanel panel, Node node) { MapNode(MapPanel panel, Node node) {
this.node = node; this.node = node;
} }
boolean tick(long time) {
boolean r = false;
if (tick > 0) {
tick--;
r = true;
}
for (int i = 0, n = node.getLinkCount(); i < n; i++) {
Link link = node.getLink(i);
long age = (time - link.getLastActive()) / 100;
if (age < 200) {
r = true;
break;
}
}
return r;
}
public void paint(Graphics g, int x, int y) { public void paint(Graphics g, int x, int y) {
if (tick > (TOTAL_SHOW - SHOW_BLINK)) {
if ((tick & 4) == 0) {
// Hide circle
} else {
int index = FADE_COUNT - tick - 1;
if (index < 0) {
index = 0;
}
final int d = 8;
g.setColor(OTHER_COLOR[index]);
g.fillOval(x - d, y - d, d * 2 + 1, d * 2 + 1);
}
}
if (tick < (TOTAL_SHOW - SHOW_BLINK) && tick > 0) {
g.setColor(Color.red);
int height = 13 * tick / TOTAL_SHOW;
g.fillRect(x - 6, 5 + y - height, 2, height);
}
g.setColor(Color.black);
final int od = 3; final int od = 3;
g.setColor(Color.black);
g.drawString(node.getID(), x + od * 2 + 3, y + 4); g.drawString(node.getID(), x + od * 2 + 3, y + 4);
if (hasFixedLocation) {
g.setColor(Color.red);
}
g.fillOval(x - od, y - od, od * 2 + 1, od * 2 + 1); g.fillOval(x - od, y - od, od * 2 + 1, od * 2 + 1);
} }
} // end of inner class MapNode } // end of inner class MapNode
@Override
public void updateConfig(Properties config) {
if (isMap) {
for (MapNode n : getNodeList()) {
config.put(n.node.getID(), "" + n.x + ',' + n.y);
}
} else {
for (MapNode n : getNodeList()) {
if (n.hasFixedLocation) {
config.put("collect.map." + n.node.getID(), "" + n.x + ',' + n.y);
}
}
}
}
} }