chess/src/main/java/com/oracle/chess/client/htmljava/SettingsModel.java
branchchess
changeset 49 945fbfff28f3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/chess/src/main/java/com/oracle/chess/client/htmljava/SettingsModel.java	Tue Sep 24 22:20:24 2013 +0200
     1.3 @@ -0,0 +1,82 @@
     1.4 +/**
     1.5 + * The MIT License (MIT)
     1.6 + *
     1.7 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.8 + *
     1.9 + * Permission is hereby granted, free of charge, to any person obtaining a copy
    1.10 + * of this software and associated documentation files (the "Software"), to deal
    1.11 + * in the Software without restriction, including without limitation the rights
    1.12 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    1.13 + * copies of the Software, and to permit persons to whom the Software is
    1.14 + * furnished to do so, subject to the following conditions:
    1.15 + *
    1.16 + * The above copyright notice and this permission notice shall be included in
    1.17 + * all copies or substantial portions of the Software.
    1.18 + *
    1.19 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    1.20 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    1.21 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    1.22 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    1.23 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    1.24 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    1.25 + * THE SOFTWARE.
    1.26 + */
    1.27 +package com.oracle.chess.client.htmljava;
    1.28 +
    1.29 +import java.io.File;
    1.30 +import java.io.FileInputStream;
    1.31 +import java.io.FileOutputStream;
    1.32 +import java.io.IOException;
    1.33 +import java.util.Properties;
    1.34 +import net.java.html.json.ComputedProperty;
    1.35 +import net.java.html.json.Model;
    1.36 +import net.java.html.json.ModelOperation;
    1.37 +import net.java.html.json.Property;
    1.38 +
    1.39 +/**
    1.40 + *
    1.41 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.42 + */
    1.43 +@Model(className = "Settings", properties = {
    1.44 +    @Property(name = "username", type = String.class),
    1.45 +    @Property(name = "password", type = String.class),
    1.46 +    @Property(name = "url", type = String.class),
    1.47 +})
    1.48 +class SettingsModel {
    1.49 +    private static final String SETTINGS = ".j1chess/html.properties";
    1.50 +    private static final String DEFAULT_URL = "ws://localhost:8080/chess/chessserver";
    1.51 +    
    1.52 +    @ComputedProperty static boolean validCredentials(String username, String password) {
    1.53 +        return username != null && password != null && !username.isEmpty() && !password.isEmpty();
    1.54 +    }
    1.55 +    
    1.56 +    @ModelOperation static void read(Settings s) {
    1.57 +        File prefs = new File(System.getProperty("user.home"), SETTINGS);
    1.58 +        try (FileInputStream is = new FileInputStream(prefs)) {
    1.59 +            Properties p = new Properties();
    1.60 +            p.load(is);
    1.61 +            
    1.62 +            s.setUsername(p.getProperty("username", ""));
    1.63 +            s.setPassword(p.getProperty("password", ""));
    1.64 +            s.setUrl(p.getProperty("url", DEFAULT_URL));
    1.65 +        } catch (Throwable ex) {
    1.66 +            s.setUsername("");
    1.67 +            s.setPassword("");
    1.68 +            s.setUrl(DEFAULT_URL);
    1.69 +        }
    1.70 +    }
    1.71 +    
    1.72 +    @ModelOperation static void write(Settings s, UI ui) {
    1.73 +        File prefs = new File(System.getProperty("user.home"), SETTINGS);
    1.74 +        prefs.getParentFile().mkdirs();
    1.75 +        try (FileOutputStream os = new FileOutputStream(prefs)) {
    1.76 +            Properties p = new Properties();
    1.77 +            p.setProperty("username", s.getUsername());
    1.78 +            p.setProperty("password", s.getPassword());
    1.79 +            p.setProperty("url", s.getUrl());
    1.80 +            p.store(os, "Java One Chess Preferences for HTML/Java Client");
    1.81 +        } catch (Throwable ex) {
    1.82 +            ui.setStatus("Cannot save preferences: " + ex.getMessage());
    1.83 +        }
    1.84 +    }
    1.85 +}