chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 25 Jul 2013 15:57:52 +0200
branchchess
changeset 23 827f30fbdeab
parent 22 fb06534ab8db
child 26 b675be28fc49
permissions -rw-r--r--
Rendering the board with knockout
     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 java.util.Locale;
    27 import net.java.html.json.ComputedProperty;
    28 import net.java.html.json.Function;
    29 import net.java.html.json.Model;
    30 import net.java.html.json.Property;
    31 
    32 @Model(className="Board", properties={
    33     @Property(name = "rows", type = Row.class, array = true)
    34 })
    35 public class BoardModel {
    36  @Model(className="Row", properties = {
    37         @Property(name = "columns", type = Square.class, array = true)
    38     })
    39     static class RowsImpl {
    40     }
    41     
    42     enum PieceType {
    43         PAWN(5), ROCK(2), KNIGHT(4), BISHOP(3), QUEEN(1), KING(0);
    44         
    45         final int entityIndex;
    46         
    47         PieceType(int ei) {
    48             this.entityIndex = ei;
    49         }
    50         
    51         String computeEntity(ColorType color) {
    52             if (color == null) {
    53                 color = ColorType.WHITE;
    54             }
    55             int base;
    56             switch (color) {
    57                 case WHITE: base = 12; break;
    58                 case BLACK: base = 18; break;
    59                 default:
    60                     throw new AssertionError();
    61             }
    62             return "&#98" + String.valueOf(base + entityIndex) + ";";
    63         }
    64     }
    65     enum ColorType {
    66         WHITE, BLACK;
    67     }
    68     
    69     @Model(className="Square", properties = {
    70         @Property(name = "piece", type = PieceType.class),
    71         @Property(name = "pieceColor", type = ColorType.class),
    72         @Property(name = "color", type = ColorType.class),
    73         @Property(name = "x", type = int.class),
    74         @Property(name = "y", type = int.class),
    75     })
    76     static class PieceImpl {
    77         @Function static void changeToPawn(Square s) {
    78             s.setPiece(PieceType.PAWN);
    79         }
    80         
    81         @ComputedProperty static String pieceEntity(
    82             PieceType piece, ColorType pieceColor
    83         ) {
    84             if (piece == null) {
    85                 return "";
    86             }
    87             return piece.computeEntity(pieceColor);
    88         }
    89         
    90         @ComputedProperty static String squareColor(ColorType color) {
    91             if (color == null) {
    92                 return "";
    93             } else {
    94                 return color.toString().toLowerCase(Locale.US);
    95             }
    96         }
    97     }
    98     
    99     public static void initialize(String[] args) {
   100         Board b = createBoard();
   101         b.applyBindings();
   102     }
   103 
   104     private static Board createBoard() {
   105         Board b = new Board();
   106         for (int i = 8; i > 0; i--) {
   107             Row r = new Row();
   108             b.getRows().add(r);
   109             for (int j = 'A'; j <= 'H'; j++) {
   110                 Square s = new Square();
   111                 s.setX(j);
   112                 s.setY(i);
   113                 s.setColor((i + j) % 2 == 1 ? ColorType.WHITE : ColorType.BLACK);
   114                 r.getColumns().add(s);
   115                 if (i == 2) {
   116                     s.setPiece(PieceType.PAWN);
   117                     s.setPieceColor(ColorType.WHITE);
   118                 } else if (i == 7) {
   119                     s.setPiece(PieceType.PAWN);
   120                     s.setPieceColor(ColorType.BLACK);
   121                 } else if (i == 8 || i == 1) {
   122                     s.setPieceColor(i == 1 ? ColorType.WHITE : ColorType.BLACK);
   123                     PieceType t;
   124                     switch (j) {
   125                         case 'A': case 'H': t = PieceType.ROCK; break;
   126                         case 'B': case 'G': t = PieceType.KNIGHT; break;
   127                         case 'C': case 'F': t = PieceType.BISHOP; break;
   128                         case 'D': t = PieceType.QUEEN; break;
   129                         default: t = PieceType.KING; break;
   130                     }
   131                     s.setPiece(t);
   132                 }
   133             }
   134         }
   135         return b;
   136     }
   137 }