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

📄 agenttree.java

📁 java实现的P2P多agent中间件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************
JADE - Java Agent DEvelopment Framework is a framework to develop 
multi-agent systems in compliance with the FIPA specifications.
Copyright (C) 2000 CSELT S.p.A. 

GNU Lesser General Public License

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, 
version 2.1 of the License. 

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA  02111-1307, USA.
*****************************************************************/

package jade.gui;

//#APIDOC_EXCLUDE_FILE
//#J2ME_EXCLUDE_FILE

import javax.swing.*;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.tree.TreePath;
import javax.swing.event.TreeSelectionListener;
import java.awt.Font;
import javax.swing.tree.DefaultMutableTreeNode;
import java.net.InetAddress;
import javax.swing.tree.MutableTreeNode;
import java.util.Enumeration;
import javax.swing.ImageIcon;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.Image;
import java.util.List;
import java.util.LinkedList;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import jade.domain.FIPAAgentManagement.APDescription;
import jade.core.AID;
import jade.domain.FIPAAgentManagement.AMSAgentDescription;
/**
   
   @author Francisco Regi, Andrea Soracchi - Universita' di Parma
   @version $Date: 2007-05-09 15:28:32 +0200 (mer, 09 mag 2007) $ $Revision: 5957 $
 */
public class AgentTree extends JPanel {
	// FIXME: Use better values for TYPE constants
    public static final String TREE_ROOT_TYPE = "SUPERCONTAINER";
    
    public static final String LOCAL_PLATFORM_TYPE = "LOCALPLATFORM";
    
    public static final String AGENT_TYPE = "FIPAAGENT";
    public static final String FROZEN_AGENT_TYPE = "FROZENAGENT";
    public static final String CONTAINER_TYPE = "FIPACONTAINER";
    public static final String FROZEN_CONTAINER_TYPE = "FROZENCONTAINER";
       
    public static final String REMOTE_PLATFORMS_FOLDER_TYPE = "REMOTEPLATFORMS";
    public static final String REMOTE_PLATFORM_TYPE = "REMOTEPLATFORM";
    public static final String REMOTE_AGENT_TYPE = "REMOTEAGENT";
    
    public static final String TREE_ROOT_NAME = "AgentPlatforms";
    public static final String DAFAULT_LOCAL_PLATFORM_NAME = "ThisPlatform";
    public static final String REMOTE_PLATFORMS_FOLDER_NAME = "RemotePlatforms";
    public static final String FROZEN_AGENTS_FOLDER_NAME = "Frozen Agents";
	
	public JTree tree;
	private Map mapDescriptor;
    private String localPlatformName = DAFAULT_LOCAL_PLATFORM_NAME;


	/**
	 * Inner class Node
	 * Common base class for all AgentTree nodes 
	 */
	public abstract class  Node extends DefaultMutableTreeNode {

		protected Icon img;
		protected String name;
		protected String state;
		protected String ownership;
		protected boolean greyOut=false;

		public Node(String name) {
			this.name = name;
		}

		public Icon getIcon(String typeAgent) {
			Image image = getToolkit().getImage(getClass().getResource(getIconAgent(typeAgent)));
			if (greyOut) {
				ImageFilter colorfilter = new MyFilterImage();
				Image imageFiltered=createImage(new FilteredImageSource(image.getSource(),colorfilter));
				return new ImageIcon(imageFiltered);
			}
			else 
				return new ImageIcon(image);
		}

		public String getName(){
			return name;
		}

		public void setName(String name){
			this.name = name;
		}
   
		public String getState(){
			return state != null ? state : "";
		}

		public void setState(String state){
			this.state = state;
		}
   
		public String getOwnership(){
			return ownership != null ? ownership : "";
		}

		public void setOwnership(String ownership){
			this.ownership = ownership;
		}

		public void changeIcon(String agentState) {
			if(agentState.equalsIgnoreCase("suspended")) {
				greyOut = true;
				setType(AGENT_TYPE);
			}
			else if(agentState.equalsIgnoreCase("active")) {
				greyOut = false;
				setType(AGENT_TYPE);
			}
			else if(agentState.equalsIgnoreCase("frozen")) {
				greyOut = false;
				setType(FROZEN_AGENT_TYPE);
			}
		}

		public abstract String getType();
		public abstract void setType(String type);
		public abstract String getToolTipText();

