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 com.oracle.chess.client.htmljava;
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;
38 * @author Jaroslav Tulach <jtulach@netbeans.org>
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),
46 private static final String SETTINGS = ".j1chess/html.properties";
47 private static final String DEFAULT_URL = "ws://localhost:8080/chess/chessserver";
49 @ComputedProperty static boolean validCredentials(String username, String password) {
50 return username != null && password != null && !username.isEmpty() && !password.isEmpty();
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();
59 s.setUsername(p.getProperty("username", ""));
60 s.setPassword(p.getProperty("password", ""));
61 s.setUrl(p.getProperty("url", DEFAULT_URL));
62 } catch (Throwable ex) {
65 s.setUrl(DEFAULT_URL);
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());