personalselectwindow.java

来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 323 行

JAVA
323
字号
/* PersonalSelectWindow.java * $Id: PersonalSelectWindow.java,v 2.4 1997-11-19 19:22:41+09 ht Exp $ * $Name: alpha5_1-release $ */package com.ibm.acl.kqmlsample.travel;import com.ibm.acl.kqml.*;import java.lang.*;import java.util.*;import java.awt.*;import java.awt.image.*;import java.io.*;/** * PersonalSelectWindow shows the result of the air flight plan. * It's created by PersonalAssist object. * * @version     $Revision: 2.4 $ $Date: 1997-11-19 19:22:41+09 $ * @author      Osamu Furusawa * @author      Hajime Tsuchitani */public class PersonalSelectWindow extends Frame {private static final String rcsid = "$Id: PersonalSelectWindow.java,v 2.4 1997-11-19 19:22:41+09 ht Exp $ $Name: alpha5_1-release $";    private PersonalAssist    agent;    private Button    button1st = new Button("subscribe");    private Button    button2nd = new Button("subscribe");    private Button    button3rd = new Button("subscribe");    private TextArea text1st = new TextArea(4,30);     private TextArea text2nd = new TextArea(4,30);    private TextArea text3rd = new TextArea(4,30);    private   Button     close        = new Button("Close");    private KQMLManager kManager;    private String travelBroker;    private String localHost;    private Vector flightsInfo;    /**     * Constructor     *     * @param agent PersonalAssitAgent      * @param flightsInfo the result of the flight plan     */    public  PersonalSelectWindow(PersonalAssist agent, Vector flights) {        super("Flight Plan Result");	this.agent = agent;	kManager = agent.getKQMLManager();	travelBroker = agent.getTravelBroker();	localHost = agent.getLocalHost();	flightsInfo = flights;        // title label        Label titleLabel = new Label("Flight Plan Schedule");        titleLabel.setFont(new Font("Helvetica", Font.BOLD, 36));        titleLabel.setForeground(new Color(60, 170, 60)); //  green        Panel titlePanel = new Panel();        titlePanel.add(titleLabel);        titlePanel.setBackground(Color.white);        // Panel 1 -------------------------------        Panel panel1 = new Panel();        panel1.setLayout(new GridBagLayout());        panel1.setBackground(Color.white);        button1st.setFont(new Font("SanSerif", Font.BOLD, 13));        text1st.setFont(new Font("SanSerif", Font.BOLD, 13));        constrain(panel1, button1st,0,0,1,1, GridBagConstraints.NONE,                  GridBagConstraints.CENTER, 0.0, 0.0, 5, 5, 5, 5);        constrain(panel1, text1st,1,0,1,4, GridBagConstraints.BOTH,                  GridBagConstraints.CENTER, 1.0, 1.0, 5, 5, 5, 5);                          // Panel 1 -------------------------------        Panel panel2 = new Panel();        panel2.setLayout(new GridBagLayout());        panel2.setBackground(Color.white);        button2nd.setFont(new Font("SanSerif", Font.BOLD, 13));        text2nd.setFont(new Font("SanSerif", Font.BOLD, 13));        constrain(panel2, button2nd,0,1,1,1, GridBagConstraints.NONE,                  GridBagConstraints.CENTER, 0.0, 0.0, 5, 5, 5, 5);        constrain(panel2, text2nd,1,1,1,4, GridBagConstraints.BOTH,                  GridBagConstraints.CENTER, 1.0, 1.0, 5, 5, 5, 5);        Panel panel3 = new Panel();        panel3.setLayout(new GridBagLayout());        panel3.setBackground(Color.white);        button3rd.setFont(new Font("SanSerif", Font.BOLD, 13));        text3rd.setFont(new Font("SanSerif", Font.BOLD, 13));        constrain(panel3, button3rd,0,2,1,1, GridBagConstraints.NONE,                  GridBagConstraints.CENTER, 0.0, 0.0, 5, 5, 5, 5);        constrain(panel3, text3rd,1,2,1,4, GridBagConstraints.BOTH,                  GridBagConstraints.CENTER, 1.0, 1.0, 5, 5, 5, 5);        Panel buttonPanel = new Panel();        buttonPanel.setLayout(new GridBagLayout());        buttonPanel.setBackground(Color.white);        close.setFont(new Font("Courier", Font.BOLD, 13));        constrain(buttonPanel, close, 0,0,1,1,GridBagConstraints.NONE,                  GridBagConstraints.CENTER, 0.3, 0.0, 0,0,0,0);        //setLayout(new GridLayout(5,1));        //add(titlePanel);        //add(panel1);        //add(panel2);        //add(panel3);        //add(buttonPanel);        setLayout(new GridBagLayout());        setBackground(Color.white);        constrain(this, titlePanel,0,0,1,1, GridBagConstraints.NONE,                  GridBagConstraints.CENTER, 0.0, 0.0, 1, 1, 1, 1);        constrain(this, panel1,0,1,1,2, GridBagConstraints.BOTH,                  GridBagConstraints.NORTHWEST, 1.0, 1.0, 1, 1, 1, 1);        constrain(this, panel2,0,3,1,2, GridBagConstraints.BOTH,                  GridBagConstraints.NORTHWEST, 1.0, 1.0, 1, 1, 1, 1);        constrain(this, panel3,0,5,1,2, GridBagConstraints.BOTH,                  GridBagConstraints.NORTHWEST, 1.0, 1.0, 1, 1, 1, 1);         constrain(this, buttonPanel,0,7,1,1, GridBagConstraints.HORIZONTAL,                  GridBagConstraints.CENTER, 0.0, 0.0, 10, 0, 10,0);	// show result.	// only top 3 results is shown.        int tablesize = flightsInfo.size();	StringBuffer[] strbuf = new StringBuffer[3];	for ( int i = 0 ; (i < tablesize) && (i < 3) ; i++ ) {	    	strbuf[i] = new StringBuffer();       	    	FlightInfo flightInfo = (FlightInfo) flightsInfo.elementAt(i);	    	strbuf[i].append(		              "Carrier:" + flightInfo.getCarrier() +			      " Flight:" + flightInfo.getFlight()      +"\n"+ 			      "Departure:" + flightInfo.getDeparture() +"\n"+			      "Arrival:" + flightInfo.getArrival()     +"\n"+			      "DeptTime : " + flightInfo.getDeptTime()			      );		print("value " + strbuf[i]);	}	if ( null != strbuf[0] )	    text1st.setText(strbuf[0].toString());	if ( null != strbuf[1] )	    text2nd.setText(strbuf[1].toString());	if ( null != strbuf[2] )	    text3rd.setText(strbuf[2].toString());        setSize(520,450);        //pack();	show();   }       /**     * action on Button     *     * @param e event     * @param o object     */    public boolean action(Event e, Object o) {        if (e.target instanceof Button) {            FlightInfo finfo;            try {                if (e.target == button1st) {                    print("pushed 1go");                    if ( null != ( finfo = (FlightInfo) flightsInfo.elementAt(0)));                        sendRecruitOne(text1st, finfo);                    return true;                } else if (e.target == button2nd) {                    print("pushed 2go");                    if ( null != ( finfo = (FlightInfo) flightsInfo.elementAt(1)));                        sendRecruitOne(text2nd, finfo);                    return true;                } else if (e.target == button3rd) {                    print("pushed 3go");                    if ( null != ( finfo = (FlightInfo) flightsInfo.elementAt(2)));                        sendRecruitOne(text3rd, finfo);                    return true;                } else if (e.target == close) {                    dispose();//                  System.exit(0);                }            } catch (ArrayIndexOutOfBoundsException ex) {            }        } else if (e.target instanceof Choice) {            print("select Choice");        }        return super.action(e, o);    }    /**     * handle event     *     * @param e event     */    public boolean handleEvent(Event e) {        if (e.id == Event.WINDOW_DESTROY) {            dispose();//            System.exit(0);            return true;        }        return super.handleEvent(e);    }    private void sendRecruitOne(TextArea ta, FlightInfo flight) {        KQML kqml = new KQML();        String rw = new String(kManager.getInitialID());        kqml.setPerformative("recruit-one");        kqml.setParameter(":sender", localHost);        kqml.setParameter(":receiver", travelBroker);        kqml.setParameter(":reply-with", rw);        kqml.setParameter(":language", "KQML");        kqml.setParameter(":ontology", "travel-broker");        KQML kqml_i = new KQML();        kqml_i.setPerformative("subscribe");        kqml_i.setParameter(":sender", localHost);        kqml_i.setParameter(":reply-with", rw);        kqml_i.setParameter(":language", "KQML");        kqml_i.setParameter(":ontology", "kqml-ontology");        KQML kqml_ii = new KQML();        kqml_ii.setPerformative("ask-all");        kqml_ii.setParameter(":sender", localHost);        kqml_ii.setParameter(":reply-with", rw);        kqml_ii.setParameter(":language", "anACL");        kqml_ii.setParameter(":ontology", "travel");        String content = new String(                          "(:carrier " + flight.getCarrier()  + "\n" +                          " :flight " + flight.getFlight() + " )"                          );        kqml_ii.setContent(content);        kqml_i.setContent(kqml_ii);        kqml.setContent(kqml_i);	Conversation conv = null;	try {	  conv =	    (Conversation) kManager.sendMessage(kqml);	} catch (SendFailedException ex) {	  ex.printStackTrace();	} catch (InvalidFormatException ex) {	  ex.printStackTrace();	} catch (InvalidMessageSequenceException ex) {	  ex.printStackTrace();	} catch (JKQMLException ex) {	  ex.printStackTrace();	}         //FlightInfoSubscriber subscriber = new FlightInfoSubscriber(conv, ta);                FlightInfoSubscriber subscriber = new FlightInfoSubscriber(conv, ta, this);        subscriber.start();    }        /**     * frame utility     */    private void constrain(Container container, Component component,                          int grid_x, int grid_y, int grid_width, int grid_height,			  int fill, int anchor, double weight_x, double weight_y,			  int top, int left, int bottom, int right) {	GridBagConstraints c = new GridBagConstraints();	c.gridx = grid_x; c.gridy = grid_y;	c.gridwidth = grid_width; c.gridheight = grid_height;	c.fill = fill; c.anchor = anchor;	c.weightx = weight_x; c.weighty = weight_y;	if ( top + bottom + left + right > 0 ) 	    c.insets = new Insets(top, left, bottom, right);	((GridBagLayout)container.getLayout()).setConstraints(component,c);	container.add(component);    }    /**     * frame utility     */    private void constrain(Container container, Component component,                          int grid_x, int grid_y, int grid_width,int grid_height){	constrain(container, component, grid_x, grid_y, grid_width, grid_height,	          GridBagConstraints.NONE, 		  GridBagConstraints.NORTHWEST, 0.0, 0.0, 0, 0, 0, 0);    }    /**     * frame utility     */    private void constrain(Container container, Component component,                          int grid_x, int grid_y, int grid_width, int grid_height,			  int top, int left, int bottom, int right) {	    	constrain(container, component, grid_x, grid_y, grid_width, grid_height,	          GridBagConstraints.NONE, 		  GridBagConstraints.NORTHWEST, 0.0, 0.0, top, left, bottom, right);        }    /**     *  debug print     */    private void print(String msg) {    	System.err.println("PersonalSelectWindow" + msg);    }}

⌨️ 快捷键说明

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