⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mygraphlistener.java

📁 P2P模拟器P2Psim的程序源码
💻 JAVA
字号:
package gui;

import graph.GraphEventManager;

import org.jgraph.event.GraphSelectionListener;
import org.jgraph.event.GraphSelectionEvent;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultEdge;

import org.jgraph.JGraph;

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Component;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenu;

import org.jgraph.plaf.basic.BasicGraphUI.MouseHandler;


public class MyGraphListener implements GraphSelectionListener, MouseListener, ActionListener
{
	private DefaultGraphCell tempCell;
	private int tempX, tempY, tempScaledX, tempScaledY;
	private Object[] selectedItems;
	private MouseHandler mh;
	private String currentAction = "Selection";
	
	public MyGraphListener(MouseHandler mh)
	{
		this.mh = mh;
	}
	
	public void valueChanged(GraphSelectionEvent e)
	{
		//System.out.println(((JGraph)e.getSource()).getSelectionCell());
		
		//if (((JGraph)e.getSource()).getSelectionCell() != null)
		//{
			GUIEventManager.getReference().eventUpdateInfoJPanel((DefaultGraphCell)((JGraph)e.getSource()).getSelectionCell());	
			if (((JGraph)e.getSource()).getSelectionCells() != null)
			{
				//System.out.println(((JGraph)e.getSource()).getSelectionCells());
				selectedItems = ((JGraph)e.getSource()).getSelectionCells();
			//System.out.println("Selected items: " + ((JGraph)e.getSource()).getSelectionCells().length);
			}	
		 
	}
	
	public void mouseEntered(MouseEvent e){mh.mouseEntered(e);}
	public void mousePressed(MouseEvent e)
	{
		if (e.getButton() == MouseEvent.BUTTON3 && selectedItems != null && selectedItems.length == 2)
		{
			// Als je rechts klikt, en er zijn twee knopen geselecteerd...
			// verwijder dan de selectie niet
		}
		else
		{
			mh.mousePressed(new MouseEvent((Component)e.getSource(), e.getID(), e.getWhen(), e.getModifiers(), e.getX(), e.getY(), 1, false, MouseEvent.BUTTON1));
		}
		
	}
	public void mouseReleased(MouseEvent e)
	{
		//System.out.println("X=" + e.getX() + " Y=" + e.getY());
		//mh.mouseReleased(e);
		mh.mouseReleased(new MouseEvent((Component)e.getSource(), e.getID(), e.getWhen(), e.getModifiers(), e.getX(), e.getY(), 1, false, MouseEvent.BUTTON1));
		
	}
	
	public void mouseClicked(MouseEvent e)
	{
		
		DefaultGraphCell dgc = GUIEventManager.getReference().eventGetGraphCellAtPosition(e.getPoint().x, e.getPoint().y);
		//System.out.println("You clicked on cell: " + dgc);
		
		tempX = e.getX();
		tempY = e.getY();
		
		double scale = GUIEventManager.getReference().eventGetScale();
		//System.out.println(scale);
		tempScaledX = (int)(tempX / scale);
		tempScaledY = (int)(tempY / scale);
					
		if (e.getButton() == MouseEvent.BUTTON1)
		{
			//System.out.println(currentAction + " on " + dgc + " x/y = " + tempScaledX + "/" + tempScaledY);
			if (dgc != null && !(dgc instanceof DefaultEdge) && (currentAction.compareTo("Remove node") == 0))
			{
				GraphEventManager.getReference().eventRemoveVertex(dgc);
				dgc = null;	
				tempCell = null;
			}
			else if (dgc != null && (dgc instanceof DefaultEdge) && (currentAction.compareTo("Remove edge") == 0))
			{
				GraphEventManager.getReference().eventRemoveEdge((DefaultEdge)dgc);
				dgc = null;	
				tempCell = null;
			}
			else if (dgc == null && (currentAction.compareTo("New node") == 0))
			{
				//System.out.println("Create new node");
				GraphEventManager.getReference().eventAddVertex("new", tempScaledX, tempScaledY);	
				tempCell = null;
			}
		}
		
		if (e.getButton() == MouseEvent.BUTTON3)
		{
			tempCell = dgc;
				
			JPopupMenu popup = new JPopupMenu();
			JMenuItem menuItem;
			
			if (selectedItems != null && selectedItems.length == 2)
			{
				//System.out.println("Change name !");
				menuItem = new JMenuItem("Create new edge");
				menuItem.addActionListener(this);
				popup.add(menuItem);	
			}
			else if (dgc != null)
			{
				// Algemeen
				menuItem = new JMenuItem("Change name");
				menuItem.addActionListener(this);
				popup.add(menuItem);
				
				if (dgc instanceof DefaultEdge)
				{
					// Specifiek voor een edge
					
					menuItem = new JMenuItem("Change capacity");
					menuItem.addActionListener(this);
					popup.add(menuItem);
										
					popup.addSeparator();
					
					menuItem = new JMenuItem("Remove this edge");
					menuItem.addActionListener(this);
					popup.add(menuItem);
				
				}
				else
				{
					// Specifiek voor een vertex
					
					menuItem = new JMenuItem("Change color");
					menuItem.addActionListener(this);
					popup.add(menuItem);
							
					menuItem = new JMenuItem("Add resource");
					menuItem.addActionListener(this);
					popup.add(menuItem);
					
					popup.addSeparator();		
					
					JMenu submenu = new JMenu("Construct gradient-field");

					menuItem = new JMenuItem("with strength = 20");
					menuItem.addActionListener(this);
					submenu.add(menuItem);
					
					menuItem = new JMenuItem("with strength = 30");
					menuItem.addActionListener(this);
					submenu.add(menuItem);
					
					menuItem = new JMenuItem("with other strength");
					menuItem.addActionListener(this);
					submenu.add(menuItem);					
					
					popup.add(submenu);
				
					menuItem = new JMenuItem("Remove gradient-field");
					menuItem.addActionListener(this);
					popup.add(menuItem);				
				
					popup.addSeparator();
					
					
					String agentAction = GraphEventManager.getAgentLibrary().getAgentAction(GraphEventManager.getReference().eventgetAgentType());
					menuItem = new JMenuItem(agentAction);
					menuItem.addActionListener(this);
					popup.add(menuItem);
					
					popup.addSeparator();

					menuItem = new JMenuItem("Analyse requests");
					menuItem.addActionListener(this);
					popup.add(menuItem);

					popup.addSeparator();

					menuItem = new JMenuItem("Remove this node");
					menuItem.addActionListener(this);
					popup.add(menuItem);						
				}	
			}
			else
			{
				menuItem = new JMenuItem("Create new node");
				menuItem.addActionListener(this);
				popup.add(menuItem);
			}
			
			
			popup.show(e.getComponent(), e.getX(), e.getY());
			
		}
		
		mh.mouseClicked(new MouseEvent((Component)e.getSource(), e.getID(), e.getWhen(), e.getModifiers(), e.getX(), e.getY(), 1, false, MouseEvent.BUTTON1));
		
	}
	public void mouseDragged(MouseEvent e){mh.mouseDragged(e);}
	public void mouseExited(MouseEvent e){mh.mouseExited(e);}
	
	public void actionPerformed(ActionEvent e)
	{
		String command=e.getActionCommand();
		
		if (command.equals("Selection"))
		{
			currentAction = "Selection";
		}
		else if (command.equals("Remove node"))
		{
			currentAction = "Remove node";
		}
		else if (command.equals("Remove edge"))
		{
			currentAction = "Remove edge";
		}
		else if (command.equals("New node"))
		{
			currentAction = "New node";
		}
		else if (command.equals("New edge"))
		{
			currentAction = "New edge";
		}
		else if (command.equals("Exit"))
		{
			System.exit(0);
		}
		
		
		else if(command.equals("Change name"))
		{
			GUIEventManager.getReference().eventChangeNameFor(tempCell);
		}
		else if(command.equals("Change capacity"))
		{
			GUIEventManager.getReference().eventChangeCapacityFor(tempCell);
		}
		else if(command.equals("Change color"))
		{
			GUIEventManager.getReference().eventChangeColorFor(tempCell);	
		}
		else if(command.equals("with strength = 20"))
		{
			GraphEventManager.getReference().eventConstructGradientField(tempCell, 20);	
		}
		else if(command.equals("with strength = 30"))
		{
			GraphEventManager.getReference().eventConstructGradientField(tempCell, 30);	
		}
		else if(command.equals("with other strength"))
		{
			GUIEventManager.getReference().eventAskStrengthForGradientField(tempCell);	
		}
		else if(command.equals("Remove gradient-field"))
		{
			GraphEventManager.getReference().eventRemoveGradientField(tempCell);	
		}
		else if(command.equals("Add resource"))
		{
			GUIEventManager.getReference().eventAddResource(tempCell);
		}
		else if(command.equals("Analyse requests"))
		{
			GUIEventManager.getReference().analyseRequests(tempCell);
		}
		else if(command.equals("Create new node"))
		{
			GraphEventManager.getReference().eventAddVertex("new", tempScaledX, tempScaledY);
		}
		else if(command.equals("Create new edge"))
		{
			GraphEventManager.getReference().eventAddEdge((DefaultGraphCell)selectedItems[0], (DefaultGraphCell)selectedItems[1]);
		}
		else if(command.equals("Remove this node"))
		{
			GraphEventManager.getReference().eventRemoveVertex(tempCell);
		}
		else if(command.equals("Remove this edge"))
		{
			GraphEventManager.getReference().eventRemoveEdge((DefaultEdge)tempCell);
		}
		//else if(command.equals("Search for resource"))
		else if (command.equals(GraphEventManager.getAgentLibrary().getAgentAction(GraphEventManager.getReference().eventgetAgentType())))
		{
			//GUIEventManager.getReference().eventSearchForResource(tempCell);
			GraphEventManager.getAgentLibrary().startNewAgent(tempCell);
		}

	}
	

}

⌨️ 快捷键说明

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