Make the boards editable and submit moves to the quoridor server desktop
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 17 Sep 2010 05:52:39 -0700
branchdesktop
changeset 269430ab68846fa
parent 268 13fe01081e23
child 270 45b57eaafbb7
Make the boards editable and submit moves to the quoridor server
desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/KukTopComponent.form
desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/KukTopComponent.java
desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/Quoridor.java
desktop/desktop-sample/src/main/resources/cz/xelfi/quoridor/desktop/sample/Bundle.properties
     1.1 --- a/desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/KukTopComponent.form	Tue Sep 14 09:58:04 2010 +0200
     1.2 +++ b/desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/KukTopComponent.form	Fri Sep 17 05:52:39 2010 -0700
     1.3 @@ -50,6 +50,9 @@
     1.4    </Layout>
     1.5    <SubComponents>
     1.6      <Component class="cz.xelfi.quoridor.visidor.BoardPane" name="boardPane1">
     1.7 +      <Events>
     1.8 +        <EventHandler event="boardChanged" listener="cz.xelfi.quoridor.visidor.BoardListener" parameters="cz.xelfi.quoridor.visidor.BoardEvent" handler="boardPane1BoardChanged"/>
     1.9 +      </Events>
    1.10      </Component>
    1.11      <Component class="javax.swing.JButton" name="jButton1">
    1.12        <Properties>
     2.1 --- a/desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/KukTopComponent.java	Tue Sep 14 09:58:04 2010 +0200
     2.2 +++ b/desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/KukTopComponent.java	Fri Sep 17 05:52:39 2010 -0700
     2.3 @@ -19,6 +19,7 @@
     2.4  package cz.xelfi.quoridor.desktop.sample;
     2.5  
     2.6  import cz.xelfi.quoridor.webidor.Game;
     2.7 +import cz.xelfi.quoridor.webidor.GameStatus;
     2.8  import org.openide.util.NbBundle;
     2.9  import org.openide.windows.TopComponent;
    2.10  //import org.openide.util.ImageUtilities;
    2.11 @@ -36,6 +37,7 @@
    2.12      private static final String PREFERRED_ID = "KukTopComponent";
    2.13  
    2.14      private String id;
    2.15 +    private String player;
    2.16  
    2.17      public KukTopComponent() {
    2.18          initComponents();
    2.19 @@ -45,8 +47,15 @@
    2.20          this.id = id;
    2.21          final Game g = Quoridor.getDefault().getGame(id);
    2.22          boardPane1.setBoard(g.getBoard());
    2.23 -        setName(
    2.24 -            NbBundle.getMessage(KukTopComponent.class, "CTL_KukTopComponent",
    2.25 +        player = currentPlayer(g);
    2.26 +        final boolean edit = Quoridor.getDefault().user().equals(player);
    2.27 +        boardPane1.setEditable(edit);
    2.28 +        setName(id);
    2.29 +        setHtmlDisplayName(
    2.30 +            edit ? NbBundle.getMessage(KukTopComponent.class, "CTL_GamePlay",
    2.31 +                g.getId().getWhite(), g.getId().getBlack()
    2.32 +            ) :
    2.33 +            NbBundle.getMessage(KukTopComponent.class, "CTL_Game",
    2.34                  g.getId().getWhite(), g.getId().getBlack()
    2.35              )
    2.36          );
    2.37 @@ -56,6 +65,16 @@
    2.38              )
    2.39          );
    2.40      }
    2.41 +    
    2.42 +    private static String currentPlayer(Game g) {
    2.43 +        if (g.getId().getStatus() == GameStatus.whiteMove) {
    2.44 +            return g.getId().getWhite();
    2.45 +        }
    2.46 +        if (g.getId().getStatus() == GameStatus.blackMove) {
    2.47 +            return g.getId().getBlack();
    2.48 +        }
    2.49 +        return null;
    2.50 +    }
    2.51  
    2.52      /** This method is called from within the constructor to
    2.53       * initialize the form.
    2.54 @@ -72,6 +91,12 @@
    2.55  
    2.56          org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(KukTopComponent.class, "KukTopComponent.jLabel1.text")); // NOI18N
    2.57  
    2.58 +        boardPane1.addBoardListener(new cz.xelfi.quoridor.visidor.BoardListener() {
    2.59 +            public void boardChanged(cz.xelfi.quoridor.visidor.BoardEvent evt) {
    2.60 +                boardPane1BoardChanged(evt);
    2.61 +            }
    2.62 +        });
    2.63 +
    2.64          jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/xelfi/quoridor/desktop/sample/black.png"))); // NOI18N
    2.65          org.openide.awt.Mnemonics.setLocalizedText(jButton1, org.openide.util.NbBundle.getMessage(KukTopComponent.class, "KukTopComponent.jButton1.text")); // NOI18N
    2.66          jButton1.addActionListener(new java.awt.event.ActionListener() {
    2.67 @@ -119,6 +144,15 @@
    2.68          setGameId(id);
    2.69      }//GEN-LAST:event_jButton1ActionPerformed
    2.70  
    2.71 +    private void boardPane1BoardChanged(cz.xelfi.quoridor.visidor.BoardEvent evt) {//GEN-FIRST:event_boardPane1BoardChanged
    2.72 +        if (player != null) {
    2.73 +            Quoridor.getDefault().move(id, player, evt.getMove());
    2.74 +            setGameId(id);
    2.75 +        } else {
    2.76 +            boardPane1.setEditable(false);
    2.77 +        }
    2.78 +    }//GEN-LAST:event_boardPane1BoardChanged
    2.79 +
    2.80      // Variables declaration - do not modify//GEN-BEGIN:variables
    2.81      private cz.xelfi.quoridor.visidor.BoardPane boardPane1;
    2.82      private javax.swing.JButton jButton1;
     3.1 --- a/desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/Quoridor.java	Tue Sep 14 09:58:04 2010 +0200
     3.2 +++ b/desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/Quoridor.java	Fri Sep 17 05:52:39 2010 -0700
     3.3 @@ -22,6 +22,7 @@
     3.4  import com.sun.jersey.api.client.GenericType;
     3.5  import com.sun.jersey.api.client.UniformInterfaceException;
     3.6  import com.sun.jersey.api.client.WebResource;
     3.7 +import cz.xelfi.quoridor.Move;
     3.8  import cz.xelfi.quoridor.webidor.Game;
     3.9  import cz.xelfi.quoridor.webidor.GameId;
    3.10  import java.util.List;
    3.11 @@ -50,12 +51,16 @@
    3.12  
    3.13      public abstract List<GameId> listGames();
    3.14      public abstract Game getGame(String id);
    3.15 +    public abstract String user();
    3.16  
    3.17      public abstract boolean login(String login, String password);
    3.18  
    3.19 +    public abstract void move(String id, String player, Move move);
    3.20 +
    3.21      private static class Impl extends Quoridor {
    3.22          private final WebResource wr;
    3.23          private String id = "";
    3.24 +        private String login = "";
    3.25          private static final Logger LOG = Logger.getLogger(Impl.class.getName());
    3.26  
    3.27          public Impl() {
    3.28 @@ -84,6 +89,9 @@
    3.29              try {
    3.30                  id = wr.path("login").queryParam("name", login).
    3.31                      queryParam("password", password).put(String.class);
    3.32 +                if (id != null) {
    3.33 +                    this.login = login;
    3.34 +                }
    3.35                  return id != null;
    3.36              } catch (UniformInterfaceException ex) {
    3.37                  LOG.log(Level.INFO, "Cannot login", ex);
    3.38 @@ -91,5 +99,19 @@
    3.39              }
    3.40          }
    3.41  
    3.42 +        @Override
    3.43 +        public String user() {
    3.44 +            return login;
    3.45 +        }
    3.46 +
    3.47 +        @Override
    3.48 +        public void move(String game, String player, Move move) {
    3.49 +            String s = wr.path("games").path(game).queryParam("loginID", id).
    3.50 +                queryParam("player", player).
    3.51 +                queryParam("move", move.toString()).
    3.52 +                    accept(MediaType.TEXT_XML).put(String.class);
    3.53 +            System.err.println("s: " + s);
    3.54 +        }
    3.55 +
    3.56      }
    3.57  }
     4.1 --- a/desktop/desktop-sample/src/main/resources/cz/xelfi/quoridor/desktop/sample/Bundle.properties	Tue Sep 14 09:58:04 2010 +0200
     4.2 +++ b/desktop/desktop-sample/src/main/resources/cz/xelfi/quoridor/desktop/sample/Bundle.properties	Fri Sep 17 05:52:39 2010 -0700
     4.3 @@ -18,7 +18,8 @@
     4.4  
     4.5  CTL_Hello=Hello
     4.6  CTL_KukAction=Kuk
     4.7 -CTL_KukTopComponent={0}/{1}
     4.8 +CTL_Game={0}/{1}
     4.9 +CTL_GamePlay=<html><b>{0}/{1}</b></html>
    4.10  CTL_LoginAction=Login
    4.11  CTL_OpenGameAction=Open a game
    4.12  CTL_ShowGames=Show Games