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