~ubuntu-branches/ubuntu/oneiric/libjboss-remoting-java/oneiric

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/samples/chat/client/TalkFrame.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.jboss.remoting.samples.chat.client;
 
2
 
 
3
import java.awt.Color;
 
4
import java.awt.Font;
 
5
import java.awt.GridBagConstraints;
 
6
import java.awt.GridBagLayout;
 
7
import java.awt.Insets;
 
8
import java.awt.SystemColor;
 
9
import java.awt.event.ActionEvent;
 
10
import java.util.ArrayList;
 
11
import java.util.Iterator;
 
12
 
 
13
import javax.swing.JButton;
 
14
import javax.swing.JLabel;
 
15
import javax.swing.JScrollPane;
 
16
import javax.swing.JTextArea;
 
17
import javax.swing.JTextField;
 
18
 
 
19
import org.jboss.remoting.samples.chat.exceptions.TalkConnectionException;
 
20
 
 
21
/**
 
22
 * <p>Title: Chat4</p>
 
23
 * <p>Description: </p>
 
24
 * <p>Copyright: Copyright (c) 2003</p>
 
25
 * <p>Company: </p>
 
26
 * @author not attributable 
 
27
 * @version 1.0
 
28
 */
 
29
 
 
30
interface TalkConnectionStrategy
 
31
{
 
32
  void send(ChatMessage message) throws TalkConnectionException;
 
33
  void leave() throws TalkConnectionException;
 
34
}
 
35
 
 
36
 
 
37
public class TalkFrame extends CloseableFrame {
 
38
  JScrollPane jScrollPane1 = new JScrollPane();
 
39
  JButton closeButton = new JButton();
 
40
  JButton exitButton = new JButton();
 
41
  JTextArea backChatTextArea = new JTextArea();
 
42
  JTextField newMessage = new JTextField();
 
43
  JLabel talkLabel = new JLabel();
 
44
  GridBagLayout gridBagLayout1 = new GridBagLayout();
 
45
 
 
46
  private String description;
 
47
  private String nickname;
 
48
  private String[] backChat;
 
49
  private TalkConnectionStrategy tcs;
 
50
  private Font plainFont;
 
51
  private Font boldFont;
 
52
  private String chatKey;
 
53
 
 
54
  private static ArrayList talkWindows = new ArrayList();
 
55
 
 
56
  public final boolean NEW_CHAT = true;
 
57
  public final boolean OLD_CHAT = false;
 
58
 
 
59
  public TalkFrame(String description, String nickname, Closeable parent)
 
60
  {
 
61
    super(parent);
 
62
    this.description = description;
 
63
    this.nickname = nickname;
 
64
 
 
65
     try {
 
66
      jbInit();
 
67
    }
 
68
    catch (Exception e) {
 
69
      e.printStackTrace();
 
70
    }
 
71
 
 
72
    plainFont = new Font("SanSerif", Font.PLAIN, 12);
 
73
    boldFont = new Font("SanSerif", Font.BOLD, 12);
 
74
 
 
75
    talkWindows.add(this);
 
76
  }
 
77
 
 
78
  private void jbInit() throws Exception {
 
79
    this.getContentPane().setLayout(gridBagLayout1);
 
80
    closeButton.setFont(new java.awt.Font("SansSerif", 1, 12));
 
81
    closeButton.setText("Close");
 
82
    closeButton.addActionListener(new TalkFrame_closeButton_actionAdapter(this));
 
83
    exitButton.setFont(new java.awt.Font("SansSerif", 1, 12));
 
84
    exitButton.setText("Exit");
 
85
    exitButton.addActionListener(new TalkFrame_exitButton_actionAdapter(this));
 
86
    backChatTextArea.setFont(new java.awt.Font("Monospaced", 0, 13));
 
87
    backChatTextArea.setDisabledTextColor(Color.white);
 
88
    backChatTextArea.setEditable(false);
 
89
    backChatTextArea.setText("");
 
90
    newMessage.setText("");
 
91
    newMessage.addActionListener(new TalkFrame_newMessage_actionAdapter(this));
 
92
    newMessage.requestFocus();
 
93
    talkLabel.setFont(new java.awt.Font("SansSerif", 1, 12));
 
94
    talkLabel.setText("Talk:");
 
95
    jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
 
96
    jScrollPane1.getViewport().setBackground(Color.white);
 
97
    jScrollPane1.setAutoscrolls(true);
 
98
    this.getContentPane().setBackground(SystemColor.control);
 
99
    this.getContentPane().add(closeButton,  new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
 
100
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(28, 204, 21, 0), 6, 0));
 
101
    this.getContentPane().add(exitButton,  new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
 
102
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(28, 17, 21, 280), 18, 0));
 
