desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/KukTopComponent.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 17 Sep 2010 05:52:39 -0700
branchdesktop
changeset 269 430ab68846fa
parent 268 13fe01081e23
permissions -rw-r--r--
Make the boards editable and submit moves to the quoridor server
     1 /**
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program 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 this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 
    19 package cz.xelfi.quoridor.desktop.sample;
    20 
    21 import cz.xelfi.quoridor.webidor.Game;
    22 import cz.xelfi.quoridor.webidor.GameStatus;
    23 import org.openide.util.NbBundle;
    24 import org.openide.windows.TopComponent;
    25 //import org.openide.util.ImageUtilities;
    26 import org.netbeans.api.settings.ConvertAsProperties;
    27 
    28 /**
    29  * Top component which displays the board.
    30  */
    31 @ConvertAsProperties(dtd = "-//cz.xelfi.quoridor.desktop.sample//Kuk//EN",
    32 autostore = false)
    33 public final class KukTopComponent extends TopComponent {
    34 
    35     /** path to the icon used by the component and its open action */
    36 //    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    37     private static final String PREFERRED_ID = "KukTopComponent";
    38 
    39     private String id;
    40     private String player;
    41 
    42     public KukTopComponent() {
    43         initComponents();
    44     }
    45     
    46     public void setGameId(String id) {
    47         this.id = id;
    48         final Game g = Quoridor.getDefault().getGame(id);
    49         boardPane1.setBoard(g.getBoard());
    50         player = currentPlayer(g);
    51         final boolean edit = Quoridor.getDefault().user().equals(player);
    52         boardPane1.setEditable(edit);
    53         setName(id);
    54         setHtmlDisplayName(
    55             edit ? NbBundle.getMessage(KukTopComponent.class, "CTL_GamePlay",
    56                 g.getId().getWhite(), g.getId().getBlack()
    57             ) :
    58             NbBundle.getMessage(KukTopComponent.class, "CTL_Game",
    59                 g.getId().getWhite(), g.getId().getBlack()
    60             )
    61         );
    62         setToolTipText(
    63             NbBundle.getMessage(KukTopComponent.class, "HINT_KukTopComponent",
    64                 g.getId().getWhite(), g.getId().getBlack()
    65             )
    66         );
    67     }
    68     
    69     private static String currentPlayer(Game g) {
    70         if (g.getId().getStatus() == GameStatus.whiteMove) {
    71             return g.getId().getWhite();
    72         }
    73         if (g.getId().getStatus() == GameStatus.blackMove) {
    74             return g.getId().getBlack();
    75         }
    76         return null;
    77     }
    78 
    79     /** This method is called from within the constructor to
    80      * initialize the form.
    81      * WARNING: Do NOT modify this code. The content of this method is
    82      * always regenerated by the Form Editor.
    83      */
    84     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    85     private void initComponents() {
    86 
    87         jLabel1 = new javax.swing.JLabel();
    88         boardPane1 = new cz.xelfi.quoridor.visidor.BoardPane();
    89         jButton1 = new javax.swing.JButton();
    90         jButton2 = new javax.swing.JButton();
    91 
    92         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(KukTopComponent.class, "KukTopComponent.jLabel1.text")); // NOI18N
    93 
    94         boardPane1.addBoardListener(new cz.xelfi.quoridor.visidor.BoardListener() {
    95             public void boardChanged(cz.xelfi.quoridor.visidor.BoardEvent evt) {
    96                 boardPane1BoardChanged(evt);
    97             }
    98         });
    99 
   100         jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/xelfi/quoridor/desktop/sample/black.png"))); // NOI18N
   101         org.openide.awt.Mnemonics.setLocalizedText(jButton1, org.openide.util.NbBundle.getMessage(KukTopComponent.class, "KukTopComponent.jButton1.text")); // NOI18N
   102         jButton1.addActionListener(new java.awt.event.ActionListener() {
   103             public void actionPerformed(java.awt.event.ActionEvent evt) {
   104                 jButton1ActionPerformed(evt);
   105             }
   106         });
   107 
   108         jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/xelfi/quoridor/desktop/sample/white.png"))); // NOI18N
   109         org.openide.awt.Mnemonics.setLocalizedText(jButton2, org.openide.util.NbBundle.getMessage(KukTopComponent.class, "KukTopComponent.jButton2.text")); // NOI18N
   110         jButton2.addActionListener(new java.awt.event.ActionListener() {
   111             public void actionPerformed(java.awt.event.ActionEvent evt) {
   112                 jButton2ActionPerformed(evt);
   113             }
   114         });
   115 
   116         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
   117         this.setLayout(layout);
   118         layout.setHorizontalGroup(
   119             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   120             .addGroup(layout.createSequentialGroup()
   121                 .addComponent(jButton1)
   122                 .addContainerGap(225, Short.MAX_VALUE))
   123             .addGroup(layout.createSequentialGroup()
   124                 .addComponent(jButton2)
   125                 .addContainerGap())
   126             .addComponent(boardPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
   127         );
   128         layout.setVerticalGroup(
   129             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   130             .addGroup(layout.createSequentialGroup()
   131                 .addComponent(jButton1)
   132                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   133                 .addComponent(boardPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
   134                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   135                 .addComponent(jButton2))
   136         );
   137     }// </editor-fold>//GEN-END:initComponents
   138 
   139     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
   140         setGameId(id);
   141     }//GEN-LAST:event_jButton2ActionPerformed
   142 
   143     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
   144         setGameId(id);
   145     }//GEN-LAST:event_jButton1ActionPerformed
   146 
   147     private void boardPane1BoardChanged(cz.xelfi.quoridor.visidor.BoardEvent evt) {//GEN-FIRST:event_boardPane1BoardChanged
   148         if (player != null) {
   149             Quoridor.getDefault().move(id, player, evt.getMove());
   150             setGameId(id);
   151         } else {
   152             boardPane1.setEditable(false);
   153         }
   154     }//GEN-LAST:event_boardPane1BoardChanged
   155 
   156     // Variables declaration - do not modify//GEN-BEGIN:variables
   157     private cz.xelfi.quoridor.visidor.BoardPane boardPane1;
   158     private javax.swing.JButton jButton1;
   159     private javax.swing.JButton jButton2;
   160     private javax.swing.JLabel jLabel1;
   161     // End of variables declaration//GEN-END:variables
   162 
   163     @Override
   164     public int getPersistenceType() {
   165         return TopComponent.PERSISTENCE_ALWAYS;
   166     }
   167 
   168     @Override
   169     public void componentOpened() {
   170         // TODO add custom code on component opening
   171     }
   172 
   173     @Override
   174     public void componentClosed() {
   175         // TODO add custom code on component closing
   176     }
   177 
   178     void writeProperties(java.util.Properties p) {
   179         // better to version settings since initial version as advocated at
   180         // http://wiki.apidesign.org/wiki/PropertyFiles
   181         p.setProperty("version", "1.0");
   182         if (id != null) {
   183             p.setProperty("id", id);
   184         }
   185     }
   186 
   187     final void readProperties(java.util.Properties p) {
   188         String version = p.getProperty("version");
   189         String myId = p.getProperty("id");
   190         if (myId != null) {
   191             setGameId(myId);
   192         }
   193     }
   194 
   195     @Override
   196     protected String preferredID() {
   197         return PREFERRED_ID;
   198     }
   199 }