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

📄 mmcanvas.java

📁 java实现的P2P多agent中间件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*****************************************************************
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.tools.sniffer;

//#DOTNET_EXCLUDE_BEGIN
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;

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

import java.io.Serializable;

import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.ConcurrentModificationException;

import javax.swing.JPanel;
import javax.swing.SwingUtilities;
//#DOTNET_EXCLUDE_END
/*#DOTNET_INCLUDE_BEGIN
import java.util.*;
import System.Windows.Forms.*;
import System.Drawing.*;
#DOTNET_INCLUDE_END*/

import jade.core.AID;

import jade.gui.AclGui;

import jade.lang.acl.ACLMessage;

/**
   Javadoc documentation for the file
   @author Francisco Regi, Andrea Soracchi - Universita` di Parma
   <Br>
   <a href="mailto:a_soracchi@libero.it"> Andrea Soracchi(e-mail) </a>
   @version $Date: 2007-06-19 09:14:05 +0200 (mar, 19 giu 2007) $ $Revision: 5975 $
   @version $Date: 2007-06-19 09:14:05 +0200 (mar, 19 giu 2007) $ Modified by RR Kessler and ML Griss
   @version $Date: 2007-06-19 09:14:05 +0200 (mar, 19 giu 2007) $ Mofified by ML Griss - 3 line folding
 */

 /**
  * Manages agents and messages on both canvas. It holds an agent list, a message
  * list and all necessary methods for adding, removing and drawing these object.
  * It also registers ad handles events from mouse
  *
  * @see javax.swing.JPanel
  * @see java.awt.event.MouseListener
 */

