Crossfire JXClient, Trunk  R20561
DefaultKeyHandler.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 
30 import java.nio.ByteBuffer;
31 import org.jetbrains.annotations.NotNull;
32 
38 public class DefaultKeyHandler implements KeyHandlerListener {
39 
43  @NotNull
44  private final Exiter exiter;
45 
49  @NotNull
50  private final GuiManager guiManager;
51 
55  @NotNull
57 
61  @NotNull
62  private final Object semaphoreConnected = new Object();
63 
67  private boolean connected;
68 
73  @NotNull
74  @SuppressWarnings("FieldCanBeLocal")
76 
77  @Override
78  public void connecting() {
79  setConnected(true);
80  }
81 
82  @Override
83  public void connected() {
84  // ignore
85  }
86 
87  @Override
88  public void packetReceived(@NotNull final ByteBuffer packet) {
89  // ignore
90  }
91 
92  @Override
93  public void packetSent(@NotNull final byte[] buf, final int len) {
94  // ignore
95  }
96 
97  @Override
98  public void disconnecting(@NotNull final String reason, final boolean isError) {
99  // ignore
100  }
101 
102  @Override
103  public void disconnected(@NotNull final String reason) {
104  setConnected(false);
105  }
106 
107  };
108 
116  public DefaultKeyHandler(@NotNull final Exiter exiter, @NotNull final GuiManager guiManager, @NotNull final ServerConnection server, @NotNull final GuiStateManager guiStateManager) {
117  this.exiter = exiter;
118  this.guiManager = guiManager;
119  this.guiStateManager = guiStateManager;
120  server.addClientSocketListener(clientSocketListener);
121  }
122 
126  @Override
127  public void escPressed() {
128  if (guiStateManager.getGuiState() == GuiState.CONNECT_FAILED) {
129  guiStateManager.disconnect();
130  return;
131  }
132 
133  switch (guiManager.escPressed(isConnected())) {
134  case IGNORE:
135  break;
136 
137  case DISCONNECT:
138  guiStateManager.disconnect();
139  break;
140 
141  case QUIT:
142  exiter.terminate();
143  break;
144  }
145  }
146 
150  @Override
151  public void keyReleased() {
152  guiManager.closeKeybindDialog();
153  }
154 
159  private void setConnected(final boolean connected) {
160  synchronized (semaphoreConnected) {
161  this.connected = connected;
162  }
163  }
164 
169  private boolean isConnected() {
170  synchronized (semaphoreConnected) {
171  return connected;
172  }
173  }
174 
175 }
Interface for listeners interested in pressed ESC keys.
EscAction escPressed(final boolean connected)
The ESC key has been pressed.
GuiState getGuiState()
Returns the current GuiState.
void disconnect()
Disconnects from the Crossfire server.
void setConnected(final boolean connected)
Records whether a server connection is active.
final GuiManager guiManager
The GuiManager instance.
final Object semaphoreConnected
The synchronization object for accesses to connected.
final ClientSocketListener clientSocketListener
The ClientSocketListener used to detect connection state changes.
void keyReleased()
A key (but not ESC) has been released.
CONNECT_FAILED
Display a failed connection attempt.
Definition: GuiState.java:53
Maintains the application's main GUI state.
Definition: GuiManager.java:60
Interface for listeners interested in ClientSocket related events.
boolean isConnected()
Returns whether a server connection is active.
final GuiStateManager guiStateManager
The GuiStateManager instance.
void terminate()
Terminates the application.
Definition: Exiter.java:41
Allows to exit the application.
Definition: Exiter.java:30
A KeyHandlerListener which updates the state of a GuiManager.
boolean connected
Whether a server connection is active.
DefaultKeyHandler(@NotNull final Exiter exiter, @NotNull final GuiManager guiManager, @NotNull final ServerConnection server, @NotNull final GuiStateManager guiStateManager)
Creates a new instance.
void closeKeybindDialog()
Closes the keybinding dialog.