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

📄 acltracepanel.java

📁 JADE(JAVA Agent开发框架)是一个完全由JAVA语言开发的软件,它简化了多Agent系统的实现。
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/******************************************************************
 * JADE - Java Agent DEvelopment Framework is a framework to develop
 * multi-agent systems in compliance with the FIPA specifications.
 * Copyright (C) 2002 TILAB S.p.A.
 *
 * This file is donated by Acklin B.V. to the JADE project.
 *
 *
 * 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.gui;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import java.util.Comparator;
import java.util.Date;
import java.util.Date;
import java.util.StringTokenizer;
import javax.swing.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.text.*;
import javax.swing.tree.*;

import jade.core.AID;
import jade.core.Agent;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.StringACLCodec;
import jade.tools.sl.SLFormatter;
import jade.util.leap.*;
import jade.util.Logger;

/**
 *  This class show a List of incoming and outgoing ACLmessages. When clicking
 *  on the right button a pop menu wil be shown, containing operations on the
 *  selected ACLMessage
 *
 * @author     Chris van Aart - Acklin B.V., the Netherlands
 * @created    April 26, 2002
 */

public class ACLTracePanel extends JPanel {

  //logging
  
  private Logger logger = Logger.getMyLogger(this.getClass().getName());
  /**
   *  Constructor for the ACLTreePanel object
   *
   * @param  agent  link to the agent
   */
  public ACLTracePanel(Agent agent) {
    this.agent = agent;
    inMsgCount = 0;
    outMsgCount = 0;
    aclIndex = 0;
    try {
      jbInit();
      aclTree.setCellRenderer(aclTreeRenderer);
      // since JDK1.3 aclTree.setToggleClickCount(3);

      this.fillSortComboBoxModel();
      this.sortComboBox.setModel(this.sortComboBoxModel);
      this.sortComboBox.setRenderer(new SortRenderer());
      this.sortComboBox.setSelectedIndex(0);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }


  public ACLMessage getCurrentACL() {
    return currentACL;
  }


  public void doSystemOut() {
    if (currentACL == null) {
      return;
    }
    if(logger.isLoggable(Logger.CONFIG))
    	logger.log(Logger.CONFIG,"\n" + currentACL.toString() + "\n");
  }


  /**
   *  Adds a ACLMessageNode to the List
   *
   * @param  theNode  the ACLMessageNode
   */
  public void addMessageNode(ACLMessageNode theNode) {
    addMessageNode(theNode.getDirection(), theNode.getTime(), theNode.getMessage());
  }


  /**
   *  Adds a ACLMessage to the List
   *
   * @param  direction  the direction of the ACLMessage
   * @param  theACL     the ACLMessage to be added
   */
  public void addMessageNode(String direction, ACLMessage theACL) {
    String theTime = getTimeStamp();
    addMessageNode(direction, theTime, theACL);
  }


  /**
   *  Adds a ACLMessage to the List
   *
   * @param  direction  the direction of the ACLMessage
   * @param  theTime    timeStamp
   * @param  theACL     the ACLMessage to be added
   */
  public void addMessageNode(String direction, String theTime, ACLMessage theACL) {

    String aclString = theACL.toString();
    aclString = aclString.replace('\n', ' ');

    String contentString = (theACL.getContent() != null ? theACL.getContent().replace('\n', ' ') : "");

    String theSender = theACL.getSender().getLocalName();
    String theReceiver = "<none>";
    if (theACL.getAllReceiver().hasNext()) {
      theReceiver = ((AID)theACL.getAllReceiver().next()).getLocalName();
    }

    if (direction.startsWith("i")) {
      this.inMsgCount++;
      aclString = theACL.getPerformative(theACL.getPerformative()) +
        ": from  " + theSender + " - " + contentString + " ";
    }
    else {
      this.outMsgCount++;
      aclString = theACL.getPerformative(theACL.getPerformative()) +
        ": to  " + theReceiver + " - " + contentString + " ";

    }

    DefaultMutableTreeNode contentNode = new DefaultMutableTreeNode(":content");
    ACLMessageNode messageNode = new ACLMessageNode(theTime + " " + direction + ": " + aclString);
    messageNode.setMessage(theACL);
    messageNode.setDirection(direction);
    messageNode.setTime(theTime);
    aclModel.insertNodeInto(messageNode, aclRoot, aclIndex++);

    String contentLanguage = (theACL.getLanguage() != null ? theACL.getLanguage() : "<unknown>");
    String plainContent = (theACL.getContent() != null ? theACL.getContent() : "");

    if (contentLanguage.indexOf("SL") >= 0) {
      //Only format when SL
      try {
        plainContent = (String)new SLFormatter().format(plainContent);
      }
      catch (Exception ex) {
        //too bad!
      }
    }
    while ((plainContent.indexOf('\n')) == 0) {
      plainContent = plainContent.substring(1);
    }

    StringTokenizer stok = new StringTokenizer(plainContent, "\n");
    while (stok.hasMoreTokens()) {
      contentNode.add(new DefaultMutableTreeNode(stok.nextToken()));
    }

    int i = 0;
    aclModel.insertNodeInto(new DefaultMutableTreeNode(":act " + theACL.getPerformative(theACL.getPerformative())), messageNode, i++);

    AID sender = theACL.getSender();
    if (sender != null) {
      aclModel.insertNodeInto(new DefaultMutableTreeNode(":sender " + sender.toString()), messageNode, i++);
    }

    AID aid = new AID();
    Iterator it = theACL.getAllReceiver();
    while (it.hasNext()) {
      aid = (AID)it.next();
      aclModel.insertNodeInto(new DefaultMutableTreeNode(":receiver " + aid.toString()), messageNode, i++);
    }

    //reply-to
    Iterator replyItor = theACL.getAllReplyTo();
    while (replyItor.hasNext()) {
      aid = (AID)replyItor.next();
      aclModel.insertNodeInto(new DefaultMutableTreeNode(":reply-to " + aid.toString()), messageNode, i++);
    }

    aclModel.insertNodeInto(new DefaultMutableTreeNode("--- Description of Content ---"), messageNode, i++);
    aclModel.insertNodeInto(contentNode, messageNode, i++);

    String language = theACL.getLanguage();
    if (language != null) {
      aclModel.insertNodeInto(new DefaultMutableTreeNode(":language " + language), messageNode, i++);
    }

    String encoding = theACL.getEncoding();
    if (encoding != null) {
      aclModel.insertNodeInto(new DefaultMutableTreeNode(":encoding " + encoding), messageNode, i++);
    }

    String ontology = theACL.getOntology();
    if (ontology != null) {
      aclModel.insertNodeInto(new DefaultMutableTreeNode(":ontology " + ontology), messageNode, i++);
    }

    aclModel.insertNodeInto(new DefaultMutableTreeNode("--- Message control ---"), messageNode, i++);

    String protocol = theACL.getProtocol();
    if (protocol != null) {
      aclModel.insertNodeInto(new DefaultMutableTreeNode(":protocol " + protocol), messageNode, i++);
    }

    String convId = theACL.getConversationId();
    if (convId != null) {
      aclModel.insertNodeInto(new DefaultMutableTreeNode(":conversation-id " + convId), messageNode, i++);
    }

    String inreplyto = theACL.getInReplyTo();
    if (inreplyto != null) {
      aclModel.insertNodeInto(new DefaultMutableTreeNode(":in-reply-to " + inreplyto), messageNode, i++);
    }

    String replywith = theACL.getReplyWith();
    if (replywith != null) {
      aclModel.insertNodeInto(new DefaultMutableTreeNode(":reply-with " + replywith), messageNode, i++);
    }

    Date replyBy = theACL.getReplyByDate();
    if (replyBy != null) {
      aclModel.insertNodeInto(new DefaultMutableTreeNode(":reply-by " + replyBy.toString()), messageNode, i++);
    }

    jade.util.leap.Properties prop = theACL.getAllUserDefinedParameters();
    if (prop.size() > 0) {
      aclModel.insertNodeInto(new DefaultMutableTreeNode("--- User Defined Parameters ---"), messageNode, i++);
      java.util.Enumeration enumeration = prop.elements();
      while (enumeration.hasMoreElements()) {
        String key = (String)enumeration.nextElement();
        String value = (String)prop.getProperty(key);
        aclModel.insertNodeInto(new DefaultMutableTreeNode(":X-" + key + " " + value), messageNode, i++);
      }
    }
    if (!sorting) {
      doSort();
    }
    aclTree.expandPath(new TreePath(aclRoot.getPath()));
  }


  /**
   *  delete the current selected ACLMessage from the list
   */
  public void deleteCurrent() {
      //since JDK1.3 TreePath tp = aclTree.getAnchorSelectionPath();
    TreePath tp = aclTree.getSelectionPath();
    if (tp == null) {
      currentACL = null;
    }

    else {
      currentACL = ((ACLMessageNode)tp.getPathComponent(1)).getMessage();
      this.aclModel.removeNodeFromParent((ACLMessageNode)tp.getPathComponent(1));
      this.refresh();
      aclIndex--;
    }
  }


  /**
   *  show about screen
   */
  public void doShowAbout() {
    new AboutFrame().show();
  }


  /**
   *  save the current selected ACLMessage to a textfile
   */
  public void saveACL() {

    UIManager.put("FileChooser.saveButtonToolTipText", "Save ACLMessage");

    JFileChooser chooser = new JFileChooser();
    chooser.setFileFilter(new ACLFileFilter());
    //since JDK1.3 chooser.setAcceptAllFileFilterUsed(false);
    chooser.setSelectedFile(new File("itsmessage.acl"));
    chooser.setDialogTitle("Save ACLMessage");
    if (currentDir != null) {
      chooser.setCurrentDirectory(currentDir);
    }

    int returnVal = chooser.showSaveDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      currentDir = chooser.getCurrentDirectory();
      String fileName = chooser.getSelectedFile().getAbsolutePath();

      try {
        FileWriter f = new FileWriter(fileName);
        StringACLCodec codec = new StringACLCodec(null, f);
        // ACLMessage ACLmsg = currentMsgGui.getMsg();
        codec.write(currentACL);
        f.close();

      }
      catch (FileNotFoundException e3) {
       if(logger.isLoggable(Logger.WARNING))
       	logger.log(Logger.WARNING,"Can't open file: " + fileName);
      }
      catch (IOException e4) {
        if(logger.isLoggable(Logger.WARNING))
        	logger.log(Logger.WARNING,"IO Exception");
      }
    }

  }


