1
package org.jboss.remoting.samples.chat.client;
3
import java.awt.Component;
4
import java.util.ArrayList;
6
import javax.swing.SwingUtilities;
8
import org.jboss.logging.Logger;
9
import org.jboss.remoting.samples.chat.exceptions.ConnectionException;
10
import org.jboss.remoting.samples.chat.exceptions.CreateConnectionException;
11
import org.jboss.remoting.samples.chat.exceptions.JoinConnectionException;
12
import org.jboss.remoting.samples.chat.exceptions.ListConnectionException;
13
import org.jboss.remoting.samples.chat.exceptions.NameInUseException;
14
import org.jboss.remoting.samples.chat.exceptions.RemoteConnectionException;
15
import org.jboss.remoting.samples.chat.exceptions.ShuttingDownException;
16
import org.jboss.remoting.samples.chat.exceptions.TalkConnectionException;
17
import org.jboss.remoting.samples.chat.server.ChatServer;
18
import org.jboss.remoting.samples.chat.utility.ReadWriteArrayList;
28
public Wrapper(Object o)
33
public void set(Object o)
44
// ****************************************************************
46
public class LocalStrategy
47
implements ConnectionStrategy, CreateConnectionStrategy, InfoConnectionStrategy, JoinConnectionStrategy, ListConnectionStrategy
49
protected static final Logger log = Logger.getLogger(LocalStrategy.class);
51
private RemoteStrategy remoteStrategy;
52
private CloseableFrame parent;
53
private CreateFrame createFrame;
54
private InfoFrame infoFrame;
55
private JoinFrame joinFrame;
56
private ListFrame listFrame;
57
private TalkFrame talkFrame;
58
private Thread readThread;
59
private boolean isChatting;
60
private ShutDownDialog shuttingDownDialog = new ShutDownDialog();
63
public LocalStrategy(CloseableFrame parent, RemoteStrategy remoteStrategy)
66
this.remoteStrategy = remoteStrategy;
70
// ****************************************************************
74
final Wrapper chatRoomDescriptions = new Wrapper();
76
Thread t = new Thread()
82
chatRoomDescriptions.set(remoteStrategy.list());
84
catch (ShuttingDownException e)
86
SwingUtilities.invokeLater(shuttingDownDialogRunnable);
88
catch (RemoteConnectionException e)
93
Runnable r = new Runnable()
97
new ListFrame(LocalStrategy.this, (ArrayList) chatRoomDescriptions.get(), parent).show();
101
SwingUtilities.invokeLater(r);
109
public void create() throws ConnectionException
111
createFrame = new CreateFrame(this, parent);
116
// ****************************************************************
117
// CreateConnectionStrategy
118
public void createChat(final String description, final ChatMember owner) throws CreateConnectionException
120
Thread t = new Thread()
122
TalkFrame talkFrame = new TalkFrame(description, owner.get_name(), parent);
123
ReadWriteArrayList outgoingLines = new ReadWriteArrayList();
124
ChatServer chatServer = null;
130
chatServer = remoteStrategy.createChat(description, owner, talkFrame, outgoingLines);
132
catch (ShuttingDownException sde)
134
SwingUtilities.invokeLater(shuttingDownDialogRunnable);
136
catch (RemoteConnectionException e)
138
log.error("Cannot create chat room: " + description);
141
catch (NameInUseException e)
143
System.out.println("Pick a new name");
146
Runnable r = new Runnable()
150
talkFrame.registerStrategy(new TalkConnectionStrategyImpl(owner, chatServer, outgoingLines));
155
SwingUtilities.invokeLater(r);
163
// ****************************************************************
164
// JoinConnectionStrategy
165
public void join(final ChatInfo chatInfo, final ChatMember newMember) throws JoinConnectionException
167
Thread t = new Thread()
169
TalkFrame talkFrame = new TalkFrame(chatInfo.get_description(), newMember.get_name(), parent);
170
ReadWriteArrayList outgoingLines = new ReadWriteArrayList();
171
Wrapper remoteChatServerWrapper = new Wrapper();
172
ChatServer chatServer = null;
178
chatServer = remoteStrategy.join(chatInfo.get_key(), newMember, talkFrame, outgoingLines);
180
catch (ShuttingDownException sde)
182
SwingUtilities.invokeLater(shuttingDownDialogRunnable);
184
catch (RemoteConnectionException e)
186
log.error("Cannot join chat room: " + chatInfo.get_description());
189
catch (NameInUseException niue)
191
System.out.println("Pick a new name");
194
Runnable r = new Runnable()
198
talkFrame.registerStrategy(new TalkConnectionStrategyImpl(newMember, chatServer, outgoingLines));
199
talkFrame.registerChatKey(chatInfo.get_key());
204
SwingUtilities.invokeLater(r);
212
// ****************************************************************
213
// InfoConnectionStrategy + ListConnectionStrategy
214
public void getId(ChatInfo chatInfo)
216
joinFrame = new JoinFrame(chatInfo, this, parent);
220
public void getInfo(ArrayList chatInfoList, int key) throws ListConnectionException
222
infoFrame = new InfoFrame(this, (ChatInfo) chatInfoList.get(key), parent);
227
// ****************************************************************
228
// TalkConnectionStrategy
229
class TalkConnectionStrategyImpl implements TalkConnectionStrategy
231
private ChatServer chatServer;
232
private ChatMember member;
233
private ReadWriteArrayList outgoingLines;
235
public TalkConnectionStrategyImpl(ChatMember member, ChatServer cs, ReadWriteArrayList outgoingLines)
238
this.member = member;
239
this.outgoingLines = outgoingLines;
242
// getBackChat() is not currently used, since the ChatReceiverThread created by ChatManager_Impl
243
// sends all existing lines in the chat room, starting with line 0.
244
// public void getBackChat(final TalkFrame talkFrame)
246
// Thread t = new Thread() {
247
// public void run() {
249
// String[] backChat = remoteChatServer.getBackChat();
250
// if (backChat == null)
251
// backChat = new String[0];
252
// talkFrame.setBackChat(backChat);
254
// catch (ShuttingDownException sde) {
255
// SwingUtilities.invokeLater(shuttingDownDialogRunnable);
257
// catch (RemoteConnectionException rce) {
258
// System.out.println("TalkConnectionStrategy: unable to get back chat");
259
// System.out.println(rce);
267
public void send(ChatMessage message) throws TalkConnectionException
269
outgoingLines.add(message);
274
Thread t = new Thread()
280
chatServer.leave(member);
282
catch (ShuttingDownException sde)
284
SwingUtilities.invokeLater(shuttingDownDialogRunnable);
286
catch (RemoteConnectionException e)
288
log.error("TalkConnectionStrategy.leave(): unable to leave chat room");
300
// ****************************************************************
301
public void notifyOnClose(Component c)
306
protected boolean isChatting()
311
protected void setIsChatting(boolean b)
316
// ****************************************************************
318
private Runnable shuttingDownDialogRunnable = new Runnable()
322
shuttingDownDialog.show();
b'\\ No newline at end of file'