From 7ccead7d820f7c0a952553ac8711cbf03403e6a9 Mon Sep 17 00:00:00 2001 From: lautaroem1 Date: Tue, 25 Jun 2019 17:00:45 -0300 Subject: [PATCH] Now TimeLocks can use Date and Time --- .../java/app/controller/MainController.java | 60 ++++++++++++++++++- src/main/java/app/model/TimeLockSettings.java | 8 +-- .../app/service/timelock/TimeLockedFile.java | 8 +-- .../java/app/service/timelock/TimeLocker.java | 21 ++++--- src/main/resources/app/fxml/main.fxml | 2 + 5 files changed, 82 insertions(+), 17 deletions(-) diff --git a/src/main/java/app/controller/MainController.java b/src/main/java/app/controller/MainController.java index b20643a..014c964 100644 --- a/src/main/java/app/controller/MainController.java +++ b/src/main/java/app/controller/MainController.java @@ -5,6 +5,7 @@ import app.model.ProtectionCustomSetting; import app.model.TimeLockSettings; import app.service.TaskManager; +import app.time.converter.SafeLocalTimeStringConverter; import javafx.application.Platform; import javafx.collections.FXCollections; import javafx.fxml.FXML; @@ -20,6 +21,10 @@ import java.io.File; import java.net.URI; import java.net.URL; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; import java.util.LinkedHashMap; import java.util.ResourceBundle; import java.util.concurrent.CompletableFuture; @@ -56,6 +61,9 @@ public class MainController implements Initializable { @FXML private DatePicker timeLockUnlockDate; + @FXML + private Spinner timeLockUnlockTime; + @FXML private Button runButton; @@ -64,6 +72,7 @@ public void initialize(URL location, ResourceBundle resources) { setOperations(); setProtectionLevelOptions(); setProtectionDefaultCustomSetting(); + setTimePickerSettings(); } @FXML @@ -209,6 +218,46 @@ private void setProtectionDefaultCustomSetting() { protectionCustomSetting.setText(protectionAddRandomError); } + private void setTimePickerSettings() { + SafeLocalTimeStringConverter safeConverter = new SafeLocalTimeStringConverter( + DateTimeFormatter.ofPattern("HH:mm"), + DateTimeFormatter.ofPattern("HH:mm") + ); + + timeLockUnlockTime.valueFactoryProperty().setValue( + new SpinnerValueFactory() { + { + setConverter(safeConverter); + } + + @Override + public void decrement(int steps) { + if (getValue() == null) + setValue(LocalTime.now()); + else { + LocalTime time = getValue(); + setValue(time.minusMinutes(steps)); + } + } + + @Override + public void increment(int steps) { + if (this.getValue() == null) + setValue(LocalTime.now()); + else { + LocalTime time = getValue(); + setValue(time.plusMinutes(steps)); + } + } + } + ); + // Ensure the last "Time" in the spinner is a valid one. + timeLockUnlockTime.focusedProperty().addListener(observable -> { + String lastInput = timeLockUnlockTime.getEditor().getCharacters().toString(); + timeLockUnlockTime.getValueFactory().setValue(safeConverter.fromString(lastInput)); + }); + } + private Operations getOperations() { String selectedValue = operationsChoiceBox.getValue(); return Operations.fromString(selectedValue); @@ -230,9 +279,18 @@ private ProtectionCustomSetting getProtectionCustomSetting() { } private TimeLockSettings getTimeLockSettings() { + LocalDateTime unlockDateTime = null; + + LocalDate unlockDate = timeLockUnlockDate.getValue(); + LocalTime unlockTime = timeLockUnlockTime.getValue(); + + if (unlockDate != null && unlockTime != null) { + unlockDateTime = LocalDateTime.of(unlockDate, unlockTime); + } + return new TimeLockSettings( timeLockCheckBox.isSelected(), - timeLockUnlockDate.getValue() + unlockDateTime ); } diff --git a/src/main/java/app/model/TimeLockSettings.java b/src/main/java/app/model/TimeLockSettings.java index 5e18593..c22e918 100644 --- a/src/main/java/app/model/TimeLockSettings.java +++ b/src/main/java/app/model/TimeLockSettings.java @@ -1,12 +1,12 @@ package app.model; -import java.time.LocalDate; +import java.time.LocalDateTime; public class TimeLockSettings { private final boolean enabled; - private final LocalDate unlockDate; + private final LocalDateTime unlockDate; - public TimeLockSettings(boolean enabled, LocalDate unlockDate) { + public TimeLockSettings(boolean enabled, LocalDateTime unlockDate) { this.enabled = enabled; this.unlockDate = unlockDate; } @@ -15,7 +15,7 @@ public boolean isEnabled() { return enabled; } - public LocalDate getUnlockDate() { + public LocalDateTime getUnlockDate() { return unlockDate; } } diff --git a/src/main/java/app/service/timelock/TimeLockedFile.java b/src/main/java/app/service/timelock/TimeLockedFile.java index 11ba90c..0d5f36f 100644 --- a/src/main/java/app/service/timelock/TimeLockedFile.java +++ b/src/main/java/app/service/timelock/TimeLockedFile.java @@ -1,14 +1,14 @@ package app.service.timelock; import java.io.*; -import java.time.LocalDate; +import java.time.LocalDateTime; class TimeLockedFile implements Serializable { private final boolean locked; - private final LocalDate unlockDate; + private final LocalDateTime unlockDate; private final byte[] data; - TimeLockedFile(boolean locked, LocalDate unlockDate, byte[] data) { + TimeLockedFile(boolean locked, LocalDateTime unlockDate, byte[] data) { this.locked = locked; this.unlockDate = unlockDate; this.data = data; @@ -18,7 +18,7 @@ boolean isLocked() { return locked; } - LocalDate getUnlockDate() { + LocalDateTime getUnlockDate() { return unlockDate; } diff --git a/src/main/java/app/service/timelock/TimeLocker.java b/src/main/java/app/service/timelock/TimeLocker.java index fa2d60e..6d9ab80 100644 --- a/src/main/java/app/service/timelock/TimeLocker.java +++ b/src/main/java/app/service/timelock/TimeLocker.java @@ -1,9 +1,12 @@ package app.service.timelock; -import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; public class TimeLocker { - public static byte[] lock(byte[] dataBytes, LocalDate lockDate) { + private static final String DATE_FORMATTER = "dd/MM/yyyy HH:mm"; + + public static byte[] lock(byte[] dataBytes, LocalDateTime lockDate) { if (isValidLockDate(lockDate)) { TimeLockedFile lockedFile = new TimeLockedFile( true, @@ -16,7 +19,7 @@ public static byte[] lock(byte[] dataBytes, LocalDate lockDate) { throw new Error("Failed to Time Lock the File."); } } else { - throw new Error("Invalid Time Lock Date."); + throw new Error("Invalid Time Lock Date or Time."); } } @@ -24,11 +27,13 @@ public static byte[] unlock(byte[] dataBytes) throws Error { try { TimeLockedFile timeLockedFile = TimeLockedFile.fromByteArray(dataBytes); if (timeLockedFile.isLocked()) { - LocalDate unlockDate = timeLockedFile.getUnlockDate(); - if (LocalDate.now().isAfter(unlockDate)) { + LocalDateTime unlockDate = timeLockedFile.getUnlockDate(); + if (LocalDateTime.now().isAfter(unlockDate)) { return timeLockedFile.getData(); } else { - throw new Error("File is Time Locked until " + unlockDate.toString() + "!."); + throw new Error("File is Time Locked until " + + unlockDate.format(DateTimeFormatter.ofPattern(DATE_FORMATTER)) + + "!"); } } else { return timeLockedFile.getData(); @@ -47,7 +52,7 @@ public static byte[] buildUnlockedFile(byte[] dataBytes) throws Error { } } - private static boolean isValidLockDate(LocalDate lockDate) { - return LocalDate.now().isBefore(lockDate); + private static boolean isValidLockDate(LocalDateTime lockDate) { + return LocalDateTime.now().isBefore(lockDate); } } diff --git a/src/main/resources/app/fxml/main.fxml b/src/main/resources/app/fxml/main.fxml index cfaea9d..4251c10 100644 --- a/src/main/resources/app/fxml/main.fxml +++ b/src/main/resources/app/fxml/main.fxml @@ -9,6 +9,7 @@ + @@ -86,6 +87,7 @@