1.1 --- a/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java Thu Jul 25 16:21:25 2013 +0200
1.2 +++ b/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java Thu Jul 25 17:14:55 2013 +0200
1.3 @@ -33,7 +33,44 @@
1.4 @Property(name = "rows", type = Row.class, array = true)
1.5 })
1.6 public class BoardModel {
1.7 - @Model(className="Row", properties = {
1.8 + @Function static void selected(Board b, Square data) {
1.9 + Square previoslySelected = findSelectedSquare(b);
1.10 + if (previoslySelected == null) {
1.11 + if (data.getPiece() != null) {
1.12 + data.setSelected(true);
1.13 + }
1.14 + } else {
1.15 + data.setPieceColor(previoslySelected.getPieceColor());
1.16 + data.setPiece(previoslySelected.getPiece());
1.17 + previoslySelected.setPiece(null);
1.18 + previoslySelected.setPieceColor(null);
1.19 + previoslySelected.setSelected(false);
1.20 + }
1.21 + }
1.22 +
1.23 + static Square findSquare(Board b, char column, int row) {
1.24 + for (Row r : b.getRows()) {
1.25 + for (Square square : r.getColumns()) {
1.26 + if (square.getX() == column && square.getY() == row) {
1.27 + return square;
1.28 + }
1.29 + }
1.30 + }
1.31 + return null;
1.32 + }
1.33 +
1.34 + static Square findSelectedSquare(Board b) {
1.35 + for (Row row : b.getRows()) {
1.36 + for (Square square : row.getColumns()) {
1.37 + if (square.isSelected()) {
1.38 + return square;
1.39 + }
1.40 + }
1.41 + }
1.42 + return null;
1.43 + }
1.44 +
1.45 + @Model(className="Row", properties = {
1.46 @Property(name = "columns", type = Square.class, array = true)
1.47 })
1.48 static class RowsImpl {
1.49 @@ -72,12 +109,9 @@
1.50 @Property(name = "color", type = ColorType.class),
1.51 @Property(name = "x", type = int.class),
1.52 @Property(name = "y", type = int.class),
1.53 + @Property(name = "selected", type = boolean.class)
1.54 })
1.55 static class PieceImpl {
1.56 - @Function static void changeToPawn(Square s) {
1.57 - s.setPiece(PieceType.PAWN);
1.58 - }
1.59 -
1.60 @ComputedProperty static String pieceEntity(
1.61 PieceType piece, ColorType pieceColor
1.62 ) {
1.63 @@ -87,7 +121,11 @@
1.64 return piece.computeEntity(pieceColor);
1.65 }
1.66
1.67 - @ComputedProperty static String squareColor(ColorType color) {
1.68 + @ComputedProperty static String squareColor(ColorType color, boolean selected) {
1.69 + if (selected) {
1.70 + return "selected";
1.71 + }
1.72 +
1.73 if (color == null) {
1.74 return "";
1.75 } else {
1.76 @@ -101,7 +139,7 @@
1.77 b.applyBindings();
1.78 }
1.79
1.80 - private static Board createBoard() {
1.81 + static Board createBoard() {
1.82 Board b = new Board();
1.83 for (int i = 8; i > 0; i--) {
1.84 Row r = new Row();
2.1 --- a/chess/src/main/webapp/pages/css/chess.css Thu Jul 25 16:21:25 2013 +0200
2.2 +++ b/chess/src/main/webapp/pages/css/chess.css Thu Jul 25 17:14:55 2013 +0200
2.3 @@ -52,6 +52,9 @@
2.4 table.board td.black {
2.5 background-color: #B0B0B0;
2.6 }
2.7 +table.board td.selected {
2.8 + background-color: #B000B0;
2.9 +}
2.10 .figure {
2.11 cursor: pointer;
2.12 }
2.13 \ No newline at end of file
3.1 --- a/chess/src/main/webapp/pages/index.html Thu Jul 25 16:21:25 2013 +0200
3.2 +++ b/chess/src/main/webapp/pages/index.html Thu Jul 25 17:14:55 2013 +0200
3.3 @@ -60,7 +60,7 @@
3.4 <tbody data-bind="foreach: rows">
3.5 <tr >
3.6 <!-- ko foreach: columns -->
3.7 - <td data-bind="click: changeToPawn, css: squareColor" >
3.8 + <td data-bind="click: $root.selected, css: squareColor" >
3.9 <span data-bind='html: pieceEntity'></span>
3.10 </td>
3.11 <!-- /ko -->
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/chess/src/test/java/org/apidesign/html/demo/chess/BoardModelTest.java Thu Jul 25 17:14:55 2013 +0200
4.3 @@ -0,0 +1,65 @@
4.4 +/**
4.5 + * The MIT License (MIT)
4.6 + *
4.7 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
4.8 + *
4.9 + * Permission is hereby granted, free of charge, to any person obtaining a copy
4.10 + * of this software and associated documentation files (the "Software"), to deal
4.11 + * in the Software without restriction, including without limitation the rights
4.12 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
4.13 + * copies of the Software, and to permit persons to whom the Software is
4.14 + * furnished to do so, subject to the following conditions:
4.15 + *
4.16 + * The above copyright notice and this permission notice shall be included in
4.17 + * all copies or substantial portions of the Software.
4.18 + *
4.19 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
4.20 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
4.21 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
4.22 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
4.23 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
4.24 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
4.25 + * THE SOFTWARE.
4.26 + */
4.27 +package org.apidesign.html.demo.chess;
4.28 +
4.29 +import static org.testng.Assert.*;
4.30 +import org.testng.annotations.Test;
4.31 +
4.32 +/**
4.33 + *
4.34 + * @author Jaroslav Tulach <jtulach@netbeans.org>
4.35 + */
4.36 +public class BoardModelTest {
4.37 +
4.38 + public BoardModelTest() {
4.39 + }
4.40 +
4.41 +
4.42 + @Test public void e2e4() {
4.43 + Board b = BoardModel.createBoard();
4.44 + Square e2 = BoardModel.findSquare(b, 'E', 2);
4.45 + assertNotNull(e2);
4.46 + BoardModel.selected(b, e2);
4.47 + assertEquals(BoardModel.findSelectedSquare(b), e2, "E2 is selected");
4.48 +
4.49 + Square e4 = BoardModel.findSquare(b, 'E', 4);
4.50 + assertNotNull(e4);
4.51 +
4.52 + BoardModel.selected(b, e4);
4.53 +
4.54 + assertNull(e2.getPiece(), "No pawn at e2");
4.55 + assertEquals(e4.getPiece(), BoardModel.PieceType.PAWN, "Pawn moved successfully");
4.56 + assertNull(BoardModel.findSelectedSquare(b), "No square selected");
4.57 +
4.58 + }
4.59 +
4.60 + @Test public void cantSelectEmptySquare() {
4.61 + Board b = BoardModel.createBoard();
4.62 + Square e3 = BoardModel.findSquare(b, 'E', 3);
4.63 + assertNotNull(e3);
4.64 + BoardModel.selected(b, e3);
4.65 + assertNull(BoardModel.findSelectedSquare(b), "No square is selected");
4.66 + }
4.67 +
4.68 +}