00001 package irc;
00002
00003
00004
00005
00006
00007
00008
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
00033 JvnServerImpl js = JvnServerImpl.jvnGetServer();
00034
00035 if (js == null) {
00036 System.out.println("IRC problem : cannot create server!");
00037 }
00038 else {
00039
00040
00041 JvnObject jo = js.jvnLookupObject("IRC",JvnObject.class);
00042 if (jo == null) {
00043 jo = js.jvnCreateObject(new Sentence());
00044
00045 jo.jvnUnLock();
00046 js.jvnRegisterObject("IRC", jo);
00047 }
00048
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
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
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
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
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
00170 irc.sentence.jvnLockRead();
00171
00172
00173 String s = ((Sentence)(irc.sentence.jvnGetObjectState())).read();
00174
00175
00176 irc.data.setText(s);
00177 irc.text.append(s+"\n");
00178
00179 if( button == ButtonType.ReadAndUnlock ) {
00180
00181 irc.sentence.jvnUnLock();
00182 }
00183 } else if( button == ButtonType.Unlock ) {
00184
00185 irc.sentence.jvnUnLock();
00186 } else if( button == ButtonType.Write || button == ButtonType.WriteAndUnlock ) {
00187
00188 String s = irc.data.getText();
00189
00190
00191 irc.sentence.jvnLockWrite();
00192
00193
00194 ((Sentence)(irc.sentence.jvnGetObjectState())).write(s);
00195
00196 if( button == ButtonType.WriteAndUnlock ) {
00197
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 }