displaymanager.java

来自「用于传感器网络的节点操作系统 TinyOS 结构设计非常有意思」· Java 代码 · 共 1,152 行 · 第 1/4 页

JAVA
1,152
字号
/* "Copyright (c) 2001 and 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 and the following 
* two paragraphs 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." 
* 
* Authors:   Kamin Whitehouse <kamin@cs.berkeley.edu>
* History:   created 7/22/2001 
* Authors:   Wei Hong, modified for tinydb
*/


//***********************************************************************
//***********************************************************************
//this class is listening to the main panel on the mainFrame
//and generates events whenever a node or edge are clicked
//it can also be used for scrolling or zooming.
//This class could have been integrated with the MainFrame class
//but I wanted to leave that class to VisualCafe
//***********************************************************************
//***********************************************************************

package net.tinyos.tinydb.topology;

import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import net.tinyos.tinydb.topology.event.*;
import java.lang.Math;
import net.tinyos.tinydb.topology.Dialog.*;
import net.tinyos.tinydb.topology.util.*;
import java.awt.*;
import net.tinyos.tinydb.topology.PacketAnalyzer.*;


public class DisplayManager implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener, Runnable, NodePainter, EdgePainter, NodeEventListener, EdgeEventListener, NodeClickedEventListener, EdgeClickedEventListener, NodeDialogContributor, EdgeDialogContributor
{
  //these vectors hold all people that want to paint on the edge, nodes, or over the entire screen
	protected Vector nodePainters;//a list of all objects that want to paint nodes
	protected Vector edgePainters;//a list of all objects that want to paint edges
	protected Vector screenPainters;//a list of all objects that want to paint the screen
	
	          //these vectors contain all people who want to add to the node/edge properties dialog
	protected Vector nodeDialogContributors;//a list of all objects that want to add to the node properties Dialog	
	protected Vector edgeDialogContributors;//a list of all objects that want to add to the edge properties dialog

		//these vectors contain all the registered listeners for node and edge click events
	protected Vector NodeClickedEventListeners;
	protected Vector EdgeClickedEventListeners;

	          //these hashtables hold nodeInfo and edgeInfo objects, which hold display information about specific nodes and edges (e.g. color, image)
	protected static Hashtable proprietaryNodeInfo;
	protected static TwoKeyHashtable proprietaryEdgeInfo;


              //these variables describe how the mouse interacts with the network
	boolean selectMode;
	boolean handMode;
	boolean zoomMode;
	private boolean stopped = false;

	int pressedXCoord=-1;
	int pressedYCoord=-1;
	protected static long refreshRate;//refresh rate
	double zoomFactor;//the factor by which the screen is enlarged

	protected static Thread refreshScreenThread;//the thread that runs in the background and refreshes the screen periodically

              //------------------------------------------------------------------
              //CONSTRUCTOR
	DisplayManager(MainFrame pMainFrame)
	{
		nodePainters = new Vector();
		edgePainters = new Vector();
		screenPainters = new Vector();
		nodeDialogContributors = new Vector();
		edgeDialogContributors = new Vector();
		NodeClickedEventListeners = new Vector();
		EdgeClickedEventListeners = new Vector();	
		proprietaryNodeInfo = new Hashtable();
		proprietaryEdgeInfo = new TwoKeyHashtable();
		
              //register to recieve all mouse clicks on the display panel
		pMainFrame.GetGraphDisplayPanel().addMouseListener(this);
		pMainFrame.GetGraphDisplayPanel().addMouseMotionListener(this);
              //register (with myself) to paint nodes and edges and display info panels
		this.AddNodePainter(this);//paint the nodes
		this.AddEdgePainter(this);//paint the edges
			//register myself to recieve NodeClickedEvents and EdgeClickedEvents
		// this.AddNodeDialogContributor(this);
		this.AddEdgeDialogContributor(this);
              //register to be notified of nodes and edges being created or deleted
              //this is done in MainClass constructor because this object is instantiated before the Object Maintainer
//		MainClass.objectMaintainer.AddEdgeEventListener(this);//listen to node events
//		MainClass.objectMaintainer.AddNodeEventListener(this);//listen to edge event

		selectMode=true;
		handMode=false;
		zoomMode=false;
		refreshRate = 512;//this is the number of milliseconds that it waits before redrawing the screen again
        refreshScreenThread = new Thread(this);//this thread runs continually in the background to redraw the screen
		try{
			refreshScreenThread.setPriority(Thread.MIN_PRIORITY);
			refreshScreenThread.start(); //recall that start() calls the run() method defined in this class
		}
		catch(Exception e){e.printStackTrace();}

	}
              //CONSTRUCTOR
              //------------------------------------------------------------------

