chess/src/main/java/org/apidesign/html/demo/chess/UIModel.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 09 Jan 2015 06:10:00 +0100
changeset 227 fd26342cf23d
parent 52 6bb4070d2c20
permissions -rw-r--r--
Switching to HTML/Java@1.1 and bck2brwsr@0.12
     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.io.IOException;
    27 import java.net.URISyntaxException;
    28 import java.util.List;
    29 import java.util.logging.Logger;
    30 import net.java.html.json.ComputedProperty;
    31 import net.java.html.json.Function;
    32 import net.java.html.json.Model;
    33 import net.java.html.json.Property;
    34 
    35 /**
    36  *
    37  * @author Jaroslav Tulach <jtulach@netbeans.org>
    38  */
    39 @Model(className = "UI", targetId = "", properties = {
    40     @Property(name = "status", type = String.class),
    41     @Property(name = "selectedGameId", type = String.class),
    42     @Property(name = "boards", type = Board.class, array = true),
    43     @Property(name = "viewGames", type = Games.class),
    44 })
    45 public class UIModel {
    46     private static final Logger LOG = Logger.getLogger(UIModel.class.getName());
    47     
    48     @ComputedProperty static boolean viewGamesActive(String selectedGameId) {
    49         return selectedGameId == null;
    50     }
    51     
    52     @ComputedProperty static Board selectedBoard(String selectedGameId, List<Board> boards) {
    53         Board active = null;
    54         for (Board b : boards) {
    55             b.setActive(false);
    56             if (selectedGameId != null && selectedGameId.equals(b.getGameId())) {
    57                 b.setActive(true);
    58                 active = b;
    59             }
    60         }
    61         return active;
    62     }
    63     
    64     @Function static void activateGame(UI m, Board data) {
    65         m.setSelectedGameId(data.getGameId());
    66     }
    67 
    68     @Function static void activateSettings(UI m) {
    69         m.setSelectedGameId(null);
    70     }
    71     
    72     private static int boardCnt;
    73     
    74     @Function static void createGame(UI u) {
    75         Board b = Rules.createBoard();
    76         b.setGameId("id" + ++boardCnt);
    77         final String wp = u.getViewGames().getWhitePlayer();
    78         b.setWhitePlayer(wp);
    79         final String bp = u.getViewGames().getBlackPlayer();
    80         b.setBlackPlayer(bp);
    81         b.setStatus(wp + " vs. " + bp);
    82         u.getBoards().add(b);
    83         u.setSelectedGameId(b.getGameId());
    84         
    85         u.getViewGames().setWhitePlayer(null);
    86         u.getViewGames().setBlackPlayer(null);
    87     }
    88 
    89     
    90 
    91 
    92     private static Board findBoard(UI ui, String gameId) {
    93         for (Board tmp : ui.getBoards()) {
    94             if (tmp.getGameId().equals(gameId)) {
    95                 return tmp;
    96             }
    97         }
    98         return null;
    99     }
   100     
   101     
   102     @Function static void joinGame(UI u) {
   103     }
   104 
   105     @Function static void leave(UI ui, Board data) {
   106         ui.getBoards().remove(data);
   107         ui.setSelectedGameId(null);
   108     }
   109     
   110     public static void initialize(String... args) throws URISyntaxException, IOException {
   111         UI ui = new UI();
   112         ui.setStatus("Ready.");
   113         ui.applyBindings();
   114     }
   115 
   116 }