Crossfire JXClient, Trunk  R20561
JXCWindow.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.main;
23 
45 import java.awt.Dimension;
46 import java.awt.Insets;
47 import java.awt.event.ComponentEvent;
48 import java.awt.event.ComponentListener;
49 import java.awt.event.KeyEvent;
50 import java.awt.event.KeyListener;
51 import java.awt.event.WindowAdapter;
52 import java.awt.event.WindowEvent;
53 import java.awt.event.WindowFocusListener;
54 import java.awt.event.WindowListener;
55 import java.io.IOException;
56 import javax.swing.JFrame;
57 import javax.swing.SwingUtilities;
58 import javax.swing.WindowConstants;
59 import org.jetbrains.annotations.NotNull;
60 import org.jetbrains.annotations.Nullable;
61 
67 public class JXCWindow {
68 
72  public static final boolean DISABLE_START_GUI = true;
73 
77  @NotNull
78  private final Exiter exiter;
79 
83  @NotNull
84  private final GuiManager guiManager;
85 
89  @NotNull
91 
95  @NotNull
96  private final CommandQueue commandQueue;
97 
101  @NotNull
103 
107  @NotNull
108  private final KeyHandler keyHandler;
109 
113  @NotNull
115 
119  @NotNull
121 
125  @NotNull
126  private final JFrame frame = new JFrame("");
127 
139  @NotNull
140  @SuppressWarnings("FieldCanBeLocal")
141  private final WindowFocusListener windowFocusListener = new WindowAdapter() {
142 
143  @Override
144  public void windowLostFocus(final WindowEvent e) {
145  commandQueue.stopRunning();
146  }
147 
148  };
149 
153  @NotNull
155 
156  @Override
157  public void delinvReceived(final int tag) {
158  // ignore
159  }
160 
161  @Override
162  public void delitemReceived(@NotNull final int[] tags) {
163  // ignore
164  }
165 
166  @Override
167  public void addItemReceived(final int location, final int tag, final int flags, final int weight, final int faceNum, @NotNull final String name, @NotNull final String namePl, final int anim, final int animSpeed, final int nrof, final int type) {
168  // ignore
169  }
170 
171  @Override
172  public void playerReceived(final int tag, final int weight, final int faceNum, @NotNull final String name) {
173  //noinspection AnonymousInnerClassMayBeStatic
174  SwingUtilities.invokeLater(new Runnable() {
175 
176  @Override
177  public void run() {
178  guiManager.playerReceived();
179  }
180 
181  });
182  }
183 
184  @Override
185  public void upditemReceived(final int flags, final int tag, final int valLocation, final int valFlags, final int valWeight, final int valFaceNum, @NotNull final String valName, @NotNull final String valNamePl, final int valAnim, final int valAnimSpeed, final int valNrof) {
186  // ignore
187  }
188 
189  };
190 
194  @NotNull
195  @SuppressWarnings("FieldCanBeLocal")
196  private final WindowListener windowListener = new WindowAdapter() {
197 
198  @Override
199  public void windowClosing(@NotNull final WindowEvent e) {
200  if (!guiManager.openQuitDialog()) {
201  exiter.terminate();
202  }
203  }
204 
205  @Override
206  public void windowClosed(@NotNull final WindowEvent e) {
207  if (!frame.isVisible()) {
208  exiter.terminate();
209  }
210  }
211 
212  };
213 
217  @NotNull
218  @SuppressWarnings("FieldCanBeLocal")
219  private final KeyListener keyListener = new KeyListener() {
220 
221  @Override
222  public void keyTyped(@NotNull final KeyEvent e) {
223  // ignore
224  }
225 
226  @Override
227  public void keyPressed(@NotNull final KeyEvent e) {
229  }
230 
231  @Override
232  public void keyReleased(@NotNull final KeyEvent e) {
234  }
235 
236  };
237 
242  @NotNull
244 
245  @Override
246  public void commandQueryReceived(@NotNull final String prompt, final int queryType) {
247  SwingUtilities.invokeLater(new Runnable() {
248 
249  @Override
250  public void run() {
251  guiManager.openQueryDialog(prompt, queryType);
252  }
253 
254  });
255  }
256 
257  };
258 
263  @NotNull
264  @SuppressWarnings("FieldCanBeLocal")
266 
267  @Override
268  public void start() {
269  server.removeCrossfireQueryListener(crossfireQueryListener);
271  if (DISABLE_START_GUI) {
272  exiter.terminate();
273  }
274  }
275 
276  @Override
277  public void metaserver() {
278  server.removeCrossfireQueryListener(crossfireQueryListener);
280  }
281 
282  @Override
283  public void preConnecting(@NotNull final String serverInfo) {
284  // ignore
285  }
286 
287  @Override
288  public void connecting(@NotNull final String serverInfo) {
289  server.addCrossfireQueryListener(crossfireQueryListener);
291  }
292 
293  @Override
294  public void connecting(@NotNull final ClientSocketState clientSocketState) {
295  // ignore
296  }
297 
298  @Override
299  public void connected() {
300  // ignore
301  }
302 
303  @Override
304  public void connectFailed(@NotNull final String reason) {
305  // ignore
306  }
307 
308  };
309 
314  @NotNull
315  @SuppressWarnings("FieldCanBeLocal")
317 
318  @Override
319  public void replySent() {
321  }
322 
323  };
324 
328  @NotNull
329  @SuppressWarnings("FieldCanBeLocal")
331 
332  @Override
333  public void manageAccount() {
334  //noinspection AnonymousInnerClassMayBeStatic
335  SwingUtilities.invokeLater(new Runnable() {
336 
337  @Override
338  public void run() {
340  }
341 
342  });
343  }
344 
345  @Override
346  public void addAccount(@NotNull final CharacterInformation characterInformation) {
347  SwingUtilities.invokeLater(new Runnable() {
348 
349  @Override
350  public void run() {
351  final CharacterInformation information = characterInformation;
352  characterModel.add(information);
353  }
354 
355  });
356  }
357 
358  @Override
359  public void startAccountList(@NotNull final String accountName) {
360  SwingUtilities.invokeLater(new Runnable() {
361 
362  @Override
363  public void run() {
365  guiManager.setAccountName(accountName);
366  }
367 
368  });
369  }
370 
371  @Override
372  public void endAccountList(final int count) {
373  SwingUtilities.invokeLater(new Runnable() {
374 
375  @Override
376  public void run() {
378  guiManager.showCharacters(count);
379  }
380 
381  });
382  }
383 
384  @Override
385  public void startPlaying() {
386  //noinspection AnonymousInnerClassMayBeStatic
387  SwingUtilities.invokeLater(new Runnable() {
388 
389  @Override
390  public void run() {
392  }
393 
394  });
395  }
396 
397  @Override
398  public void selectCharacter(@NotNull final String accountName, @NotNull final String characterName) {
399  SwingUtilities.invokeLater(new Runnable() {
400 
401  @Override
402  public void run() {
403  guiManager.selectCharacter(accountName, characterName);
404  }
405 
406  });
407  }
408 
409  };
410 
424  public JXCWindow(@NotNull final Exiter exiter, @NotNull final CrossfireServerConnection server, @NotNull final OptionManager optionManager, @NotNull final GuiStateManager guiStateManager, @NotNull final JXCWindowRenderer windowRenderer, @NotNull final CommandQueue commandQueue, @NotNull final GuiManager guiManager, @NotNull final KeyHandler keyHandler, @NotNull final CharacterModel characterModel, @NotNull final JXCConnection connection) {
425  this.exiter = exiter;
426  this.server = server;
427  this.optionManager = optionManager;
428  this.windowRenderer = windowRenderer;
429  this.commandQueue = commandQueue;
430  this.guiManager = guiManager;
431  this.keyHandler = keyHandler;
432  this.characterModel = characterModel;
433  frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
434  try {
435  frame.setIconImage(ResourceUtils.loadImage(ResourceUtils.APPLICATION_ICON).getImage());
436  } catch (final IOException ex) {
437  System.err.println("Cannot find application icon: "+ex.getMessage());
438  }
439  frame.setFocusTraversalKeysEnabled(false);
440  frame.addWindowFocusListener(windowFocusListener);
441  frame.addWindowListener(windowListener);
442  frame.addComponentListener(new ComponentListener() {
443 
444  @Override
445  public void componentResized(final ComponentEvent e) {
446  final int width = frame.getContentPane().getWidth();
447  final int height = frame.getContentPane().getHeight();
448  guiManager.updateWindowSize(width, height);
449  }
450 
451  @Override
452  public void componentMoved(final ComponentEvent e) {
453  // ignore
454  }
455 
456  @Override
457  public void componentShown(final ComponentEvent e) {
458  // ignore
459  }
460 
461  @Override
462  public void componentHidden(final ComponentEvent e) {
463  // ignore
464  }
465 
466  });
467  server.addSentReplyListener(sentReplyListener);
468  server.addCrossfireAccountListener(accountListener);
469  guiStateManager.addGuiStateListener(guiStateListener);
470  frame.addKeyListener(keyListener);
471  connection.setFrame(frame);
472  }
473 
481  public void init(@Nullable final Resolution resolution, @NotNull final String skinName, final boolean fullScreen, @NotNull final SkinLoader skinLoader) {
482  JXCSkin skin;
483  try {
484  skin = skinLoader.loadSkin(skinName);
485  } catch (final JXCSkinException ex) {
486  if (skinName.equals(Options.DEFAULT_SKIN)) {
487  System.err.println("cannot load skin "+skinName+": "+ex.getMessage());
488  System.exit(1);
489  }
490 
491  System.err.println("cannot load skin "+skinName+": "+ex.getMessage()+", trying default skin");
492  try {
493  skin = skinLoader.loadSkin(Options.DEFAULT_SKIN);
494  } catch (final JXCSkinException ex2) {
495  System.err.println("cannot load default skin "+Options.DEFAULT_SKIN+": "+ex2.getMessage());
496  System.exit(1);
497  throw new AssertionError(ex2);
498  }
499  }
503 
504  final Resolution minResolution = skin.getMinResolution();
505  final Dimension minSize = minResolution.asDimension();
506  final Dimension maxSize = skin.getMaxResolution().asDimension();
507  final Insets insets = frame.getInsets();
508  minSize.width += insets.left+insets.right;
509  minSize.height += insets.top+insets.bottom;
510  maxSize.width += insets.left+insets.right;
511  maxSize.height += insets.top+insets.bottom;
512  frame.setMinimumSize(minSize);
513  frame.setMaximumSize(maxSize);
514 
515  if (!fullScreen || !windowRenderer.setFullScreenMode(frame, resolution)) {
516  windowRenderer.setWindowMode(frame, resolution, minResolution, minSize.equals(maxSize));
517  }
518 
519  guiManager.setSkin(skin);
520  guiManager.updateWindowSize(frame.getWidth(), frame.getHeight());
522  }
523 
527  public void term() {
528  guiManager.term();
530  }
531 
532 }
void addCrossfireUpdateItemListener(@NotNull CrossfireUpdateItemListener crossfireUpdateItemListener)
Adds a listener to be notified about item changes.
void selectCharacter(@NotNull final String accountName, @NotNull final String characterName)
Updates the selected character name in an account.
boolean setFullScreenMode(@NotNull final JFrame frame, @Nullable final Resolution resolution)
Tries to switch to the given resolution.
Interface for listeners interested gui state changes.
final WindowFocusListener windowFocusListener
The WindowFocusListener registered for this window.
Definition: JXCWindow.java:141
void setKeyBindings(@NotNull final KeyBindings keyBindings)
Sets the active KeyBindings.
static final String APPLICATION_ICON
The resource name for the application icon.
void removeCrossfireUpdateItemListener(@NotNull CrossfireUpdateItemListener crossfireUpdateItemListener)
Removes a listener to be notified about item changes.
void keyPressed(@NotNull final KeyEvent e)
Handles a "key pressed" event.
final KeyListener keyListener
The KeyListener attached to the main window.
Definition: JXCWindow.java:219
void setAccountName(@NotNull final String accountName)
Updates the current account name.
void updateWindowSize(final int width, final int height)
Sets a new window size.
final GuiManager guiManager
The GuiManager for controlling the main GUI state.
Definition: JXCWindow.java:84
static final boolean DISABLE_START_GUI
TODO: Remove when more options are implemented in the start screen gui.
Definition: JXCWindow.java:72
boolean stopRunning()
Tells the server to stop running.
KeyBindings getDefaultKeyBindings()
Returns the default key bindings for this skin.
Dimension asDimension()
Returns the resolution as a Dimension instance.
boolean equals(@Nullable final Object obj)
void hideAccountWindows()
Hides all account-related windows.
Resolution getMaxResolution()
Returns the maximal resolution of this skin.
final GuiStateListener guiStateListener
The GuiStateListener for detecting established or dropped connections.
Definition: JXCWindow.java:265
final OptionManager optionManager
The option manager for this window.
Definition: JXCWindow.java:114
void openQueryDialog(@NotNull final String prompt, final int queryType)
Opens the "query" dialog.
static ImageIcon loadImage(@NotNull final String name)
Loads an image file.
Defines a JXClient skin consisting of a main Gui and zero or more dialog Guis.
Definition: JXCSkin.java:40
Utility class to store or restore the dialog states to/from a file.
Listener for classes interested in sent "reply" commands.
void commit()
Finishes an update transaction.
JXCWindow(@NotNull final Exiter exiter, @NotNull final CrossfireServerConnection server, @NotNull final OptionManager optionManager, @NotNull final GuiStateManager guiStateManager, @NotNull final JXCWindowRenderer windowRenderer, @NotNull final CommandQueue commandQueue, @NotNull final GuiManager guiManager, @NotNull final KeyHandler keyHandler, @NotNull final CharacterModel characterModel, @NotNull final JXCConnection connection)
Creates a new instance.
Definition: JXCWindow.java:424
void setSkin(@NotNull final JXCSkin skin)
Sets a new skin.
void setWindowMode(@NotNull final JFrame frame, @Nullable final Resolution resolution, @NotNull final Resolution minResolution, final boolean fixedSize)
Tries to switch to the given resolution.
Utility class for loading information from resources.
boolean openQuitDialog()
Opens the "quit" dialog.
final CrossfireServerConnection server
The CrossfireServerConnection to use.
Definition: JXCWindow.java:90
final CommandQueue commandQueue
The command queue instance for this window.
Definition: JXCWindow.java:96
Maintains the application's main GUI state.
Definition: GuiManager.java:60
Handles keyboard input processing.
Definition: KeyHandler.java:44
void loadOptions()
Loads all options' states from the backing settings instance.
Interface for listeners interested in account information related messages received from the Crossfir...
final SentReplyListener sentReplyListener
The SentReplyListener for detecting "reply" commands sent to the server.
Definition: JXCWindow.java:316
void unsetSkin()
Unsets the current skin.
final KeyHandler keyHandler
The KeyHandler for processing keyboard input.
Definition: JXCWindow.java:108
Maintains the character list for an account.
static void load(@NotNull final JXCSkin skin, @NotNull final JXCWindowRenderer windowRenderer)
Loads the dialogs state from a file.
final CrossfireAccountListener accountListener
The CrossfireAccountListener attached to server.
Definition: JXCWindow.java:330
void init(@Nullable final Resolution resolution, @NotNull final String skinName, final boolean fullScreen, @NotNull final SkinLoader skinLoader)
Initializes the instance: loads and displays the skin.
Definition: JXCWindow.java:481
void terminate()
Terminates the application.
Definition: Exiter.java:41
void playerReceived()
A "player" protocol command has been received.
final Exiter exiter
The Exiter to use.
Definition: JXCWindow.java:78
Exception thrown if a skin related problem occurs.
Allows to exit the application.
Definition: Exiter.java:30
final WindowListener windowListener
The window listener attached to this frame.
Definition: JXCWindow.java:196
void addCrossfireQueryListener(@NotNull CrossfireQueryListener listener)
Adds a new listener monitoring the query S->C messages.
final CrossfireQueryListener crossfireQueryListener
The CrossfireQueryListener attached to server.
Definition: JXCWindow.java:243
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
void saveOptions()
Saves all options' states to the backing settings instance.
Maintains the pending (ncom) commands sent to the server.
void showCharacters(final int count)
Displays the window with the characters for an account.
final JFrame frame
The main window.
Definition: JXCWindow.java:126
Information about JXClient's screen/window resolution.
Definition: Resolution.java:35
Loader for JXCSkins and attaching them to the client.
Definition: SkinLoader.java:59
void closeQueryDialog()
Closes the "query" dialog.
Command line argument parser.
Definition: Options.java:34
final JXCWindowRenderer windowRenderer
The JXCWindowRenderer for this window.
Definition: JXCWindow.java:102
Interface for listeners interested in query messages received from the Crossfire server.
final CharacterModel characterModel
The CharacterModel to update.
Definition: JXCWindow.java:120
Connection progress states of the Crossfire server connection.
void add(@NotNull final CharacterInformation characterInformation)
Adds an entry.
final CrossfireUpdateItemListener crossfireUpdateItemListener
The CrossfireUpdateItemListener to receive item updates.
Definition: JXCWindow.java:154
void manageAccount()
Displays the main account dialog, to let the player login or create a new account.
void keyReleased(@NotNull final KeyEvent e)
Handles a "key released" event.
void removeCrossfireQueryListener(@NotNull CrossfireQueryListener listener)
Removes the given listener from the list of objects listening to the query S->C messages.
static final String DEFAULT_SKIN
The default skin name.
Definition: Options.java:117
Resolution getMinResolution()
Returns the minimal resolution of this skin.