2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
38 package gnu.classpath.examples.CORBA.swing.x5;
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;
46 import java.io.FileWriter;
48 import java.rmi.RemoteException;
50 import javax.rmi.PortableRemoteObject;
53 import java.awt.Dimension;
56 * The JFrame of the GUI client.
58 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
60 public class ClientFrame
64 * The size of the playing field.
66 public final Dimension DESK_SIZE =
67 new Dimension(624, 352-PlayingDesk.W);
70 * Use serialVersionUID for interoperability.
72 private static final long serialVersionUID = 1;
74 // Define the application components:
77 * Central panel where the main action takes place.
79 PlayingDesk desk = new PlayingDesk();
82 * The scroll pane for canvas.
84 JScrollPane scroll = new JScrollPane();
87 * Will remember the manager IOR.
91 // The bottom panel contains the area that is used both to enter URL and
93 JPanel pnBottom = new JPanel();
95 BorderLayout layBottom = new BorderLayout();
97 JTextField taUrl = new JTextField();
99 // The top primitive chatting panel, composed from labels.
100 JPanel pnChat = new JPanel();
102 GridLayout layChat = new GridLayout();
104 JLabel lbC3 = new JLabel();
106 JLabel lbC2 = new JLabel();
108 JLabel lbC1 = new JLabel();
111 JPanel pnButtons = new JPanel();
113 GridLayout layButtons = new GridLayout();
115 JButton bLeave = new JButton();
117 JButton bConnect = new JButton();
119 JButton bExit = new JButton();
121 JButton bReset = new JButton();
123 JLabel lbState = new JLabel();
125 JButton bChat = new JButton();
127 JButton bPaste = new JButton();
141 private void jbInit()
146 pnBottom.setLayout(layBottom);
148 pnChat.setLayout(layChat);
149 layChat.setColumns(1);
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 "
159 bLeave.setText("Leave game");
160 bLeave.addActionListener(new java.awt.event.ActionListener()
162 public void actionPerformed(ActionEvent e)
164 bLeave_actionPerformed(e);
167 bConnect.setToolTipText("Connect your playing partner");
168 bConnect.setText("Connect");
169 bConnect.addActionListener(new java.awt.event.ActionListener()
171 public void actionPerformed(ActionEvent e)
173 bConnect_actionPerformed(e);
176 pnButtons.setLayout(layButtons);
177 bExit.setToolTipText("Exit this program");
178 bExit.setText("Exit");
179 bExit.addActionListener(new java.awt.event.ActionListener()
181 public void actionPerformed(ActionEvent e)
183 bExit_actionPerformed(e);
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()
191 public void actionPerformed(ActionEvent e)
193 bReset_actionPerformed(e);
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()
202 public void actionPerformed(ActionEvent e)
204 bChat_actionPerformed(e);
208 bPaste.setText("Paste");
209 bPaste.setToolTipText("Paste, same as Ctrl-V");
210 bPaste.addActionListener(new java.awt.event.ActionListener()
212 public void actionPerformed(ActionEvent e)
214 bPaste_actionPerformed(e);
218 desk.setMaximumSize(DESK_SIZE);
219 desk.setPreferredSize(DESK_SIZE);
221 scroll.getViewport().add(desk, null);
222 getContentPane().add(scroll, BorderLayout.CENTER);
223 getContentPane().add(pnBottom, BorderLayout.SOUTH);
225 pnBottom.add(taUrl, BorderLayout.CENTER);
226 pnBottom.add(pnChat, BorderLayout.NORTH);
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);
240 desk.player.set_current_state(State.DISCONNECTED);
244 * Handles exit procedure.
246 protected void processWindowEvent(WindowEvent e)
248 super.processWindowEvent(e);
249 if (e.getID() == WindowEvent.WINDOW_CLOSING)
251 bExit_actionPerformed(null);
256 * Handles the connection procedure.
258 void bConnect_actionPerformed(ActionEvent e)
262 int state = desk.player.get_current_state();
264 if (state == State.DISCONNECTED || state == State.ERROR)
266 talk(ChatConstants.colors[0], "Connecting...");
268 if (desk.manager == null)
270 mior = taUrl.getText().trim();
272 // Obtain the manager object:
273 org.omg.CORBA.Object object = null;
277 object = desk.orb.string_to_object(mior);
279 catch (Exception exc)
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));
289 desk.manager = (GameManager) PortableRemoteObject.narrow(
290 object, GameManager.class);
292 // Export the desk.player as a remote object.
293 PortableRemoteObject.exportObject(desk.player);
296 desk.player.set_current_state(State.QUEUED);
297 desk.manager.requestTheGame(desk.player);
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);
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);
317 * Display the new message with the given color. Shift the other messages over
320 public void talk(Color color, String text)
322 lbC1.setText(lbC2.getText());
323 lbC1.setForeground(lbC2.getForeground());
325 lbC2.setText(lbC3.getText());
326 lbC2.setForeground(lbC3.getForeground());
329 lbC3.setForeground(color);
335 void bExit_actionPerformed(ActionEvent e)
339 if (desk.player.get_current_state() != State.DISCONNECTED
340 && desk.player.partner != null)
342 desk.player.partner.receive_chat(ChatConstants.REMOTE_PLAYER,
343 "I close the program!");
344 desk.player.partner.disconnect();
347 catch (RemoteException ex)
349 // We will print the exception because this is a demo application that
350 // may be modified for learning purposes.
351 ex.printStackTrace();
356 void bReset_actionPerformed(ActionEvent e)
358 if (desk.player.partner != null)
362 desk.player.partner.receive_chat(ChatConstants.REMOTE_PLAYER,
363 "Your partner restarted the game.");
365 desk.player.start_game(desk.player.partner, false);
366 desk.player.partner.start_game(desk.player, true);
368 catch (RemoteException ex)
370 // We will print the exception because this is a demo application
372 // may be modified for learning purposes.
373 ex.printStackTrace();
377 talk(Color.black, "You have not started the game yet.");
380 void bLeave_actionPerformed(ActionEvent e)
385 void bChat_actionPerformed(ActionEvent e)
389 if (desk.player.partner != null)
391 String message = taUrl.getText();
392 desk.player.partner.receive_chat(ChatConstants.REMOTE_PLAYER, message);
393 talk(ChatConstants.colors[ChatConstants.SELF], message);
398 talk(Color.black, "Sorry, not connected to anybody");
401 catch (RemoteException ex)
403 // We will print the exception because this is a demo application that
404 // may be modified for learning purposes.
405 ex.printStackTrace();
410 * Work around our keyboard shortcut handling that is still not working
413 void bPaste_actionPerformed(ActionEvent e)