              //------------------------------------------------------------------
			//MOUSE CLICKED
	public void mouseClicked(MouseEvent e){}
	public void mouseClickedCustom(MouseEvent e)
	{
		if (javax.swing.SwingUtilities.isLeftMouseButton(e))
		{
			LocationAnalyzer.NodeInfo nodeLocationInfo= FindNearestNode(e.getX(), e.getY());
			if(nodeLocationInfo == null) return;
			TriggerNodeClickedEvent(nodeLocationInfo.GetNodeNumber());
			DisplayNodePropertyDialog(nodeLocationInfo.GetNodeNumber());
		}
		else
		{
			LocationAnalyzer.EdgeInfo edgeLocationInfo = FindNearestEdge(e.getX(), e.getY());
			TriggerEdgeClickedEvent(edgeLocationInfo.GetSourceNodeNumber(), edgeLocationInfo.GetDestinationNodeNumber());
			DisplayEdgePropertyDialog(edgeLocationInfo.GetSourceNodeNumber(), edgeLocationInfo.GetDestinationNodeNumber());
		}
	}
			//MOUSE CLICKED
              //------------------------------------------------------------------


              //------------------------------------------------------------------
			//MOUSE DRAGGED
	public void mouseDragged(MouseEvent e) 
	{
		// DrawCoords(e);
	}//even though using this method instead of my custom method would make the graphics look better
	          //I use a custom mouse dragged method because otherwise handling all the events overwhelms the system and all the nodes might time-out
	
	public void mouseDraggedCustom(int startX, int startY, MouseEvent e)
    {
		if (javax.swing.SwingUtilities.isLeftMouseButton(e))
		{
			if(selectMode)
			{
				DragNearestNode(startX, startY, e);

			}
			else if(handMode)
			{
				ScrollWithMouseDrag(startX, startY, e);
			}
			else if(zoomMode)
			{
				ZoomToMouseDragRectangle(startX, startY, e);
			}
		}
		else if (javax.swing.SwingUtilities.isMiddleMouseButton(e))
		{
			ScrollWithMouseDrag(startX, startY, e);
		}
		else if (javax.swing.SwingUtilities.isRightMouseButton(e))
		{
			ZoomToMouseDragRectangle(startX, startY, e);
		}
    }
			//MOUSE DRAGGED
              //------------------------------------------------------------------

	public void mouseEntered(MouseEvent e)
	{
	}
		
	public void mouseExited(MouseEvent e)
	{         //leave these lines if you don't want the custom mouse click to fire after the mouse has left and re-entered the screen
//		pressedXCoord = -1;
//		pressedYCoord = -1;
	}
		
		      //this function triggers an event for a mouse press
	public void mousePressed(MouseEvent e)
	{
		pressedXCoord = e.getX();
		pressedYCoord = e.getY();
	}
		
	public void mouseReleased(MouseEvent e)
	{
		if((pressedXCoord == -1) || (pressedYCoord == -1))
		{
			return;
		}
		
		int x = e.getX();
		int y = e.getY();

		if((pressedXCoord == x) && (pressedYCoord == y))
		{     //if a mouse click
			mouseClickedCustom(e);
		}
		else
		{     //if it was a drag, pass the original coords and the final coords
			mouseDraggedCustom(pressedXCoord, pressedYCoord, e);
		}
		pressedXCoord = -1;//reset
		pressedYCoord = -1;
	}
		
    public void mouseMoved(MouseEvent e)
    {
		// DrawCoords(e);
    }


		      //this function adds the node dialog contributors
	public  void AddNodeDialogContributor(NodeDialogContributor pContributor)
	{
		nodeDialogContributors.add(pContributor);
	}

	public  void RemoveNodeDialogContributor(NodeDialogContributor pContributor)
	{
		nodeDialogContributors.remove(pContributor);
	}

		      //this function adds the edge dialog contributors
	public  void AddEdgeDialogContributor(EdgeDialogContributor pContributor)
	{
		edgeDialogContributors.add(pContributor);
	}

	public  void RemoveEdgeDialogContributor(EdgeDialogContributor pContributor)
	{
		edgeDialogContributors.remove(pContributor);
	}

		      //this function adds the nodeclicked Listeners
	public  void AddNodeClickedEventListener(NodeClickedEventListener pListener)
	{
		NodeClickedEventListeners.add(pListener);
	}

	public  void RemoveNodeClickedEventListener(NodeClickedEventListener pListener)
	{
		NodeClickedEventListeners.remove(pListener);
	}

              //this function adds the edge clicked Listeners
	public  void AddEdgeClickedEventListener(EdgeClickedEventListener pListener)
	{
		EdgeClickedEventListeners.add(pListener);
	}
		
	public  void RemoveEdgeClickedEventListener(EdgeClickedEventListener pListener)
	{
		EdgeClickedEventListeners.remove(pListener);
	}
		
		
				          //*************************************************************
	          //*************************************************************
	          //this is where we register the node, edge and screen painters
	          //*************************************************************
	          //*************************************************************
	          
	public void AddNodePainter(NodePainter painter)
	{
		nodePainters.add(painter);//add the painters to the painter list
	}

	public void RemoveNodePainter(NodePainter painter)
	{

⌨️ 快捷键说明

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