desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/KukTopComponent.java
2 * Quoridor server and related libraries
3 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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/.
19 package cz.xelfi.quoridor.desktop.sample;
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;
29 * Top component which displays the board.
31 @ConvertAsProperties(dtd = "-//cz.xelfi.quoridor.desktop.sample//Kuk//EN",
33 public final class KukTopComponent extends TopComponent {
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";
40 private String player;
42 public KukTopComponent() {
46 public void setGameId(String 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);
55 edit ? NbBundle.getMessage(KukTopComponent.class, "CTL_GamePlay",
56 g.getId().getWhite(), g.getId().getBlack()
58 NbBundle.getMessage(KukTopComponent.class, "CTL_Game",
59 g.getId().getWhite(), g.getId().getBlack()
63 NbBundle.getMessage(KukTopComponent.class, "HINT_KukTopComponent",
64 g.getId().getWhite(), g.getId().getBlack()
69 private static String currentPlayer(Game g) {
70 if (g.getId().getStatus() == GameStatus.whiteMove) {
71 return g.getId().getWhite();
73 if (g.getId().getStatus() == GameStatus.blackMove) {
74 return g.getId().getBlack();
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.
84 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
85 private void initComponents() {
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();
92 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(KukTopComponent.class, "KukTopComponent.jLabel1.text")); // NOI18N
94 boardPane1.addBoardListener(new cz.xelfi.quoridor.visidor.BoardListener() {
95 public void boardChanged(cz.xelfi.quoridor.visidor.BoardEvent evt) {
96 boardPane1BoardChanged(evt);
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);
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);
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)
126 .addComponent(boardPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
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))
137 }// </editor-fold>//GEN-END:initComponents
139 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
141 }//GEN-LAST:event_jButton2ActionPerformed
143 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
145 }//GEN-LAST:event_jButton1ActionPerformed
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());
152 boardPane1.setEditable(false);
154 }//GEN-LAST:event_boardPane1BoardChanged
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
164 public int getPersistenceType() {
165 return TopComponent.PERSISTENCE_ALWAYS;
169 public void componentOpened() {
170 // TODO add custom code on component opening
174 public void componentClosed() {
175 // TODO add custom code on component closing
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");
183 p.setProperty("id", id);
187 final void readProperties(java.util.Properties p) {
188 String version = p.getProperty("version");
189 String myId = p.getProperty("id");
196 protected String preferredID() {