KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > reunion > Reunion


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2002 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.reunion;
20
21 import org.lucane.applications.reunion.ui.MainWindow;
22 import org.lucane.applications.reunion.ui.SubjectDialog;
23 import org.lucane.client.*;
24 import org.lucane.client.widgets.*;
25 import org.lucane.common.*;
26 import org.lucane.common.net.ObjectConnection;
27 import org.lucane.common.net.ObjectListener;
28
29 import java.io.IOException JavaDoc;
30 import java.util.*;
31
32
33 public class Reunion
34 extends Plugin
35 implements ObjectListener
36 {
37     private ConnectInfo coordinator;
38     private ConnectInfo[] friends;
39     
40     private String JavaDoc subject = null;
41     private boolean starter;
42     
43     private MainWindow window;
44     
45     private ObjectConnection ocCoordinator;
46     private ArrayList connections;
47     
48     //--
49

50     ReunionUsersProperties reunionUsersProperties = new ReunionUsersProperties();
51     
52     public Reunion()
53     {
54     }
55         
56     public Plugin newInstance(ConnectInfo[] friends)
57     {
58         return new Reunion(friends);
59     }
60     
61     public Reunion(ConnectInfo[] ci)
62     {
63         this.friends = ci;
64     }
65     
66     public void load(ObjectConnection oc, ConnectInfo who, String JavaDoc data)
67     {
68         this.coordinator = who;
69         this.subject = data;
70         ocCoordinator = oc;
71     }
72     
73     public void start()
74     {
75         this.starter = true;
76         this.connections = new ArrayList();
77         
78         // quit if no friends has been selected
79
if(friends.length < 1)
80         {
81             DialogBox.info(tr("noselect"));
82             exit();
83             return;
84         }
85         
86         SubjectDialog dialog = new SubjectDialog(this, friends);
87         dialog.show();
88     }
89     
90     public void follow()
91     {
92         this.starter = false;
93         
94         SubjectDialog dialog = new SubjectDialog(this, coordinator);
95         dialog.show();
96     }
97     
98     //--
99

100     public void setSubject(String JavaDoc subject)
101     {
102         this.subject = subject;
103     }
104     
105     public String JavaDoc getSubject()
106     {
107         return this.subject;
108     }
109     
110     public void inviteUser(ConnectInfo friend)
111     {
112         ObjectConnection connection = Communicator.getInstance().sendMessageTo(friend, getName(), subject);
113         connection.addObjectListener(this);
114         connection.listen();
115         connections.add(connection);
116     }
117     
118     public void joinMeeting()
119     {
120         ReunionMessage msg = ReunionMessage.createJoinInstance(
121                 Client.getInstance().getMyInfos().getName());
122         ocCoordinator.addObjectListener(this);
123         ocCoordinator.listen();
124         try {
125             ocCoordinator.write(msg);
126         } catch (IOException JavaDoc e) {
127             e.printStackTrace();
128         }
129     }
130     
131     public void showMainWindow()
132     {
133         this.window = new MainWindow(this, this.starter);
134         window.show();
135         
136         if(this.starter)
137         {
138             ReunionUserProperties userProperties = reunionUsersProperties.addUser(
139                     Client.getInstance().getMyInfos().getName());
140             window.addUser(userProperties);
141         }
142     }
143     
144     public void closeMeeting()
145     {
146         if(starter)
147         {
148             ReunionMessage msg = ReunionMessage.createEndInstance();
149             window.pushMessage(msg.toString(this));
150             shareMessage(msg);
151             saveReunion();
152         }
153         else
154         {
155             ReunionMessage msg = ReunionMessage.createLeaveInstance(Client.getInstance().getMyInfos().getName());
156             try {
157                 ocCoordinator.write(msg);
158             } catch(Exception JavaDoc e) {}
159         }
160         
161         exit();
162     }
163     
164     public void sendTextMessage(String JavaDoc message)
165     {
166         ReunionMessage msg = ReunionMessage.createTextInstance(
167                     Client.getInstance().getMyInfos().getName(), message);
168         if(starter)
169         {
170             window.pushMessage(msg.toString(this));
171             shareMessage(msg);
172         }
173         else {
174             try {
175                 ocCoordinator.write(msg);
176             } catch (IOException JavaDoc e) {
177                 e.printStackTrace();
178             }
179         }
180     }
181     
182     public void setUserList(ReunionUsersProperties props)
183     {
184         this.reunionUsersProperties = props;
185         window.setUsers(props);
186     }
187     
188     //-- old stuff
189

190     public void end()
191     {
192         window.pushMessage(ReunionMessage.createHTMLInfoMessage(this, tr("endMsg")));
193         window.removeListeners();
194         
195         if(starter)
196         {
197             Iterator connections = this.connections.iterator();
198             while(connections.hasNext())
199             {
200                 ObjectConnection connection = (ObjectConnection)connections.next();
201                 connection.close();
202             }
203         }
204         else
205             ocCoordinator.close();
206     }
207     
208     private void saveReunion()
209     {
210         //TODO or useless ?
211
}
212     
213     //-- network object management
214

215     public void objectRead(Object JavaDoc o)
216     {
217         try {
218             process((ReunionMessage) o);
219             if(starter)
220                 shareMessage((ReunionMessage) o);
221         } catch (Exception JavaDoc e) {
222             e.printStackTrace(System.out);
223         }
224     }
225     
226     private void shareMessage(ReunionMessage msg)
227     {
228         //share messages
229
Iterator connections = this.connections.iterator();
230         while(connections.hasNext())
231         {
232             ObjectConnection connection = (ObjectConnection)connections.next();
233             try {
234                 connection.write(msg);
235             } catch(Exception JavaDoc e) {}
236         }
237     }
238     
239     private void process(ReunionMessage msg)
240     {
241         if(msg == null)
242             return;
243         
244         //normal message
245
if(msg.getType()==ReunionMessage.TYPE_TEXT)
246         {
247             window.pushMessage(msg.toString(this));
248         }
249         
250         //command
251
else
252         {
253             //JOIN
254
if(msg.getType()==ReunionMessage.TYPE_JOIN)
255             {
256                 window.pushMessage(msg.toString(this));
257                 String JavaDoc user = (String JavaDoc)(msg.getData());
258                 
259                 if(starter)
260                 {
261                     reunionUsersProperties.addUser(user);
262                     setUserList(reunionUsersProperties);
263                     shareMessage(ReunionMessage.createUserListInstance(reunionUsersProperties));
264                 }
265             }
266             
267             //LEAVE
268
else if(msg.getType()==ReunionMessage.TYPE_LEAVE)
269             {
270                 window.pushMessage(msg.toString(this));
271                 String JavaDoc user = (String JavaDoc)(msg.getData());
272
273                 ReunionUserProperties userProperties = reunionUsersProperties.getUserProperties(user);
274                 window.removeUser(userProperties);
275             }
276             
277             //END
278
else if(msg.getType()==ReunionMessage.TYPE_END)
279             {
280                 end();
281             }
282             
283             //USERLIST
284
else if(msg.getType() == ReunionMessage.TYPE_USERLIST)
285             {
286                 setUserList((ReunionUsersProperties)msg.getData());
287             }
288             
289             else
290                 window.pushMessage(msg.toString(this));
291         }
292     }
293     
294     public ReunionUsersProperties getUsersProperties() {
295         return reunionUsersProperties;
296     }
297 }
298
299
Popular Tags