import java.awt.Dimension; import java.awt.Frame; import java.awt.Panel; import java.awt.TextArea; import java.awt.TextField; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.util.Hashtable; import java.util.Vector; import lava.net.common.UNL; import lava.net.psyc.PSYCDeliveryException; import lava.net.psyc.PSYCMessageCenter; import lava.net.psyc.UNI; import lava.net.psyc.packages.Echo; import lava.net.psyc.packages.Trace; import lava.net.psyc.packages.Conferencing; import lava.net.psyc.packages.ConferencingListener; import lava.net.psyc.packages.LinkPeer; import lava.net.psyc.packages.LinkPeerListener; /** * **/ public class PSYCClientAWT extends Panel implements WindowListener, // KeyListener, FocusListener, ActionListener, // ConferencingListener, LinkPeerListener { /** * **/ public String prefix = "*** "; /** * **/ public String i_say_prefix = "> "; /** * **/ public String public_open = "<"; /** * **/ public String public_close = "> "; /** * **/ public String pager_open = "["; /** * **/ public String pager_close = "] "; /** * **/ public String private_open = "*"; /** * **/ public String private_close = "* "; /** * **/ //private String place = "psyc://psyc.tu-ilmenau.de/"; private String place = "psyc://xanadu.lava.de/myPlace"; /** * **/ private UNL place_unl = null; /** * **/ private String context = null; /** * **/ private PSYCMessageCenter center = null; /** * **/ private Conferencing conf = null; /** * **/ private LinkPeer link = null; /** * **/ private String base = null; /** * **/ private Frame frame = new Frame(); /** * **/ private TextArea output = null; /** * **/ private TextField input = null; /** * **/ private Vector history = new Vector(); /** * **/ private int historyPosition = -1; /** * **/ public int historyMax = 30; /** * **/ public boolean markHistory = false; /** * **/ public PSYCClientAWT() { this (null); } /** * **/ public PSYCClientAWT(String base) { super(); this.base = base; frame.addWindowListener(this); frame.setTitle("PSYC Chat Window"); frame.add(this); setLayout(new BorderLayout()); add(output = new TextArea(null,24,80,// TextArea.SCROLLBARS_VERTICAL_ONLY),BorderLayout.CENTER); output.setEditable(false); output.addFocusListener(this); add(input = new TextField(),BorderLayout.SOUTH); input.addKeyListener(this); input.addActionListener(this); //frame.setSize(300,200); frame.pack(); frame.show(); center = new PSYCMessageCenter(); center.setContentType(null,"text/plain"); center.addPackage(new Echo()); center.addPackage(new Trace()); center.addPackage(conf = new Conferencing(this)); conf.acceptPlace(place_unl = new UNL(place)); center.addPackage(link = new LinkPeer(this)); try { link.link(place_unl); } catch(PSYCDeliveryException e) { serverUnlinked(place_unl,null); } } /** * **/ public Dimension getPreferredSize() { Dimension d = output.getPreferredSize(); Dimension d1 = input.getPreferredSize(); if(d.width > d.width) d.width = d1.width + 10; d.height += d1.height + 10; return d; } /** * **/ public void focusGained(FocusEvent e) { output.transferFocus(); } /** * **/ public void focusLost(FocusEvent e) { } /** * **/ public void keyTyped(KeyEvent e) { } /** * **/ public void keyPressed(KeyEvent e) { int s; switch(e.getKeyCode()) { case KeyEvent.VK_UP: s = history.size(); if(--historyPosition < 0) historyPosition = s - 1; if(historyPosition < s) { input.setText((String)history.elementAt(historyPosition)); if(markHistory) input.selectAll(); } break; case KeyEvent.VK_DOWN: s = history.size(); if(++historyPosition >= s) historyPosition = 0; if(historyPosition < s) { input.setText((String)history.elementAt(historyPosition)); if(markHistory) input.selectAll(); } break; } } /** * **/ public void keyReleased(KeyEvent e) { } /** * **/ public void actionPerformed(ActionEvent e) { String text = input.getText(); historyPosition = -1; if(history.size() >= historyMax) history.removeElementAt(0); history.addElement(text); output.append(i_say_prefix + text); output.append("\n"); conf.send(context,text); input.setText(null); } /** * **/ public void windowOpened(WindowEvent e) { } /** * **/ public void windowClosing(WindowEvent e) { center.close(); if(base != null) frame.dispose(); else System.exit(0); } /** * **/ public void windowClosed(WindowEvent e) { } /** * **/ public void windowIconified(WindowEvent e) { } /** * **/ public void windowDeiconified(WindowEvent e) { } /** * **/ public void windowActivated(WindowEvent e) { } /** * **/ public void windowDeactivated(WindowEvent e) { } /** * **/ public void contextIntroduced(UNL source, String context) { } /** * **/ public void placeEntered(String context, UNI user) { this.context = context; output.append(prefix + user + " entered place\n"); } /** * **/ public void placeLeft(String context, UNI user) { output.append(prefix + user + " left place\n"); } /** * **/ public void placeDeleted(String context) { windowClosing(new WindowEvent(frame,WindowEvent.WINDOW_CLOSING)); } /** * **/ public void memberChanged(String context, UNI user, UNL multicaster, // Hashtable locations, boolean speak, boolean listen, boolean authority) { } /** * **/ public void placePropertiesChanged(String context, // boolean allowExternals, // boolean speakersAddSpeakers, boolean speakersAddListeners, // boolean listenersAddListeners, // boolean speakersRemoveSpeakers, boolean speakersRemoveListeners) { } /** * **/ public void conversationPublic(String context, UNI user, String message) { output.append(public_open + user + public_close + message + "\n"); } /** * **/ public void conversationPager(UNL user, String message) { output.append(pager_open + user + pager_close + message + "\n"); } /** * **/ public void conversationPrivate(UNL user, String message) { output.append(private_open + user + private_close + message + "\n"); } /** * **/ public void serverLinked(UNL server, String body) { output.append(prefix + server + " connected" + // (body == null || body.length() <= 0 ? "." : ": " + body) + "\n"); } /** * **/ public void serverUnlinked(UNL server, String body) { output.append(prefix + server + " disconnected" + // (body == null || body.length() <= 0 ? "." : ": " + body) + "\n"); windowClosing(new WindowEvent(frame,WindowEvent.WINDOW_CLOSING)); } /** * **/ final public static void main(String args[]) { new PSYCClientAWT(); } }