001
002 /**
003 * Title: Advanced Network Client Sample<p>
004 * Description: <p>
005 * Copyright: Copyright (C) 2009 Alexey Veremenko<p>
006 * Company: <p>
007 * @author Alexey Veremenko
008 * @version 1.0
009 */
010 package networking.client.applet;
011
012 import networking.client.*;
013 import networking.client.frame.ClientFrame;
014
015 import java.applet.*;
016 import java.awt.*;
017 import java.awt.event.*;
018
019 /**
020 * ClientAppletUI - this class lays out UI controls of the application applet
021 */
022 class ClientAppletUI extends Applet
023 {
024 /**
025 * Client state constant - connected
026 */
027 protected static final int CONNECTED = 0;
028
029 /**
030 * Client state constant - connected as leader
031 */
032 protected static final int LEADER = 1;
033
034 /**
035 * Client state constant - disconnected
036 */
037 protected static final int DISCONNECTED = 2;
038
039 /**
040 * Client state constant - in process
041 */
042 protected static final int INPROCESS = 3;
043
044 /**
045 * Title label
046 */
047 SmartLabel m_lblTitle = new SmartLabel("Client:");
048
049 /**
050 * Status label
051 */
052 private SmartLabel m_lblStatus = new SmartLabel("Status:");
053
054 /**
055 * Host text field
056 */
057 TextFieldEx m_tfHost = new TextFieldEx("", 15);
058
059 /**
060 * Port text field
061 */
062 TextFieldEx m_tfPort = new TextFieldEx("", 5);
063
064 /**
065 * Name text field
066 */
067 TextFieldEx m_tfName = new TextFieldEx("", 15);
068
069 /**
070 * Message text field
071 */
072 TextFieldEx m_tfMessage = new TextFieldEx("", 25);
073
074 /**
075 * Messages text area
076 */
077 TextArea m_taMessages = new TextArea();
078
079 /**
080 * List of clients
081 */
082 ListEx m_clients = new ListEx(10);
083
084 /**
085 * Buttons
086 */
087 Button m_buttonForce = new Button();
088 Button m_buttonSend = new Button();
089 Button m_buttonSendAll = new Button();
090 Button m_buttonConnect = new Button();
091 Button m_buttonDisconnect = new Button();
092
093 /**
094 * Construct new ClientAppletUI object
095 */
096 public ClientAppletUI()
097 {
098 }
099
100 /*
101 * Override Applet
102 */
103 public void init()
104 {
105 setLayout(new BorderLayout());
106
107 Panel p1 = new Panel();
108 Panel p2 = new Panel();
109 Panel p3 = new Panel();
110
111 add(p1, BorderLayout.NORTH);
112 add(p2, BorderLayout.CENTER);
113 add(p3, BorderLayout.SOUTH);
114
115 setupNorth(p1);
116 setupCenter(p2);
117 setupSouth(p3);
118
119 validate();
120 setControlsState(DISCONNECTED);
121 setBackground(Color.lightGray);
122 m_taMessages.setEditable(false);
123 }
124
125 /**
126 * Setup center panel
127 */
128 private void setupCenter(Panel p)
129 {
130 p.setLayout(new GridBagLayout());
131
132 // Setup left part controls
133 p.add(new Label("Connected clients:"), new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0,
134 GridBagConstraints.WEST,
135 GridBagConstraints.NONE,
136 new Insets(10, 10, 10, 10), 0, 0));
137 p.add(m_clients, new GridBagConstraints(0, 1, 1, GridBagConstraints.REMAINDER, 0.5, 1.0,
138 GridBagConstraints.WEST,
139 GridBagConstraints.BOTH,
140 new Insets(0, 10, 10, 10), 0, 0));
141
142 // Setup right part controls
143 int y = 1;
144 p.add(new Label(""), new GridBagConstraints(1, y++, GridBagConstraints.REMAINDER, 1, 0.5, 0.5,
145 GridBagConstraints.CENTER,
146 GridBagConstraints.BOTH,
147 new Insets(0, 10, 10, 10), 0, 0));
148 p.add(m_buttonForce, new GridBagConstraints(1, y++, 1, 1, 0.0, 0.0,
149 GridBagConstraints.WEST,
150 GridBagConstraints.NONE,
151 new Insets(0, 10, 10, 10), 0, 0));
152 p.add(new Label("Send message to selected clients:"),
153 new GridBagConstraints(1, y++, 1, 1, 0.0, 0.0,
154 GridBagConstraints.WEST,
155 GridBagConstraints.NONE,
156 new Insets(10, 10, 10, 10), 0, 0));
157 p.add(m_tfMessage, new GridBagConstraints(1, y++, 1, 1, 0.0, 0.0,
158 GridBagConstraints.WEST,
159 GridBagConstraints.NONE,
160 new Insets(0, 10, 10, 10), 0, 0));
161
162 // Add two send buttons
163 Panel p1 = new Panel(new FlowLayout(FlowLayout.LEFT, 10, 0));
164 p1.add(m_buttonSend);
165 p1.add(m_buttonSendAll);
166 p.add(p1, new GridBagConstraints(1, y++, 1, 1, 0.0, 0.0,
167 GridBagConstraints.WEST,
168 GridBagConstraints.NONE,
169 new Insets(0, 0, 10, 10), 0, 0));
170
171 p.add(new Label(""), new GridBagConstraints(1, y++, GridBagConstraints.REMAINDER, 1, 0.5, 0.5,
172 GridBagConstraints.CENTER,
173 GridBagConstraints.BOTH,
174 new Insets(0, 10, 10, 10), 0, 0));
175
176 // Setup buttons handlers
177 m_buttonForce.setLabel(" Disconnect selected clients ");
178 m_buttonForce.addActionListener(new java.awt.event.ActionListener()
179 {
180 public void actionPerformed(ActionEvent e)
181 {
182 buttonForce_actionPerformed(e);
183 }
184 });
185 m_buttonSend.setLabel(" Send ");
186 m_buttonSend.addActionListener(new java.awt.event.ActionListener()
187 {
188 public void actionPerformed(ActionEvent e)
189 {
190 buttonSend_actionPerformed(e);
191 }
192 });
193 m_buttonSendAll.setLabel(" Send to All ");
194 m_buttonSendAll.addActionListener(new java.awt.event.ActionListener()
195 {
196 public void actionPerformed(ActionEvent e)
197 {
198 buttonSendAll_actionPerformed(e);
199 }
200 });
201 }
202
203 /**
204 * Setup south panel
205 */
206 private void setupSouth(Panel p)
207 {
208 // Clear button
209 Button button = new Button(" Clear ");
210 button.addActionListener(new ActionListener()
211 {
212 public void actionPerformed(ActionEvent e)
213 {
214 m_taMessages.setText("");
215 }
216 });
217
218 p.setLayout(new GridBagLayout());
219 p.add(new Label("Messages:"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
220 GridBagConstraints.NORTHWEST,
221 GridBagConstraints.NONE,
222 new Insets(10, 10, 10, 10), 0, 0));
223 p.add(button, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
224 GridBagConstraints.NORTHEAST,
225 GridBagConstraints.NONE,
226 new Insets(10, 10, 10, 20), 0, 0));
227 p.add(m_taMessages, new GridBagConstraints(0, 1, 2, 1, 1.0, 1.0,
228 GridBagConstraints.CENTER,
229 GridBagConstraints.BOTH,
230 new Insets(0, 10, 10, 10), 0, 0));
231 }
232
233 /**
234 * Setup north panel
235 * @param p panel
236 */
237 private void setupNorth(Panel p)
238 {
239 p.setLayout(new GridBagLayout());
240
241 Panel p1 = new Panel();
242
243 int y = 0;
244 // First line
245 p.add(m_lblTitle, new GridBagConstraints(0, y, 4, 1, 1.0, 0.0,
246 GridBagConstraints.WEST,
247 GridBagConstraints.HORIZONTAL,
248 new Insets(15, 10, 0, 0), 0, 0));
249
250 // Frame button
251 Button button = new Button(" Frame ");
252 p.add(button, new GridBagConstraints(4, y++, GridBagConstraints.REMAINDER, 1, 0.0, 0.0,
253 GridBagConstraints.EAST,
254 GridBagConstraints.NONE,
255 new Insets(15, 0, 0, 20), 0, 0));
256 button.addActionListener(new ActionListener()
257 {
258 public void actionPerformed(ActionEvent e)
259 {
260 // Invoke frame with the same host
261 String[] args = new String[1];
262 args[0] = m_tfHost.getText();
263 ClientFrame.main(args);
264 }
265 });
266
267 p.add(m_lblStatus, new GridBagConstraints(0, y++, GridBagConstraints.REMAINDER, 1, 0.0, 0.0,
268 GridBagConstraints.WEST,
269 GridBagConstraints.NONE,
270 new Insets(10, 10, 0, 0), 0, 0));
271
272 // Second line
273 p.add(new Label("Host:"), new GridBagConstraints(0, y, 1, 1, 0.0, 0.0,
274 GridBagConstraints.EAST,
275 GridBagConstraints.NONE,
276 new Insets(15, 10, 0, 0), 0, 0));
277 p.add(m_tfHost, new GridBagConstraints(1, y, 1, 1, 0.0, 0.0,
278 GridBagConstraints.WEST,
279 GridBagConstraints.NONE,
280 new Insets(10, 0, 0, 0), 0, 0));
281 p.add(new Label("Port:"), new GridBagConstraints(2, y, 1, 1, 0.0, 0.0,
282 GridBagConstraints.EAST,
283 GridBagConstraints.NONE,
284 new Insets(10, 10, 0, 0), 0, 0));
285 p.add(m_tfPort, new GridBagConstraints(3, y, 1, 1, 0.0, 0.0,
286 GridBagConstraints.WEST,
287 GridBagConstraints.NONE,
288 new Insets(10, 0, 0, 0), 0, 0));
289 p.add(new Label(""), new GridBagConstraints(3, y++, 1, 1, 1.0, 0.0,
290 GridBagConstraints.WEST,
291 GridBagConstraints.HORIZONTAL,
292 new Insets(10, 0, 0, 0), 0, 0));
293
294 // 3-rd line
295 p.add(new Label("Name:"), new GridBagConstraints(0, y, 1, 1, 0.0, 0.0,
296 GridBagConstraints.WEST,
297 GridBagConstraints.NONE,
298 new Insets(15, 10, 0, 0), 0, 0));
299 p.add(m_tfName, new GridBagConstraints(1, y, 1, 1, 0.0, 0.0,
300 GridBagConstraints.WEST,
301 GridBagConstraints.NONE,
302 new Insets(10, 0, 0, 0), 0, 0));
303 p.add(p1, new GridBagConstraints(2, y++, GridBagConstraints.REMAINDER, 1, 1.0, 0.0,
304 GridBagConstraints.WEST,
305 GridBagConstraints.HORIZONTAL,
306 new Insets(10, 10, 0, 0), 0, 0));
307
308 setupConnect(p1);
309 }
310
311 /**
312 * Setup connect panel
313 * @param p panel
314 */
315 private void setupConnect(Panel p)
316 {
317 p.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0));
318
319 m_buttonConnect.setLabel(" Connect ");
320 m_buttonConnect.addActionListener(new java.awt.event.ActionListener()
321 {
322 public void actionPerformed(ActionEvent e)
323 {
324 buttonConnect_actionPerformed(e);
325 }
326 });
327 m_buttonDisconnect.setLabel(" Disconnect ");
328 m_buttonDisconnect.addActionListener(new java.awt.event.ActionListener()
329 {
330 public void actionPerformed(ActionEvent e)
331 {
332 buttonDisconnect_actionPerformed(e);
333 }
334 });
335
336 p.add(m_buttonConnect);
337 p.add(m_buttonDisconnect);
338 }
339
340 /**
341 * Set controls state
342 * @param n state to conform with
343 */
344 void setControlsState(int n)
345 {
346 m_tfHost.setEnabled(n == DISCONNECTED);
347 m_tfPort.setEnabled(n == DISCONNECTED);
348 m_tfName.setEnabled(n == DISCONNECTED);
349 m_tfMessage.setEnabled(n == CONNECTED || n == LEADER);
350 m_buttonConnect.setEnabled(n == DISCONNECTED);
351 m_buttonDisconnect.setEnabled(n == CONNECTED || n == LEADER);
352 m_buttonForce.setEnabled(n == LEADER);
353 m_buttonSend.setEnabled(n == CONNECTED || n == LEADER);
354 m_buttonSendAll.setEnabled(n == CONNECTED || n == LEADER);
355
356 switch (n)
357 {
358 case CONNECTED:
359 {
360 setStatus("Connected");
361 break;
362 }
363 case LEADER:
364 {
365 setStatus("Connected as Leader");
366 break;
367 }
368 case DISCONNECTED:
369 {
370 setStatus("Disconnected");
371 break;
372 }
373 case INPROCESS:
374 {
375 setStatus("In-process");
376 break;
377 }
378 }
379 }
380
381 /**
382 * Connect
383 */
384 void buttonConnect_actionPerformed(ActionEvent event)
385 {
386 }
387
388 /**
389 * Disconnect
390 */
391 void buttonDisconnect_actionPerformed(ActionEvent event)
392 {
393 }
394
395 /**
396 * Force disconection
397 */
398 void buttonForce_actionPerformed(ActionEvent event)
399 {
400 }
401
402 /**
403 * Send message
404 */
405 void buttonSend_actionPerformed(ActionEvent event)
406 {
407 }
408
409 /**
410 * Broadcast message
411 */
412 void buttonSendAll_actionPerformed(ActionEvent event)
413 {
414 }
415
416 /**
417 * TextFieldEx extends textField to specify minimum size
418 */
419 class TextFieldEx extends TextField
420 {
421 /**
422 * Construct new TextFieldEx object
423 */
424 TextFieldEx(String s, int n)
425 {
426 super(s, n);
427 }
428
429 /*
430 * Specify minimum size
431 */
432 public Dimension getMinimumSize()
433 {
434 return getPreferredSize();
435 }
436 }
437
438 /**
439 * SmartLabel - extends label to facilitate partial
440 * text changes, i.e. "Status: " part will always present as
441 * a constant part of the label's text
442 */
443 class SmartLabel extends Label
444 {
445 /**
446 * Const part of label's text
447 */
448 private String m_strConstPart;
449
450 /**
451 * Construct new SmartLabel object
452 */
453 SmartLabel(String strConstPart)
454 {
455 super(strConstPart);
456 m_strConstPart = strConstPart;
457 }
458
459 /**
460 * Override Label
461 */
462 public void setText(String s)
463 {
464 super.setText(m_strConstPart + " " + s);
465 }
466
467 /**
468 * Specify preferred size
469 */
470 public Dimension getPreferredSize()
471 {
472 Dimension d = super.getPreferredSize();
473 d.width += 140;
474 return d;
475 }
476
477 /**
478 * Specify minimum size
479 */
480 public Dimension getMinimumSize()
481 {
482 return getPreferredSize();
483 }
484 }
485
486 /**
487 * Set status text
488 * @param s text
489 */
490 void setStatus(String s)
491 {
492 showStatus(s);
493 m_lblStatus.setText(s);
494 }
495 }
|