2 * The MIT License (MIT)
4 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 package org.apidesign.html.demo.chess;
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;
32 @Model(className="Board", properties={
33 @Property(name = "rows", type = Row.class, array = true)
35 public class BoardModel {
36 @Function static void selected(Board b, Square data) {
37 Square previoslySelected = findSelectedSquare(b);
38 if (previoslySelected == null) {
39 if (data.getPiece() != null) {
40 data.setSelected(true);
43 data.setPieceColor(previoslySelected.getPieceColor());
44 data.setPiece(previoslySelected.getPiece());
45 previoslySelected.setPiece(null);
46 previoslySelected.setPieceColor(null);
47 previoslySelected.setSelected(false);
51 static Square findSquare(Board b, char column, int row) {
52 for (Row r : b.getRows()) {
53 for (Square square : r.getColumns()) {
54 if (square.getX() == column && square.getY() == row) {
62 static Square findSelectedSquare(Board b) {
63 for (Row row : b.getRows()) {
64 for (Square square : row.getColumns()) {
65 if (square.isSelected()) {
73 @Model(className="Row", properties = {
74 @Property(name = "columns", type = Square.class, array = true)
76 static class RowsImpl {
80 PAWN(5), ROCK(2), KNIGHT(4), BISHOP(3), QUEEN(1), KING(0);
82 final int entityIndex;
85 this.entityIndex = ei;
88 String computeEntity(ColorType color) {
90 color = ColorType.WHITE;
94 case WHITE: base = 12; break;
95 case BLACK: base = 18; break;
97 throw new AssertionError();
99 return "b" + String.valueOf(base + entityIndex) + ";";
106 @Model(className="Square", properties = {
107 @Property(name = "piece", type = PieceType.class),
108 @Property(name = "pieceColor", type = ColorType.class),
109 @Property(name = "color", type = ColorType.class),
110 @Property(name = "x", type = int.class),
111 @Property(name = "y", type = int.class),
112 @Property(name = "selected", type = boolean.class)
114 static class PieceImpl {
115 @ComputedProperty static String pieceEntity(
116 PieceType piece, ColorType pieceColor
121 return piece.computeEntity(pieceColor);
124 @ComputedProperty static String squareColor(ColorType color, boolean selected) {
132 return color.toString().toLowerCase(Locale.US);
137 public static void initialize(String[] args) {
138 Board b = createBoard();
142 static Board createBoard() {
143 Board b = new Board();
144 for (int i = 8; i > 0; i--) {
147 for (int j = 'A'; j <= 'H'; j++) {
148 Square s = new Square();
151 s.setColor((i + j) % 2 == 1 ? ColorType.WHITE : ColorType.BLACK);
152 r.getColumns().add(s);
154 s.setPiece(PieceType.PAWN);
155 s.setPieceColor(ColorType.WHITE);
157 s.setPiece(PieceType.PAWN);
158 s.setPieceColor(ColorType.BLACK);
159 } else if (i == 8 || i == 1) {
160 s.setPieceColor(i == 1 ? ColorType.WHITE : ColorType.BLACK);
163 case 'A': case 'H': t = PieceType.ROCK; break;
164 case 'B': case 'G': t = PieceType.KNIGHT; break;
165 case 'C': case 'F': t = PieceType.BISHOP; break;
166 case 'D': t = PieceType.QUEEN; break;
167 default: t = PieceType.KING; break;