desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/KukTopComponent.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 09:58:04 +0200
branchdesktop
changeset 268 13fe01081e23
parent 261 cf7e6b8259e1
child 269 430ab68846fa
permissions -rw-r--r--
Updating licenses in desktop modules
     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 org.openide.util.NbBundle;
    23 import org.openide.windows.TopComponent;
    24 //import org.openide.util.ImageUtilities;
    25 import org.netbeans.api.settings.ConvertAsProperties;
    26 
    27 /**
    28  * Top component which displays the board.
    29  */
    30 @ConvertAsProperties(dtd = "-//cz.xelfi.quoridor.desktop.sample//Kuk//EN",
    31 autostore = false)
    32 public final class KukTopComponent extends TopComponent {
    33 
    34     /** path to the icon used by the component and its open action */
    35 //    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    36     private static final String PREFERRED_ID = "KukTopComponent";
    37 
    38     private String id;
    39 
    40     public KukTopComponent() {
    41         initComponents();
    42     }
    43     
    44     public void setGameId(String id) {
    45         this.id = id;
    46         final Game g = Quoridor.getDefault().getGame(id);
    47         boardPane1.setBoard(g.getBoard());
    48         setName(
    49             NbBundle.getMessage(KukTopComponent.class, "CTL_KukTopComponent",
    50                 g.getId().getWhite(), g.getId().getBlack()
    51             )
    52         );
    53         setToolTipText(
    54             NbBundle.getMessage(KukTopComponent.class, "HINT_KukTopComponent",
    55                 g.getId().getWhite(), g.getId().getBlack()
    56             )
    57         );
    58     }
    59 
    60     /** This method is called from within the constructor to
    61      * initialize the form.
    62      * WARNING: Do NOT modify this code. The content of this method is
    63      * always regenerated by the Form Editor.
    64      */
    65     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    66     private void initComponents() {
    67 
    68         jLabel1 = new javax.swing.JLabel();
    69         boardPane1 = new cz.xelfi.quoridor.visidor.BoardPane();
    70         jButton1 = new javax.swing.JButton();
    71         jButton2 = new javax.swing.JButton();
    72 
    73         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(KukTopComponent.class, "KukTopComponent.jLabel1.text")); // NOI18N
    74 
    75         jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/xelfi/quoridor/desktop/sample/black.png"))); // NOI18N
    76         org.openide.awt.Mnemonics.setLocalizedText(jButton1, org.openide.util.NbBundle.getMessage(KukTopComponent.class, "KukTopComponent.jButton1.text")); // NOI18N
    77         jButton1.addActionListener(new java.awt.event.ActionListener() {
    78             public void actionPerformed(java.awt.event.ActionEvent evt) {
    79                 jButton1ActionPerformed(evt);
    80             }
    81         });
    82 
    83         jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/xelfi/quoridor/desktop/sample/white.png"))); // NOI18N
    84         org.openide.awt.Mnemonics.setLocalizedText(jButton2, org.openide.util.NbBundle.getMessage(KukTopComponent.class, "KukTopComponent.jButton2.text")); // NOI18N
    85         jButton2.addActionListener(new java.awt.event.ActionListener() {
    86             public void actionPerformed(java.awt.event.ActionEvent evt) {
    87                 jButton2ActionPerformed(evt);
    88             }
    89         });
    90 
    91         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    92         this.setLayout(layout);
    93         layout.setHorizontalGroup(
    94             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    95             .addGroup(layout.createSequentialGroup()
    96                 .addComponent(jButton1)
    97                 .addContainerGap(225, Short.MAX_VALUE))
    98             .addGroup(layout.createSequentialGroup()
    99                 .addComponent(jButton2)
   100                 .addContainerGap())
   101             .addComponent(boardPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
   102         );
   103         layout.setVerticalGroup(
   104             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   105             .addGroup(layout.createSequentialGroup()
   106                 .addComponent(jButton1)
   107                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   108                 .addComponent(boardPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
   109                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   110                 .addComponent(jButton2))
   111         );
   112     }// </editor-fold>//GEN-END:initComponents
   113 
   114     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
   115         setGameId(id);
   116     }//GEN-LAST:event_jButton2ActionPerformed
   117 
   118     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
   119         setGameId(id);
   120     }//GEN-LAST:event_jButton1ActionPerformed
   121 
   122     // Variables declaration - do not modify//GEN-BEGIN:variables
   123     private cz.xelfi.quoridor.visidor.BoardPane boardPane1;
   124     private javax.swing.JButton jButton1;
   125     private javax.swing.JButton jButton2;
   126     private javax.swing.JLabel jLabel1;
   127     // End of variables declaration//GEN-END:variables
   128 
   129     @Override
   130     public int getPersistenceType() {
   131         return TopComponent.PERSISTENCE_ALWAYS;
   132     }
   133 
   134     @Override
   135     public void componentOpened() {
   136         // TODO add custom code on component opening
   137     }
   138 
   139     @Override
   140     public void componentClosed() {
   141         // TODO add custom code on component closing
   142     }
   143 
   144     void writeProperties(java.util.Properties p) {
   145         // better to version settings since initial version as advocated at
   146         // http://wiki.apidesign.org/wiki/PropertyFiles
   147         p.setProperty("version", "1.0");
   148         if (id != null) {
   149             p.setProperty("id", id);
   150         }
   151     }
   152 
   153     final void readProperties(java.util.Properties p) {
   154         String version = p.getProperty("version");
   155         String myId = p.getProperty("id");
   156         if (myId != null) {
   157             setGameId(myId);
   158         }
   159     }
   160 
   161     @Override
   162     protected String preferredID() {
   163         return PREFERRED_ID;
   164     }
   165 }