aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.4.3/libjava/classpath/examples/gnu/classpath/examples/CORBA/swing/x5/ClientFrame.java
blob: c3d8300b7c3490093bf6bd667da405b99ba3def4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* ClientFrame.java --
 Copyright (C) 2005 Free Software Foundation, Inc.

 This file is part of GNU Classpath.

 GNU Classpath is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2, or (at your option)
 any later version.

 GNU Classpath is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with GNU Classpath; see the file COPYING.  If not, write to the
 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 02110-1301 USA.

 Linking this library statically or dynamically with other modules is
 making a combined work based on this library.  Thus, the terms and
 conditions of the GNU General Public License cover the whole
 combination.

 As a special exception, the copyright holders of this library give you
 permission to link this library with independent modules to produce an
 executable, regardless of the license terms of these independent
 modules, and to copy and distribute the resulting executable under
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on this library.  If you modify this library, you may extend
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */

package gnu.classpath.examples.CORBA.swing.x5;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import java.rmi.RemoteException;

import javax.rmi.PortableRemoteObject;

import javax.swing.*;
import java.awt.Dimension;

/**
 * The JFrame of the GUI client.
 *
 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
 */
public class ClientFrame
  extends JFrame
{
  /**
   * The size of the playing field.
   */
  public final Dimension DESK_SIZE = 
	new Dimension(624, 352-PlayingDesk.W);

  /**
   * Use serialVersionUID for interoperability.
   */
  private static final long serialVersionUID = 1;

  // Define the application components:

  /**
   * Central panel where the main action takes place.
   */
  PlayingDesk desk = new PlayingDesk();

  /**
   * The scroll pane for canvas.
   */
  JScrollPane scroll = new JScrollPane();

  /**
   * Will remember the manager IOR.
   */
  String mior = "";

  // The bottom panel contains the area that is used both to enter URL and
  // for chatting.
  JPanel pnBottom = new JPanel();

  BorderLayout layBottom = new BorderLayout();

  JTextField taUrl = new JTextField();

  // The top primitive chatting panel, composed from labels.
  JPanel pnChat = new JPanel();

  GridLayout layChat = new GridLayout();

  JLabel lbC3 = new JLabel();

  JLabel lbC2 = new JLabel();

  JLabel lbC1 = new JLabel();

  // The button panel.
  JPanel pnButtons = new JPanel();

  GridLayout layButtons = new GridLayout();

  JButton bLeave = new JButton();

  JButton bConnect = new JButton();

  JButton bExit = new JButton();

  JButton bReset = new JButton();

  JLabel lbState = new JLabel();

  JButton bChat = new JButton();
  
  JButton bPaste = new JButton();

  public ClientFrame()
  {
    try
      {
        jbInit();
      }
    catch (Exception e)
      {
        e.printStackTrace();
      }
  }

  private void jbInit()
    throws Exception
  {
    desk.frame = this;

    pnBottom.setLayout(layBottom);

    pnChat.setLayout(layChat);
    layChat.setColumns(1);
    layChat.setRows(3);

    lbC1.setText("This program needs the game server (see README on how to start it).");
    lbC2.setText("Enter the game server address (host:port)");
    lbC3.setText("Pressing \'Connect\' with the empty address will start the server on "
      + "the local machine.");
    bLeave.setEnabled(true);
    bLeave.setToolTipText("Leave if either you have lost or do not want longer to play with "
      + "this partner.");
    bLeave.setText("Leave game");
    bLeave.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        bLeave_actionPerformed(e);
      }
    });
    bConnect.setToolTipText("Connect your playing partner");
    bConnect.setText("Connect");
    bConnect.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        bConnect_actionPerformed(e);
      }
    });
    pnButtons.setLayout(layButtons);
    bExit.setToolTipText("Exit this program");
    bExit.setText("Exit");
    bExit.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        bExit_actionPerformed(e);
      }
    });
    layButtons.setHgap(2);
    bReset.setToolTipText("Restart the game. The partner may choose to exit!");
    bReset.setText("Reset game");
    bReset.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        bReset_actionPerformed(e);
      }
    });
    lbState.setText("Disconnected");
    bChat.setToolTipText("Send message to player. Reuse the address "+
                         "field to enter the message.");
    bChat.setText("Chat");
    bChat.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        bChat_actionPerformed(e);
      }
    });
    
    bPaste.setText("Paste");
    bPaste.setToolTipText("Paste, same as Ctrl-V");
    bPaste.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        bPaste_actionPerformed(e);
      }
    });
    
    desk.setMaximumSize(DESK_SIZE);
    desk.setPreferredSize(DESK_SIZE);
    
    scroll.getViewport().add(desk, null);
    getContentPane().add(scroll, BorderLayout.CENTER);
    getContentPane().add(pnBottom, BorderLayout.SOUTH);

    pnBottom.add(taUrl, BorderLayout.CENTER);
    pnBottom.add(pnChat, BorderLayout.NORTH);

    pnChat.add(lbC1, null);
    pnChat.add(lbC2, null);
    pnChat.add(lbC3, null);
    pnBottom.add(pnButtons, BorderLayout.SOUTH);
    pnButtons.add(lbState, null);
    pnButtons.add(bConnect, null);
    pnButtons.add(bChat, null);
    pnButtons.add(bLeave, null);
    pnButtons.add(bReset, null);
    pnButtons.add(bExit, null);
    pnButtons.add(bPaste, null);    

    desk.player.set_current_state(State.DISCONNECTED);
  }

  /**
   * Handles exit procedure.
   */
  protected void processWindowEvent(WindowEvent e)
  {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING)
      {
        bExit_actionPerformed(null);
      }
  }

  /**
   * Handles the connection procedure.
   */
  void bConnect_actionPerformed(ActionEvent e)
  {
    try
      {
        int state = desk.player.get_current_state();

        if (state == State.DISCONNECTED || state == State.ERROR)
          {
            talk(ChatConstants.colors[0], "Connecting...");

            if (desk.manager == null)
              {
                mior = taUrl.getText().trim();

                // Obtain the manager object:
                org.omg.CORBA.Object object = null;

                try
                  {
                    object = desk.orb.string_to_object(mior);
                  }
                catch (Exception exc)
                  {
                    // Maybe CORBA 3.0.3 is not completely implemented?
                    if (mior.startsWith("http://") || mior.startsWith("ftp://")
                      || mior.startsWith("file://"))
                      object = desk.orb.string_to_object(IorReader.readUrl(mior));
                    else
                      throw exc;
                  }

                desk.manager = (GameManager) PortableRemoteObject.narrow(
                  object, GameManager.class);

                // Export the desk.player as a remote object.
                PortableRemoteObject.exportObject(desk.player);
              }

            desk.player.set_current_state(State.QUEUED);
            desk.manager.requestTheGame(desk.player);
          }

        // Save the specified IOR for the future use:
        File gmf = new File(OrbStarter.WRITE_URL_TO_FILE);
        FileWriter f = new FileWriter(gmf);
        BufferedWriter b = new BufferedWriter(f);

        b.write(mior);
        b.close();
      }
    catch (Exception ex)
      {
        talk(Color.red, "The manager is not reachable by this address.");
        talk(Color.red, ex.getMessage());
        desk.player.set_current_state(State.DISCONNECTED);
      }
  }

  /**
   * Display the new message with the given color. Shift the other messages over
   * the labels.
   */
  public void talk(Color color, String text)
  {
    lbC1.setText(lbC2.getText());
    lbC1.setForeground(lbC2.getForeground());

    lbC2.setText(lbC3.getText());
    lbC2.setForeground(lbC3.getForeground());

    lbC3.setText(text);
    lbC3.setForeground(color);
  }

  /**
   * Exit this program.
   */
  void bExit_actionPerformed(ActionEvent e)
  {
    try
      {
        if (desk.player.get_current_state() != State.DISCONNECTED
          && desk.player.partner != null)
          {
            desk.player.partner.receive_chat(ChatConstants.REMOTE_PLAYER,
              "I close the program!");
            desk.player.partner.disconnect();
          }
      }
    catch (RemoteException ex)
      {
        // We will print the exception because this is a demo application that
        // may be modified for learning purposes.
        ex.printStackTrace();
      }
    System.exit(0);
  }

  void bReset_actionPerformed(ActionEvent e)
  {
    if (desk.player.partner != null)
      {
        try
          {
            desk.player.partner.receive_chat(ChatConstants.REMOTE_PLAYER,
              "Your partner restarted the game.");

            desk.player.start_game(desk.player.partner, false);
            desk.player.partner.start_game(desk.player, true);
          }
        catch (RemoteException ex)
          {
            // We will print the exception because this is a demo application
            // that
            // may be modified for learning purposes.
            ex.printStackTrace();
          }
      }
    else
      talk(Color.black, "You have not started the game yet.");
  }

  void bLeave_actionPerformed(ActionEvent e)
  {
    desk.player.leave();
  }

  void bChat_actionPerformed(ActionEvent e)
  {
    try
      {
        if (desk.player.partner != null)
          {
            String message = taUrl.getText();
            desk.player.partner.receive_chat(ChatConstants.REMOTE_PLAYER, message);
            talk(ChatConstants.colors[ChatConstants.SELF], message);
            taUrl.setText("");
          }
        else
          {
            talk(Color.black, "Sorry, not connected to anybody");
          }
      }
    catch (RemoteException ex)
      {
        // We will print the exception because this is a demo application that
        // may be modified for learning purposes.
        ex.printStackTrace();
      }
  }
  
  /**
   * Work around our keyboard shortcut handling that is still not working
   * properly.
   */
  void bPaste_actionPerformed(ActionEvent e)
  {
    taUrl.paste();
  }  
}