Crossfire JXClient, Trunk  R20561
GuiStateManager.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.guistate;
23 
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
28 
34 public class GuiStateManager {
35 
39  @Nullable
40  private GuiState guiState;
41 
45  @NotNull
46  private final Object sync = new Object();
47 
51  @NotNull
53 
58  public void changeGUI(@NotNull final GuiState guiState) {
59  synchronized (sync) {
60  if (this.guiState == guiState) {
61  return;
62  }
63 
64  this.guiState = guiState;
65  }
66 
68  switch (guiState) {
69  case START:
70  for (final GuiStateListener listener : guiStateListeners) {
71  listener.start();
72  }
73  break;
74 
75  case METASERVER:
76  for (final GuiStateListener listener : guiStateListeners) {
77  listener.metaserver();
78  }
79  break;
80 
81  case CONNECTING:
82  throw new IllegalArgumentException("changeGUI() called in state CONNECTING");
83 
84  case CONNECTED:
85  for (final GuiStateListener listener : guiStateListeners) {
86  listener.connected();
87  }
88  break;
89 
90  case CONNECT_FAILED:
91  throw new IllegalArgumentException("changeGUI() called in state CONNECT_FAILED");
92  }
93  });
94  }
95 
101  private void changeGUI(@NotNull final GuiState guiState, @NotNull final String param) {
102  synchronized (sync) {
103  if (this.guiState == guiState) {
104  return;
105  }
106 
107  this.guiState = guiState;
108 
109  switch (guiState) {
110  case START:
111  throw new IllegalArgumentException("changeGUI() called in state START");
112 
113  case METASERVER:
114  throw new IllegalArgumentException("changeGUI() called in state METASERVER");
115 
116  case CONNECTING:
117  for (final GuiStateListener listener : guiStateListeners) {
118  listener.preConnecting(param);
119  }
120  for (final GuiStateListener listener : guiStateListeners) {
121  listener.connecting(param);
122  }
123  break;
124 
125  case CONNECTED:
126  throw new IllegalArgumentException("changeGUI() called in state CONNECTED");
127 
128  case CONNECT_FAILED:
129  for (final GuiStateListener listener : guiStateListeners) {
130  listener.connectFailed(param);
131  }
132  break;
133  }
134  }
135  }
136 
141  @Nullable
143  synchronized (sync) {
144  return guiState;
145  }
146  }
147 
152  public void addGuiStateListener(@NotNull final GuiStateListener listener) {
153  guiStateListeners.add(listener);
154  }
155 
160  public void removeGuiStateListener(@NotNull final GuiStateListener listener) {
161  guiStateListeners.remove(listener);
162  }
163 
168  public void connect(@NotNull final String serverInfo) {
169  changeGUI(GuiState.CONNECTING, serverInfo);
170  }
171 
175  public void disconnect() {
177  }
178 
183  public void setClientSocketState(@NotNull final ClientSocketState clientSocketState) {
184  for (final GuiStateListener listener : guiStateListeners) {
185  listener.connecting(clientSocketState);
186  }
187  if (clientSocketState == ClientSocketState.CONNECTED) {
189  }
190  }
191 
197  public void disconnecting(@NotNull final String reason, final boolean isError) {
198  synchronized (sync) {
199  if (guiState == GuiState.CONNECTING || (isError && guiState == GuiState.CONNECTED)) {
201  }
202  }
203  }
204 
208  public void disconnected() {
209  synchronized (sync) {
210  if (guiState != GuiState.CONNECT_FAILED) {
212  }
213  }
214  }
215 
216 }
Interface for listeners interested gui state changes.
void addGuiStateListener(@NotNull final GuiStateListener listener)
Adds a gui state listener.
final EventListenerList2< GuiStateListener > guiStateListeners
The connection state listeners to notify.
void disconnected()
Called after the connection has been closed.
void changeGUI(@NotNull final GuiState guiState)
Sets a new GuiState.
GuiState getGuiState()
Returns the current GuiState.
void disconnect()
Disconnects from the Crossfire server.
void connect(@NotNull final String serverInfo)
Connects to a Crossfire server.
final Object sync
The synchronization object for accessing guiState.
CONNECTING
Display the screen while a server connection is established.
Definition: GuiState.java:43
CONNECT_FAILED
Display a failed connection attempt.
Definition: GuiState.java:53
void removeGuiStateListener(@NotNull final GuiStateListener listener)
Removes a gui state listener.
void disconnecting(@NotNull final String reason, final boolean isError)
Called when the connection is being teared down.
CONNECTED
Display the main playing screen.
Definition: GuiState.java:48
void add(@NotNull final T listener)
Adds a listener.
void setClientSocketState(@NotNull final ClientSocketState clientSocketState)
Sets the new ClientSocketState.
Utility class for Swing related functions.
void changeGUI(@NotNull final GuiState guiState, @NotNull final String param)
Sets a new GuiState.
METASERVER
Display the server selection screen.
Definition: GuiState.java:38
void remove(@NotNull final T listener)
Removes a listener.
Connection progress states of the Crossfire server connection.
static void invokeAndWait(@NotNull final Runnable runnable)
Calls SwingUtilities#invokeAndWait(Runnable) if not on the EDT or calls the Runnable directly if on t...