-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
14 changed files
with
234 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cn.reddragon.eportal.account; | ||
|
||
public final class Account { | ||
private final String name; | ||
private final String password; | ||
|
||
public Account(String name, String password) { | ||
this.name = name; | ||
this.password = password; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/cn/reddragon/eportal/account/AccountManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cn.reddragon.eportal.account; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class AccountManager { | ||
public static final ArrayList<Account> accounts = new ArrayList<>(); | ||
|
||
public static void addAccount(String username, String password) { | ||
accounts.add(new Account(username, password)); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/cn/reddragon/eportal/config/AbstractConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package cn.reddragon.eportal.config; | ||
|
||
import com.google.gson.JsonElement; | ||
|
||
public abstract class AbstractConfig { | ||
public String name; | ||
|
||
public AbstractConfig(String name) { | ||
this.name = name; | ||
} | ||
|
||
public abstract JsonElement toJson(); | ||
|
||
public abstract void fromJson(JsonElement element); | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/cn/reddragon/eportal/config/ConfigManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package cn.reddragon.eportal.config; | ||
|
||
import cn.reddragon.eportal.config.configs.AccountConfig; | ||
import cn.reddragon.eportal.config.configs.NetTypeConfig; | ||
import com.google.gson.*; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.io.*; | ||
import java.util.ArrayList; | ||
import java.util.Map.Entry; | ||
|
||
public class ConfigManager { | ||
public static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
private static final File configFile = new File("EPortal.json"); | ||
private static final ArrayList<AbstractConfig> configs = new ArrayList<>(); | ||
private static final Logger logger = LogManager.getLogger("ConfigManager"); | ||
|
||
static { | ||
if (!configFile.exists()) { | ||
try { | ||
logger.info("未找到配置文件. 新建配置文件."); | ||
configFile.createNewFile(); | ||
saveConfigs(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
configs.add(new AccountConfig()); | ||
configs.add(new NetTypeConfig()); | ||
} | ||
|
||
public static void loadConfigs() { | ||
try { | ||
JsonElement element = JsonParser.parseReader(new BufferedReader(new FileReader(configFile))); | ||
if (!element.isJsonNull()) { | ||
JsonObject object = element.getAsJsonObject(); | ||
for (Entry<String, JsonElement> next : object.entrySet()) { | ||
for (AbstractConfig config : configs) { | ||
if (config.name.equals(next.getKey())) { | ||
config.fromJson(next.getValue()); | ||
logger.info("成功加载配置文件 {}", config.name); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} catch (FileNotFoundException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static void saveConfigs() { | ||
JsonObject root = new JsonObject(); | ||
for (AbstractConfig config : configs) { | ||
root.add(config.name, config.toJson()); | ||
logger.info("成功保存配置文件 {}", config.name); | ||
} | ||
try { | ||
BufferedWriter writer = new BufferedWriter(new FileWriter(configFile)); | ||
writer.write(gson.toJson(root)); | ||
writer.flush(); | ||
writer.close(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/cn/reddragon/eportal/config/configs/AccountConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cn.reddragon.eportal.config.configs; | ||
|
||
import cn.reddragon.eportal.account.Account; | ||
import cn.reddragon.eportal.account.AccountManager; | ||
import cn.reddragon.eportal.config.AbstractConfig; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
|
||
public class AccountConfig extends AbstractConfig { | ||
|
||
public AccountConfig() { | ||
super("Account"); | ||
} | ||
|
||
@Override | ||
public JsonElement toJson() { | ||
JsonArray array = new JsonArray(); | ||
for (Account it : AccountManager.accounts) { | ||
JsonObject object = new JsonObject(); | ||
object.addProperty("Username", it.getName()); | ||
object.addProperty("Password", it.getPassword()); | ||
array.add(object); | ||
} | ||
return array; | ||
} | ||
|
||
@Override | ||
public void fromJson(JsonElement element) { | ||
JsonArray array = element.getAsJsonArray(); | ||
for (JsonElement it : array) { | ||
JsonObject object = it.getAsJsonObject(); | ||
AccountManager.addAccount(object.get("Username").getAsString(), object.get("Password").getAsString()); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/cn/reddragon/eportal/config/configs/NetTypeConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cn.reddragon.eportal.config.configs; | ||
|
||
import cn.reddragon.eportal.EPortal; | ||
import cn.reddragon.eportal.config.AbstractConfig; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonPrimitive; | ||
import javafx.scene.control.ChoiceBox; | ||
|
||
public class NetTypeConfig extends AbstractConfig { | ||
private final ChoiceBox<String> selector = EPortal.controller.selector; | ||
|
||
public NetTypeConfig() { | ||
super("NetType"); | ||
} | ||
|
||
@Override | ||
public JsonElement toJson() { | ||
return new JsonPrimitive(EPortal.controller.selector.getItems().indexOf(EPortal.controller.selector.getValue())); | ||
} | ||
|
||
@Override | ||
public void fromJson(JsonElement element) { | ||
selector.setValue(selector.getItems().get(element.getAsInt())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/java/cn/reddragon/eportal/controllers/SettingsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package cn.reddragon.eportal.controllers; | ||
|
||
public class SettingsController { | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.