From 01d5ebd634af52dbedfbbf10077d8d2b4a82a75c Mon Sep 17 00:00:00 2001 From: Dmitry Isaenko Date: Wed, 22 May 2019 04:41:35 +0300 Subject: [PATCH] Preferences and Setting refactoring --- src/main/java/konogonka/AppPreferences.java | 53 +---- .../Settings/ListSelectorController.java | 3 +- .../Settings/SettingsController.java | 210 +++++++----------- .../FXML/Settings/ListSelectorLayout.fxml | 4 +- .../FXML/Settings/SettingsLayout.fxml | 155 +------------ 5 files changed, 100 insertions(+), 325 deletions(-) diff --git a/src/main/java/konogonka/AppPreferences.java b/src/main/java/konogonka/AppPreferences.java index 0eda866..77cade0 100644 --- a/src/main/java/konogonka/AppPreferences.java +++ b/src/main/java/konogonka/AppPreferences.java @@ -16,61 +16,10 @@ public class AppPreferences { public void setAll( String xciHeaderKey, - String headerKey, - String applicationKey0, - String applicationKey1, - String applicationKey2, - String applicationKey3, - String applicationKey4, - String applicationKey5, - String applicationKey6, - String applicationKey7, - String oceanKey0, - String oceanKey1, - String oceanKey2, - String oceanKey3, - String oceanKey4, - String oceanKey5, - String oceanKey6, - String oceanKey7, - String systemKey0, - String systemKey1, - String systemKey2, - String systemKey3, - String systemKey4, - String systemKey5, - String systemKey6, - String systemKey7 + String headerKey ) { setXciHeaderKey(xciHeaderKey); setHeaderKey(headerKey); - - setApplicationKey(0, applicationKey0); - setApplicationKey(1, applicationKey1); - setApplicationKey(2, applicationKey2); - setApplicationKey(3, applicationKey3); - setApplicationKey(4, applicationKey4); - setApplicationKey(5, applicationKey5); - setApplicationKey(6, applicationKey6); - setApplicationKey(7, applicationKey7); - - setOceanKey(0, oceanKey0); - setOceanKey(1, oceanKey1); - setOceanKey(2, oceanKey2); - setOceanKey(3, oceanKey3); - setOceanKey(4, oceanKey4); - setOceanKey(5, oceanKey5); - setOceanKey(6, oceanKey6); - setOceanKey(7, oceanKey7); - - setSystemKey(0, systemKey0); - setSystemKey(1, systemKey1); - setSystemKey(2, systemKey2); - setSystemKey(3, systemKey3); - setSystemKey(4, systemKey4); - setSystemKey(5, systemKey5); - setSystemKey(6, systemKey6); - setSystemKey(7, systemKey7); } public String getRecentPath(){return preferences.get("recent_path", System.getProperty("user.home"));} diff --git a/src/main/java/konogonka/Settings/ListSelectorController.java b/src/main/java/konogonka/Settings/ListSelectorController.java index fd925c6..6847a1f 100644 --- a/src/main/java/konogonka/Settings/ListSelectorController.java +++ b/src/main/java/konogonka/Settings/ListSelectorController.java @@ -13,6 +13,7 @@ import konogonka.ServiceWindow; import java.net.URL; import java.util.Arrays; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.ResourceBundle; public class ListSelectorController implements Initializable { @@ -61,7 +62,7 @@ public class ListSelectorController implements Initializable { * Must be run on start * Set list content */ - void setList(HashMap stringPairsArray){ + void setList(LinkedHashMap stringPairsArray){ if (stringPairsArray != null && ! stringPairsArray.isEmpty()) for (String name: stringPairsArray.keySet()) validateAndAdd(name+" = "+stringPairsArray.get(name)); diff --git a/src/main/java/konogonka/Settings/SettingsController.java b/src/main/java/konogonka/Settings/SettingsController.java index 0e5bcaf..1faf364 100644 --- a/src/main/java/konogonka/Settings/SettingsController.java +++ b/src/main/java/konogonka/Settings/SettingsController.java @@ -13,6 +13,7 @@ import konogonka.AppPreferences; import java.io.*; import java.net.URL; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.ResourceBundle; public class SettingsController implements Initializable { @@ -20,42 +21,51 @@ public class SettingsController implements Initializable { private Button okBtn, cancelBtn, importKeysBtn, importTitleKeysBtn; @FXML - ListSelectorController ListSelectorTitleKeysController; + ListSelectorController + ListSelectorKAEKAppController, + ListSelectorKAEKOceanController, + ListSelectorKAEKSysController, + ListSelectorTitleKeysController; @FXML private TextField xciHdrKeyTF, - hdrKeyTF, - keyApp0TF, - keyApp1TF, - keyApp2TF, - keyApp3TF, - keyApp4TF, - keyApp5TF, - keyApp6TF, - keyApp7TF, - keyOcean0TF, - keyOcean1TF, - keyOcean2TF, - keyOcean3TF, - keyOcean4TF, - keyOcean5TF, - keyOcean6TF, - keyOcean7TF, - keySys0TF, - keySys1TF, - keySys2TF, - keySys3TF, - keySys4TF, - keySys5TF, - keySys6TF, - keySys7TF; + hdrKeyTF; @Override public void initialize(URL url, ResourceBundle resourceBundle) { + ListSelectorKAEKAppController.initSelector(32, "key_area_key_application_"); + ListSelectorKAEKOceanController.initSelector(32, "key_area_key_ocean_"); + ListSelectorKAEKSysController.initSelector(32, "key_area_key_sys_"); ListSelectorTitleKeysController.initSelector(32, null); // 32 required - HashMap preparedPairsMapInit = new HashMap<>(); + LinkedHashMap preparedPairsMapInit = new LinkedHashMap<>(); + String kaekApp; + int cnt = 0; + + while (!(kaekApp = AppPreferences.getInstance().getApplicationKey(cnt)).isEmpty()){ + preparedPairsMapInit.put("key_area_key_application_"+String.format("%02d", cnt), kaekApp); + cnt++; + } + ListSelectorKAEKAppController.setList(preparedPairsMapInit); + + preparedPairsMapInit.clear(); + cnt = 0; + while (!(kaekApp = AppPreferences.getInstance().getOceanKey(cnt)).isEmpty()){ + preparedPairsMapInit.put("key_area_key_ocean_"+String.format("%02d", cnt), kaekApp); + cnt++; + } + ListSelectorKAEKOceanController.setList(preparedPairsMapInit); + + preparedPairsMapInit.clear(); + cnt = 0; + while (!(kaekApp = AppPreferences.getInstance().getSystemKey(cnt)).isEmpty()){ + preparedPairsMapInit.put("key_area_key_sys_"+String.format("%02d", cnt), kaekApp); + cnt++; + } + ListSelectorKAEKSysController.setList(preparedPairsMapInit); + + preparedPairsMapInit.clear(); for (int i = 0; i < AppPreferences.getInstance().getTitleKeysCount(); i++){ preparedPairsMapInit.put( AppPreferences.getInstance().getTitleKeyPair(i)[0], @@ -66,57 +76,9 @@ public class SettingsController implements Initializable { xciHdrKeyTF.setText(AppPreferences.getInstance().getXciHeaderKey()); hdrKeyTF.setText(AppPreferences.getInstance().getHeaderKey()); - keyApp0TF.setText(AppPreferences.getInstance().getApplicationKey(0)); - keyApp1TF.setText(AppPreferences.getInstance().getApplicationKey(1)); - keyApp2TF.setText(AppPreferences.getInstance().getApplicationKey(2)); - keyApp3TF.setText(AppPreferences.getInstance().getApplicationKey(3)); - keyApp4TF.setText(AppPreferences.getInstance().getApplicationKey(4)); - keyApp5TF.setText(AppPreferences.getInstance().getApplicationKey(5)); - keyApp6TF.setText(AppPreferences.getInstance().getApplicationKey(6)); - keyApp7TF.setText(AppPreferences.getInstance().getApplicationKey(7)); - keyOcean0TF.setText(AppPreferences.getInstance().getOceanKey(0)); - keyOcean1TF.setText(AppPreferences.getInstance().getOceanKey(1)); - keyOcean2TF.setText(AppPreferences.getInstance().getOceanKey(2)); - keyOcean3TF.setText(AppPreferences.getInstance().getOceanKey(3)); - keyOcean4TF.setText(AppPreferences.getInstance().getOceanKey(4)); - keyOcean5TF.setText(AppPreferences.getInstance().getOceanKey(5)); - keyOcean6TF.setText(AppPreferences.getInstance().getOceanKey(6)); - keyOcean7TF.setText(AppPreferences.getInstance().getOceanKey(7)); - keySys0TF.setText(AppPreferences.getInstance().getSystemKey(0)); - keySys1TF.setText(AppPreferences.getInstance().getSystemKey(1)); - keySys2TF.setText(AppPreferences.getInstance().getSystemKey(2)); - keySys3TF.setText(AppPreferences.getInstance().getSystemKey(3)); - keySys4TF.setText(AppPreferences.getInstance().getSystemKey(4)); - keySys5TF.setText(AppPreferences.getInstance().getSystemKey(5)); - keySys6TF.setText(AppPreferences.getInstance().getSystemKey(6)); - keySys7TF.setText(AppPreferences.getInstance().getSystemKey(7)); setTextValidation(xciHdrKeyTF); setTextValidation(hdrKeyTF); - setTextValidation(keyApp0TF); - setTextValidation(keyApp1TF); - setTextValidation(keyApp2TF); - setTextValidation(keyApp3TF); - setTextValidation(keyApp4TF); - setTextValidation(keyApp5TF); - setTextValidation(keyApp6TF); - setTextValidation(keyApp7TF); - setTextValidation(keyOcean0TF); - setTextValidation(keyOcean1TF); - setTextValidation(keyOcean2TF); - setTextValidation(keyOcean3TF); - setTextValidation(keyOcean4TF); - setTextValidation(keyOcean5TF); - setTextValidation(keyOcean6TF); - setTextValidation(keyOcean7TF); - setTextValidation(keySys0TF); - setTextValidation(keySys1TF); - setTextValidation(keySys2TF); - setTextValidation(keySys3TF); - setTextValidation(keySys4TF); - setTextValidation(keySys5TF); - setTextValidation(keySys6TF); - setTextValidation(keySys7TF); importKeysBtn.setOnAction(e->{ FileChooser fileChooser = new FileChooser(); @@ -141,30 +103,32 @@ public class SettingsController implements Initializable { fileMap.put(keyValue[0], keyValue[1]); } hdrKeyTF.setText(fileMap.get("header_key")); - keyApp0TF.setText(fileMap.get("key_area_key_application_00")); - keyApp1TF.setText(fileMap.get("key_area_key_application_01")); - keyApp2TF.setText(fileMap.get("key_area_key_application_02")); - keyApp3TF.setText(fileMap.get("key_area_key_application_03")); - keyApp4TF.setText(fileMap.get("key_area_key_application_04")); - keyApp5TF.setText(fileMap.get("key_area_key_application_05")); - keyApp6TF.setText(fileMap.get("key_area_key_application_06")); - keyApp7TF.setText(fileMap.get("key_area_key_application_07")); - keyOcean0TF.setText(fileMap.get("key_area_key_ocean_00")); - keyOcean1TF.setText(fileMap.get("key_area_key_ocean_01")); - keyOcean2TF.setText(fileMap.get("key_area_key_ocean_02")); - keyOcean3TF.setText(fileMap.get("key_area_key_ocean_03")); - keyOcean4TF.setText(fileMap.get("key_area_key_ocean_04")); - keyOcean5TF.setText(fileMap.get("key_area_key_ocean_05")); - keyOcean6TF.setText(fileMap.get("key_area_key_ocean_06")); - keyOcean7TF.setText(fileMap.get("key_area_key_ocean_07")); - keySys0TF.setText(fileMap.get("key_area_key_system_00")); - keySys1TF.setText(fileMap.get("key_area_key_system_01")); - keySys2TF.setText(fileMap.get("key_area_key_system_02")); - keySys3TF.setText(fileMap.get("key_area_key_system_03")); - keySys4TF.setText(fileMap.get("key_area_key_system_04")); - keySys5TF.setText(fileMap.get("key_area_key_system_05")); - keySys6TF.setText(fileMap.get("key_area_key_system_06")); - keySys7TF.setText(fileMap.get("key_area_key_system_07")); + + LinkedHashMap kaekSingle = new LinkedHashMap<>(); + + String keyParsed; + int counter = 0; + while ((keyParsed = fileMap.get("key_area_key_application_"+String.format("%02d", counter))) != null){ + kaekSingle.put("key_area_key_application_"+String.format("%02d", counter), keyParsed); + counter++; + } + ListSelectorKAEKAppController.setList(kaekSingle); + + kaekSingle.clear(); + counter = 0; + while ((keyParsed = fileMap.get("key_area_key_ocean_"+String.format("%02d", counter))) != null){ + kaekSingle.put("key_area_key_ocean_"+String.format("%02d", counter), keyParsed); + counter++; + } + ListSelectorKAEKOceanController.setList(kaekSingle); + + kaekSingle.clear(); + counter = 0; + while ((keyParsed = fileMap.get("key_area_key_system_"+String.format("%02d", counter))) != null){ + kaekSingle.put("key_area_key_system_"+String.format("%02d", counter), keyParsed); + counter++; + } + ListSelectorKAEKSysController.setList(kaekSingle); } catch (IOException ioe){ ioe.printStackTrace(); @@ -188,7 +152,7 @@ public class SettingsController implements Initializable { String fileLine; String[] keyValue; - HashMap preparedPairsMap = new HashMap<>(); + LinkedHashMap preparedPairsMap = new LinkedHashMap<>(); while ((fileLine = br.readLine()) != null){ keyValue = fileLine.trim().split("\\s+?=\\s+?", 2); if (keyValue.length == 2 && keyValue[0].length() == 32 && keyValue[1].length() == 32){ @@ -210,34 +174,30 @@ public class SettingsController implements Initializable { okBtn.setOnAction(e->{ Stage thisStage = (Stage)cancelBtn.getScene().getWindow(); + // Saving all keys we have AppPreferences.getInstance().setAll( xciHdrKeyTF.getText(), - hdrKeyTF.getText(), - keyApp0TF.getText(), - keyApp1TF.getText(), - keyApp2TF.getText(), - keyApp3TF.getText(), - keyApp4TF.getText(), - keyApp5TF.getText(), - keyApp6TF.getText(), - keyApp7TF.getText(), - keyOcean0TF.getText(), - keyOcean1TF.getText(), - keyOcean2TF.getText(), - keyOcean3TF.getText(), - keyOcean4TF.getText(), - keyOcean5TF.getText(), - keyOcean6TF.getText(), - keyOcean7TF.getText(), - keySys0TF.getText(), - keySys1TF.getText(), - keySys2TF.getText(), - keySys3TF.getText(), - keySys4TF.getText(), - keySys5TF.getText(), - keySys6TF.getText(), - keySys7TF.getText() + hdrKeyTF.getText() ); + + String[] kaekAppKeySet = ListSelectorKAEKAppController.getList(); + if (kaekAppKeySet != null){ + for (int i = 0; i < kaekAppKeySet.length; i++) + AppPreferences.getInstance().setApplicationKey(i, kaekAppKeySet[i].split("\\s=\\s", 2)[1]); + } + + String[] kaekOceanKeySet = ListSelectorKAEKOceanController.getList(); + if (kaekOceanKeySet != null){ + for (int i = 0; i < kaekOceanKeySet.length; i++) + AppPreferences.getInstance().setOceanKey(i, kaekOceanKeySet[i].split("\\s=\\s", 2)[1]); + } + + String[] kaekSysKeySet = ListSelectorKAEKSysController.getList(); + if (kaekSysKeySet != null){ + for (int i = 0; i < kaekSysKeySet.length; i++) + AppPreferences.getInstance().setOceanKey(i, kaekSysKeySet[i].split("\\s=\\s", 2)[1]); + } + String[] titleKeysSet = ListSelectorTitleKeysController.getList(); if (titleKeysSet != null){ AppPreferences.getInstance().setTitleKeysCount(titleKeysSet.length); diff --git a/src/main/resources/FXML/Settings/ListSelectorLayout.fxml b/src/main/resources/FXML/Settings/ListSelectorLayout.fxml index a887551..d91ea84 100644 --- a/src/main/resources/FXML/Settings/ListSelectorLayout.fxml +++ b/src/main/resources/FXML/Settings/ListSelectorLayout.fxml @@ -8,11 +8,11 @@ - + - +