public class MMCanvas 
	//#DOTNET_EXCLUDE_BEGIN
	extends JPanel implements MouseListener, MouseMotionListener, Serializable
	//#DOTNET_EXCLUDE_END
  {

  private static final int V_TOL = 4;
  private static final int H_TOL = 4;
  private static final int timeUnitWidth = 20;
  private static final int xOffset = 38;
  private int positionAgent=0;


  private int x1,x2,y;
  private MainWindow mWnd;
  private PanelCanvas panCan; /* To resize and modify the scroll bars */
  private MainPanel mPan;
  private int horDim = 400;
  private int vertDim = 200;
  private boolean typeCanv;
  private boolean nameShown = false;
  private List noSniffAgents=new ArrayList();
  //#DOTNET_EXCLUDE_BEGIN
  private Font font1 = new Font("Helvetica",Font.ITALIC,12);
  private Font font2 = new Font("SanSerif",Font.BOLD,12);
  // font3 is used to display the name of the performative above the messages.
  // Needed something a bit smaller than 1 or 2 above so it isn't too obtrusive.
  private Font font3 = new Font("SanSerif", Font.PLAIN, 10);
  //#DOTNET_EXCLUDE_END
  /*#DOTNET_INCLUDE_BEGIN
  private Font font1 = new Font("Helvetica", 12, FontStyle.Italic);
  private Font font2 = new Font("SanSerif",12, FontStyle.Bold);
  // font3 is used to display the name of the performative above the messages.
  // Needed something a bit smaller than 1 or 2 above so it isn't too obtrusive.
  private Font font3 = new Font("SanSerif", 10, FontStyle.Regular);
  private Font font4 = new Font("Helvetica", 10, FontStyle.Italic);
  private Panel myPanel;
  #DOTNET_INCLUDE_END*/
  private MMCanvas otherCanv;
  public AgentList al;
  public MessageList ml;
  
  // These vars are used to make messages grouped by conversationID appear as the
  // same color.  It makes it easier to pick out various conversations.
  private HashMap mapToColor = new HashMap();
  // Removed green, orange, and pink.  They were too hard to see.
  //#DOTNET_EXCLUDE_BEGIN
  private Color colorTable[] = {new Color(200, 0, 150), Color.blue, new Color(230, 230, 0), Color.red, Color.black, Color.magenta, Color.cyan, 
  Color.pink, new Color(0, 200, 150), Color.green};
  private Color noConversationColor = Color.gray;
  //#DOTNET_EXCLUDE_END
  /*#DOTNET_INCLUDE_BEGIN
  	private Color colorTable[] = 
		{
			Color.get_Blue(), Color.get_Black(), Color.get_Cyan(), 
			Color.get_Magenta(), Color.get_Red(), Color.get_White(), Color.get_Yellow()
		};
  private Color noConversationColor = Color.gray;
  #DOTNET_INCLUDE_END*/
  private int colorCounter = 0;
  
  public MMCanvas(boolean type,MainWindow mWnd, PanelCanvas panCan, MainPanel mPan, MMCanvas other ) {
   super();
   otherCanv=other;
   typeCanv=type;
   al=new AgentList();
   ml=new MessageList();
   this.panCan = panCan;
   //#DOTNET_EXCLUDE_BEGIN
   setDoubleBuffered(false);
   addMouseListener(this);
   addMouseMotionListener(this);
   //#DOTNET_EXCLUDE_END
   this.mWnd = mWnd;
   this.mPan=mPan;

   //#DOTNET_EXCLUDE_BEGIN
   if (typeCanv)
     setPreferredSize( new Dimension(horDim,50));
   else
     setPreferredSize( new Dimension(horDim,vertDim));
   //#DOTNET_EXCLUDE_END
   }

  // drawing is all here

  /*#DOTNET_INCLUDE_BEGIN
  public void setPanel(Panel panel)
  {
	myPanel = panel;
	myPanel.add_Paint( new PaintEventHandler( this.paint ) );
  }
  
  public Panel getPanel()
  {
	return myPanel;
  }
  #DOTNET_INCLUDE_END*/

  //#DOTNET_EXCLUDE_BEGIN
  public void paintComponent(Graphics g)
  //#DOTNET_EXCLUDE_END
  /*#DOTNET_INCLUDE_BEGIN
  public void paint(Object o, PaintEventArgs e)
  #DOTNET_INCLUDE_END*/
  {
   //#DOTNET_EXCLUDE_BEGIN
   super.paintComponent(g);
   //#DOTNET_EXCLUDE_END

   int yDim			= 0;
   int xSource		= 0;
   int xDest		= 0;

   int xCanvDim		= 0;
   /*#DOTNET_INCLUDE_BEGIN
   double yOffset		= 0;
   Color color = Color.get_White();
   Graphics g = e.get_Graphics();	
   #DOTNET_INCLUDE_END*/
   try 
   {
     if(typeCanv == true) {
 
       Iterator it = al.getAgents();
       while(it.hasNext()) {

         Agent agent = (Agent)it.next();

         int x = Agent.yRet+(xCanvDim++)*80;

	     //#DOTNET_EXCLUDE_BEGIN
         if(agent.onCanv == false) g.setColor(Color.gray);
         else g.setColor(Color.red);

         if(checkNoSniffedVector(agent)) g.setColor(Color.yellow);
	     
   	     g.draw3DRect(x,Agent.yRet,Agent.bRet,Agent.hRet,true);
		 g.fill3DRect(x,Agent.yRet,Agent.bRet,Agent.hRet,true);
	     g.setColor(Color.black);
		 FontMetrics fm = g.getFontMetrics();
	     //#DOTNET_EXCLUDE_END

		 /*#DOTNET_INCLUDE_BEGIN
		 if (agent.onCanv == false)
			color = Color.get_Gray();
		 else
			color = Color.get_Red();
		 
		 if(checkNoSniffedVector(agent)) 
			color = Color.get_Yellow();
		 
		 Pen pen = new Pen(color);
		 g.DrawRectangle(pen, x, Agent.yRet, Agent.bRet, Agent.hRet);
		 g.FillRectangle(new SolidBrush(color), x, Agent.yRet, Agent.bRet, Agent.hRet);
		 color = Color.get_Black();
		 #DOTNET_INCLUDE_END*/

         String aName=agent.agentName;
         aName = nameClip(aName);
		 //#DOTNET_EXCLUDE_BEGIN
         int nameWidth = fm.stringWidth(aName);
	     //#DOTNET_EXCLUDE_END
	     /*#DOTNET_INCLUDE_BEGIN
		 Font f = new Font("Arial", 10);
		 SizeF sizef = g.MeasureString(aName, f );
		 int nameWidth = (int) sizef.get_Width();
		 #DOTNET_INCLUDE_END*/
         if (nameWidth < Agent.bRet) 
		 {
		    //#DOTNET_EXCLUDE_BEGIN
            g.drawString(aName,x+(Agent.bRet-nameWidth)/2,Agent.yRet+(Agent.hRet/2) + (fm.getAscent()/2));
		    //#DOTNET_EXCLUDE_END
		    /*#DOTNET_INCLUDE_BEGIN
			g.DrawString(aName, f, new SolidBrush(color), x+(Agent.bRet-nameWidth)/2, Agent.yRet+(Agent.hRet/2) - (sizef.get_Height()/2));
			yOffset = Agent.yRet+(Agent.hRet/2) - (sizef.get_Height()/2);
			#DOTNET_INCLUDE_END*/
         } 
		 else 
		 {
           // Need to chop the string up into at most 2 or 3 pieces, truncating the rest.
           int len = aName.length();
           String part1;
           String part2;
           String part3;
           if (nameWidth < Agent.bRet * 2) {
               // Ok, it is not quite twice as big, so cut in half
               part1 = aName.substring(0, len/2);
               part2 = aName.substring(len/2);
			   //#DOTNET_EXCLUDE_BEGIN
               g.drawString(part1, x+(Agent.bRet-fm.stringWidth(part1))/2,Agent.yRet+(Agent.hRet/2) - (int)(fm.getAscent() * 0.2));
               g.drawString(part2, x+(Agent.bRet-fm.stringWidth(part2))/2,Agent.yRet+(Agent.hRet/2) + (int)(fm.getAscent() * 0.9));
			   //#DOTNET_EXCLUDE_END
			   /*#DOTNET_INCLUDE_BEGIN
			   SolidBrush sb = new SolidBrush(color);
			   SizeF sizef1 = g.MeasureString(part1, f );
			   g.DrawString(part1, f, sb, x+( Agent.bRet-sizef1.get_Width() )/2, Agent.yRet+( Agent.hRet/2) - (int) (sizef1.get_Height() * 0.9) );
			   SizeF sizef2 = g.MeasureString(part2, f );
			   g.DrawString(part2, f, sb, x+( Agent.bRet-sizef2.get_Width() )/2, Agent.yRet+( Agent.hRet/2) - (int) (sizef2.get_Height() * 0.2) );
			   #DOTNET_INCLUDE_END*/

           } else if (nameWidth < Agent.bRet * 3) {
               // Ok, it is not quite thrice as big, so cut in three
               part1 = aName.substring(0, len/3);
               part2 = aName.substring(len/3, 2*len/3);
               part3 = aName.substring(2*len/3);
			   //#DOTNET_EXCLUDE_BEGIN
               g.drawString(part1, x+(Agent.bRet-fm.stringWidth(part1))/2,Agent.yRet+(Agent.hRet/2) - (int)(fm.getAscent() * 0.65));
               g.drawString(part2, x+(Agent.bRet-fm.stringWidth(part2))/2,Agent.yRet+(Agent.hRet/2) + (int)(fm.getAscent() * 0.3));
               g.drawString(part3, x+(Agent.bRet-fm.stringWidth(part3))/2,Agent.yRet+(Agent.hRet/2) + (int)(fm.getAscent() * 0.95));
			   //#DOTNET_EXCLUDE_END
			   /*#DOTNET_INCLUDE_BEGIN
  			   SolidBrush sb = new SolidBrush(color);
			   SizeF sizef1 = g.MeasureString(part1, f );
			   g.DrawString(part1, f, sb, x+( Agent.bRet-sizef1.get_Width() )/2, Agent.yRet+( Agent.hRet/2) - (int) (sizef1.get_Height() * 1.00) );
			   SizeF sizef2 = g.MeasureString(part2, f );
			   g.DrawString(part2, f, sb, x+( Agent.bRet-sizef2.get_Width() )/2, Agent.yRet+( Agent.hRet/2) - (int) (sizef2.get_Height() * 0.50) );
			   SizeF sizef3 = g.MeasureString(part3, f );
			   g.DrawString(part3, f, sb, x+( Agent.bRet-sizef3.get_Width() )/2, Agent.yRet+( Agent.hRet/2) - (int) (sizef3.get_Height() * 0.01) );
			   #DOTNET_INCLUDE_END*/
           } 
		   else 
		   {
               // This is rounded down the size of each char.
               int approxCharWidth = nameWidth / agent.agentName.length();
               int charCount = Agent.bRet / approxCharWidth;
               part1 = aName.substring(0, charCount);
               if (aName.length() < (charCount * 2) ) {
                   part2 = aName.substring(charCount);
                   part3 = "";
               } else {
                   part2 = aName.substring(charCount, (charCount * 2));
                   if (charCount * 3 > aName.length()) {
                       part3 = aName.substring(charCount * 2);
                   } else {
                    part3 = aName.substring(charCount*2, (charCount * 3));
                   }
               }
			   //#DOTNET_EXCLUDE_BEGIN
               g.drawString(part1, x+(Agent.bRet-fm.stringWidth(part1))/2,Agent.yRet+(Agent.hRet/2) - (int)(fm.getAscent() * 0.65));
               g.drawString(part2, x+(Agent.bRet-fm.stringWidth(part2))/2,Agent.yRet+(Agent.hRet/2) + (int)(fm.getAscent() * 0.3));
               g.drawString(part3, x+(Agent.bRet-fm.stringWidth(part3))/2,Agent.yRet+(Agent.hRet/2) + (int)(fm.getAscent() * 0.95));
			   //#DOTNET_EXCLUDE_END
			   /*#DOTNET_INCLUDE_BEGIN
			   SolidBrush sb = new SolidBrush(color);
			   SizeF sizef1 = g.MeasureString(part1, f);
			   g.DrawString(part1, f, sb, x+(agent.bRet-sizef1.get_Width())/2, Agent.yRet+(Agent.hRet/2) - (int) (sizef1.get_Height() * 1.00));
			   SizeF sizef2 = g.MeasureString(part2, f);
			   g.DrawString(part2, f, sb, x+(agent.bRet-sizef2.get_Width())/2, Agent.yRet+(Agent.hRet/2) - (int) (sizef2.get_Height() * 0.50));
			   SizeF sizef3 = g.MeasureString(part3, f);
			   g.DrawString(part3, f, sb, x+(agent.bRet-sizef3.get_Width())/2, Agent.yRet+(Agent.hRet/2) - (int) (sizef3.get_Height() * 0.01));
			   #DOTNET_INCLUDE_END*/
             }
         }
       }

       horDim = 100+(xCanvDim*80);

     }
     
     /*#DOTNET_INCLUDE_BEGIN
	 int myOffset = Agent.yRet + Agent.hRet;
     #DOTNET_INCLUDE_END*/

     if((typeCanv == false)) 
	 {

       /* This is the Message Canvas: so let's paint all the messages */

       int x1,x2,y;
       int xCoords[] = new int[3];
       int yCoords[] = new int[3];
       xCanvDim = otherCanv.al.size();

       Iterator it = ml.getMessages();
       int AllReceiver = 0;  
       while(it.hasNext()) {
         Message mess = (Message)it.next();
         String senderName = mess.getSender().getName();    
         xSource = otherCanv.al.getPos(senderName);
         //int receiverForAMessage = 0;
         //for(Iterator i = mess.getAllReceiver(); i.hasNext();) {
       	   //receiverForAMessage++;
       	   //String receiverName = ((AID)i.next()).getName();
         String receiverName = mess.getUnicastReceiver().getName();
           xDest = otherCanv.al.getPos(receiverName);
       
           x1 = mess.getInitSeg(xSource);

⌨️ 快捷键说明

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