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
jaroslav@49
     1
/**
jaroslav@49
     2
 * The MIT License (MIT)
jaroslav@49
     3
 *
jaroslav@49
     4
 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@49
     5
 *
jaroslav@49
     6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
jaroslav@49
     7
 * of this software and associated documentation files (the "Software"), to deal
jaroslav@49
     8
 * in the Software without restriction, including without limitation the rights
jaroslav@49
     9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jaroslav@49
    10
 * copies of the Software, and to permit persons to whom the Software is
jaroslav@49
    11
 * furnished to do so, subject to the following conditions:
jaroslav@49
    12
 *
jaroslav@49
    13
 * The above copyright notice and this permission notice shall be included in
jaroslav@49
    14
 * all copies or substantial portions of the Software.
jaroslav@49
    15
 *
jaroslav@49
    16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jaroslav@49
    17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jaroslav@49
    18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jaroslav@49
    19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jaroslav@49
    20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jaroslav@49
    21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jaroslav@49
    22
 * THE SOFTWARE.
jaroslav@49
    23
 */
jaroslav@51
    24
package org.apidesign.html.demo.chess;
jaroslav@49
    25
jaroslav@49
    26
import java.io.IOException;
jaroslav@49
    27
import java.net.URISyntaxException;
jaroslav@49
    28
import java.util.List;
jaroslav@49
    29
import java.util.logging.Logger;
jaroslav@49
    30
import net.java.html.json.ComputedProperty;
jaroslav@49
    31
import net.java.html.json.Function;
jaroslav@49
    32
import net.java.html.json.Model;
jaroslav@49
    33
import net.java.html.json.Property;
jaroslav@49
    34
jaroslav@49
    35
/**
jaroslav@49
    36
 *
jaroslav@49
    37
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@49
    38
 */
jaroslav@227
    39
@Model(className = "UI", targetId = "", properties = {
jaroslav@49
    40
    @Property(name = "status", type = String.class),
jaroslav@49
    41
    @Property(name = "selectedGameId", type = String.class),
jaroslav@49
    42
    @Property(name = "boards", type = Board.class, array = true),
jaroslav@49
    43
    @Property(name = "viewGames", type = Games.class),
jaroslav@49
    44
})
jaroslav@49
    45
public class UIModel {
jaroslav@49
    46
    private static final Logger LOG = Logger.getLogger(UIModel.class.getName());
jaroslav@49
    47
    
jaroslav@52
    48
    @ComputedProperty static boolean viewGamesActive(String selectedGameId) {
jaroslav@49
    49
        return selectedGameId == null;
jaroslav@49
    50
    }
jaroslav@49
    51
    
jaroslav@49
    52
    @ComputedProperty static Board selectedBoard(String selectedGameId, List<Board> boards) {
jaroslav@49
    53
        Board active = null;
jaroslav@49
    54
        for (Board b : boards) {
jaroslav@49
    55
            b.setActive(false);
jaroslav@49
    56
            if (selectedGameId != null && selectedGameId.equals(b.getGameId())) {
jaroslav@49
    57
                b.setActive(true);
jaroslav@49
    58
                active = b;
jaroslav@49
    59
            }
jaroslav@49
    60
        }
jaroslav@49
    61
        return active;
jaroslav@49
    62
    }
jaroslav@49
    63
    
jaroslav@49
    64
    @Function static void activateGame(UI m, Board data) {
jaroslav@49
    65
        m.setSelectedGameId(data.getGameId());
jaroslav@49
    66
    }
jaroslav@49
    67
jaroslav@49
    68
    @Function static void activateSettings(UI m) {
jaroslav@49
    69
        m.setSelectedGameId(null);
jaroslav@49
    70
    }
jaroslav@49
    71
    
jaroslav@52
    72
    private static int boardCnt;
jaroslav@52
    73
    
jaroslav@49
    74
    @Function static void createGame(UI u) {
jaroslav@52
    75
        Board b = Rules.createBoard();
jaroslav@52
    76
        b.setGameId("id" + ++boardCnt);
jaroslav@52
    77
        final String wp = u.getViewGames().getWhitePlayer();
jaroslav@52
    78
        b.setWhitePlayer(wp);
jaroslav@52
    79
        final String bp = u.getViewGames().getBlackPlayer();
jaroslav@52
    80
        b.setBlackPlayer(bp);
jaroslav@52
    81
        b.setStatus(wp + " vs. " + bp);
jaroslav@52
    82
        u.getBoards().add(b);
jaroslav@52
    83
        u.setSelectedGameId(b.getGameId());
jaroslav@52
    84
        
jaroslav@52
    85
        u.getViewGames().setWhitePlayer(null);
jaroslav@52
    86
        u.getViewGames().setBlackPlayer(null);
jaroslav@49
    87
    }
jaroslav@49
    88
jaroslav@49
    89
    
jaroslav@49
    90
jaroslav@49
    91
jaroslav@49
    92
    private static Board findBoard(UI ui, String gameId) {
jaroslav@49
    93
        for (Board tmp : ui.getBoards()) {
jaroslav@49
    94
            if (tmp.getGameId().equals(gameId)) {
jaroslav@49
    95
                return tmp;
jaroslav@49
    96
            }
jaroslav@49
    97
        }
jaroslav@49
    98
        return null;
jaroslav@49
    99
    }
jaroslav@49
   100
    
jaroslav@49
   101
    
jaroslav@52
   102
    @Function static void joinGame(UI u) {
jaroslav@49
   103
    }
jaroslav@49
   104
jaroslav@49
   105
    @Function static void leave(UI ui, Board data) {
jaroslav@49
   106
        ui.getBoards().remove(data);
jaroslav@49
   107
        ui.setSelectedGameId(null);
jaroslav@49
   108
    }
jaroslav@49
   109
    
jaroslav@49
   110
    public static void initialize(String... args) throws URISyntaxException, IOException {
jaroslav@49
   111
        UI ui = new UI();
jaroslav@49
   112
        ui.setStatus("Ready.");
jaroslav@49
   113
        ui.applyBindings();
jaroslav@49
   114
    }
jaroslav@49
   115
jaroslav@49
   116
}