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