📄 dummyagentgui.java
字号:
/*****************************************************************
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.DummyAgent;
// Import required Java classes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.io.*;
// Import required Jade classes
import jade.core.*;
import jade.lang.acl.*;
import jade.gui.*;
import jade.domain.FIPAAgentManagement.Envelope;
import jade.util.Logger;
/**
@author Giovanni Caire - CSELT S.p.A
@version $Date: 2004-07-19 17:54:06 +0200 (lun, 19 lug 2004) $ $Revision: 5217 $
*/
class DummyAgentGui extends JFrame
{
DummyAgent myAgent;
AID agentName;
AclGui currentMsgGui;
DefaultListModel queuedMsgListModel;
JList queuedMsgList;
File currentDir;
String logoDummy = "images/dummyagent.gif";
DummyAgentGui thisGUI;
//logging
private Logger logger = Logger.getMyLogger(this.getClass().getName());
// Constructor
DummyAgentGui(DummyAgent a)
{
//////////////////////////
// Call JFrame constructor
super();
thisGUI = this;
//////////////////////////////////////////////////////////
// Store pointer to the Dummy agent controlled by this GUI
myAgent = a;
/////////////////////////////////////////////////////////////////////
// Get agent name and initialize the saving/opening directory to null
agentName = myAgent.getAID();
currentDir = null;
////////////////////////////////////////////////////////////////
// Prepare for killing the agent when the agent window is closed
addWindowListener(new WindowAdapter()
{
// This is executed when the user attempts to close the DummyAgent
// GUI window using the button on the upper right corner
public void windowClosing(WindowEvent e)
{
myAgent.doDelete();
}
} );
//////////////////////////
// Set title in GUI window
try{
setTitle(agentName.getName() + " - DummyAgent");
}catch(Exception e){setTitle("DummyAgent");}
Image image = getToolkit().getImage(getClass().getResource(logoDummy));
setIconImage(image);
////////////////////////////////
// Set GUI window layout manager
getContentPane().setLayout(new BorderLayout());
//////////////////////////////////////////////////////////////////////////////////////
// Add the queued message scroll pane to the CENTER part of the border layout manager
queuedMsgListModel = new DefaultListModel();
queuedMsgList = new JList(queuedMsgListModel);
queuedMsgList.setCellRenderer(new ToFromCellRenderer());
JScrollPane pane = new JScrollPane();
pane.getViewport().setView(queuedMsgList);
getContentPane().add("Center", pane);
///////////////////////////////////////////////////////////////////////////////////////////////////
// Add the current message editing fields (an AclGui) to the WEST part of the border layout manager
currentMsgGui = new AclGui(this);
//currentMsgGui.setBorder(new TitledBorder("Current message"));
ACLMessage msg = new ACLMessage(ACLMessage.ACCEPT_PROPOSAL);
msg.setSender(agentName);
currentMsgGui.setMsg(msg);
getContentPane().add("West", currentMsgGui);
/////////////////////////////////////
// Add main menu to the GUI window
JMenuBar jmb = new JMenuBar();
JMenuItem item;
JMenu generalMenu = new JMenu ("General");
generalMenu.add (item = new JMenuItem ("Exit"));
Action exitAction = new AbstractAction("Exit"){
public void actionPerformed(ActionEvent e)
{
myAgent.doDelete();
}
};
item.addActionListener (exitAction);
jmb.add (generalMenu);
Icon resetImg = GuiProperties.getIcon("reset");
Icon sendImg = GuiProperties.getIcon("send");
Icon openImg = GuiProperties.getIcon("open");
Icon saveImg = GuiProperties.getIcon("save");
Icon openQImg = GuiProperties.getIcon("openq");
Icon saveQImg = GuiProperties.getIcon("saveq");
Icon setImg = GuiProperties.getIcon("set");
Icon replyImg = GuiProperties.getIcon("reply");
Icon viewImg = GuiProperties.getIcon("view");
Icon deleteImg = GuiProperties.getIcon("delete");
JMenu currentMsgMenu = new JMenu ("Current message");
currentMsgMenu.add (item = new JMenuItem ("Reset"));
Action currentMessageAction = new AbstractAction("Current message", resetImg){
public void actionPerformed(ActionEvent e)
{
ACLMessage m = new ACLMessage(ACLMessage.ACCEPT_PROPOSAL);
m.setSender(agentName);
m.setEnvelope(new jade.domain.FIPAAgentManagement.Envelope());
currentMsgGui.setMsg(m);
}
};
item.addActionListener(currentMessageAction);
item.setIcon(resetImg);
currentMsgMenu.add (item = new JMenuItem ("Send"));
Action sendAction = new AbstractAction("Send", sendImg){
public void actionPerformed(ActionEvent e) {
ACLMessage m = currentMsgGui.getMsg();
queuedMsgListModel.add(0, (Object) new MsgIndication(m, MsgIndication.OUTGOING, new Date()));
StringACLCodec codec = new StringACLCodec();
try {
String charset;
Envelope env;
if (((env = m.getEnvelope()) == null) ||
((charset = env.getPayloadEncoding()) == null)) {
charset = ACLCodec.DEFAULT_CHARSET;
}
codec.decode(codec.encode(m,charset),charset);
myAgent.send(m);
}
catch (ACLCodec.CodecException ce) {
if(logger.isLoggable(Logger.WARNING))
logger.log(Logger.WARNING,"Wrong ACL Message " + m.toString());
ce.printStackTrace();
JOptionPane.showMessageDialog(null,"Wrong ACL Message: "+"\n"+ ce.getMessage(),"Error Message",JOptionPane.ERROR_MESSAGE);
}
}
};
item.addActionListener (sendAction);
item.setIcon(sendImg);
currentMsgMenu.add (item = new JMenuItem ("Open"));
Action openAction = new AbstractAction("Open", openImg){
public void actionPerformed(ActionEvent e)
{
JFileChooser chooser = new JFileChooser();
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 {
// Note the save/read functionality uses default US-ASCII charset
StringACLCodec codec = new StringACLCodec(new FileReader(fileName),null);
currentMsgGui.setMsg(codec.decode());
}
catch(FileNotFoundException e1) {
JOptionPane.showMessageDialog(null,"File not found: "+ fileName + e1.getMessage(),"Error Message",JOptionPane.ERROR_MESSAGE);
if(logger.isLoggable(Logger.WARNING))
logger.log(Logger.WARNING,"File Not Found: " + fileName); }
catch (ACLCodec.CodecException e2) {
if(logger.isLoggable(Logger.WARNING))
logger.log(Logger.WARNING,"Wrong ACL Message in file: " +fileName);
// e2.printStackTrace();
JOptionPane.showMessageDialog(null,"Wrong ACL Message in file: "+ fileName +"\n"+ e2.getMessage(),"Error Message",JOptionPane.ERROR_MESSAGE);
}
}
}
};
item.addActionListener (openAction);
item.setIcon(openImg);
currentMsgMenu.add (item = new JMenuItem ("Save"));
Action saveAction = new AbstractAction("Save", saveImg){
public void actionPerformed(ActionEvent e)
{
JFileChooser chooser = new JFileChooser();
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);
// Note the save/read functionality uses default US-ASCII charset
StringACLCodec codec = new StringACLCodec(null,f);
ACLMessage ACLmsg = currentMsgGui.getMsg();
codec.write(ACLmsg);
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"); }
}
}
};
item.addActionListener (saveAction);
item.setIcon(saveImg);
currentMsgMenu.addSeparator();
jmb.add (currentMsgMenu);
JMenu queuedMsgMenu = new JMenu ("Queued message");
queuedMsgMenu.add (item = new JMenuItem ("Open queue"));
Action openQAction = new AbstractAction("Open queue", openQImg){
public void actionPerformed(ActionEvent e)
{
JFileChooser chooser = new JFileChooser();
if (currentDir != null)
chooser.setCurrentDirectory(currentDir);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -