package org.blueshard.cryptogx; import javafx.application.Platform; import javafx.collections.FXCollections; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.KeyCode; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.VBox; import javafx.stage.*; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import java.io.*; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import java.util.*; import java.util.concurrent.atomic.AtomicReference; import static org.blueshard.cryptogx.Main.*; /** *
Class for the user configuration / settings
* * @since 1.12.0 */ public class Settings { private static double addSettingsGUIX, addSettingsGUIY; private static final HashSetShows a GUI where the user can save settings, which can load later
* * @param rootWindow from which this GUI will get called * @param userSetting * @throws IOException * * @since 1.11.0 */ public static void addSettingGUI(Window rootWindow, MapShows a GUI where the user can export settings to a extra file
* * @param rootWindow from which this GUI will get called * @throws IOException * * @since 1.11.0 */ public static void exportSettingsGUI(Window rootWindow) throws IOException { Stage rootStage = new Stage(); rootStage.initOwner(rootWindow); Parent exportSettingsRoot = FXMLLoader.load(Settings.class.getResource("resources/exportSettingsGUI.fxml")); rootStage.initStyle(StageStyle.UNDECORATED); rootStage.initModality(Modality.WINDOW_MODAL); rootStage.setResizable(false); rootStage.setTitle("cryptoGX"); Scene scene = new Scene(exportSettingsRoot, 254, 253); rootStage.setScene(scene); scene.setOnMouseDragged(event -> { rootStage.setX(event.getScreenX() + addSettingsGUIX); rootStage.setY(event.getScreenY() + addSettingsGUIY); }); scene.setOnMousePressed(event -> { addSettingsGUIX = scene.getX() - event.getSceneX(); addSettingsGUIY = scene.getY() - event.getSceneY(); }); Thread thread = new Thread(() -> { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } MenuBar menuBar = (MenuBar) exportSettingsRoot.lookup("#menuBar"); menuBar.setOnMouseDragged(event -> { rootStage.setX(event.getScreenX() + addSettingsGUIX); rootStage.setY(event.getScreenY() + addSettingsGUIY); }); menuBar.setOnMousePressed(event -> { addSettingsGUIX = menuBar.getLayoutX() - event.getSceneX(); addSettingsGUIY = menuBar.getLayoutY() - event.getSceneY(); }); ImageView closeButton = (ImageView) exportSettingsRoot.lookup("#closeButton"); closeButton.setOnMouseClicked(event -> rootStage.close()); VBox settingsBox = (VBox) exportSettingsRoot.lookup("#settingsBox"); Platform.runLater(() -> readSettings(config).keySet().forEach(s -> { CheckBox newCheckBox = new CheckBox(); newCheckBox.setText(s); settingsBox.getChildren().add(newCheckBox); })); Button exportButton = (Button) exportSettingsRoot.lookup("#exportButton"); exportButton.setOnAction(event -> { FileChooser fileChooser = new FileChooser(); fileChooser.setTitle("Export settings"); fileChooser.setInitialFileName("settings.config"); fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Config files", "*.config"), new FileChooser.ExtensionFilter("All files", "*.*")); File file = fileChooser.showSaveDialog(exportSettingsRoot.getScene().getWindow()); if (file != null) { TreeMapShows a GUI where the user can save the current settings
* * @param settingName name of the new setting * @param newSetting is the new setting key value pair * * @since 1.12.0 */ public static void addSetting(File file, String settingName, MapDeletes a saved setting
* * @param settingName of the setting * @return if the setting could be found * * @since 1.12.0 */ public static boolean deleteSetting(File file, String settingName) { StringBuilder newConfig = new StringBuilder(); boolean delete = false; boolean found = false; try { BufferedReader configReader = new BufferedReader(new FileReader(file)); String line; while ((line = configReader.readLine()) != null) { line = line.trim(); if (line.startsWith("[") && line.endsWith("]")) { if (line.replace("[", "").replace("]", "").split(" ")[0].equals(settingName)) { delete = true; found = true; } else if (delete) { delete = false; newConfig.append(line).append("\n"); } else { newConfig.append(line).append("\n"); } } else if (!delete) { newConfig.append(line).append("\n"); } } configReader.close(); BufferedWriter configFile = new BufferedWriter(new FileWriter(file)); configFile.write(newConfig.toString()); configFile.newLine(); configFile.close(); } catch (IOException e) { e.printStackTrace(); } return found; } /** *Reads all settings saved in a file>
*
* @param file from which the settings should be read from
* @return the settings
*
* @since 1.12.0
*/
public static TreeMap Writes settings (could be more than one) to a file