displaymanager.java

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

JAVA
1,152
字号
		nodePainters.remove(painter);//add the painters to the painter list
	}

	public void AddEdgePainter(EdgePainter painter)
	{
		edgePainters.add(painter);//add the painters to the painter list
	}

	public void RemoveEdgePainter(EdgePainter painter)
	{
		edgePainters.remove(painter);//add the painters to the painter list
	}
                            
	public void AddScreenPainter(ScreenPainter painter)
	{
		screenPainters.add(painter);//add the painters to the painter list
	}

	public void RemoveScreenPainter(ScreenPainter painter)
	{
		screenPainters.remove(painter);//add the painters to the painter list
	}
	

	          //------------------------------------------------------------------------
	          //*****---TRIGGER NODE CLICKED EVENT---******//
		      //this function sends an event to all node clicked listeners
	protected void TriggerNodeClickedEvent(Integer pNodeNumber)
	{
		for(int index = 0; index < NodeClickedEventListeners.size(); index++)
		{
			NodeClickedEvent e = new NodeClickedEvent(this, pNodeNumber, Calendar.getInstance().getTime());
			((NodeClickedEventListener)NodeClickedEventListeners.get(index)).NodeClicked(e);
		}
	}
	          //*****---TRIGGER NODE CLICKED EVENT---******//
	          //------------------------------------------------------------------------
		
		
	          //------------------------------------------------------------------------
	          //*****---TRIGGER EDGE CLICKED EVENT---******//
		      //this function sends an event to all edge-clicked listeners
	protected void TriggerEdgeClickedEvent(Integer pSourceNodeNumber, Integer pDestinationNodeNumber)
	{
		for(int index = 0; index < EdgeClickedEventListeners.size(); index++)
		{
			EdgeClickedEvent e = new EdgeClickedEvent(this, pSourceNodeNumber, pDestinationNodeNumber, Calendar.getInstance().getTime());
			((EdgeClickedEventListener)EdgeClickedEventListeners.get(index)).EdgeClicked(e);
		}
	}
	          //*****---TRIGGER EDGE CLICKED EVENT---******//
	          //------------------------------------------------------------------------
	          //------------------------------------------------------------------------
	          //*****---DRAG NEAREST NODE---******//
	          //when in select mode, the mouse draw will drag the nearest node with it
	public void DragNearestNode(int startX, int startY, MouseEvent e)
	{
		LocationAnalyzer.NodeInfo selectedNode = FindNearestNode(startX, startY);
		
		if(selectedNode!=null)
		{   
			selectedNode.SetX(MainClass.mainFrame.GetGraphDisplayPanel().ScaleScreenXCoordToNodeCoord(e.getX()).doubleValue());
			selectedNode.SetY(MainClass.mainFrame.GetGraphDisplayPanel().ScaleScreenYCoordToNodeCoord(e.getY()).doubleValue());
			selectedNode.SetFixed(true);
			// selectedNode.RecordLocation();
			this.RefreshScreenNow();
		}
	}
	          //*****---DRAG NEAREST NODE---******//
			  //
	          //*****---SCROLL WITH MOUSE DRAG---******//
	          //------------------------------------------------------------------------

	          //------------------------------------------------------------------------
	          //*****---SCROLL WITH MOUSE DRAG---******//
	          //when in hand mode, the mouse draw will scroll the screen
	public void ScrollWithMouseDrag(int startX, int startY, MouseEvent e)
	{
		int endX = e.getX();
		int endY = e.getY();
		Point currentPosition = MainClass.mainFrame.GetMainScrollPane().getViewport().getViewPosition();
		Point newPosition = new Point(currentPosition.x +(endX-startX), currentPosition.y +(endY-startY));
		MainClass.mainFrame.GetMainScrollPane().getViewport().setViewPosition(newPosition);
	}
	          //*****---SCROLL WITH MOUSE DRAG---******//
	          //------------------------------------------------------------------------


	          //------------------------------------------------------------------------
	          //*****---ZOOM TO MOUSE DRAG RECTANGLE---******//
	          //when in zoom mode, the mouse drag will zoom to the selected rectangle
	public void ZoomToMouseDragRectangle(int startX, int startY, MouseEvent e)
	{
		int x,y, width, height;
		double scale;
		Dimension graphPanelSize = MainClass.mainFrame.GetGraphDisplayPanel().getSize();
		Dimension scrollPaneSize = MainClass.mainFrame.GetMainScrollPane().getSize();
		int endX = e.getX();
		int endY = e.getY();
		if(startX < endX)//use the edge of the rectangle that is longer in proportion that the proportion of the window size
		{
			x = startX;
			width = endX-startX;
		}
		else
		{
			x = endX;
			width = startX - endX;
		}
		if(startY < endY)
		{
			y = startY;
			height = endY-startY;
		}
		else
		{
			y = endY;
			height = startY - endY;
		}
		if( (height==0) || (width==0)) return;
		if(width/height > graphPanelSize.getWidth()/graphPanelSize.getHeight())//zoom so that the longer of the width/height of the new rectangle is in view (Since the rect will not be the same proportion as the window)
		{
			scale = scrollPaneSize.getWidth()/width;
		}
		else
		{
			scale = scrollPaneSize.getHeight()/height;
		}
		scale = scale*MainClass.displayManager.GetScaleFactor();
		this.MultiplyGraphDisplayPanelSize(scale);
		MainClass.mainFrame.GetMainScrollPane().getViewport().setViewPosition(new Point(x,y));
	}                                            
	          //*****---ZOOM TO MOUSE DRAG RECTANGLE---******//
	          //*****---FIND NEAREST NODE---******//
	public LocationAnalyzer.NodeInfo FindNearestNode(int x, int y)
	{
		LocationAnalyzer.NodeInfo selectedNode = null;
		LocationAnalyzer.NodeInfo tempNode = null;
		double dist = Double.MAX_VALUE;
		double bestdist = Double.MAX_VALUE;
		double xDist, yDist;
		GraphDisplayPanel display = MainClass.mainFrame.GetGraphDisplayPanel();
		for(Enumeration nodes = MainClass.locationAnalyzer.GetNodeInfo(); nodes.hasMoreElements();) 
		{
			tempNode = (LocationAnalyzer.NodeInfo)nodes.nextElement();
//			synchronized(tempNode)
//			{
				xDist = Math.pow(display.ScaleNodeXCoordToScreenCoord(tempNode.GetX()) - x,2.0);
				yDist = Math.pow(display.ScaleNodeYCoordToScreenCoord(tempNode.GetY()) - y,2.0);
				dist = Math.sqrt(xDist + yDist);
//			}
			if (dist < bestdist) {
				selectedNode = tempNode;
				bestdist = dist;
			}
		}
    	return selectedNode;
	}
	          //*****---FIND NEAREST NODE---******//
	          //*****---FIND NEAREST EDGE---******//
	public LocationAnalyzer.EdgeInfo FindNearestEdge(int x, int y)
	{
		LocationAnalyzer.EdgeInfo selectedEdge = null;
		LocationAnalyzer.EdgeInfo tempEdge = null;
		double dist = Double.MAX_VALUE;
		double bestdist = Double.MAX_VALUE;
		GraphDisplayPanel display = MainClass.mainFrame.GetGraphDisplayPanel();
		
		for(Enumeration edges = MainClass.locationAnalyzer.GetEdgeInfo(); edges.hasMoreElements();) 
		{
			double x1, y1, x2, y2, xCenter, yCenter;
			tempEdge = (LocationAnalyzer.EdgeInfo)edges.nextElement();
	//		synchronized(tempEdge)
	//		{
				Integer sourceNodeNumber = tempEdge.GetSourceNodeNumber();
				Integer destinationNodeNumber = tempEdge.GetDestinationNodeNumber();
				x1=MainClass.locationAnalyzer.GetX(sourceNodeNumber);
				y1=MainClass.locationAnalyzer.GetY(sourceNodeNumber);
				x2=MainClass.locationAnalyzer.GetX(destinationNodeNumber);
				y2=MainClass.locationAnalyzer.GetY(destinationNodeNumber);
				xCenter= display.ScaleNodeXCoordToScreenCoord((x1 + x2)/2);
				yCenter= display.ScaleNodeYCoordToScreenCoord((y1 + y2)/2);
				dist = Math.sqrt(Math.pow(xCenter-x,2) + Math.pow(yCenter-y,2));
				if (dist < bestdist) {
					selectedEdge = tempEdge;
					bestdist = dist;
				}
		//	}
		}
		return selectedEdge;
	}
	          //*****---FIND NEAREST EDGE---******//

