📄 displaymanager.java
字号:
// $Id: DisplayManager.java,v 1.3 2003/10/07 21:46:05 idgay Exp $/* tab:4 * "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. *//** * @author Wei Hong * @author 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.surge;import java.lang.Math;import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import net.tinyos.surge.event.*;import net.tinyos.surge.Dialog.*;import net.tinyos.surge.util.*;import net.tinyos.surge.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; // dchi - added this because when the user drags then unclicks the mouse, make sure it doesn't think it was a mouse click boolean dragging = false; 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 // XXX MDW: Removed //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; //this is the number of milliseconds that it waits before redrawing the screen again refreshRate = 100; System.err.println("Starting DisplayManager thread..."); 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()); // XXX MDW: Taking this out for now DisplayNodePropertyDialog(nodeLocationInfo.GetNodeNumber()); } else if (javax.swing.SwingUtilities.isRightMouseButton(e)) { LocationAnalyzer.NodeInfo nodeLocationInfo= FindNearestNode(e.getX(), e.getY()); if(nodeLocationInfo == null) return; MainClass.mainFrame.sendFocusCommand(nodeLocationInfo.GetNodeNumber()); } else { LocationAnalyzer.EdgeInfo edgeLocationInfo = FindNearestEdge(e.getX(), e.getY()); TriggerEdgeClickedEvent(edgeLocationInfo.GetSourceNodeNumber(), edgeLocationInfo.GetDestinationNodeNumber()); // XXX MDW: Taking this out for now //DisplayEdgePropertyDialog(edgeLocationInfo.GetSourceNodeNumber(), edgeLocationInfo.GetDestinationNodeNumber()); } } //MOUSE CLICKED //------------------------------------------------------------------ //------------------------------------------------------------------ //MOUSE DRAGGED public void mouseDragged(MouseEvent e) { // dchi dragging = true; mouseDraggedCustom (pressedXCoord, pressedYCoord, e); pressedXCoord = e.getX(); pressedYCoord = e.getY(); // 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 // this check is necessary because when the user drags and then releases the mouse // don't want the screen to pop up if (!dragging) 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; // dchi dragging = false; } 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -