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

📄 grapheventmanager.java

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

import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.DefaultPort;

import gui.GUIEventManager;

import java.util.Iterator;
import java.util.LinkedList;

import java.awt.Color;

public class GraphEventManager 
{	
	//	De enige instantie van deze klasse
	static private GraphEventManager graphEventManager;
	
	// Controle of er reeds instantie van deze klasse werd gemaakt
	static private boolean $created = false;

	private String agentType = "Pheromone agents";

	private Controller myController;

	private ResourceLibrary resourceLibrary;
	
	static private AgentLibrary agentLibrary = new AgentLibrary();

	// Geef referentie naar deze klasse terug aan controller
	static public GraphEventManager getReference()
	{
		if (!$created)
		{
			graphEventManager = new GraphEventManager();
			$created = true;
		}
		return(graphEventManager);
	}
			
	// Private constructor 
	private GraphEventManager() 
	{
		setResourceLibrary(new ResourceLibrary());
	}
	
	public void eventAddVertex(String label, int xpos, int ypos)
	{
		getMyController().addVertex(label, xpos, ypos);	
	}	
	 
	public void eventAddEdge(DefaultGraphCell dgc1, DefaultGraphCell dgc2)
	{
	   getMyController().addEdge(dgc1, dgc2);
	}

	public void eventRemoveVertex(DefaultGraphCell dgc)
	{
		getMyController().removeVertex(dgc);
	}

	public void eventRemoveEdge(DefaultEdge dgc)
	{
		getMyController().removeEdge(dgc);
	}
	
	public void eventChangeNameFor(DefaultGraphCell dgc, String newName)
	{
		if (newName != "")
		{
			((Properties)GraphConstants.getValue(dgc.getAttributes())).setName(newName);
		}
	}

	public void eventChangeCapacityFor(DefaultGraphCell dgc, int newCapacity)
	{
		if (newCapacity > 0 && newCapacity < 11 && dgc instanceof DefaultEdge)
		{
			((EdgeProperties)GraphConstants.getValue(((DefaultEdge)dgc).getAttributes())).setCapacity(newCapacity);
		}
		else
		{
			System.out.println("Invalid number");
		}
	}
	
	public void eventChangeColorFor(DefaultGraphCell dgc, Color newColor)
	{
		if (newColor != null)
		{
			((Properties) GraphConstants.getValue(dgc.getAttributes())).setColor(newColor);
		}
	}
		
	public DefaultEdge eventGetConnectingEdge(DefaultGraphCell dgc1, DefaultGraphCell dgc2)
	{
		return getConnectingEdge(dgc1, dgc2);
	}

	public Properties getProperties(DefaultGraphCell dgc)
	{
		return (Properties)GraphConstants.getValue(dgc.getAttributes());
	}

	public EdgeProperties getEdgeProperties(DefaultEdge de)
	{
		return (EdgeProperties)GraphConstants.getValue(de.getAttributes());
	}
		
	public void eventConstructNetworkGraph(int[] nbNodes, int[] organisation, double[] edgeProbs, int[] capacities)
	{
		getMyController().loadRandomGraph(nbNodes, organisation, edgeProbs, capacities);	

		GUIEventManager.getReference().eventApplyStandardLayoutAlgorithm();
		GUIEventManager.getReference().eventApplyStandardLayoutAlgorithm();
	}
	
	public void eventGetExistingGraph(String pathToFile)
	{
		getMyController().loadExistingGraph(pathToFile);
	}
	
	public void eventSaveModel(String filename)
	{
		getMyController().saveModel(filename);
	}
	
	public void eventOpenModel(String filename)
	{
		getMyController().openModel(filename);
	}

	
	public void eventConstructAllGradientFields(int strength)
	{
		if (strength > 0)
		{
			getMyController().constructAllGradientFields(strength);
			GUIEventManager.getReference().eventRedrawGraph();
		}
		else
		{
			System.out.println("Invalid number");
		}
	}
	
	public void eventConstructGradientField(DefaultGraphCell dgc, int strength)
	{
		if (strength > 0)
		{
			getMyController().constructGradientField(dgc, dgc, strength);
			GUIEventManager.getReference().eventRedrawGraph();
		}
		else
		{
			System.out.println("Invalid number");
		}
	}

	public void eventRemoveAllGradientFields()
	{
		getMyController().removeAllGradientFields();
		GUIEventManager.getReference().eventRedrawGraph();
	}
	
	public void eventRemoveGradientField(DefaultGraphCell dgc)
	{
		if (dgc != null)
		{
			getMyController().removeGradientField(dgc, dgc, true);
			GUIEventManager.getReference().eventRedrawGraph();
		}
		else
		{
			System.out.println("Invalid graphcell");
		}
	}
	
