next up previous contents
Next: B.7 FOO Up: B Source-Code des Voyager-MASA-Gateways Previous: B.5 VoyagerProxyAgentMobileAgent

B.6 VoyagerMasaGatewayApplet

package de.unimuenchen.informatik.mnm.masa.agent.voyagermasagateway;

import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
import com.sun.java.swing.event.*;

import de.unimuenchen.informatik.mnm.masa.agentSystem.*;
import de.unimuenchen.informatik.mnm.masa.agent.*;
import de.unimuenchen.informatik.mnm.masa.tools.*;


public class VoyagerMasaGatewayApplet extends AgentApplet
{
  
    
    /**
     * AgentSystemService
     */
    AgentSystemService _ass;
    
    /**
     * VoyagerMasaGateway
     */
    VoyagerMasaGateway _voyagerMasaGateway;
    
    /**
     * GUI Elements
     */
    private GridBagLayout _gridLayout = new GridBagLayout();
    
    private JTabbedPane _jtp_panels = new JTabbedPane();
    private JTextArea _jta_output = new JTextArea();
    private JScrollPane _jscr_output;  
    
    private JButton _jb_create = new JButton("Create");
    private JButton _jb_create_clear = new JButton("Clear");
    private JTextField _jt_create_classname = new JTextField("FOOVoyagerAgent",20);
    private JTextField _jt_create_package = new JTextField("voyageragent",20);
    private JTextField _jt_create_agentname = new JTextField(20);
    private JComboBox _jc_create_server = new JComboBox();
    
    private JButton _jb_execute = new JButton("Execute");
    private JComboBox _jc_execute_agents= new JComboBox(); 
    private JTextField _jt_execute_method = new JTextField(40);
    private JTextField _jt_execute_arguments = new JTextField(40);
    
    
    private JButton _jb_migrate = new JButton("Migrate");
    private JComboBox _jc_migrate_agents = new JComboBox();
    private JComboBox _jc_migrate_server = new JComboBox();
    private JTextField _jt_migrate_method = new JTextField(40);
    private JTextField _jt_migrate_arguments = new JTextField(40);
    
