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.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;
37 * @author Jaroslav Tulach <jtulach@netbeans.org>
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),
45 public class UIModel {
46 private static final Logger LOG = Logger.getLogger(UIModel.class.getName());
48 @ComputedProperty static boolean viewGamesActive(String selectedGameId) {
49 return selectedGameId == null;
52 @ComputedProperty static Board selectedBoard(String selectedGameId, List<Board> boards) {
54 for (Board b : boards) {
56 if (selectedGameId != null && selectedGameId.equals(b.getGameId())) {
64 @Function static void activateGame(UI m, Board data) {
65 m.setSelectedGameId(data.getGameId());
68 @Function static void activateSettings(UI m) {
69 m.setSelectedGameId(null);
72 private static int boardCnt;
74 @Function static void createGame(UI u) {
75 Board b = Rules.createBoard();
76 b.setGameId("id" + ++boardCnt);
77 final String wp = u.getViewGames().getWhitePlayer();
79 final String bp = u.getViewGames().getBlackPlayer();
81 b.setStatus(wp + " vs. " + bp);
83 u.setSelectedGameId(b.getGameId());
85 u.getViewGames().setWhitePlayer(null);
86 u.getViewGames().setBlackPlayer(null);
92 private static Board findBoard(UI ui, String gameId) {
93 for (Board tmp : ui.getBoards()) {
94 if (tmp.getGameId().equals(gameId)) {
102 @Function static void joinGame(UI u) {
105 @Function static void leave(UI ui, Board data) {
106 ui.getBoards().remove(data);
107 ui.setSelectedGameId(null);
110 public static void initialize(String... args) throws URISyntaxException, IOException {
112 ui.setStatus("Ready.");