chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 25 Jul 2013 15:09:49 +0200
branchchess
changeset 22 fb06534ab8db
child 23 827f30fbdeab
permissions -rw-r--r--
Initial version of the chess board displaying application
     1 /**
     2  * The MIT License (MIT)
     3  *
     4  * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     5  *
     6  * Permission is hereby granted, free of charge, to any person obtaining a copy
     7  * of this software and associated documentation files (the "Software"), to deal
     8  * in the Software without restriction, including without limitation the rights
     9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10  * copies of the Software, and to permit persons to whom the Software is
    11  * furnished to do so, subject to the following conditions:
    12  *
    13  * The above copyright notice and this permission notice shall be included in
    14  * all copies or substantial portions of the Software.
    15  *
    16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    22  * THE SOFTWARE.
    23  */
    24 package org.apidesign.html.demo.chess;
    25 
    26 import net.java.html.json.ComputedProperty;
    27 import net.java.html.json.Function;
    28 import net.java.html.json.Model;
    29 import net.java.html.json.Property;
    30 
    31 @Model(className="Board", properties={
    32     @Property(name = "rows", type = Row.class, array = true)
    33 })
    34 public class BoardModel {
    35  @Model(className="Row", properties = {
    36         @Property(name = "columns", type = Square.class, array = true)
    37     })
    38     static class RowsImpl {
    39     }
    40     
    41     enum PieceType {
    42         PAWN, ROCK, KNIGHT, BISHOP, QUEEN, KING;
    43     }
    44     enum ColorType {
    45         WHITE, BLACK;
    46     }
    47     
    48     @Model(className="Square", properties = {
    49         @Property(name = "piece", type = PieceType.class),
    50         @Property(name = "pieceColor", type = ColorType.class),
    51         @Property(name = "color", type = ColorType.class),
    52         @Property(name = "x", type = int.class),
    53         @Property(name = "y", type = int.class),
    54     })
    55     static class PieceImpl {
    56         @Function static void changeToPawn(Square s) {
    57             s.setPiece(PieceType.PAWN);
    58         }
    59         
    60         @ComputedProperty static String imageURL(PieceType piece) {
    61             if (piece == null) {
    62                 return "";
    63             }
    64             if (piece == PieceType.BISHOP) {
    65                 return "bishop.png";
    66             }
    67             return "knight.jpg";
    68         }
    69     }
    70     
    71     public static void initialize(String[] args) {
    72         Board b = createBoard();
    73         b.applyBindings();
    74     }
    75 
    76     private static Board createBoard() {
    77         Board b = new Board();
    78         for (int i = 8; i > 0; i--) {
    79             Row r = new Row();
    80             b.getRows().add(r);
    81             for (int j = 'A'; j <= 'H'; j++) {
    82                 Square s = new Square();
    83                 s.setX(j);
    84                 s.setY(i);
    85                 s.setColor((i + j) % 2 == 0 ? ColorType.WHITE : ColorType.BLACK);
    86                 r.getColumns().add(s);
    87                 if (i == 2) {
    88                     s.setPiece(PieceType.PAWN);
    89                     s.setPieceColor(ColorType.WHITE);
    90                 } else if (i == 7) {
    91                     s.setPiece(PieceType.PAWN);
    92                     s.setPieceColor(ColorType.BLACK);
    93                 } else if (i == 8 || i == 1) {
    94                     s.setColor(i == 1 ? ColorType.WHITE : ColorType.BLACK);
    95                     PieceType t;
    96                     switch (j) {
    97                         case 'A': case 'H': t = PieceType.ROCK; break;
    98                         case 'B': case 'G': t = PieceType.KNIGHT; break;
    99                         case 'C': case 'F': t = PieceType.BISHOP; break;
   100                         case 'D': t = PieceType.QUEEN; break;
   101                         default: t = PieceType.KING; break;
   102                     }
   103                     s.setPiece(t);
   104                 }
   105             }
   106         }
   107         return b;
   108     }
   109 }