    private JComboBox _jc_info_agents = new JComboBox();
    private JLabel _jl_info_voyagerserver;
    
    
    private JButton _jb_terminate = new JButton("Terminate");
    private JComboBox _jc_terminate_agents= new JComboBox(); 
    
    
		/**
		 * Initialize the GUI, init and add elements tu applet
		 */
    private void init_gui(){     
				// create constraint for gridbaglayout, not yet used
				GridBagConstraints c = new GridBagConstraints();
				
				///////////////////////
				// create create-panel
				//////////////////////
				JPanel jpanel_create = new JPanel(false);
				jpanel_create.setLayout(_gridLayout);
				
				// set action listener
				_jb_create.addActionListener(new AppletActionListener());
				_jt_create_agentname.addCaretListener(new AppletCaretListener());
        
				// create labels
				JLabel jl_create_titel = new JLabel("Create Voyager Agent");
				JLabel jl_create_agentname = new JLabel("Agentname:");
				JLabel jl_create_classname = new JLabel("Classname:");
				JLabel jl_create_package = new JLabel("Package:");
				JLabel jl_create_server = new JLabel("Server:");
				
				// set constraints
				// labels
				c.gridwidth = GridBagConstraints.REMAINDER;
				c.anchor = GridBagConstraints.WEST;
				c.fill = GridBagConstraints.HORIZONTAL;
				c.insets = new Insets(10,10,1,10);    
				_gridLayout.setConstraints(jl_create_agentname,c);
				_gridLayout.setConstraints(jl_create_classname,c);
				_gridLayout.setConstraints(jl_create_package,c);
				_gridLayout.setConstraints(jl_create_server,c);
				c.insets = new Insets(10,10,10,10);
				c.anchor = GridBagConstraints.CENTER;
				_gridLayout.setConstraints(jl_create_titel,c);
				// elements for user input
				c.insets = new Insets(1,10,10,10);    
				c.anchor = GridBagConstraints.WEST;
				_gridLayout.setConstraints(_jc_create_server,c);
				c.fill = GridBagConstraints.HORIZONTAL;
				_gridLayout.setConstraints(_jt_create_agentname,c);
				_gridLayout.setConstraints(_jt_create_classname,c);
				_gridLayout.setConstraints(_jt_create_package,c);
				
				c.insets = new Insets(10,10,10,10);
				c.anchor = GridBagConstraints.CENTER;
				c.fill = GridBagConstraints.NONE;
				_gridLayout.setConstraints(_jb_create,c);
				
				// set size
				_jc_create_server.setMaximumSize(new Dimension(getContentPane().getSize().width,_jc_create_server.getSize().height));
				
				// add elements to panel
				jpanel_create.add(jl_create_titel);
				
				jpanel_create.add(jl_create_agentname);
				jpanel_create.add(_jt_create_agentname);
				
				jpanel_create.add(jl_create_classname);
				jpanel_create.add(_jt_create_classname);
				
				jpanel_create.add(jl_create_package);
				jpanel_create.add(_jt_create_package);
				
				jpanel_create.add(jl_create_server);
				jpanel_create.add(_jc_create_server);
				
				jpanel_create.add(_jb_create);
				
				// create and add panel to contentpane
				_jtp_panels.addTab("create",jpanel_create);
				

				////////////////////////
				// execute panel
				///////////////////////
				JPanel jpanel_execute = new JPanel(false);
				jpanel_execute.setLayout(_gridLayout);
				
				// set actnio listener
				_jb_execute.addActionListener(new AppletActionListener());
				_jt_execute_method.addCaretListener(new AppletCaretListener());
	
				// create labels
				JLabel jl_execute_titel = new JLabel("Execute Method of Voyager Agent");
				JLabel jl_execute_agentname = new JLabel("Agentname:");
				JLabel jl_execute_method = new JLabel("Method:");
				JLabel jl_execute_arguments = new JLabel("Arguments:");
				JLabel jl_execute_descript = new JLabel("(seperated by comma, e.g. \"java.lang.Short 4,java.lang.String Message\")");
				
				// set constraints
				// labels
				c.gridwidth = GridBagConstraints.REMAINDER;
				c.anchor = GridBagConstraints.WEST;
				c.insets = new Insets(10,10,1,10);    
				_gridLayout.setConstraints(jl_execute_agentname,c);
				_gridLayout.setConstraints(jl_execute_method,c);
				_gridLayout.setConstraints(jl_execute_arguments,c);
				c.insets = new Insets(1,10,1,10);  
				_gridLayout.setConstraints(jl_execute_descript,c);
				c.insets = new Insets(10,10,10,10);
				c.anchor = GridBagConstraints.CENTER;
				_gridLayout.setConstraints(jl_execute_titel,c);
				// elements for user input
				c.insets = new Insets(1,10,10,10);    
				c.anchor = GridBagConstraints.WEST;    
				_gridLayout.setConstraints(_jc_execute_agents,c);
				c.fill = GridBagConstraints.HORIZONTAL;
				_gridLayout.setConstraints(_jt_execute_method,c);
				_gridLayout.setConstraints(_jt_execute_arguments,c);
				c.anchor = GridBagConstraints.CENTER;
				c.insets = new Insets(10,10,10,10);
				c.fill = GridBagConstraints.NONE;
				_gridLayout.setConstraints(_jb_execute,c);
				
				// set size
				_jc_execute_agents.setMaximumSize(new Dimension(jpanel_execute.getSize().width,_jc_execute_agents.getSize().height));  
				
				// add elements to panel
				jpanel_execute.add(jl_execute_titel);
				
				jpanel_execute.add(jl_execute_agentname);
				jpanel_execute.add(_jc_execute_agents);
				
				jpanel_execute.add(jl_execute_method);
				jpanel_execute.add(_jt_execute_method);
				
				jpanel_execute.add(jl_execute_arguments);
				jpanel_execute.add(jl_execute_descript);
				jpanel_execute.add(_jt_execute_arguments);
				
				jpanel_execute.add(_jb_execute);
				_jtp_panels.addTab("execute",jpanel_execute);
				
	
				////////////////////////
				// migrate panel
				///////////////////////
				JPanel jpanel_migrate = new JPanel(false);
				jpanel_migrate.setLayout(_gridLayout);
	
				// init button
				_jb_migrate.addActionListener(new AppletActionListener());

				// create labels
				JLabel jl_migrate_titel = new JLabel("Migrate Voyager Agent to Server and Execute Method");
				JLabel jl_migrate_agentname = new JLabel("Agentname:");
				JLabel jl_migrate_server = new JLabel("Server:");
				JLabel jl_migrate_method = new JLabel("Method:");
				JLabel jl_migrate_arguments = new JLabel("Arguments:");
				JLabel jl_migrate_descript = new JLabel("(seperated by comma, e.g. \"java.lang.Short 4,java.lang.String Message\")");
	
				// set constraints
				// labels
				c.gridwidth = GridBagConstraints.REMAINDER;
				c.anchor = GridBagConstraints.WEST;
				c.insets = new Insets(10,10,1,10);    
				_gridLayout.setConstraints(jl_migrate_agentname,c);
				_gridLayout.setConstraints(jl_migrate_server,c);
				_gridLayout.setConstraints(jl_migrate_method,c);
				_gridLayout.setConstraints(jl_migrate_arguments,c);
				c.insets = new Insets(1,10,1,10);   
				_gridLayout.setConstraints(jl_migrate_descript,c); 
				c.insets = new Insets(10,10,10,10);
				c.anchor = GridBagConstraints.CENTER;
				_gridLayout.setConstraints(jl_migrate_titel,c);
				// elements for user input
				c.insets = new Insets(1,10,10,10);    
				c.anchor = GridBagConstraints.WEST;    
				_gridLayout.setConstraints(_jc_migrate_agents,c);
				_gridLayout.setConstraints(_jc_migrate_server,c);
				c.fill = GridBagConstraints.HORIZONTAL;
				_gridLayout.setConstraints(_jt_migrate_method,c);
				_gridLayout.setConstraints(_jt_migrate_arguments,c);
				c.anchor = GridBagConstraints.CENTER;
				c.insets = new Insets(10,10,10,10);
				c.fill = GridBagConstraints.NONE;
				_gridLayout.setConstraints(_jb_migrate,c);
	
				// set size
				_jc_migrate_agents.setMaximumSize(new Dimension(jpanel_migrate.getSize().width,_jc_migrate_agents.getSize().height));  
				_jc_migrate_server.setMaximumSize(new Dimension(jpanel_migrate.getSize().width,_jc_migrate_server.getSize().height));  
            
				// add elements to panel
				jpanel_migrate.add(jl_migrate_titel);
    
				jpanel_migrate.add(jl_migrate_agentname);
				jpanel_migrate.add(_jc_migrate_agents);
	
				jpanel_migrate.add(jl_migrate_server);
				jpanel_migrate.add(_jc_migrate_server);
	
				jpanel_migrate.add(jl_migrate_method);
				jpanel_migrate.add(_jt_migrate_method);
	
				jpanel_migrate.add(jl_migrate_arguments);
				jpanel_migrate.add(jl_migrate_descript);
				jpanel_migrate.add(_jt_migrate_arguments);
	
				jpanel_migrate.add(_jb_migrate);
				_jtp_panels.addTab("migrate",jpanel_migrate);
	
				////////////////////////
				// terminate panel
				///////////////////////
				JPanel jpanel_terminate = new JPanel(false);
				jpanel_terminate.setLayout(_gridLayout);

				// set action listener
				_jb_terminate.addActionListener(new AppletActionListener());
	
				// create label
				JLabel jl_terminate_titel = new JLabel("Terminate Voyager Agent");
				JLabel jl_terminate_agentname = new JLabel("Agentname:");
	
				// set constraints
				c.anchor = GridBagConstraints.CENTER;
				_gridLayout.setConstraints(jl_terminate_titel,c);
				c.anchor = GridBagConstraints.WEST;
				c.insets = new Insets(10,10,1,10);    
				_gridLayout.setConstraints(jl_terminate_agentname,c);
				c.gridwidth = GridBagConstraints.REMAINDER;
				c.insets = new Insets(1,10,10,10);
				_gridLayout.setConstraints(_jc_terminate_agents,c);
				c.anchor = GridBagConstraints.CENTER;
				c.insets = new Insets(10,10,10,10);
				c.fill = GridBagConstraints.NONE;
				_gridLayout.setConstraints(_jb_terminate,c);
	
				// set size
				_jc_terminate_agents.setMaximumSize(new Dimension(jpanel_terminate.getSize().width,_jc_execute_agents.getSize().height));  
	
        
				// add elements to panel
				jpanel_terminate.add(jl_terminate_titel);
				jpanel_terminate.add(jl_terminate_agentname);
				jpanel_terminate.add(_jc_terminate_agents);
				jpanel_terminate.add(_jb_terminate);
	
				// add panel to tabbed panel
				_jtp_panels.addTab("terminate",jpanel_terminate);
	
				/////////////////////////////
				// info panel
				////////////////////////////
				JPanel jpanel_info = new JPanel(false);
				jpanel_info.setLayout(_gridLayout);

				// set action listener
				_jc_info_agents.addItemListener(new ItemListener(){
								public  void itemStateChanged(ItemEvent e){
										// update info 
										if(_jc_info_agents.getSelectedItem() != null)
												_jl_info_voyagerserver.setText(_voyagerMasaGateway.get_server((String) _jc_info_agents.getSelectedItem()));
								}
						});

				// create label
				_jl_info_voyagerserver = new JLabel();
				JLabel jl_info_titel = new JLabel("Location of Voyager Agent");
				JLabel jl_info_isrun = new JLabel("is running on");

				// set constraints
				// labels
				c.gridwidth = GridBagConstraints.REMAINDER;
				c.anchor = GridBagConstraints.CENTER;
				c.insets = new Insets(10,10,10,10);
				_gridLayout.setConstraints(jl_info_titel,c);
				c.insets = new Insets(10,10,1,10);
				_gridLayout.setConstraints(_jc_info_agents,c);
				c.insets = new Insets(1,10,1,10);
				_gridLayout.setConstraints(jl_info_isrun,c);
				c.insets = new Insets(1,10,10,10);
				_gridLayout.setConstraints(_jl_info_voyagerserver,c);
	
				jpanel_info.add(jl_info_titel);
				jpanel_info.add(_jc_info_agents);
				jpanel_info.add(jl_info_isrun);
				jpanel_info.add(_jl_info_voyagerserver);


				// add panel to tabbed panel
				_jtp_panels.addTab("info",jpanel_info);

				///////////////////////////
				// init content pane
				///////////////////////////
				getContentPane().setLayout(_gridLayout);    
	
				// init text area for output
				_jta_output.setMargin(new Insets(5,5,5,5));
				_jta_output.setEditable(false);
				Color color = new Color(188,210,238);
				_jta_output.setBackground(color);
				_jscr_output = new JScrollPane(_jta_output,
																			 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
																			 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
				_jscr_output.setBorder(BorderFactory.createTitledBorder("Output"));
				_jscr_output.setMinimumSize(new Dimension(getContentPane().getSize().width,100));
	
				// init control panel
				//    _jtp_panels.setBorder(BorderFactory.createTitledBorder("Control Agents"));
    
				// set constraints
				c.gridwidth = GridBagConstraints.REMAINDER;
				c.anchor = GridBagConstraints.CENTER;
				_gridLayout.setConstraints(_jtp_panels,c);
				c.fill = GridBagConstraints.BOTH;
				c.weighty = 2;
				_gridLayout.setConstraints(_jscr_output,c);
	
				// add elements to content pane
				getContentPane().add(_jtp_panels);
				getContentPane().add(_jscr_output);

		}

		/**
		 * Update the gui after values, like list of agents, have chenged
		 */
		private void update_gui(){
				String [] agents = _voyagerMasaGateway.list_all_agents();
				String [] server = _voyagerMasaGateway.list_all_voyagerserver();

				// update comboboxes
				if (_jc_create_server.getItemCount()!=0) _jc_create_server.removeAllItems();
				if (_jc_migrate_agents.getItemCount()!=0) _jc_migrate_agents.removeAllItems();
				if (_jc_migrate_server.getItemCount()!=0) _jc_migrate_server.removeAllItems();
				if (_jc_terminate_agents.getItemCount()!=0) _jc_terminate_agents.removeAllItems();
				if (_jc_execute_agents.getItemCount()!=0) _jc_execute_agents.removeAllItems();
				if (_jc_info_agents.getItemCount()!=0) _jc_info_agents.removeAllItems();

				// list of agents
				for(int i=0;i<agents.length;i++){
						_jc_migrate_agents.addItem(agents[i]);
						_jc_terminate_agents.addItem(agents[i]);
						_jc_execute_agents.addItem(agents[i]);
						_jc_info_agents.addItem(agents[i]);
				}
    
				//list of voyager server
				for(int i=0;i<server.length;i++){
						_jc_create_server.addItem(server[i]);
						_jc_migrate_server.addItem(server[i]);
				}
				set_buttons();
		}
 
		/**
		 * Enables or disables buttons depending on the user input
		 */
		public void set_buttons(){
				// enable or disable buttons
				// enables create button if user has specified agent name and there is a Voyager server for the agent
				_jb_create.setEnabled((_jt_create_agentname.getText().length()!=0)&&(_jc_create_server.getItemCount()!= 0));
				// enables execute button only if ther is a agent and a method specified
				_jb_execute.setEnabled((_jc_execute_agents.getItemCount()!= 0)&&(_jt_execute_method.getText().length()!=0));
				// enbled if there is an agent and a server to migrate to
				_jb_migrate.setEnabled((_jc_migrate_agents.getItemCount()!= 0)&&(_jc_migrate_server.getItemCount()!= 0));
				// only enabled if there is an agent
				_jb_terminate.setEnabled(_jc_terminate_agents.getItemCount()!= 0);
		}


		/**
		 * Initializes applet: gets AgenSystemService and adds GUI elements to applet
		 */
		public void init(){
				// get voyager masa gaetway
				try {
						org.omg.CORBA.Object obj = initCORBA("VoyagerMasaGateway");
						_voyagerMasaGateway = VoyagerMasaGatewayHelper.narrow(obj);
				}catch(Exception e){
						e.printStackTrace();
				}
				init_gui();
				update_gui();
		}

 

		/**
		 * Handles GUI events
		 */
		private class AppletActionListener implements ActionListener{
    
				public void actionPerformed(ActionEvent e){
						// create agent event
						if(e.getActionCommand().equals("Create")){
								try{
										String name = _voyagerMasaGateway.create_agent(_jt_create_agentname.getText(),
																																	 _jt_create_classname.getText(),
																																	 _jt_create_package.getText(),
																																	 (String) _jc_create_server.getSelectedItem());
										_jta_output.append(_jt_create_agentname.getText() + " as " + name + " created.\n");
								}catch(Exception ex){
										// user output
										_jta_output.append("Creation failed: " + ex + "!\n");
								}
						}
						// migrate agent event
						else if(e.getActionCommand().equals("Migrate")){
								try{
										_voyagerMasaGateway.migrate_agent((String) _jc_migrate_agents.getSelectedItem(), 
																											(String) _jc_migrate_server.getSelectedItem(), 
																											_jt_migrate_method.getText(),
																											_jt_migrate_arguments.getText());
										_jta_output.append((String) _jc_migrate_agents.getSelectedItem() + " migrated to " + (String) _jc_migrate_server.getSelectedItem() + ".\n");
								}catch(Exception ex){
										// user output
										_jta_output.append("Migration failed: " + ex + "!\n");
								}
						}
						// terminate agent event
						else if((e.getActionCommand().equals("Terminate"))){
								try{
										_voyagerMasaGateway.terminate_agent((String) _jc_terminate_agents.getSelectedItem());
										_jta_output.append((String) _jc_terminate_agents.getSelectedItem() + " terminated.\n");
								}catch(Exception ex){
										// user output
										_jta_output.append("Termination failed: " + ex + "!\n");
								}
						}
						// execute method
						else if((e.getActionCommand().equals("Execute"))){
								try{
										String result = _voyagerMasaGateway.execute_agent((String) _jc_execute_agents.getSelectedItem(),_jt_execute_method.getText(),_jt_execute_arguments.getText());
										_jta_output.append((String) _jc_execute_agents.getSelectedItem() + "." 
																			 +_jt_execute_method.getText() + "(" 
																			 + _jt_execute_arguments.getText() + ") executed, result: " 
																			 + result + "\n");
								}catch(Exception ex){
										// user output
										_jta_output.append("Execution failed: " + ex + "!\n");
								}
						}

						// Update list of agents and voyager server in comboboxes
						update_gui();
				}
		}

		/**
		 * ActionListener for the text areas to check whether there was an input
		 * and to update buttons
		 */
		private class AppletCaretListener implements CaretListener{
				public void caretUpdate(CaretEvent e) {
						set_buttons();
				}
		}
}



Copyright Munich Network Management Team