taskvisualization.java

来自「tinyos最新版」· Java 代码 · 共 1,070 行 · 第 1/3 页

JAVA
1,070
字号
/*
 * "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 = 750;

  /**
   * Height of the scrolling pane
   */
  public static final int SCROLL_HEIGHT = 500;

    static float MIN_VAL = 0;
    static float MAX_VAL = 100;
    static final int STEP_SIZE = 25;
    Color fromColor = new Color(0f,0f,1.0f,1.0f);
    Color toColor = new Color(1.0f,0f,0f,1.0f);
    
    public ZGroup gradientGroup;

  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;
    
    private boolean doGradient = false;

  java.util.Timer timer;

  private JFrame parentFrame;
  private JPanel parentPanel;
  private TASKVisualizer parent;

    private ZRectangle gRects[][] = null;
    
    private JPanel mainPanel;
    private SpringLayout layout;

    SpringLayout.Constraints toolbarCons;

  /**
   * 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;
    layout = new SpringLayout();
    mainPanel = new JPanel(layout);
    parentPanel.addComponentListener(new ComponentAdapter() {
	    public void componentResized(ComponentEvent event) {
//  		int wid = event.getComponent().getWidth();
//  		int hgt = event.getComponent().getHeight();
		
//  		System.out.println("Width = " + wid + ", Height = " + hgt);
//  		mainPanel.resize(wid,hgt);
//  		if (scrollPane != null) {
//  		    scrollPane.resize(wid-100,hgt-100);
//  		}
		//  		if (config != null) preparePanel(config);
	    }
	});
    
    mainPanel.setMinimumSize(new Dimension(200,200));
    mainPanel.setMaximumSize(new Dimension(1000,1000));
    mainPanel.setPreferredSize(new Dimension(parentFrame.getWidth(),
    					     parentFrame.getHeight()));
    
    parentPanel.add(mainPanel);

    
  }


    public static void setComponentSizes(Container parent, int pad) {
        SpringLayout layout = (SpringLayout) parent.getLayout();
        Component[] components = parent.getComponents();
        Spring maxHeightSpring = Spring.constant(0);
	Spring topSpring;

        SpringLayout.Constraints pCons = layout.getConstraints(parent);
        //Set the container's max X to the max X
        //of its rightmost component + padding.
        Component rightmost = components[components.length - 1];
        SpringLayout.Constraints rCons =
                layout.getConstraints(rightmost);
	rCons.setConstraint(SpringLayout.EAST,
			     Spring.sum(Spring.constant(pad),
					pCons.getConstraint(SpringLayout.EAST)));


	maxHeightSpring = Spring.sum(Spring.constant(pad), pCons.getConstraint(SpringLayout.SOUTH));
	topSpring = pCons.getConstraint(SpringLayout.NORTH);

        for (int i = 0; i < components.length; i++) {
	    SpringLayout.Constraints cons =
		layout.getConstraints(components[i]);
	    cons.setConstraint(SpringLayout.SOUTH,
				maxHeightSpring);
	    cons.setConstraint(SpringLayout.NORTH,
				topSpring);

		
        }


    }

    public static void setContainerSize(Container parent,
                                        int pad) {
        SpringLayout layout = (SpringLayout) parent.getLayout();
        Component[] components = parent.getComponents();
        Spring maxHeightSpring = Spring.constant(0);
        SpringLayout.Constraints pCons = layout.getConstraints(parent);

        //Set the container's max X to the max X
        //of its rightmost component + padding.
        Component rightmost = components[components.length - 1];
        SpringLayout.Constraints rCons =
                layout.getConstraints(rightmost);
        pCons.setConstraint(
                SpringLayout.EAST,
                Spring.sum(Spring.constant(pad),
                           rCons.getConstraint(SpringLayout.EAST)));

        //Set the container's max Y to the max Y of its tallest
        //component + padding.
        for (int i = 0; i < components.length; i++) {
            SpringLayout.Constraints cons =
                layout.getConstraints(components[i]);
            maxHeightSpring = Spring.max(maxHeightSpring,
                                         cons.getConstraint(
                                                SpringLayout.SOUTH));
        }
        pCons.setConstraint(
                SpringLayout.SOUTH,
                Spring.sum(Spring.constant(pad),
                           maxHeightSpring));
   }


  public void preparePanel(Configuration config) {
    this.config = config;

    mainPanel.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) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?