		public String toString() {
			return (getType() != null ? getType()+"-"+name : name);
		}
		
		public int compareTo(Node n) {
			return name.compareTo(n.getName());
		}
	} // END of inner class Node

	
	/**
	 * Inner class AgentNode
	 */
	public class AgentNode extends Node {
		private String agentType;
		private String agentAddress;

		public AgentNode(String name) {
			super(name);
			agentType = AGENT_TYPE;
		}

		public String getAddress() {
			return agentAddress;
		}

		public void setAddress(String address) {
			agentAddress=address;
		}

		public void setType(String type) {
			agentType=type;
		}

		public String getType() {
			return agentType;
		}

		public String getToolTipText() {
			return ("Local Agent");
		}
	}  // END of inner class AgentNode



	/**
	 * Inner class ContainerNode
	 */
	public class ContainerNode extends Node {
		private InetAddress addressmachine;
		private String containerType;

		public ContainerNode(String name) {
			super(name);
			containerType = CONTAINER_TYPE;
		}

		public void setAddress(InetAddress addr) {
			addressmachine = addr;
		}

		public void setType(String type) {
			containerType = type;
		}

		public String getType() {
			return containerType;
		}

		public String getToolTipText() {
			if(addressmachine != null)
				return name + " " + "[" + addressmachine.getHostAddress() + "]";
			else
				return name + " " + "[???:???:???:???]";
		}
	} // END of inner class ContainerNode

	
	/**
	 * Inner class SuperContainer
	 */
	public class SuperContainer extends Node {

		public SuperContainer(String name) {
			super(name);
		}

		public String getToolTipText() {
			return ("Java Agent DEvelopment Framework");
		}

		public String getType(){
			return TREE_ROOT_TYPE;
		}

		public void setType(String noType) {}
	} // END of inner class SuperContainer
	
	
	/**
	 * Inner class RemotePlatformsFolderNode
	 */
	public class RemotePlatformsFolderNode extends Node{
	
		public RemotePlatformsFolderNode(String name){
			super(name);
		}
	
		public String getToolTipText(){
			return ("List of RemotePlatforms");
		}
	
		public void setType(String noType){	
		}
	
		public String getType(){
			return(REMOTE_PLATFORMS_FOLDER_TYPE);
		}
	} // END of inner class RemotePlatformsFolderNode


	/**
	 * Inner class localPlatformFolderNode
	 */
	public class LocalPlatformFolderNode extends Node{

		public LocalPlatformFolderNode(String name){
			super(name);
		}
	
		public String getToolTipText(){
			return("Local JADE Platform");
		}
	
		public void setType(String noType){}
	
		public String getType(){
			return("LOCALPLATFORM");
		}
	} // END of inner class LocalPlatformFolderNode


	/**
	 * Inner class RemotePlatformNode
	 */
	public class RemotePlatformNode extends Node{
	
		private APDescription AP_Profile;
		private AID amsAID;
	
		public RemotePlatformNode(String name){
			super(name);
		}
	
		public String getToolTipText(){
			return ("Remote Platform");
		}
	
		public void setType(String noType){
		}
	
		public String getType(){
			return("REMOTEPLATFORM");
		}
	
		public void setAPDescription(APDescription desc){
			AP_Profile = desc;
		}
	
		public APDescription getAPDescription(){
			return AP_Profile;
		}

		public void setAmsAID(AID id){
			amsAID = id;
		}
	
		public AID getAmsAID(){
			return amsAID;
		}
	} // END of inner class RemotePlatformNode

	
	/**
	 * Inner class RemoteAgentNode
	 */
	public class RemoteAgentNode extends AgentNode{

		private AMSAgentDescription amsd;
	
		public RemoteAgentNode(String name){
			super(name);
		}
	
		public String getToolTipText(){
			return ("Remote Agent");
		}
	
		public void setType(String noType){
		}
	
		public String getType(){
			return("REMOTEAGENT");
		}
	
		public void setAMSDescription(AMSAgentDescription id){
			amsd = id;
		}
	
		public AMSAgentDescription getAMSDescription(){
			return amsd;
		}
	} // END of inner class RemoteAgentNode

	
	
	
    public AgentTree() {
    	this(null);
    }
    
