OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / CORBA / swing / x5 / ClientFrame.java
1 /* ClientFrame.java --
2  Copyright (C) 2005 Free Software Foundation, Inc.
3
4  This file is part of GNU Classpath.
5
6  GNU Classpath is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2, or (at your option)
9  any later version.
10
11  GNU Classpath is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with GNU Classpath; see the file COPYING.  If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301 USA.
20
21  Linking this library statically or dynamically with other modules is
22  making a combined work based on this library.  Thus, the terms and
23  conditions of the GNU General Public License cover the whole
24  combination.
25
26  As a special exception, the copyright holders of this library give you
27  permission to link this library with independent modules to produce an
28  executable, regardless of the license terms of these independent
29  modules, and to copy and distribute the resulting executable under
30  terms of your choice, provided that you also meet, for each linked
31  independent module, the terms and conditions of the license of that
32  module.  An independent module is a module which is not derived from
33  or based on this library.  If you modify this library, you may extend
34  this exception to your version of the library, but you are not
35  obligated to do so.  If you do not wish to do so, delete this
36  exception statement from your version. */
37
38 package gnu.classpath.examples.CORBA.swing.x5;
39
40 import java.awt.BorderLayout;
41 import java.awt.Color;
42 import java.awt.GridLayout;
43 import java.awt.event.*;
44 import java.io.BufferedWriter;
45 import java.io.File;
46 import java.io.FileWriter;
47
48 import java.rmi.RemoteException;
49
50 import javax.rmi.PortableRemoteObject;
51
52 import javax.swing.*;
53 import java.awt.Dimension;
54
55 /**
56  * The JFrame of the GUI client.
57  *
58  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
59  */
60 public class ClientFrame
61   extends JFrame
62 {
63   /**
64    * The size of the playing field.
65    */
66   public final Dimension DESK_SIZE = 
67         new Dimension(624, 352-PlayingDesk.W);
68
69   /**
70    * Use serialVersionUID for interoperability.
71    */
72   private static final long serialVersionUID = 1;
73
74   // Define the application components:
75
76   /**
77    * Central panel where the main action takes place.
78    */
79   PlayingDesk desk = new PlayingDesk();
80
81   /**
82    * The scroll pane for canvas.
83    */
84   JScrollPane scroll = new JScrollPane();
85
86   /**
87    * Will remember the manager IOR.
88    */
89   String mior = "";
90
91   // The bottom panel contains the area that is used both to enter URL and
92   // for chatting.
93   JPanel pnBottom = new JPanel();
94
95   BorderLayout layBottom = new BorderLayout();
96
97   JTextField taUrl = new JTextField();
98
99   // The top primitive chatting panel, composed from labels.
100   JPanel pnChat = new JPanel();
101
102   GridLayout layChat = new GridLayout();
103
104   JLabel lbC3 = new JLabel();
105
106   JLabel lbC2 = new JLabel();
107
108   JLabel lbC1 = new JLabel();
109
110   // The button panel.
111   JPanel pnButtons = new JPanel();
112
113   GridLayout layButtons = new GridLayout();
114
115   JButton bLeave = new JButton();
116
117   JButton bConnect = new JButton();
118
119   JButton bExit = new JButton();
120
121   JButton bReset = new JButton();
122
123   JLabel lbState = new JLabel();
124
125   JButton bChat = new JButton();
126   
127   JButton bPaste = new JButton();
128
129   public ClientFrame()
130   {
131     try
132       {
133         jbInit();
134       }
135     catch (Exception e)
136       {
137         e.printStackTrace();
138       }
139   }
140
141   private void jbInit()
142     throws Exception
143   {
144     desk.frame = this;
145
146     pnBottom.setLayout(layBottom);
147
148     pnChat.setLayout(layChat);
149     layChat.setColumns(1);
150     layChat.setRows(3);
151
152     lbC1.setText("This program needs the game server (see README on how to start it).");
153     lbC2.setText("Enter the game server address (host:port)");
154     lbC3.setText("Pressing \'Connect\' with the empty address will start the server on "
155       + "the local machine.");
156     bLeave.setEnabled(true);
157     bLeave.setToolTipText("Leave if either you have lost or do not want longer to play with "
158       + "this partner.");
159     bLeave.setText("Leave game");
160     bLeave.addActionListener(new java.awt.event.ActionListener()
161     {
162       public void actionPerformed(ActionEvent e)
163       {
164         bLeave_actionPerformed(e);
165       }
166     });
167     bConnect.setToolTipText("Connect your playing partner");
168     bConnect.setText("Connect");
169     bConnect.addActionListener(new java.awt.event.ActionListener()
170     {
171       public void actionPerformed(ActionEvent e)
172       {
173         bConnect_actionPerformed(e);
174       }
175     });
176     pnButtons.setLayout(layButtons);
177     bExit.setToolTipText("Exit this program");
178     bExit.setText("Exit");
179     bExit.addActionListener(new java.awt.event.ActionListener()
180     {
181       public void actionPerformed(ActionEvent e)
182       {
183         bExit_actionPerformed(e);
184       }
185     });
186     layButtons.setHgap(2);
187     bReset.setToolTipText("Restart the game. The partner may choose to exit!");
188     bReset.setText("Reset game");
189     bReset.addActionListener(new java.awt.event.ActionListener()
190     {
191       public void actionPerformed(ActionEvent e)
192       {
193         bReset_actionPerformed(e);
194       }
195     });
196     lbState.setText("Disconnected");
197     bChat.setToolTipText("Send message to player. Reuse the address "+
198                          "field to enter the message.");
199     bChat.setText("Chat");
200     bChat.addActionListener(new java.awt.event.ActionListener()
201     {
202       public void actionPerformed(ActionEvent e)
203       {
204         bChat_actionPerformed(e);
205       }
206     });
207     
208     bPaste.setText("Paste");
209     bPaste.setToolTipText("Paste, same as Ctrl-V");
210     bPaste.addActionListener(new java.awt.event.ActionListener()
211     {
212       public void actionPerformed(ActionEvent e)
213       {
214         bPaste_actionPerformed(e);
215       }
216     });
217     
218     desk.setMaximumSize(DESK_SIZE);
219     desk.setPreferredSize(DESK_SIZE);
220     
221     scroll.getViewport().add(desk, null);
222     getContentPane().add(scroll, BorderLayout.CENTER);
223     getContentPane().add(pnBottom, BorderLayout.SOUTH);
224
225     pnBottom.add(taUrl, BorderLayout.CENTER);
226     pnBottom.add(pnChat, BorderLayout.NORTH);
227
228     pnChat.add(lbC1, null);
229     pnChat.add(lbC2, null);
230     pnChat.add(lbC3, null);
231     pnBottom.add(pnButtons, BorderLayout.SOUTH);
232     pnButtons.add(lbState, null);
233     pnButtons.add(bConnect, null);
234     pnButtons.add(bChat, null);
235     pnButtons.add(bLeave, null);
236     pnButtons.add(bReset, null);
237     pnButtons.add(bExit, null);
238     pnButtons.add(bPaste, null);    
239
240     desk.player.set_current_state(State.DISCONNECTED);
241   }
242
243   /**
244    * Handles exit procedure.
245    */
246   protected void processWindowEvent(WindowEvent e)
247   {
248     super.processWindowEvent(e);
249     if (e.getID() == WindowEvent.WINDOW_CLOSING)
250       {
251         bExit_actionPerformed(null);
252       }
253   }
254
255   /**
256    * Handles the connection procedure.
257    */
258   void bConnect_actionPerformed(ActionEvent e)
259   {
260     try
261       {
262         int state = desk.player.get_current_state();
263
264         if (state == State.DISCONNECTED || state == State.ERROR)
265           {
266             talk(ChatConstants.colors[0], "Connecting...");
267
268             if (desk.manager == null)
269               {
270                 mior = taUrl.getText().trim();
271
272                 // Obtain the manager object:
273                 org.omg.CORBA.Object object = null;
274
275                 try
276                   {
277                     object = desk.orb.string_to_object(mior);
278                   }
279                 catch (Exception exc)
280                   {
281                     // Maybe CORBA 3.0.3 is not completely implemented?
282                     if (mior.startsWith("http://") || mior.startsWith("ftp://")
283                       || mior.startsWith("file://"))
284                       object = desk.orb.string_to_object(IorReader.readUrl(mior));
285                     else
286                       throw exc;
287                   }
288
289                 desk.manager = (GameManager) PortableRemoteObject.narrow(
290                   object, GameManager.class);
291
292                 // Export the desk.player as a remote object.
293                 PortableRemoteObject.exportObject(desk.player);
294               }
295
296             desk.player.set_current_state(State.QUEUED);
297             desk.manager.requestTheGame(desk.player);
298           }
299
300         // Save the specified IOR for the future use:
301         File gmf = new File(OrbStarter.WRITE_URL_TO_FILE);
302         FileWriter f = new FileWriter(gmf);
303         BufferedWriter b = new BufferedWriter(f);
304
305         b.write(mior);
306         b.close();
307       }
308     catch (Exception ex)
309       {
310         talk(Color.red, "The manager is not reachable by this address.");
311         talk(Color.red, ex.getMessage());
312         desk.player.set_current_state(State.DISCONNECTED);
313       }
314   }
315
316   /**
317    * Display the new message with the given color. Shift the other messages over
318    * the labels.
319    */
320   public void talk(Color color, String text)
321   {
322     lbC1.setText(lbC2.getText());
323     lbC1.setForeground(lbC2.getForeground());
324
325     lbC2.setText(lbC3.getText());
326     lbC2.setForeground(lbC3.getForeground());
327
328     lbC3.setText(text);
329     lbC3.setForeground(color);
330   }
331
332   /**
333    * Exit this program.
334    */
335   void bExit_actionPerformed(ActionEvent e)
336   {
337     try
338       {
339         if (desk.player.get_current_state() != State.DISCONNECTED
340           && desk.player.partner != null)
341           {
342             desk.player.partner.receive_chat(ChatConstants.REMOTE_PLAYER,
343               "I close the program!");
344             desk.player.partner.disconnect();
345           }
346       }
347     catch (RemoteException ex)
348       {
349         // We will print the exception because this is a demo application that
350         // may be modified for learning purposes.
351         ex.printStackTrace();
352       }
353     System.exit(0);
354   }
355
356   void bReset_actionPerformed(ActionEvent e)
357   {
358     if (desk.player.partner != null)
359       {
360         try
361           {
362             desk.player.partner.receive_chat(ChatConstants.REMOTE_PLAYER,
363               "Your partner restarted the game.");
364
365             desk.player.start_game(desk.player.partner, false);
366             desk.player.partner.start_game(desk.player, true);
367           }
368         catch (RemoteException ex)
369           {
370             // We will print the exception because this is a demo application
371             // that
372             // may be modified for learning purposes.
373             ex.printStackTrace();
374           }
375       }
376     else
377       talk(Color.black, "You have not started the game yet.");
378   }
379
380   void bLeave_actionPerformed(ActionEvent e)
381   {
382     desk.player.leave();
383   }
384
385   void bChat_actionPerformed(ActionEvent e)
386   {
387     try
388       {
389         if (desk.player.partner != null)
390           {
391             String message = taUrl.getText();
392             desk.player.partner.receive_chat(ChatConstants.REMOTE_PLAYER, message);
393             talk(ChatConstants.colors[ChatConstants.SELF], message);
394             taUrl.setText("");
395           }
396         else
397           {
398             talk(Color.black, "Sorry, not connected to anybody");
399           }
400       }
401     catch (RemoteException ex)
402       {
403         // We will print the exception because this is a demo application that
404         // may be modified for learning purposes.
405         ex.printStackTrace();
406       }
407   }
408   
409   /**
410    * Work around our keyboard shortcut handling that is still not working
411    * properly.
412    */
413   void bPaste_actionPerformed(ActionEvent e)
414   {
415     taUrl.paste();
416   }  
417 }