[Tinyos-contrib-commits] CVS: tinyos-1.x/contrib/wustl/tools/java/edu/wustl/mobilab/agilla/plugins GroupCommChatPlugin.java, NONE, 1.1

Chien-Liang Fok chien-liang at users.sourceforge.net
Wed Sep 20 07:16:47 PDT 2006


Update of /cvsroot/tinyos/tinyos-1.x/contrib/wustl/tools/java/edu/wustl/mobilab/agilla/plugins
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv21920/plugins

Added Files:
	GroupCommChatPlugin.java 
Log Message:
Got groupcomm chat to work.


--- NEW FILE: GroupCommChatPlugin.java ---
package edu.wustl.mobilab.agilla.plugins;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import edu.wustl.mobilab.agilla.AgillaConstants;
import edu.wustl.mobilab.agilla.AgentInjector;
import edu.wustl.mobilab.agilla.AgillaProperties;
import edu.wustl.mobilab.agilla.Reaction;
import edu.wustl.mobilab.agilla.ReactionListener;
import edu.wustl.mobilab.agilla.Tuple;
import edu.wustl.mobilab.agilla.variables.AgillaAgentID;
import edu.wustl.mobilab.agilla.variables.AgillaString;
import edu.wustl.mobilab.agilla.variables.AgillaType;

import java.awt.event.*;
import java.util.*;

/**
 * A GUI displaying the messages exchanged with a particular agent.
 *
 * @author Chien-Liang Fok
 * @version 5/13/2003
 */