//------------------------------------------------------------------------
			  //*****---DISPLAY NODE PROPERTY DIALOG---******//
//this function displays the dialog showing all node properties
	protected void DisplayNodePropertyDialog(Integer pNodeNumber)
	{
		TabbedDialog nodeDialog = new TabbedDialog("Node Properties");		
		ActivePanel currentPanel;
		NodeDialogContributor listener;

		for(Enumeration e = nodeDialogContributors.elements(); e.hasMoreElements();)
		{
			listener = ((NodeDialogContributor)e.nextElement());
			currentPanel = listener.GetProprietaryNodeInfoPanel(pNodeNumber);
			if(currentPanel != null)//if you don't have proprietary info, return a null panel
			{
				if(currentPanel.GetCancelInfoDialog())
				{//if you don't want a node dialog to show up, return an Active Component with this set to true
					return;
				}
				nodeDialog.AddActivePanel(currentPanel.GetTabTitle(), currentPanel);
			}
		}
		nodeDialog.setModal(false);
		nodeDialog.show();
	}
			  //*****---DISPLAY NODE PROPERTY DIALOG---******//
	          //------------------------------------------------------------------------
		

	          //------------------------------------------------------------------------
			  //*****---DISPLAY EDGEPROPERTY DIALOG---******//
				//this function displays the dialog showing all edge properties
	protected void DisplayEdgePropertyDialog(Integer pSourceNodeNumber, Integer pDestinationNodeNumber)
	{
		TabbedDialog edgeDialog = new TabbedDialog("Edge Properties");		
		ActivePanel currentPanel;
		
		for(Enumeration e = edgeDialogContributors.elements(); e.hasMoreElements();)
		{
			currentPanel = ((EdgeDialogContributor)e.nextElement()).GetProprietaryEdgeInfoPanel(pSourceNodeNumber, pDestinationNodeNumber);
			if(currentPanel != null)//if you don't have proprietary info, return a null panel
			{
				if(currentPanel.GetCancelInfoDialog())
				{//if you don't want a node dialog to show up, return an Active Component with this set to true
					return;
				}
				edgeDialog.AddActivePanel(currentPanel.GetTabTitle(),currentPanel);
			}
		}
		edgeDialog.setModal(false);
		edgeDialog.show();
	}
			  //*****---DISPLAY EDGEPROPERTY DIALOG---******//
	          //------------------------------------------------------------------------


	          //*****---refresh screen NOW---******//
	          //this function will redraw the main screen with all nodes and edges in current positions
	public static void RefreshScreenNow()
	{
		MainClass.mainFrame.paint(MainClass.mainFrame.getGraphics());
	}
	          //*****---refresh screen NOW---******//

	
              //-----------------------------------------------------------------------
	          //*****---MultiplyGraphDisplayPanelSize---******//
	public static void MultiplyGraphDisplayPanelSize(double factor)
	{
		Insets inset = MainClass.mainFrame.GetMainScrollPane().getInsets();	 
		Dimension d = MainClass.mainFrame.GetMainScrollPane().getSize();	
		int x = (int)( (d.width-(inset.left+inset.right))*factor);
		int y = (int)( (d.height-(inset.top+inset.bottom))*factor);
		MainClass.mainFrame.GetGraphDisplayPanel().setSize(new Dimension(x, y));//this line makes the scroll pane put scroll bars if necessary
		MainClass.mainFrame.GetGraphDisplayPanel().setPreferredSize(new Dimension(x, y));//this line changes the size, and the node coordinates are automatically rescaled
		MainClass.mainFrame.GetMainScrollPane().setViewportView(MainClass.mainFrame.GetGraphDisplayPanel());//this line allows the scroll pane to reevaluate whether scroll bars are needed
	}
	          //*****---MultiplyGraphDisplayPanelSize---******//
              //-----------------------------------------------------------------------

	public void PaintUnderScreen(Graphics g)
	{
		ScreenPainter screenPainter;
		for(Enumeration painters = screenPainters.elements(); painters.hasMoreElements();) 
		{
			screenPainter = (ScreenPainter)painters.nextElement();
			screenPainter.PaintScreenBefore(g);
		}
    }

	public void PaintOverScreen(Graphics g)
	{
		ScreenPainter screenPainter;
		for(Enumeration painters = screenPainters.elements(); painters.hasMoreElements();) 
		{

⌨️ 快捷键说明

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