1
2
3
4
5 package spellcast.client;
6
7 import java.beans.PropertyChangeEvent;
8 import java.beans.PropertyChangeListener;
9
10 import spellcast.beings.IWizard;
11 import spellcast.event.DisconnectionRequestEvent;
12 import spellcast.event.MessageEvent;
13 import spellcast.event.OkEvent;
14 import spellcast.game.Game;
15 import spellcast.model.Id;
16 import spellcast.net.VersionDetails;
17 import spellcast.questions.Question;
18 import spellcast.ui.ConnectToServerEvent;
19 import spellcast.ui.SpellcastView;
20 import spellcast.ui.SpellcastWindow;
21 import spellcast.ui.UIProperties;
22
23 /***
24 * The Client class for Spellcast. This is a Facade View. It accepts user input
25 * via property change events and forwards the corresponding network event to
26 * the ClientConnectionHandler. All network input is received by the
27 * ClientConnectionHandler and dispatched to this class via
28 * <code>SpellcastView</code> calls. If a gui has been installed then the gui
29 * is notified of all SpellcastView calls.
30 *
31 * @author Barrie Treloar
32 */
33 public class Client implements SpellcastView, PropertyChangeListener {
34 public static final VersionDetails programVersionDetails = new VersionDetails(
35 "Spellcast Client", 0, 0, 1);
36
37 private Id id = Id.NO_ONE;
38
39 private ClientConnectionHandler clientConnectionHandler;
40
41 private SpellcastWindow gui;
42
43 public Client() {
44 clientConnectionHandler = new ClientConnectionHandler(this,
45 programVersionDetails);
46 cengine stateMachine = new cengine(this, clientConnectionHandler);
47 Thread stateMachineThread = new Thread(stateMachine,
48 "ClientStateMachine");
49 stateMachineThread.start();
50 }
51
52 public void createGUI() {
53 gui = new SpellcastWindow();
54 gui.addPropertyChangeListener(this);
55 }
56
57 public void setVisible(boolean visible) {
58 if (gui != null) {
59 gui.setVisible(visible);
60 }
61 }
62
63 /***
64 * Listen for property changes from the GUI
65 */
66 public void propertyChange(PropertyChangeEvent evt) {
67 if (evt.getPropertyName().equals(UIProperties.MESSAGE_PROP)) {
68 String message = (String) evt.getNewValue();
69 clientConnectionHandler.send(new MessageEvent(message));
70 } else if (evt.getPropertyName().equals(UIProperties.OK_PROP)) {
71 clientConnectionHandler.send((OkEvent) evt.getNewValue());
72 } else if (evt.getPropertyName().equals(UIProperties.CONNECT_PROP)) {
73 ConnectToServerEvent ctse = (ConnectToServerEvent) evt
74 .getNewValue();
75 clientConnectionHandler.connect(ctse.getServerAddress(), ctse
76 .getWizardName(), ctse.getGender());
77 } else if (evt.getPropertyName().equals(UIProperties.DISCONNECT_PROP)) {
78 clientConnectionHandler.send(new DisconnectionRequestEvent());
79 }
80 }
81
82
83
84
85 /***
86 * Add a message to the view.
87 */
88 public void addMessage(String message) {
89 if (gui != null) {
90 gui.addMessage(message);
91 }
92 }
93
94 /***
95 * Add a query to the view.
96 */
97 public void addQuery(Question query) {
98 if (gui != null) {
99 gui.addQuery(query);
100 }
101 }
102
103 /***
104 * Add a wizard to the view.
105 */
106 public void addWizard(IWizard w) {
107 if (gui != null) {
108 gui.addWizard(w);
109 }
110 }
111
112 /***
113 * Remove a wizard from the view.
114 */
115 public void removeWizard(IWizard w) {
116 if (gui != null) {
117 gui.removeWizard(w);
118 }
119 }
120
121 /***
122 * Indicate that the system has connected to a Spellcast server. A
123 * successful connection will pass true, otherwise false is used.
124 *
125 * @param success
126 * indicates whether the connection was success (true), or
127 * unsuccessful (false)
128 */
129 public void connected(boolean success) {
130 if (gui != null) {
131 gui.connected(success);
132 }
133 }
134
135 /***
136 * Indicate that the system has disconnected from a Spellcast server.
137 */
138 public void disconnected() {
139 if (gui != null) {
140 gui.disconnected();
141 }
142 }
143
144 /***
145 * Set the value of id.
146 *
147 * @param v
148 * Value to assign to id.
149 */
150 public void setID(Id v) {
151 id = v;
152 if (gui != null) {
153 gui.setID(v);
154 }
155 }
156
157 /***
158 * Set the value of the current Game.
159 *
160 * @param v
161 * Value to assign to game.
162 */
163 public void setGame(Game v) {
164 if (gui != null) {
165 gui.setGame(v);
166 }
167 }
168
169 }
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195