public class GroupCommChatPlugin extends Plugin
	implements ActionListener, java.io.Serializable, AgillaConstants, ReactionListener {
	
	public static final String FONT_NAMES[] = {
		"Arial", "Helvetica", "sans-serif",
			"Times New Roman", "Times", "serif",
			"Courier New", "Courier", "mono",
			"Georgia", "Verdana", "Geneva"
	};
	
	private JFrame frame;
	
	private JEditorPane inText, outText;
	private JScrollPane inTextScroller, outTextScroller;
	
	private JButton send;
	private String	font;
	private String	fontSize;
	private String	inTextString = "";	
	
	private AgentInjector injector;
	private String name = "unk";
	
	/**
	 * Creates GroupCommChatPlugin with the default user name of
	 * "unk".
	 * 
	 * @param injector The AgentInjector.
	 */
	public GroupCommChatPlugin(AgentInjector injector) {
		this(injector, new String[]{"-name", "unk"});
	}
	
	/**
	 * Creates an Instant Messaging GUI.
	 *
	 * @param user the ChatUser running this instant messaging window.
	 * @param aID the destination agent's agent ID
	 */
	public GroupCommChatPlugin(AgentInjector injector, String[] args){
		
		this.injector = injector;
		try {
			for (int i = 0; i < args.length; i++) {
				if (args[i].equals("-name")) {
					this.name = args[++i];
					if (name.length() > 3)
						throw new Exception("Invalid name, length must be 3.");
				}
				else throw new Exception("Unknown parameter: " + args[i]);
			}
		} catch(Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
		
		log("Registering reaction sensitive to chat messages...");
		Tuple chatMsgTemplate = new Tuple();
		chatMsgTemplate.addField(new AgillaString("lbm"));
		chatMsgTemplate.addField(new AgillaType(AGILLA_TYPE_STRING));
		chatMsgTemplate.addField(new AgillaType(AGILLA_TYPE_STRING));
		
		Reaction chatMsgRxn = new Reaction(new AgillaAgentID(), 0, chatMsgTemplate);
		injector.getTS().registerReaction(chatMsgRxn, this);
		
		frame = new JFrame();
		frame.setTitle("Chat - " + name);
		
		initGUI();
	}
	
	public void reactionFired(Tuple t) {				
		AgillaString name = (AgillaString)t.getField(1);
		AgillaString msg = (AgillaString)t.getField(2);
		log("Reacted to a message!\n\tName: " + name + "\n\tMessage: " + msg);
		if (name.toChars().equals(this.name))
			addMyText(msg.toChars());
		else
			addRecievedText(name.toChars(), msg.toChars());
	}
	
	/*public void agentArrived(){
		inTextString += "<i><font color=\"green\">[" +
			aID.getName() + " has re-engaged]</font></i><br>";
		setInText();
		otherPersonHere = true;
	}
	
	public void agentLeft(){
		inTextString += "<i><font color=\"green\">[" +
			aID.getName() + " has disengaged]</font></i><br>";
		setInText();
		otherPersonHere = false;
	}*/
	
	/**
	 * Returns true if the provided object is of type InstantMessagingGUI
	 * and contains the same AgentID as this one.
	 */
	public boolean equals(Object o){
		if (o instanceof GroupCommChatPlugin)
			return ((GroupCommChatPlugin)o).getName().equals(name);
		return false;
	}
	
	public String getName() {
		return name;
	}
	
	public void setFont(String font){
		this.font = font;
		setInText();
	}
	
	public void setFontSize(String size){
		this.fontSize = size;
		setInText();
	}
	
	public void clearMessages(){
		int answer = JOptionPane.showConfirmDialog(
			null,
			"Are you sure you want to clear the icomming messages?",
			"Confirmation",
			JOptionPane.YES_NO_OPTION,
			JOptionPane.QUESTION_MESSAGE);
		if (answer == JOptionPane.YES_OPTION){
			inTextString = "";
			inText.setText(inTextString);
		}
	}
	
	public void refreshGUI(){
		SwingUtilities.updateComponentTreeUI(frame);
		frame.pack();
	}
	
	public void actionPerformed(ActionEvent ae){
		if(ae.getActionCommand().equals("Send Message")){
			if (outText.getText() != null && !outText.getText().equals("\n") && !outText.getText().equals("")) {
				String outMessage = outText.getText();
				if (outMessage.length() > 3) {
					 JOptionPane.showMessageDialog(frame, "Message must be 3 characters", "Error", JOptionPane.ERROR_MESSAGE);
					 return;
				}
				Tuple chatMsg = new Tuple();
				chatMsg.addField(new AgillaString("snd"));
				chatMsg.addField(new AgillaString(name));
				chatMsg.addField(new AgillaString(outMessage));
				
				log("Sending a chat message to 0: " + chatMsg);				
				injector.getTS().rout(chatMsg, 0);
				
				outText.setText("");

				/*if (otherPersonHere){
					String outMessage = outText.getText();
					addMyText(outMessage);
					user.sendIM(aID, outMessage);
				}
				else{
					addMyText(outText.getText());
					inTextString += "<i><font color=\"green\">[" + aID.getName()
						+ " has disengaged and will not recieve the previous message]"
						+ "</font></i><br>";
					setInText();
				}*/
			}
			else
				outText.setText("");
			return;
		}
		
		if(ae.getActionCommand().equals("Close")){
			frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
			frame.setVisible(false);
			frame.dispose();
			return;
		}
		
		/*if (ae.getActionCommand().equals("Send AgentID")){
			addMyText(user.sendIMAgentID(aID).toString());
			return;
		}*/
		
		if (ae.getActionCommand().equals("Clear Messages")){
			clearMessages();
			return;
		}
	}
	
	private void initGUI(){
		frame.getContentPane().setLayout(new BorderLayout());
		
		inText = new JEditorPane();
		inText.setContentType("text/html");
		inText.setEditable(false);
		inTextScroller = new JScrollPane(inText);
		inTextScroller.setPreferredSize(new Dimension(320, 200));
		
		JPanel incommingMessages = new JPanel(new BorderLayout());
		incommingMessages.add("Center",inTextScroller);
		incommingMessages.add("North",new JLabel("Incomming Messages:"));
		
		outText = new JEditorPane();
		outText.setEditable(true);
		outTextScroller = new JScrollPane(outText);
		outTextScroller.setPreferredSize(new Dimension(320, 110));
		
		JPanel outgoingMessages = new JPanel(new BorderLayout());
		outgoingMessages.add("Center",outTextScroller);
		outgoingMessages.add("North",new JLabel("Outgoing Messages:"));
		
		send = new JButton("Send");
		send.setActionCommand("Send Message");
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BorderLayout());
		buttonPanel.add("East",send);
		
		// add action listener to the "send" button
		send.addActionListener(this);
		
		// add Text listener to the input text field,
		// to send text when the enter key is hit
		outText.addKeyListener(new KeyListener(){
					public void keyPressed(KeyEvent e) {}
					
					public void keyReleased(KeyEvent e) {}
					
					public void keyTyped(KeyEvent e){
						if (e.getKeyChar() == KeyEvent.VK_ENTER){
							actionPerformed(new ActionEvent(this, 0, "Send Message"));
						}
					}
				});
		
		JSplitPane ioSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
												incommingMessages,outgoingMessages);
		ioSplitPane.setResizeWeight(0.75);
		
		frame.getContentPane().add("Center",ioSplitPane);
		frame.getContentPane().add("South",buttonPanel);
		createDropMenu();
		
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		
		frame.pack();
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = frame.getSize();
		if (frameSize.height > screenSize.height)
			frameSize.height = screenSize.height;
		if (frameSize.width > screenSize.width)
			frameSize.width = screenSize.width;
		
		frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
		
		//frame.show();
		frame.setVisible(true);
	}
	
	private void createDropMenu(){
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		// File menu
		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic(KeyEvent.VK_F);
		menuBar.add(fileMenu);
		
		JMenuItem closeMI = new JMenuItem("Exit", KeyEvent.VK_X);
		closeMI.setAccelerator(KeyStroke.getKeyStroke(
								   KeyEvent.VK_X,ActionEvent.CTRL_MASK));
		closeMI.addActionListener(this);
		closeMI.setActionCommand("Close");
		fileMenu.add(closeMI);
		
		// Options menu
		JMenu optionMenu = new JMenu("Options");
		optionMenu.setMnemonic(KeyEvent.VK_O);
		
		// add the font submenu
		JMenu fontMenu = new JMenu("Font");
		fontMenu.setMnemonic(KeyEvent.VK_F);
		
		ButtonGroup fontRadioGroup = new ButtonGroup();
		
		for (int i = 0; i < FONT_NAMES.length; i++){
			final String fontName = FONT_NAMES[i];
			
			JMenuItem fontMenuItem;
			if (fontName.equals("Arial"))
				fontMenuItem = new JRadioButtonMenuItem(fontName, true);
			else
				fontMenuItem = new JRadioButtonMenuItem(fontName, false);
			
			fontMenuItem.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent ae){
							setFont(fontName);
						}
					});
			
			fontRadioGroup.add(fontMenuItem);
			fontMenu.add(fontMenuItem);
		}
		
		optionMenu.add(fontMenu);
		
		// add the font size submenu
		JMenu fontSizeMenu = new JMenu("Font Size");
		fontSizeMenu.setMnemonic(KeyEvent.VK_S);
		
		ButtonGroup fontSizeRadioGroup = new ButtonGroup();
		for (int i = -3; i <= 3; i++){
			final String value = String.valueOf(i);
			
			JMenuItem fontSizeMenuItem;
			if (i == 0)
				fontSizeMenuItem = new JRadioButtonMenuItem(value, true);
			else
				if (i > 0)
					fontSizeMenuItem = new JRadioButtonMenuItem("+" + value, false);
				else
					fontSizeMenuItem = new JRadioButtonMenuItem(value, false);
			
			fontSizeMenuItem.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent ae){
							String fontSize;
							if (Integer.valueOf(value).intValue() >= 0)
								fontSize = "+" + value;
							else
								fontSize = value;
							setFontSize(fontSize);
						}
					});
			
			fontSizeRadioGroup.add(fontSizeMenuItem);
			fontSizeMenu.add(fontSizeMenuItem);
		}
		
		optionMenu.add(fontSizeMenu);
		
		optionMenu.addSeparator();
		
		JMenuItem sendAgentIDMI = new JMenuItem("Send AgentID", KeyEvent.VK_A);
		sendAgentIDMI.setAccelerator(KeyStroke.getKeyStroke(
										 KeyEvent.VK_S,
										 ActionEvent.ALT_MASK));
		sendAgentIDMI.addActionListener(this);
		sendAgentIDMI.setActionCommand("Send AgentID");
		optionMenu.add(sendAgentIDMI);
		
		JMenuItem clearMessagesMI = new JMenuItem("Clear Incomming Messages",
												  KeyEvent.VK_C);
		clearMessagesMI.addActionListener(this);
		clearMessagesMI.setActionCommand("Clear Messages");
		optionMenu.add(clearMessagesMI);
		
		menuBar.add(optionMenu);
		
		// Help menu
		/*
		 JMenu helpMenu = new JMenu("Help");
		 helpMenu.setMnemonic(KeyEvent.VK_H);
		 
		 JMenuItem helpMenuItem = new JMenuItem("About Instant Messaging", KeyEvent.VK_A);
		 helpMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.ALT_MASK));
		 helpMenuItem.addActionListener(this);
		 helpMenuItem.setActionCommand("Display About");
		 helpMenu.add(helpMenuItem);
		 
		 
		 menuBar.add(helpMenu);
		 */
	}
	
	private void setInText(){
		inText.setText("<font face=\"" + font + "\" size=\"" + fontSize + "\">" + inTextString + "</font>");
		JScrollBar scrollBar = inTextScroller.getVerticalScrollBar();
		scrollBar.setValue(scrollBar.getMaximum());
	}
	
	public void addRecievedText(String userName, String text) {		
		inTextString += "<b><font color=\"red\">" + userName + ":</font></b> " + text + "<br>";
		setInText();
	}
	
	public void addMyText(String text) {
		inTextString += "<b><font color=\"blue\">" + name + ":</font></b> "+ text + "<br>";
		setInText();
	}
	
	private void log(String str){
		System.out.println("GroupCommChatPlugin: " + str);
	}
	
	/**
	 * Implements the Plugin interface.
	 */
	public void reset() {
		
	}
	
	public String toString(){
		return "An chat GUI for: " + name;
	}
}



More information about the Tinyos-contrib-commits mailing list