00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.guistate;
00023
00024 import com.realtime.crossfire.jxclient.util.EventListenerList2;
00025 import org.jetbrains.annotations.NotNull;
00026 import org.jetbrains.annotations.Nullable;
00027
00033 public class GuiStateManager {
00034
00038 @Nullable
00039 private GuiState guiState = null;
00040
00044 @NotNull
00045 private final Object sync = new Object();
00046
00050 @NotNull
00051 private final EventListenerList2<GuiStateListener> guiStateListeners = new EventListenerList2<GuiStateListener>(GuiStateListener.class);
00052
00057 public void changeGUI(final GuiState guiState) {
00058 synchronized (sync) {
00059 if (this.guiState == guiState) {
00060 return;
00061 }
00062
00063 this.guiState = guiState;
00064
00065 switch (guiState) {
00066 case START:
00067 for (final GuiStateListener listener : guiStateListeners.getListeners()) {
00068 listener.start();
00069 }
00070 break;
00071
00072 case METASERVER:
00073 for (final GuiStateListener listener : guiStateListeners.getListeners()) {
00074 listener.metaserver();
00075 }
00076 break;
00077
00078 case CONNECTING:
00079 throw new IllegalArgumentException();
00080
00081 case CONNECTED:
00082 for (final GuiStateListener listener : guiStateListeners.getListeners()) {
00083 listener.connected();
00084 }
00085 break;
00086
00087 case CONNECT_FAILED:
00088 throw new IllegalArgumentException();
00089 }
00090 }
00091 }
00092
00098 private void changeGUI(@NotNull final GuiState guiState, @NotNull final String param) {
00099 synchronized (sync) {
00100 if (this.guiState == guiState) {
00101 return;
00102 }
00103
00104 this.guiState = guiState;
00105
00106 switch (guiState) {
00107 case START:
00108 case METASERVER:
00109 throw new IllegalArgumentException();
00110
00111 case CONNECTING:
00112 for (final GuiStateListener listener : guiStateListeners.getListeners()) {
00113 listener.preConnecting(param);
00114 }
00115 for (final GuiStateListener listener : guiStateListeners.getListeners()) {
00116 listener.connecting(param);
00117 }
00118 break;
00119
00120 case CONNECTED:
00121 throw new IllegalArgumentException();
00122
00123 case CONNECT_FAILED:
00124 for (final GuiStateListener listener : guiStateListeners.getListeners()) {
00125 listener.connectFailed(param);
00126 }
00127 break;
00128 }
00129 }
00130 }
00131
00136 @Nullable
00137 public GuiState getGuiState() {
00138 synchronized (sync) {
00139 return guiState;
00140 }
00141 }
00142
00147 public void addGuiStateListener(@NotNull final GuiStateListener listener) {
00148 guiStateListeners.add(listener);
00149 }
00150
00155 public void removeGuiStateListener(@NotNull final GuiStateListener listener) {
00156 guiStateListeners.remove(listener);
00157 }
00158
00163 public void connect(@NotNull final String serverInfo) {
00164 changeGUI(GuiState.CONNECTING, serverInfo);
00165 }
00166
00170 public void disconnect() {
00171 changeGUI(GuiState.METASERVER);
00172 }
00173
00178 public void setClientSocketState(@NotNull final ClientSocketState clientSocketState) {
00179 for (final GuiStateListener listener : guiStateListeners.getListeners()) {
00180 listener.connecting(clientSocketState);
00181 }
00182 if (clientSocketState == ClientSocketState.CONNECTED) {
00183 changeGUI(GuiState.CONNECTED);
00184 }
00185 }
00186
00192 public void disconnecting(@NotNull final String reason, final boolean isError) {
00193 synchronized (sync) {
00194 if (guiState == GuiState.CONNECTING || (isError && guiState == GuiState.CONNECTED)) {
00195 changeGUI(GuiState.CONNECT_FAILED, reason);
00196 }
00197 }
00198 }
00199
00203 public void disconnected() {
00204 synchronized (sync) {
00205 if (guiState != GuiState.CONNECT_FAILED) {
00206 changeGUI(GuiState.METASERVER);
00207 }
00208 }
00209 }
00210
00211 }