chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java
branchchess
changeset 26 b675be28fc49
parent 23 827f30fbdeab
child 29 9fb64f6528b5
     1.1 --- a/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java	Thu Jul 25 15:57:52 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();