103
    this.getContentPane().add(jScrollPane1,  new GridBagConstraints(1, 0, 2, 1, 1.0, 1.0
 
104
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10, 8, 0, 33), 602, 264));
 
105
    this.getContentPane().add(talkLabel,  new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
 
106
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(34, 15, 0, 0), 7, -1));
 
107
    this.getContentPane().add(newMessage,  new GridBagConstraints(1, 1, 2, 1, 1.0, 0.0
 
108
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(25, 10, 0, 33), 602, 6));
 
109
    jScrollPane1.getViewport().add(backChatTextArea, null);
 
110
 
 
111
    setTitle(description);
 
112
 
 
113
    pack();
 
114
    center();
 
115
  }
 
116
 
 
117
  public void registerStrategy(TalkConnectionStrategy tcs)
 
118
  {
 
119
    this.tcs = tcs;
 
120
  }
 
121
  
 
122
  
 
123
  public void registerChatKey(String chatKey)
 
124
  {
 
125
        System.out.println("TalkFrame: registering chat key: " + chatKey);
 
126
        this.chatKey = chatKey;
 
127
  }
 
128
 
 
129
 
 
130
  public void appendMessage(ChatMessage chatMessage)
 
131
  {
 
132
        String message = chatMessage.get_message();
 
133
    int mark = message.indexOf(':');
 
134
    int boundedMark = java.lang.Math.max(0, mark); // if ':' is missing, mark == -1
 
135
    boundedMark = java.lang.Math.min(8, boundedMark); // restrict names to 8 characters
 
136
    String name = message.substring(0, boundedMark) + ":" + "        ".substring(0, 8 - boundedMark);
 
137
    String content = message.substring(mark + 1) + "\n";
 
138
    backChatTextArea.append(name + content);
 
139
 
 
140
//    backChatTextArea.append(message);
 
141
/*
 
142
//    System.out.println("****appendMessage: scrollbar position: " + jScrollPane1.getViewport().getViewPosition());
 
143
    int value = jScrollPane1.getVerticalScrollBar().getModel().getMaximum()
 
144
        - jScrollPane1.getVerticalScrollBar().getModel().getExtent();
 
145
    value = (int) Math.round(.9 * value);
 
146
//    System.out.println("****appendMessage: setting scrollbar value to " + value);
 
147
    jScrollPane1.getVerticalScrollBar().getModel().setValue(value);
 
148
*/
 
149
  }
 
150
 
 
151
 
 
152
  public void appendMessages(ArrayList messages) {
 
153
    for (int i = 0; i < messages.size(); i++) {
 
154
      appendMessage((ChatMessage) messages.get(i));
 
155
    }
 
156
  }
 
157
 
 
158
 
 
159
  public static void main(String[] args)
 
160
  {
 
161
      String[] backChat = {
 
162
          "roger: take this", "mike: take that"};
 
163
 
 
164
     TalkFrame talkFrame = new TalkFrame(
 
165
          "dummy title",
 
166
          "h_brewski",
 
167
          null);
 
168
 
 
169
      talkFrame.show();
 
170
  }
 
171
 
 
172
  void close()
 
