Irc.java

Go to the documentation of this file.
00001 package irc;
00002 
00003 /***
00004  * Irc class : simple implementation of a chat using JAVANAISE
00005  *
00006  * @author Fabienne BOYER,
00007  * @author Lionel DEBROUX, http://lionel.debroux.free.fr
00008  * @author Savas Ali TOKMEN, http://ali.tokmen.com
00009  */
00010 
00011 import java.awt.*;
00012 import java.awt.event.*;
00013 
00014 import jvn.*;
00015 
00016 
00022 public class Irc {
00023         protected TextArea text;
00024         protected TextField data;
00025         protected JvnObject sentence;
00026 
00030         public static void main(String argv[]) {
00031                 try {
00032                         // initialize JVN
00033                         JvnServerImpl js = JvnServerImpl.jvnGetServer();
00034 
00035                         if (js == null) {
00036                                 System.out.println("IRC problem : cannot create server!");
00037                         }
00038                         else {
00039                                 // look up the IRC object in the JVN server
00040                                 // if not found, create it, and register it in the JVN server
00041                                 JvnObject jo = js.jvnLookupObject("IRC",JvnObject.class);
00042                                 if (jo == null) {
00043                                         jo = js.jvnCreateObject(new Sentence());
00044                                         // after creation, I have a write lock on the object
00045                                         jo.jvnUnLock();
00046                                         js.jvnRegisterObject("IRC", jo);
00047                                 }
00048                                 // create the graphical part of the Chat application
00049                                 new Irc(jo);
00050                         }
00051                 } catch (JvnException je) {
00052                         System.out.println("IRC problem : " + je.getMessage());
00053                 }
00054         }
00055 
00061         public Irc(JvnObject jo) {
00062                 // Create frame
00063                 sentence = jo;
00064                 Frame frame=new Frame("Javanaise Client: IRC");
00065                 frame.addWindowListener(new WindowAdapter() {
00066                         public void windowClosing(WindowEvent e) {
00067                                 e.getWindow().dispose();
00068                                 try {
00069                                         JvnServerImpl.jvnGetServer().jvnTerminate();
00070                                 } catch (JvnException je) {}
00071                                 System.exit(0);
00072                         }
00073                 });
00074                 frame.setLayout(new BorderLayout(1,1));
00075 
00076                 // Create and add texts
00077                 Panel texts = new Panel(new GridLayout(1,1));
00078                 text=new TextArea(10,60);
00079                 text.setEditable(false);
00080                 text.setForeground(Color.red);
00081                 text.setBackground(Color.black);
00082                 texts.add(text);
00083                 data=new TextField(40);
00084                 texts.add(data);
00085 
00086                 // Create and add buttons
00087                 Panel buttons = new Panel(new GridLayout(1,1));
00088                 Button read_and_unlock_button = new Button("Read + unlock");
00089                 read_and_unlock_button.addActionListener(new ButtonListenerJvn1(this, ButtonType.ReadAndUnlock));
00090                 buttons.add(read_and_unlock_button);
00091                 Button read_button = new Button("Read");
00092                 read_button.addActionListener(new ButtonListenerJvn1(this, ButtonType.Read));
00093                 buttons.add(read_button);
00094                 Button unlock_button = new Button("Unlock");
00095                 unlock_button.addActionListener(new ButtonListenerJvn1(this, ButtonType.Unlock));
00096                 buttons.add(unlock_button);
00097                 Button write_button = new Button("Write");
00098                 write_button.addActionListener(new ButtonListenerJvn1(this, ButtonType.Write));
00099                 buttons.add(write_button);
00100                 Button write_and_unlock_button = new Button("Write + unlock");
00101                 write_and_unlock_button.addActionListener(new ButtonListenerJvn1(this, ButtonType.WriteAndUnlock));
00102                 buttons.add(write_and_unlock_button);
00103 
00104                 // Add texts + buttons and display frame
00105                 frame.add(texts, BorderLayout.CENTER);
00106                 frame.add(buttons, BorderLayout.SOUTH);
00107                 frame.setSize(500,200);
00108                 frame.setVisible(true);
00109         }
00110 }
00111 
00112 
00116 class ButtonListenerJvn1 implements ActionListener {
00117         Irc irc;
00118         ButtonType button;
00119 
00126         public ButtonListenerJvn1 (Irc i, ButtonType b) {
00127                 irc = i;
00128                 button = b;
00129         }
00130 
00136         public void actionPerformed (ActionEvent e) {
00137                 (new ButtonThreadJvn1(irc, button)).start();
00138         }
00139 }
00140 
00146 class ButtonThreadJvn1 extends Thread {
00147         private Irc irc;
00148         private ButtonType button;
00149 
00156         ButtonThreadJvn1( Irc i, ButtonType b ) {
00157                 irc = i;
00158                 button = b;
00159         }
00160 
00166         public void run() {
00167                 try {
00168                         if( button == ButtonType.Read || button == ButtonType.ReadAndUnlock ) {
00169                                 // lock the object in read mode
00170                                 irc.sentence.jvnLockRead();
00171 
00172                                 // invoke the method
00173                                 String s = ((Sentence)(irc.sentence.jvnGetObjectState())).read();
00174 
00175                                 // display the read value
00176                                 irc.data.setText(s);
00177                                 irc.text.append(s+"\n");
00178 
00179                                 if( button == ButtonType.ReadAndUnlock ) {
00180                                         // unlock the object
00181                                         irc.sentence.jvnUnLock();
00182                                 }
00183                         } else if( button == ButtonType.Unlock ) {
00184                                 // unlock the object
00185                                 irc.sentence.jvnUnLock();
00186                         } else if( button == ButtonType.Write || button == ButtonType.WriteAndUnlock ) {
00187                                 // get the value to be written from the buffer
00188                                 String s = irc.data.getText();
00189 
00190                                 // lock the object in write mode
00191                                 irc.sentence.jvnLockWrite();
00192 
00193                                 // invoke the method
00194                                 ((Sentence)(irc.sentence.jvnGetObjectState())).write(s);
00195 
00196                                 if( button == ButtonType.WriteAndUnlock ) {
00197                                         // unlock the object
00198                                         irc.sentence.jvnUnLock();
00199                                 }
00200                         }
00201                 } catch (JvnException je) {
00202                         System.out.println("Javanaise error while processing IRC button action: " + je.getMessage());
00203                 } catch( Exception e ) {
00204                         System.out.println( "Error while processing IRC button action: "+e);
00205                 }
00206         }
00207 }

Generated on Wed Jan 2 10:15:54 2008 for Javanaise by  doxygen 1.5.4