	public void eventAddResource(DefaultGraphCell dgc, Resource resource)
	{
		//Resource resource = new Resource(GUIEventManager.getReference().eventGetString("What's the name of the resource ?", "Resource name"));
		
		//Resource resource = GUIEventManager.getReference().eventGetResource("What's the name of the resource ?");
		getMyController().addResource(dgc, resource);
		//GUIEventManager.getReference().eventRedrawGraph();
	}
	
	public void eventAddResourceToLibrary(Resource resource)
	{
		getResourceLibrary().addResource(resource);
	}

	public void eventSearchForResource(DefaultGraphCell dgc, String resourceName)
	{
		//Resource resource = GUIEventManager.getReference().eventGetResource("What are you looking for ?");
		//String resourceName = GUIEventManager.getReference().eventGetString("What are you looking for ?", "Resource name");
		if (resourceName != null)
		{
			getMyController().searchForResource(dgc, resourceName);
		}
	}
	
	
	
	public LinkedList getConnectedNodes(DefaultGraphCell dgc)
	{
		LinkedList neighbours = new LinkedList();
	
		if (dgc.getChildCount() != 0)
		{
			Iterator iterator = ((DefaultPort)dgc.getChildAt(0)).edges();
			DefaultEdge edge;
		
			int i = 0;	
			
			while (iterator.hasNext())	
			{
				i++;
				edge = ( (DefaultEdge)iterator.next());
				if (edge != null)
				{
					if ( ((DefaultGraphCell)((DefaultPort)edge.getTarget()).getParent()).equals(dgc))
					{
						neighbours.add((DefaultGraphCell)((DefaultPort)edge.getSource()).getParent());
						
					}
					else
					{
						neighbours.add((DefaultGraphCell)((DefaultPort)edge.getTarget()).getParent()); 
					}
				}
			}
		
			return neighbours;
		}
		else
		{
			return null;
		}
	}
	
	
	public Iterator getConnectingEdges(DefaultGraphCell dgc)
	{
		if (dgc.getChildCount() != 0)
		{
			return ((DefaultPort)dgc.getChildAt(0)).edges();
		}
		return null;	
	}
	
	public DefaultEdge getConnectingEdge(DefaultGraphCell dgc1, DefaultGraphCell dgc2)
		{
			Iterator iterator = ((DefaultPort)dgc1.getChildAt(0)).edges();
			DefaultEdge edge;
			
			while (iterator.hasNext())	
			{
				edge = ( (DefaultEdge)iterator.next());
			
				if ( ((DefaultGraphCell)((DefaultPort)edge.getTarget()).getParent()).equals(dgc2) || 
					 ((DefaultGraphCell)((DefaultPort)edge.getSource()).getParent()).equals(dgc2)   )
				{
					return edge;
				}
			}
			return null;	
		}
	
	public void eventStartSimulation()
	{
		getMyController().startTimer();
	}

	public void eventStopSimulation()
	{
		getMyController().stopTimer();
	}	
	
	public void eventMakeAStep()
	{
		getMyController().actionPerformed(null);
	}
	
	public void eventDecreaseDelay()
	{
		getMyController().decreaseDelay();
	}
	
	public void eventIncreaseDelay()
	{
		getMyController().increaseDelay();
	}
	
	public Controller getMyController()
	{
		return myController;
	}

	public void setMyController(Controller myController)
	{
		this.myController = myController;
	}
	
	public void eventSetAgentType(String type)
	{
		System.out.println(type);
		this.agentType = type;
	}
	
	public String eventgetAgentType()
	{
		return agentType;
	}

	
	public LinkedList getAgentTypes()
	{
		return agentLibrary.getAgentTypes();
	}
	
	public Agent constructNewAgent(DefaultGraphCell origin, ResourceQuery searchFor)
	{
		return agentLibrary.constructNewAgent(eventgetAgentType(), origin, searchFor);
	}

	public static AgentLibrary getAgentLibrary()
	{
		return agentLibrary;
	}

	private ResourceLibrary getResourceLibrary()
	{
		return resourceLibrary;
	}

	private void setResourceLibrary(ResourceLibrary resourceLibrary)
	{
		this.resourceLibrary = resourceLibrary;
	}
	
	public Iterator eventGetRandomResources(int aantal)
	{
		return getResourceLibrary().getRandomResources(aantal);
	}
	
	public void eventAddRandomResources(int min, int max)
	{
		getMyController().addRandomResources(min, max);
		GUIEventManager.getReference().eventRedrawGraph();
	}
	
	public void eventAddNewResourceToLibrary(String newResource)
	{
		getResourceLibrary().addResource(new Resource(newResource));
	}
	
	public Resource eventGetResource(String nameResource)
	{
		return getResourceLibrary().getResource(nameResource);
	}
	
	public String[] eventGetResourceNames()
	{
		return getResourceLibrary().getResourceNames();
	}
	
	public void eventSetQueryProbability(int prob)
	{
		getMyController().setQueryProbability(prob);
	}
	
}

⌨️ 快捷键说明

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