173
  {
 
174
    if (tcs == null)
 
175
      throw new Error("TalkFrame '" + description + "' close(): no TalkStrategy registered");
 
176
 
 
177
    try {
 
178
      tcs.leave();
 
179
    }
 
180
    catch (TalkConnectionException rce) {
 
181
      System.out.println("TalkFrame: unable to close");
 
182
    }
 
183
 
 
184
    hide();
 
185
    System.out.println("member '" + nickname + "' is leaving chat room '" + description + "'");
 
186
    talkWindows.remove(this);
 
187
    notifyOnClose(this);
 
188
  }
 
189
 
 
190
  void closeButton_actionPerformed(ActionEvent e)
 
191
  {
 
192
    close();
 
193
  }
 
194
 
 
195
  public static void exit()
 
196
  {
 
197
    Iterator it =  new ArrayList(talkWindows).iterator(); // close() modifies talkWindows
 
198
    while (it.hasNext())
 
199
    {
 
200
      ((TalkFrame)it.next()).close();
 
201
    }
 
202
    System.exit(0);
 
203
  }
 
204
 
 
205
  void exitButton_actionPerformed(ActionEvent e)
 
206
  {
 
207
    exit();
 
208
  }
 
209
 
 
210
  void newMessage_actionPerformed(ActionEvent e)
 
211
  {
 
212
    if (tcs == null)
 
213
      throw new Error("TalkFrame exit button: no TalkStrategy registered");
 
214
 
 
215
    try {
 
216
      tcs.send(new ChatMessage(chatKey, nickname + ": " + newMessage.getText()));
 
217
    }
 
218
    catch (TalkConnectionException rce) {
 
219
      System.out.println("TalkFrame: unable to send new chat line");
 
220
    }
 
221
 
 
222
    newMessage.setText("");
 
223
  }
 
224
 
 
225
 
 
226
}
 
227
 
 
228
 
 
229
class TalkFrame_closeButton_actionAdapter implements java.awt.event.ActionListener {
 
230
  TalkFrame adaptee;
 
231
 
 
232
  TalkFrame_closeButton_actionAdapter(TalkFrame adaptee) {
 
233
    this.adaptee = adaptee;
 
234
  }
 
235
  public void actionPerformed(ActionEvent e) {
 
236
    adaptee.closeButton_actionPerformed(e);
 
237
  }
 
238
}
 
239
 
 
240
class TalkFrame_exitButton_actionAdapter implements java.awt.event.ActionListener {
 
241
  TalkFrame adaptee;
 
242
 
 
243
  TalkFrame_exitButton_actionAdapter(TalkFrame adaptee) {
 
244
    this.adaptee = adaptee;
 
245
  }
 
246
  public void actionPerformed(ActionEvent e) {
 
247
    adaptee.exitButton_actionPerformed(e);
 
248
  }
 
249
}
 
250
 
 
251
class TalkFrame_newMessage_actionAdapter implements java.awt.event.ActionListener {
 
252
  TalkFrame adaptee;
 
253
 
 
254
  TalkFrame_newMessage_actionAdapter(TalkFrame adaptee) {
 
255
    this.adaptee = adaptee;
 
256
  }
 
257
  public void actionPerformed(ActionEvent e) {
 
258
    adaptee.newMessage_actionPerformed(e);
 
259
  }
 
260
}
 
261
 
 
262
class DummyTalkConnectionStrategy implements TalkConnectionStrategy
 
263
{
 
264
    public DummyTalkConnectionStrategy() throws TalkConnectionException
 
265
    {
 
266
        System.out.println("DummyTalkConnectionStrategy()");
 
267
    }
 
268
 
 
269
    public void getBackChat(TalkFrame talkFrame) throws TalkConnectionException
 
270
    {
 
271
      System.out.println("DummyTalkConnectionStrategy.getBackChat()");
 
272
    }
 
273
 
 
274
    public void send(ChatMessage message)
 
275
    {
 
276
      System.out.println("DummyTalkConnectionStrategy.send()");
 
277
    }
 
278
 
 
279
    public void leave() throws TalkConnectionException
 
280
    {
 
281
        System.out.println("DummyTalkConnectionStrategy.leave()");
 
282
    }
 
283
 
 
284
 
 
285
}
 
286
 
 
287