| AVR Z-LINKŪ |

# ChatWindow.java

Go to the documentation of this file.

    00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
    00002 package gui;
    00003
    00004 import java.awt.Dimension;
    00005 import java.awt.Font;
    00006 import java.awt.event.ActionEvent;
    00007 import java.awt.event.ActionListener;
    00008 import java.awt.event.KeyEvent;
    00009 import java.net.URL;
    00010
    00011 import javax.swing.AbstractAction;
    00012 import javax.swing.ImageIcon;
    00013 import javax.swing.JButton;
    00014 import javax.swing.JLabel;
    00015 import javax.swing.JPanel;
    00016 import javax.swing.JScrollPane;
    00017 import javax.swing.JTextArea;
    00018 import javax.swing.KeyStroke;
    00019 import javax.swing.event.EventListenerList;
    00020
    00039 public class ChatWindow extends JPanel implements ActionListener{
    00040
    00041         private static final long serialVersionUID = 1L;
    00042
    00043         private JTextArea conversation, message;
    00044         private JButton
    00045                 clear = new JButton( "Clear" ),
    00046                 send = new JButton( "Send" );
    00047         private JLabel imageAVR;
    00048         private JLabel imageAtmel;
    00049         private Font textFont;
    00050
    00051         private EventListenerList ChatWindowEventListeners = new EventListenerList( );
    00052
    00062         public ChatWindow( ){
    00063
    00064                 this.setSize( new Dimension( 600, 400 ) );
    00065                 this.setLayout( null );
    00066
    00067                 message = new JTextArea( "" );
    00068                 message.setEditable( false );
    00069
    00070 //              Add enter to input map. So that it is possible to send messages using the eneter key.
    00071                 message.getInputMap().put( KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0),
    00072                                                                                                                   "enter" );
    00073
    00074 //              Connect the key event with an action.
    00075                 message.getActionMap( ).put( "enter", new AbstractAction( ){
    00076
    00077 //                      Suppress warning
    00078                         private static final long serialVersionUID = 1L;
    00079
    00080                         public void actionPerformed( ActionEvent ae ){
    00081                                 //Send new event to the EventHandler.
    00082                                 fireChatWindowEvent( new ChatWindowEvent( ChatWindowEvent.ENTER, message.getText() ) );
    00083             }
    00084         } );
    00085
    00086                 conversation = new JTextArea( "" );
    00087                 conversation.setEditable( false );
    00088
    00089                 JScrollPane conversationBackground = new JScrollPane( conversation );
    00090                 conversationBackground.setBounds( 151, 10, 399, 260 );
    00091
    00092                 message.setBounds( 151, 272, 290, 64 );
    00093
    00094                 clear.setBounds( 450, 294, 100, 20 );
    00095                 clear.addActionListener( this );
    00096
    00097                 send.setBounds( 450, 272, 100, 20 );
    00098                 send.addActionListener( this );
    00099
    00100                 URL avrLogoURL = ChatWindow.class.getResource( "AVR_logo_black.gif" );
    00101
    00102                 ImageIcon avrLogo = new ImageIcon( avrLogoURL );
    00103                 imageAVR = new JLabel( avrLogo );
    00104                 imageAVR.setBounds( 8, 100, 135, 97 );
    00105
    00106                 URL atmelURL = ChatWindow.class.getResource( "Atmel_logo_black.gif" );
    00107
    00108                 ImageIcon atmelLogo = new ImageIcon( atmelURL );
    00109                 imageAtmel = new JLabel( atmelLogo );
    00110                 imageAtmel.setBounds( 8, 0, 135, 97 );
    00111
    00112                 textFont = new Font( "Monospaced", Font.BOLD, 14 );
    00113                 conversation.setFont( textFont );
    00114                 message.setFont( textFont );
    00115
    00116                 this.add( conversationBackground );
    00117                 this.add( message );
    00118                 this.add( clear );
    00119                 this.add( send );
    00120                 this.add( imageAVR );
    00121                 this.add( imageAtmel );
    00122         }
    00123
    00131         public void appendTextToConversation( String msg ){
    00132
    00133                 conversation.setText( conversation.getText( ) + msg );
    00134         }
    00135
    00141         public void clearConversation( ){
    00142                 conversation.setText( "" );
    00143         }
    00144
    00150         public void clearMessage( ){
    00151                 message.setText("");
    00152         }
    00153
    00160         public String getMessage( ){
    00161                 return message.getText( );
    00162         }
    00163
    00174         public void appendIncommingMessage( String iMsg ){
    00175                 appendTextToConversation( " -> " + iMsg + "\r\n" );
    00176         }
    00177
    00188         public void appendOutgoingMessage( String oMsg ){
    00189                 appendTextToConversation( " <- " + oMsg + "\r\n" );
    00190         }
    00191
    00197         public void enableMessageWindow( ){
    00198                 message.setEditable( true );
    00199         }
    00200
    00206         public void disableMessageWindow( ){
    00207                 message.setEditable( false );
    00208         }
    00209
    00219         public void actionPerformed( ActionEvent ae ){
    00220
    00221                 String event = ae.getActionCommand( ).toString( );
    00222
    00223                 if( event.equals( "Send" ) ){
    00224
    00225                         fireChatWindowEvent( new ChatWindowEvent( ChatWindowEvent.SEND, message.getText( ) ) );
    00226                 }
    00227
    00228                 else{
    00229
    00230                         fireChatWindowEvent( new ChatWindowEvent( ChatWindowEvent.CLEAR, null ) );
    00231                 }
    00232         }
    00233
    00242         public void addChatWindowEventListener( ChatWindowEventListener cweListener ){
    00243                 ChatWindowEventListeners.add( ChatWindowEventListener.class, cweListener );
    00244         }
    00245
    00254         public void removeChatWindowEventListener( ChatWindowEventListener cweListener ){
    00255                 ChatWindowEventListeners.remove( ChatWindowEventListener.class, cweListener );
    00256         }
    00257
    00266         protected void fireChatWindowEvent( ChatWindowEvent event ){
    00267
    00268                 Object[ ] listeners = ChatWindowEventListeners.getListenerList( );
    00269                 int numberOfListeners = listeners.length;
    00270
    00271                 for( int i = 0; i < numberOfListeners; i+=2 ){
    00272
    00273                         if( listeners[ i ] == ChatWindowEventListener.class ){
    00274                                 ( (ChatWindowEventListener)listeners[ i + 1 ] ).ChatWindowEvent( event );
    00275                         }
    00276                 }
    00277         }
    00278 }
    00279

@DOC_TITLE@