View Javadoc

1   /*
2    @See License.txt@
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      /* Spellcast View Inteface */
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  * if ( Server.getServer() != null ) { int choice =
174  * JOptionPane.showConfirmDialog( this, "Warning: A Server Is Already Running\n" +
175  * "Do you want to stop it and start a new server?", "Spellcast: Server Already
176  * Running", JOptionPane.WARNING_MESSAGE, JOptionPane.YES_NO_OPTION); if (
177  * choice == JOptionPane.YES_OPTION ) { Server.getServer().stop(); } else {
178  * return; } }
179  *
180  * int port = TextUtil.getPort( serverAddressString ); if ( port == -1 ) { port =
181  * NetConstants.SPELLCAST_NET_DEFAULT_PORT; }
182  *
183  * Game theGame = new Game( theGameName ); try { InetAddress bindAddress =
184  * InetAddress.getByName( TextUtil.getIPAddress( serverAddressString ) );
185  * Server.createServer( bindAddress, port, theGame );
186  * saveMostRecentUsedGameNames( c_gameName.getModel() ); cat.info( "Server
187  * started:\n" + bindAddress.getHostAddress() + ":" + port + "\n" +
188  * theGame.getName() ); } catch ( UnknownHostException uhe ) {
189  * JOptionPane.showMessageDialog( this, "Error: Unknown Host: " +
190  * uhe.getMessage(), "Spellcast: Error", JOptionPane.ERROR_MESSAGE ); cat.warn(
191  * "Could start server.", uhe ); } catch ( ServerException e ) {
192  * JOptionPane.showMessageDialog( this, "Error: Could not start server: " +
193  * e.getMessage(), "Spellcast: Error", JOptionPane.ERROR_MESSAGE ); cat.warn(
194  * "Could start server.", e ); }
195  */