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.frame;
011 
012 import networking.*;
013 import networking.client.*;
014 
015 import java.awt.*;
016 import java.awt.event.*;
017 
018 /**
019  * ClientFrameUI - this class lays out UI controls of the client frame
020  */
021 class ClientFrameUI extends Frame
022 {
023     /**
024      * Facility variable
025      */
026     protected static int s_racer = 1;
027 
028     /**
029      * Client state constant - connected
030      */
031     protected static final int CONNECTED = s_racer++;
032 
033     /**
034      * Client state constant - connected as leader
035      */
036     protected static final int LEADER = s_racer++;
037 
038     /**
039      * Client state constant - disconnected
040      */
041     protected static final int DISCONNECTED = s_racer++;
042 
043     /**
044      * Client state constant - in process
045      */
046     protected static final int INPROCESS = s_racer++;
047 
048     /**
049      * Host text field
050      */
051     TextFieldEx m_tfHost = new TextFieldEx(""15);
052 
053     /**
054      * Port text field
055      */
056     TextFieldEx m_tfPort = new TextFieldEx(""5);
057 
058     /**
059      * Name text field
060      */
061     TextFieldEx m_tfName = new TextFieldEx(""15);
062 
063     /**
064      * Message text field
065      */
066     TextFieldEx m_tfMessage = new TextFieldEx(""25);
067 
068     /**
069      * Messages text area
070      */
071     TextArea m_taMessages = new TextArea();
072 
073     /**
074      * List of clients
075      */
076     ListEx m_clients = new ListEx(10);
077 
078     /**
079      * Buttons
080      */
081     Button m_buttonConnect = new Button();
082     Button m_buttonDisconnect = new Button();
083     Button m_buttonAbout = new Button();
084     Button m_buttonForce = new Button();
085     Button m_buttonSend = new Button();
086     Button m_buttonSendAll = new Button();
087     Button m_buttonSendFile = new Button();
088 
089     /**
090      * Construct new ClientFrameUI object
091      */
092     public ClientFrameUI()
093     {
094         m_taMessages.setEditable(false);
095 
096         // Exit listener
097         addWindowListener(new WindowAdapter()
098         {
099             public void windowClosing(WindowEvent e)
100             {
101                 dispose();
102                 System.exit(0);
103             }
104         });
105     }
106 
107     /**
108      * Initialize frame
109      */
110     void init()
111     {
112         setLayout(new BorderLayout());
113 
114         Panel p1 = new Panel();
115         Panel p2 = new Panel();
116         Panel p3 = new Panel();
117 
118         add(p1, BorderLayout.NORTH);
119         add(p2, BorderLayout.CENTER);
120         add(p3, BorderLayout.SOUTH);
121 
122         setupNorth(p1);
123         setupCenter(p2);
124         setupSouth(p3);
125 
126         setControlsState(DISCONNECTED);
127 
128         // Frame setup
129         setBackground(Color.lightGray);
130         setTitle("Client");
131         pack();
132         Util.setCentered(this);
133     }
134 
135     /**
136      * Setup center panel
137      */
138     private void setupCenter(Panel p)
139     {
140         p.setLayout(new GridBagLayout());
141 
142         // Setup left part controls
143         p.add(new Label("Connected clients:")new GridBagConstraints(00210.00.0,
144                    GridBagConstraints.WEST,
145                    GridBagConstraints.NONE,
146                    new Insets(10101010)00));
147         p.add(m_clients, new GridBagConstraints(011, GridBagConstraints.REMAINDER, 0.51.0,
148                    GridBagConstraints.WEST,
149                    GridBagConstraints.BOTH,
150                    new Insets(0101010)00));
151 
152         // Setup right part controls
153         int y = 1;
154         p.add(new Label("")new GridBagConstraints(1, y++, GridBagConstraints.REMAINDER, 10.50.5,
155                    GridBagConstraints.CENTER,
156                    GridBagConstraints.BOTH,
157                    new Insets(0101010)00));
158         p.add(m_buttonForce, new GridBagConstraints(1, y++, 110.00.0,
159                    GridBagConstraints.WEST,
160                    GridBagConstraints.NONE,
161                    new Insets(0101010)00));
162         p.add(new Label("Send message to selected clients:"),
163                   new GridBagConstraints(1, y++, 110.00.0,
164                    GridBagConstraints.WEST,
165                    GridBagConstraints.NONE,
166                    new Insets(10101010)00));
167         p.add(m_tfMessage, new GridBagConstraints(1, y++, 110.00.0,
168                    GridBagConstraints.WEST,
169                    GridBagConstraints.NONE,
170                    new Insets(0101010)00));
171 
172         // Add two send buttons
173         Panel p1 = new Panel(new FlowLayout(FlowLayout.LEFT, 100));
174         p1.add(m_buttonSend);
175         p1.add(m_buttonSendAll);
176         p.add(p1, new GridBagConstraints(1, y++, 110.00.0,
177                    GridBagConstraints.WEST,
178                    GridBagConstraints.NONE,
179                    new Insets(001010)00));
180 
181         p.add(m_buttonSendFile, new GridBagConstraints(1, y++, 110.00.0,
182                    GridBagConstraints.WEST,
183                    GridBagConstraints.NONE,
184                    new Insets(10101010)00));
185         p.add(new Label("")new GridBagConstraints(1, y++, GridBagConstraints.REMAINDER, 10.50.5,
186                    GridBagConstraints.CENTER,
187                    GridBagConstraints.BOTH,
188                    new Insets(0101010)00));
189 
190         // Setup buttons handlers
191         m_buttonForce.setLabel(" Disconnect selected clients ");
192         m_buttonForce.addActionListener(new java.awt.event.ActionListener()
193         {
194             public void actionPerformed(ActionEvent e)
195             {
196                 buttonForce_actionPerformed(e);
197             }
198         });
199         m_buttonSend.setLabel(" Send ");
200         m_buttonSend.addActionListener(new java.awt.event.ActionListener()
201         {
202             public void actionPerformed(ActionEvent e)
203             {
204                 buttonSend_actionPerformed(e);
205             }
206         });
207         m_buttonSendAll.setLabel(" Send to All ");
208         m_buttonSendAll.addActionListener(new java.awt.event.ActionListener()
209         {
210             public void actionPerformed(ActionEvent e)
211             {
212                 buttonSendAll_actionPerformed(e);
213             }
214         });
215         m_buttonSendFile.setLabel(" Send File ... ");
216         m_buttonSendFile.addActionListener(new java.awt.event.ActionListener()
217         {
218             public void actionPerformed(ActionEvent e)
219             {
220                 buttonSendFile_actionPerformed(e);
221             }
222         });
223     }
224 
225     /**
226      * Setup south panel
227      */
228     private void setupSouth(Panel p)
229     {
230         // Clear button
231         Button button = new Button(" Clear ");
232         button.addActionListener(new ActionListener()
233         {
234             public void actionPerformed(ActionEvent e)
235             {
236                 m_taMessages.setText("");
237             }
238         });
239 
240         p.setLayout(new GridBagLayout());
241         p.add(new Label("Messages:")new GridBagConstraints(00110.00.0,
242                    GridBagConstraints.NORTHWEST,
243                    GridBagConstraints.NONE,
244                    new Insets(10101010)00));
245         p.add(button, new GridBagConstraints(10110.00.0,
246                    GridBagConstraints.NORTHEAST,
247                    GridBagConstraints.NONE,
248                    new Insets(10101020)00));
249         p.add(m_taMessages, new GridBagConstraints(01211.01.0,
250                    GridBagConstraints.CENTER,
251                    GridBagConstraints.BOTH,
252                    new Insets(0101010)00));
253     }
254 
255     /**
256      * Setup north panel
257      @param p panel
258      */
259     private void setupNorth(Panel p)
260     {
261         p.setLayout(new GridBagLayout());
262 
263         Panel p1 = new Panel();
264 
265         // First line
266         p.add(new Label("Host:")new GridBagConstraints(00110.00.0,
267                    GridBagConstraints.EAST,
268                    GridBagConstraints.NONE,
269                    new Insets(151000)00));
270         p.add(m_tfHost, new GridBagConstraints(10110.00.0,
271                    GridBagConstraints.WEST,
272                    GridBagConstraints.NONE,
273                    new Insets(10000)00));
274         p.add(new Label("Port:")new GridBagConstraints(20110.00.0,
275                    GridBagConstraints.EAST,
276                    GridBagConstraints.NONE,
277                    new Insets(101000)00));
278         p.add(m_tfPort, new GridBagConstraints(30110.00.0,
279                    GridBagConstraints.WEST,
280                    GridBagConstraints.NONE,
281                    new Insets(10000)00));
282         p.add(new Label("")new GridBagConstraints(30111.00.0,
283                    GridBagConstraints.WEST,
284                    GridBagConstraints.HORIZONTAL,
285                    new Insets(10000)00));
286 
287         m_buttonAbout.setLabel(" About ");
288         m_buttonAbout.addActionListener(new java.awt.event.ActionListener()
289         {
290             public void actionPerformed(ActionEvent e)
291             {
292                 buttonAbout_actionPerformed(e);
293             }
294         });
295 
296         p.add(m_buttonAbout, new GridBagConstraints(40110.00.0,
297                    GridBagConstraints.WEST,
298                    GridBagConstraints.NONE,
299                    new Insets(10000)00));
300         p.add(new Label("")new GridBagConstraints(40111.00.0,
301                    GridBagConstraints.WEST,
302                    GridBagConstraints.HORIZONTAL,
303                    new Insets(10000)00));
304 
305         // Second line
306         p.add(new Label("Name:")new GridBagConstraints(01110.00.0,
307                    GridBagConstraints.EAST,
308                    GridBagConstraints.NONE,
309                    new Insets(151000)00));
310         p.add(m_tfName, new GridBagConstraints(11110.00.0,
311                    GridBagConstraints.WEST,
312                    GridBagConstraints.NONE,
313                    new Insets(10000)00));
314         p.add(p1, new GridBagConstraints(21, GridBagConstraints.REMAINDER, 11.00.0,
315                    GridBagConstraints.WEST,
316                    GridBagConstraints.HORIZONTAL,
317                    new Insets(101000)00));
318 
319         setupConnect(p1);
320     }
321 
322     /**
323      * Setup connect panel
324      @param p panel
325      */
326     private void setupConnect(Panel p)
327     {
328         p.setLayout(new FlowLayout(FlowLayout.CENTER, 100));
329 
330         m_buttonConnect.setLabel(" Connect ");
331         m_buttonConnect.addActionListener(new java.awt.event.ActionListener()
332         {
333             public void actionPerformed(ActionEvent e)
334             {
335                 buttonConnect_actionPerformed(e);
336             }
337         });
338         m_buttonDisconnect.setLabel(" Disconnect ");
339         m_buttonDisconnect.addActionListener(new java.awt.event.ActionListener()
340         {
341             public void actionPerformed(ActionEvent e)
342             {
343                 buttonDisconnect_actionPerformed(e);
344             }
345         });
346 
347         p.add(m_buttonConnect);
348         p.add(m_buttonDisconnect);
349     }
350 
351     /**
352      * Set controls state
353      @param n state to conform with
354      */
355     void setControlsState(int n)
356     {
357         m_tfHost.setEnabled(n == DISCONNECTED);
358         m_tfPort.setEnabled(n == DISCONNECTED);
359         m_tfName.setEnabled(n == DISCONNECTED);
360         m_tfMessage.setEnabled(n == CONNECTED || n == LEADER);
361         m_buttonConnect.setEnabled(n == DISCONNECTED);
362         m_buttonDisconnect.setEnabled(n == CONNECTED || n == LEADER);
363         m_buttonForce.setEnabled(n == LEADER);
364         m_buttonSend.setEnabled(n == CONNECTED || n == LEADER);
365         m_buttonSendAll.setEnabled(n == CONNECTED || n == LEADER);
366         m_buttonSendFile.setEnabled(n == CONNECTED || n == LEADER);
367     }
368 
369     /**
370      * Connect
371      */
372     void buttonAbout_actionPerformed(ActionEvent event)
373     {
374       MsgDialog.MsgBox(this,
375       "IXSF Messenger 1.1. Copyright (C) 2009-2020 Alexey Veremenko. All rights reserved.");
376     }
377 
378     /**
379      * Connect
380      */
381     void buttonConnect_actionPerformed(ActionEvent event)
382     {
383     }
384 
385     /**
386      * Disconnect
387      */
388     void buttonDisconnect_actionPerformed(ActionEvent event)
389     {
390     }
391 
392     /**
393      * Force disconection
394      */
395     void buttonForce_actionPerformed(ActionEvent event)
396     {
397     }
398 
399     /**
400      * Send message
401      */
402     void buttonSend_actionPerformed(ActionEvent event)
403     {
404     }
405 
406     /**
407      * Broadcast message
408      */
409     void buttonSendAll_actionPerformed(ActionEvent event)
410     {
411     }
412 
413     /**
414      * Send file
415      */
416     void buttonSendFile_actionPerformed(ActionEvent event)
417     {
418     }
419 
420     /**
421      * TextFieldEx extends textField to specify minimum size
422      */
423     class TextFieldEx extends TextField
424     {
425         /**
426          * Construct new TextFieldEx object
427          */
428         TextFieldEx(String s, int n)
429         {
430             super(s, n);
431         }
432 
433         /*
434          * Specify minimum size
435          */
436         public Dimension getMinimumSize()
437         {
438             return getPreferredSize();
439         }
440     }
441 }
Java2html