chess/src/main/java/com/oracle/chess/client/htmljava/SettingsModel.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 24 Sep 2013 22:20:24 +0200
branchchess
changeset 49 945fbfff28f3
permissions -rw-r--r--
Advanced version of the chess game
     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 com.oracle.chess.client.htmljava;
    25 
    26 import java.io.File;
    27 import java.io.FileInputStream;
    28 import java.io.FileOutputStream;
    29 import java.io.IOException;
    30 import java.util.Properties;
    31 import net.java.html.json.ComputedProperty;
    32 import net.java.html.json.Model;
    33 import net.java.html.json.ModelOperation;
    34 import net.java.html.json.Property;
    35 
    36 /**
    37  *
    38  * @author Jaroslav Tulach <jtulach@netbeans.org>
    39  */
    40 @Model(className = "Settings", properties = {
    41     @Property(name = "username", type = String.class),
    42     @Property(name = "password", type = String.class),
    43     @Property(name = "url", type = String.class),
    44 })
    45 class SettingsModel {
    46     private static final String SETTINGS = ".j1chess/html.properties";
    47     private static final String DEFAULT_URL = "ws://localhost:8080/chess/chessserver";
    48     
    49     @ComputedProperty static boolean validCredentials(String username, String password) {
    50         return username != null && password != null && !username.isEmpty() && !password.isEmpty();
    51     }
    52     
    53     @ModelOperation static void read(Settings s) {
    54         File prefs = new File(System.getProperty("user.home"), SETTINGS);
    55         try (FileInputStream is = new FileInputStream(prefs)) {
    56             Properties p = new Properties();
    57             p.load(is);
    58             
    59             s.setUsername(p.getProperty("username", ""));
    60             s.setPassword(p.getProperty("password", ""));
    61             s.setUrl(p.getProperty("url", DEFAULT_URL));
    62         } catch (Throwable ex) {
    63             s.setUsername("");
    64             s.setPassword("");
    65             s.setUrl(DEFAULT_URL);
    66         }
    67     }
    68     
    69     @ModelOperation static void write(Settings s, UI ui) {
    70         File prefs = new File(System.getProperty("user.home"), SETTINGS);
    71         prefs.getParentFile().mkdirs();
    72         try (FileOutputStream os = new FileOutputStream(prefs)) {
    73             Properties p = new Properties();
    74             p.setProperty("username", s.getUsername());
    75             p.setProperty("password", s.getPassword());
    76             p.setProperty("url", s.getUrl());
    77             p.store(os, "Java One Chess Preferences for HTML/Java Client");
    78         } catch (Throwable ex) {
    79             ui.setStatus("Cannot save preferences: " + ex.getMessage());
    80         }
    81     }
    82 }