	public AgentTree(Font f) {
		mapDescriptor = new HashMap();
		register(TREE_ROOT_TYPE, null, "images/folderyellow.gif");
		register(LOCAL_PLATFORM_TYPE, null, "images/folderyellow.gif");
		register(CONTAINER_TYPE, null, "images/foldergreen.gif");
		register(AGENT_TYPE, null, "images/runtree.gif");
		
		register(FROZEN_CONTAINER_TYPE, null, "images/frozenagents.gif");
		register(FROZEN_AGENT_TYPE, null, "images/freezeagent.gif");
		
  		register(REMOTE_PLATFORMS_FOLDER_TYPE, null, "images/folderblue.gif");
		register(REMOTE_PLATFORM_TYPE, null , "images/folderlightblue.gif");
	    register(REMOTE_AGENT_TYPE, null, "images/runtree.gif");
	    
		tree = new JTree();
		if (f != null) {
			tree.setFont(f);
		}
		tree.setModel(new AgentTreeModel(new SuperContainer(TREE_ROOT_NAME)));
		tree.setLargeModel(false);
		tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);

		// Add localPlatform folder.
		AgentTreeModel model = getModel();
		MutableTreeNode root = (MutableTreeNode) model.getRoot();
		LocalPlatformFolderNode localAP = new LocalPlatformFolderNode(localPlatformName);
		model.insertNodeInto(localAP, root, root.getChildCount());

		ToolTipManager.sharedInstance().registerComponent(tree);
		tree.setShowsRootHandles(true);
		tree.setCellRenderer(new TreeIconRenderer());
		tree.setRowHeight(0);

		tree.addMouseListener(new AgentTreePopupManager(this));		
	}

	//public void listenerTree(TreeSelectionListener panel) {
	//	tree.addTreeSelectionListener(panel);
	//}

	public AgentNode createAgentNode(String name) {
		return new AgentNode(name);
	}
	
	public ContainerNode createContainerNode(String name) {
		return new ContainerNode(name);
	}
	
	/**
	 * @deprecated Use createAgentNode() and createContainerNode() instead
	 */
	public AgentTree.Node createNewNode(String name, int i) {
		switch (i) {
		case 0:
			return new AgentTree.ContainerNode(name);
		case 1:
			return new AgentTree.AgentNode(name);
		}
		return null;
	}

	public void refreshLocalPlatformName(String newName) {
		String oldName = localPlatformName;
		localPlatformName = newName;
		AgentTreeModel model = getModel();
		MutableTreeNode root = (MutableTreeNode) model.getRoot();
		Enumeration children = root.children();
		while (children.hasMoreElements()) {
			AgentTree.Node node = (AgentTree.Node) children.nextElement();
			String name = node.getName();
			if (name.equalsIgnoreCase(oldName)) {
				node.setName(newName);
				return;
			}
		}
	}

	public void clearLocalPlatform() {
		AgentTreeModel model = getModel();
		MutableTreeNode root = (MutableTreeNode) model.getRoot();
		Enumeration folders = root.children();
		while (folders.hasMoreElements()) {
			AgentTree.Node folderNode = (AgentTree.Node) folders.nextElement();
			String folderName = folderNode.getName();
			if (folderName.equalsIgnoreCase(localPlatformName)) {
				Enumeration containers = folderNode.children();
				List toRemove = new LinkedList();
				while (containers.hasMoreElements()) {
					AgentTree.Node container = (AgentTree.Node) containers.nextElement();
					toRemove.add(container);
				}

				Iterator it = toRemove.iterator();
				while (it.hasNext()) {
					MutableTreeNode node = (MutableTreeNode) it.next();
					model.removeNodeFromParent(node);
				}
			}
		}
	}

	public void addContainerNode(String containerName, InetAddress addr) {
		ContainerNode node = new ContainerNode(containerName);
		node.setAddress(addr);
		
		AgentTreeModel model = getModel();
		MutableTreeNode root = (MutableTreeNode) model.getRoot();
		Enumeration folders = root.children();
		while (folders.hasMoreElements()) {
			Node folderNode = (Node) folders.nextElement();
			String folderName = folderNode.getName();
			if (folderName.equalsIgnoreCase(localPlatformName)) {
				model.insertNodeInto(node, folderNode, folderNode.getChildCount());
				return;
			}
		}
	}

⌨️ 快捷键说明

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