  public void doShowCurrentACL() {
    showCurrentACL(x, y);
  }


  public void showStastistics() {
    ACLStatisticsFrame.show(this.aclModel);
  }


  public void saveQueue() {

    UIManager.put("FileChooser.saveButtonToolTipText", "Save ACLMessage Trace");

    JFileChooser chooser = new JFileChooser();
    chooser.setFileFilter(new TraceFileFilter());
    // since JDK1.3 chooser.setAcceptAllFileFilterUsed(false);
    chooser.setSelectedFile(new File("itsTrace.trc"));
    chooser.setDialogTitle("Save ACLMessage Trace");
    if (currentDir != null) {
      chooser.setCurrentDirectory(currentDir);
    }

    int returnVal = chooser.showSaveDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      currentDir = chooser.getCurrentDirectory();
      String fileName = chooser.getSelectedFile().getAbsolutePath();

      if (!fileName.endsWith(".trc")) {
        fileName = fileName + ".trc";
      }

      try {
        FileWriter f = new FileWriter(fileName);
        BufferedWriter bw = new BufferedWriter(f);
        int size = aclModel.getChildCount(this.aclRoot);
        for (int i = 0; i < size; i++) {
          System.out.println("aclModel: " + aclModel.getChild(this.aclRoot, i).getClass());
          ACLMessageNode theNode = (ACLMessageNode)aclModel.getChild(this.aclRoot, i);
          String direction = theNode.getDirection();
          String theTime = theNode.getTime();
          ACLMessage theMsg = theNode.getMessage();
          bw.newLine();
          bw.write(direction);
          bw.newLine();
          bw.write(theTime);
          bw.newLine();
          bw.write(theMsg.toString());
          bw.newLine();
        }
        bw.flush();
        bw.close();
        f.close();

      }
      catch (FileNotFoundException e3) {
        if(logger.isLoggable(Logger.WARNING))
        	logger.log(Logger.WARNING,"Can't open file: " + fileName);
      }
      catch (IOException e4) {
        if(logger.isLoggable(Logger.WARNING))
	        logger.log(Logger.WARNING,"IO Exception");
      }
    }
  }


  public void loadQueue() {

    UIManager.put("FileChooser.openButtonToolTipText", "Open ACLMessage Trace");

    JFileChooser chooser = new JFileChooser();
    chooser.setFileFilter(new TraceFileFilter());
    // since JDK1.3 chooser.setAcceptAllFileFilterUsed(false);
    chooser.setDialogTitle("Open ACLMessage Trace From File");
    if (currentDir != null) {
      chooser.setCurrentDirectory(currentDir);
    }

    int returnVal = chooser.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      currentDir = chooser.getCurrentDirectory();
      String fileName = chooser.getSelectedFile().getAbsolutePath();

      try {
        clearACLModel();
        FileReader f = new FileReader(fileName);
        BufferedReader br = new BufferedReader(f);
        String line = br.readLine();
        String direction;
        String theTime;
        while (line != null) {
          direction = br.readLine();
          theTime = br.readLine();

          String theMessageLine = "";
          line = br.readLine();
          while ((line != null) && (!line.equals(""))) {
            theMessageLine = theMessageLine + "\n" + line;
            line = br.readLine();
          }
          char[] chararray = theMessageLine.toCharArray();

⌨️ 快捷键说明

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