📄 taskvisualization.java
字号:
/*
* "Copyright (c) 2000-2003 The Regents of the University of California.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice, the following
* two paragraphs and the author appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
* Copyright (c) 2002-2003 Intel Corporation
* All rights reserved.
*
* This file is distributed under the terms in the attached INTEL-LICENSE
* file. If you do not find these files, copies can be found by writing to
* Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA,
* 94704. Attention: Intel License Inquiry.
*/
package net.tinyos.task.taskviz;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
import java.io.File;
import java.io.IOException;
import javax.swing.border.*;
import edu.umd.cs.jazz.*;
import edu.umd.cs.jazz.component.*;
import edu.umd.cs.jazz.event.*;
import edu.umd.cs.jazz.util.*;
import net.tinyos.task.taskapi.*;
import net.tinyos.task.taskviz.sensor.SensorMotes;
import net.tinyos.task.taskviz.sensor.SensorMote;
import net.tinyos.task.taskviz.sensor.SensorLine;
/**
* This class is a visualization of environmental data: heat and light, along with network route information.
* The visualization reads the data from the database at a regular interval (every 60 seconds) and updates
* the display. Temperature and light can be visualized as transparent circles: temperature varying between
* blue and red, light with shades of gray; or they can be visualized as a gradient map. Routing information
* between sensor nodes are shown with a red line between a node and its parent and a green line between a node
* and any other node it can communicate with.
*/
public class TASKVisualization implements TASKResultListener {
/**
* Route information
*/
public static final String ROUTE = "Route";
/**
* Pan mode for interaction
*/
public static final int PAN_MODE = 1;
/**
* Put the visualization in no interaction mode
*/
public static final int NO_MODE = 2;
/**
* Select mode for interaction
*/
public static final int SELECT_MODE = 3;
/**
* Width of the scrolling pane
*/
public static final int SCROLL_WIDTH = 600;
/**
* Height of the scrolling pane
*/
public static final int SCROLL_HEIGHT = 600;
public int sensorType;
private GregorianCalendar cal;
private ZEventHandler currentEventHandler = null;
private ZPanEventHandler panEventHandler = null;
private ZoomEventHandler zoomEventHandler = null;
private MoveEventHandler2 moveEventHandler = null;
private ZCompositeSelectionHandler selectionHandler = null;
// private ToolBarButton pan, select;
private JToggleButton pan, select;
ZImageCanvas canvas = null;
JScrollPane scrollPane, scrollPane2;
JMenu viz, sensor;
ZLayerGroup layer;
ZGroup routeGroup;
SensorMotes motes = new SensorMotes();
private Configuration config;
int imageWidth, imageHeight;
private TASKClient client;
private int healthQueryId;
private int sensorQueryId;
public int healthSamplePeriod;
private JRadioButtonMenuItem[] sensorItems = new JRadioButtonMenuItem[20];
private int sensorIndex = -1;
private DefaultListModel unknownNodesModel;
private JList unknownNodesList;
java.util.Timer timer;
private JFrame parentFrame;
private JPanel parentPanel;
private TASKVisualizer parent;
/**
* Constructor for the sensor network visualization using default TASK Server port
*
* @param host TASKServer host
*/
public TASKVisualization(JFrame parentFrame, TASKVisualizer parent, TASKClient client, JPanel parentPanel) {
this.parentFrame = parentFrame;
this.parent = parent;
this.client = client;
this.parentPanel = parentPanel;
}
public void preparePanel(Configuration config) {
this.config = config;
parentPanel.removeAll();
unknownNodesModel = new DefaultListModel();
unknownNodesList = new JList(unknownNodesModel);
scrollPane2 = new JScrollPane(unknownNodesList);
motes = getSensorMotes();
sensor = new JMenu("Sensor");
sensor.setMnemonic(KeyEvent.VK_S);
ButtonGroup group = new ButtonGroup();
TASKQuery healthQuery = client.getHealthQuery();
int i=0;
if (healthQuery != null) {
Vector v = healthQuery.getSelectEntries();
for (i=0; i<v.size(); i++) {
String fieldName = ((TASKAttrExpr)v.elementAt(i)).getAttrName();
sensorItems[i] = new JRadioButtonMenuItem(fieldName);
sensorItems[i].setActionCommand(String.valueOf(i));
sensorItems[i].setToolTipText(fieldName);
sensorItems[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JRadioButtonMenuItem item = (JRadioButtonMenuItem)ae.getSource();
int command = Integer.parseInt(item.getActionCommand());
int color = 0;
if (command%4 == 0) {
color = SensorMote.GRAY;
}
else if (command%4 == 1) {
color = SensorMote.RED;
}
else if (command%4 == 2) {
color = SensorMote.BLUE;
}
else {
color = SensorMote.GREEN;
}
sensorType = command+1;
System.out.println("SWITCHING TO: healthType: "+sensorType+" with color: "+color);
for (Enumeration e=motes.elements(); e.hasMoreElements(); ) {
SensorMote sm = (SensorMote)e.nextElement();
sm.setSensorToVisualize(sensorType);
sm.setColorScheme(color, 1024);
}
}
});
group.add(sensorItems[i]);
sensor.add(sensorItems[i]);
}
}
sensorIndex = i;
TASKQuery sensorQuery = client.getSensorQuery();
if (sensorQuery != null) {
Vector v = sensorQuery.getSelectEntries();
for (int j=0; j<v.size(); j++) {
String fieldName = ((TASKAttrExpr)v.elementAt(j)).getAttrName();
sensorItems[j+i] = new JRadioButtonMenuItem(fieldName);
sensorItems[j+i].setActionCommand(String.valueOf(j+i));
sensorItems[j+i].setToolTipText(fieldName);
System.out.println("created sensor item: "+(j+i));
sensorItems[j+i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JRadioButtonMenuItem item = (JRadioButtonMenuItem)ae.getSource();
int command = Integer.parseInt(item.getActionCommand());
int color = 0;
if (command%4 == 0) {
color = SensorMote.GRAY;
}
else if (command%4 == 1) {
color = SensorMote.RED;
}
else if (command%4 == 2) {
color = SensorMote.BLUE;
}
else {
color = SensorMote.GREEN;
}
sensorType = command+1;
System.out.println("SWITCHING TO: sensorType: "+sensorType+" with color: "+color);
for (Enumeration e=motes.elements(); e.hasMoreElements(); ) {
SensorMote sm = (SensorMote)e.nextElement();
sm.setSensorToVisualize(sensorType);
sm.setColorScheme(color, 1024);
}
}
});
group.add(sensorItems[j+i]);
sensor.add(sensorItems[j+i]);
}
}
sensor.addSeparator();
JCheckBoxMenuItem route = new JCheckBoxMenuItem(ROUTE, true);
route.setActionCommand(ROUTE);
route.setToolTipText(ROUTE);
route.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ie) {
if (ie.getStateChange() == ItemEvent.SELECTED) {
layer.addChild(routeGroup);
}
else if (ie.getStateChange() == ItemEvent.DESELECTED) {
layer.removeChild(routeGroup);
}
}
});
sensor.add(route);
parentPanel.add(createToolBar(), BorderLayout.NORTH);
parent.removeSensorMenu();
parent.addSensorMenu(sensor);
viewConfiguration();
}
/**
* Creates a toolbar to allow panning of the frame
*
* @return the created toolbar
*/
private JToolBar createToolBar() {
JToolBar toolbar = new JToolBar();
ButtonGroup group = new ButtonGroup();
Insets margins = new Insets(0, 0, 0, 0);
/* pan = new ToolBarButton("images/P.gif");
pan.setSelectedIcon(new ImageIcon("images/P-selected.gif"));
*/
pan = new JToggleButton("Pan view");
pan.setToolTipText("Pan view");
pan.setMargin(margins);
pan.setActionCommand("pan");
pan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
setMode(PAN_MODE);
}
});
toolbar.add(pan);
group.add(pan);
/* select = new ToolBarButton("images/S.gif");
select.setSelectedIcon(new ImageIcon("images/S-selected.gif"));
*/
select = new JToggleButton("Select motes");
select.setToolTipText("Select motes to graph");
select.setMargin(margins);
select.setActionCommand("select");
select.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
setMode(SELECT_MODE);
}
});
toolbar.add(select);
group.add(select);
/* ToolBarButton graph = new ToolBarButton("images/G.gif");
*/
JToggleButton graph = new JToggleButton("Graph data");
graph.setToolTipText("Graph data");
graph.setMargin(margins);
graph.setActionCommand("graph");
graph.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Vector v = new Vector();
Component menuitems[] = sensor.getMenuComponents();
for (int i=0; i<menuitems.length; i++) {
if (menuitems[i] instanceof JMenuItem) {
JMenuItem menuitem = (JMenuItem)menuitems[i];
if (!menuitem.getText().equalsIgnoreCase("nodeid")) {
v.addElement(menuitem.getText());
}
}
}
AttributeSelectDialog asd = new AttributeSelectDialog(parentFrame, v);
asd.pack();
asd.setLocationRelativeTo(parentFrame);
asd.setVisible(true);
if (asd.isDataValid()) {
Vector attributes = asd.getSelectedAttributes();
attributes.insertElementAt("nodeid",0);
Collection c = selectionHandler.getSelectionModifyHandler().getCurrentSelection();
if (c.size() > 0) {
Vector nodes = new Vector();
for (Iterator i=c.iterator(); i.hasNext(); ) {
ZVisualComponent zvc = ((ZVisualLeaf)i.next()).getFirstVisualComponent();
if (zvc instanceof Mote) {
nodes.addElement(new Integer(((Mote)zvc).getId()));
}
}
new ResultFrame(client, nodes, attributes);
}
}
}
});
toolbar.add(graph);
toolbar.setFloatable(true);
return toolbar;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -