From eb34edb11067cd0547ad6fa03cc914607bb01a18 Mon Sep 17 00:00:00 2001 From: swiftsatchel Date: Tue, 14 Dec 2021 19:59:50 -0500 Subject: [PATCH 1/5] Close resource leaks with try-with-resource --- .../java/me/soggysandwich/bedroom/Main.java | 21 +++++++------------ .../soggysandwich/bedroom/util/Settings.java | 7 +++---- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/main/java/me/soggysandwich/bedroom/Main.java b/src/main/java/me/soggysandwich/bedroom/Main.java index be5797f..8dfb2d6 100644 --- a/src/main/java/me/soggysandwich/bedroom/Main.java +++ b/src/main/java/me/soggysandwich/bedroom/Main.java @@ -455,26 +455,19 @@ private static void createHistoryFileAt(Path path) { try { - if (shiftHistoryFile.createNewFile()) { // If the file does not exist attempt to make it: + if (shiftHistoryFile.createNewFile()) { // If the file does not exist attempt to make it + try (FileWriter writer = new FileWriter(shiftHistoryFile)) { + if (shiftHistory != null) { - FileWriter writer = new FileWriter(shiftHistoryFile); + writer.write(shiftHistory.toString()); // Write history - if (shiftHistory != null) { - - writer.write(shiftHistory.toString()); // Write history - - } else writer.write("{}"); // If history is null, just write empty brackets - - writer.close(); - - // Else if it exists, attempt to delete and remake it: - } else if (shiftHistoryFile.delete()) saveHistoryToFile(); + } else writer.write("{}"); // If history is null, just write empty brackets + } + } else if (shiftHistoryFile.delete()) saveHistoryToFile(); // delete and remake it if it does exists } catch (Exception e) { - e.printStackTrace(); new AlertDialog(wnd, "Unable to save history to file."); - } } diff --git a/src/main/java/me/soggysandwich/bedroom/util/Settings.java b/src/main/java/me/soggysandwich/bedroom/util/Settings.java index 6d68b17..7063253 100644 --- a/src/main/java/me/soggysandwich/bedroom/util/Settings.java +++ b/src/main/java/me/soggysandwich/bedroom/util/Settings.java @@ -272,11 +272,10 @@ private static String readShiftHistory() { File file = new File(Settings.getWorkingDir() + File.separator + "shift.history"); // Get file if (file.exists()) { - try { + try (Scanner reader = new Scanner(file)) { - Scanner reader = new Scanner(file); // Make a new scanner - if (reader.hasNextLine()) // If there is a line to read: - return reader.nextLine(); // Read line of data + if (reader.hasNextLine()) // Read the next line (we only save history in a single line) + return reader.nextLine(); } catch (FileNotFoundException e) { e.printStackTrace(); From e077c3cd5b3e59bf4c17cbc0bea3a8e1d8e6072b Mon Sep 17 00:00:00 2001 From: swiftsatchel Date: Tue, 14 Dec 2021 20:08:13 -0500 Subject: [PATCH 2/5] fix: add order tool tip would show incorrect order amount --- src/main/java/me/soggysandwich/bedroom/Main.java | 6 +++--- .../bedroom/dialog/time/SelectTimeDialog.java | 10 +++++----- src/main/java/me/soggysandwich/bedroom/main/UI.java | 4 ++-- src/main/java/me/soggysandwich/bedroom/util/Ops.java | 4 +--- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/java/me/soggysandwich/bedroom/Main.java b/src/main/java/me/soggysandwich/bedroom/Main.java index 8dfb2d6..63a5923 100644 --- a/src/main/java/me/soggysandwich/bedroom/Main.java +++ b/src/main/java/me/soggysandwich/bedroom/Main.java @@ -257,7 +257,7 @@ private static String getStats() { } private static String getUntilTargetText() { - int ordersNeeded = getOrdersNeededForTarget(); + int ordersNeeded = getOrdersLeftForTarget(); if (ordersNeeded > 0) { return "$u until target of $t/hr" @@ -268,8 +268,8 @@ private static String getUntilTargetText() { } } - // Tell us how many orders we need to reach our target - public static int getOrdersNeededForTarget() { + /** Returns how many orders user has left to reach their target */ + public static int getOrdersLeftForTarget() { double neededForTarget = (double) secondsWorked/3600 * target; if (neededForTarget > orders) { diff --git a/src/main/java/me/soggysandwich/bedroom/dialog/time/SelectTimeDialog.java b/src/main/java/me/soggysandwich/bedroom/dialog/time/SelectTimeDialog.java index 6af66b6..59d0963 100644 --- a/src/main/java/me/soggysandwich/bedroom/dialog/time/SelectTimeDialog.java +++ b/src/main/java/me/soggysandwich/bedroom/dialog/time/SelectTimeDialog.java @@ -33,8 +33,10 @@ public SelectTimeDialog(BedroomWindow parent, TimeWindowType type) { init(); } - // Creates a continued select time dialog , given a parent and window type, plus the last selected time - // and the original parent of this group of select time dialogs + /** + * Creates a continued select time dialog , given a parent and window type, plus the last selected time + * and the original parent of this group of select time dialogs + */ public SelectTimeDialog(SelectTimeDialog parent, TimeWindowType type, LocalDateTime lastTime) { this.type = type; initParent = parent.getInitParent(); @@ -87,9 +89,7 @@ public void close() { } else initParent.setEnabled(true); // Re-enable parent } - /** - * Dispose this window and its parent, to finish this set and clear up memory - */ + /** Dispose this window and its parent, to finish this set and clear up memory */ protected void finish() { if (lastDialog != null) lastDialog.dispose(); dispose(); diff --git a/src/main/java/me/soggysandwich/bedroom/main/UI.java b/src/main/java/me/soggysandwich/bedroom/main/UI.java index 9f0112f..c9d7d67 100644 --- a/src/main/java/me/soggysandwich/bedroom/main/UI.java +++ b/src/main/java/me/soggysandwich/bedroom/main/UI.java @@ -82,9 +82,9 @@ public void display(String message) { private void setAddOrderToolTip() { - if (Main.getOrdersNeededForTarget() > Main.getOrders()) { // Tell us how many orders we need to reach our target + if (Main.getOrdersLeftForTarget() > 0) { // Tell us how many orders we need to reach our target addOrder.setToolTipText("You are $n orders behind your hourly target." - .replace("$n", String.valueOf(Main.getOrdersNeededForTarget()))); + .replace("$n", String.valueOf(Main.getOrdersLeftForTarget()))); } else if (Main.getOrders() > Main.getOrdersNeeded()) { addOrder.setToolTipText("You are done for the day!"); } else { // If we have gotten all the orders needed for our shift. diff --git a/src/main/java/me/soggysandwich/bedroom/util/Ops.java b/src/main/java/me/soggysandwich/bedroom/util/Ops.java index 48fe3a3..f62c856 100644 --- a/src/main/java/me/soggysandwich/bedroom/util/Ops.java +++ b/src/main/java/me/soggysandwich/bedroom/util/Ops.java @@ -37,9 +37,7 @@ public static String addZeroUnder10(int number) { return (number < 10) ? "0" + number : String.valueOf(number); } - /** - * Sets hand cursor on the needed components in the given JPanel - */ + /** Sets hand cursor on the needed components in the given JPanel */ public static void setHandCursorOnCompsFrom(Container container) { for (Component c : container.getComponents()) { // Go through the component list of this container From 4082f4d61559cafcd3aba751139e4885f2e08d49 Mon Sep 17 00:00:00 2001 From: swiftsatchel Date: Tue, 14 Dec 2021 20:09:28 -0500 Subject: [PATCH 3/5] remove jvm args from build.gradle --- build.gradle | 1 - 1 file changed, 1 deletion(-) diff --git a/build.gradle b/build.gradle index 9f2c6cd..d591fbf 100644 --- a/build.gradle +++ b/build.gradle @@ -14,7 +14,6 @@ repositories { application { mainClass = 'me.soggysandwich.bedroom.Main' - applicationDefaultJvmArgs = ['-Xmx32M', '-Xms16M'] } jar { From 799a6b8f9f7fa9489ab5605cc1d8035fb37f3552 Mon Sep 17 00:00:00 2001 From: swiftsatchel Date: Tue, 14 Dec 2021 20:09:42 -0500 Subject: [PATCH 4/5] bump version to 3.1.2 --- build.gradle | 2 +- src/main/java/me/soggysandwich/bedroom/Main.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index d591fbf..958d6da 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { } group 'me.soggysandwich' -version '3.1.1' +version '3.1.2' sourceCompatibility = JavaVersion.VERSION_16 targetCompatibility = JavaVersion.VERSION_16 diff --git a/src/main/java/me/soggysandwich/bedroom/Main.java b/src/main/java/me/soggysandwich/bedroom/Main.java index 63a5923..d81d17d 100644 --- a/src/main/java/me/soggysandwich/bedroom/Main.java +++ b/src/main/java/me/soggysandwich/bedroom/Main.java @@ -26,7 +26,7 @@ public class Main { // ======= Global Variables ======= - public static final String VERSION = "3.1.1"; + public static final String VERSION = "3.1.2"; public static final Preferences userPrefs = Preferences.userRoot(); // User preferences directory // ======= Variables ======= From e4fbd7f5a86124783400543b66094375b5c53920 Mon Sep 17 00:00:00 2001 From: swiftsatchel Date: Tue, 14 Dec 2021 21:21:12 -0500 Subject: [PATCH 5/5] fix: add warning before opening .jar startup items To not create an endless loop of Bedroom processes + code cleanup --- .../java/me/soggysandwich/bedroom/Main.java | 80 ++++++++++--------- .../soggysandwich/bedroom/util/Settings.java | 8 +- 2 files changed, 48 insertions(+), 40 deletions(-) diff --git a/src/main/java/me/soggysandwich/bedroom/Main.java b/src/main/java/me/soggysandwich/bedroom/Main.java index d81d17d..59589c8 100644 --- a/src/main/java/me/soggysandwich/bedroom/Main.java +++ b/src/main/java/me/soggysandwich/bedroom/Main.java @@ -1,6 +1,7 @@ package me.soggysandwich.bedroom; import me.soggysandwich.bedroom.dialog.alert.AlertDialog; +import me.soggysandwich.bedroom.dialog.alert.YesNoDialog; import me.soggysandwich.bedroom.dialog.time.SelectTimeDialog; import me.soggysandwich.bedroom.util.TimeWindowType; import me.soggysandwich.bedroom.main.BedroomWindow; @@ -20,6 +21,7 @@ import java.time.LocalDateTime; import java.time.format.DateTimeParseException; import java.time.temporal.ChronoUnit; +import java.util.Locale; import java.util.TreeMap; import java.util.prefs.Preferences; @@ -61,25 +63,12 @@ public static void main(String[] args) { Theme.setColors(); // Set extra color accents through UIManager init(); SwingUtilities.invokeLater(() -> { - Main.openStartupItems(); - - try { // Try to load shift history - shiftHistory = Settings.loadShiftHistory(); - } catch (NumberFormatException e) { // If unable to load due to NumberFormatException show error: - new AlertDialog(null, """ - Bedroom was unable to load - your past shift history as - a character loaded was not - a number. Please check - your history file."""); - } - + Main.loadShiftHistory(); }); // Create a timer to run every second, updating the time new Timer(1000, e -> update()).start(); - System.out.println(LocalDateTime.now().toString().substring(0, 16)); } @@ -134,33 +123,52 @@ private static boolean lastSavedBreakIsInShift() { } - private static void openStartupItems() { - - for (String location : Settings.getStartupItemsList()) { - - if (!location.equals("")) { - - File workApp = new File(location); - if (workApp.exists()) { - - try { - Desktop.getDesktop().open(workApp); - } catch (IOException e) { - e.printStackTrace(); - } + private static void loadShiftHistory() { + try { // Try to load shift history + shiftHistory = Settings.loadShiftHistory(); + } catch (NumberFormatException e) { // If unable to load due to NumberFormatException show error: + new AlertDialog(null, """ + Bedroom was unable to load + your past shift history as + a character loaded was not + a number. Please check + your history file."""); + } + } - } else { - new AlertDialog(wnd, """ - One of your startup items was - not able to be started as it - no longer exists. Please go to - Settings > Manage Startup Items."""); - } + private static void openStartupItems() { + String[] list = Settings.getStartupItemsList(); + for (String location : list) { + if (!location.isEmpty()) { + openItem(location); } - } + } + private static void openItem(String loc) { + File workApp = new File(loc); + if (!loc.toLowerCase(Locale.ROOT).endsWith(".jar") || new YesNoDialog(null, """ + For safety reasons, Bedroom does not + automatically open .jar files so users + do not create an endless loop of + Bedroom processes, is this startup item + ok to run?: + + """ + loc).accepted()) { + + if (workApp.exists()) { + try { + Desktop.getDesktop().open(workApp); + } catch (IOException e) { + e.printStackTrace(); + } + } else new AlertDialog(wnd, """ + One of your startup items was + not able to be started as it + no longer exists. Please go to + Settings > Manage Startup Items."""); + } } public static void updateSettings() { diff --git a/src/main/java/me/soggysandwich/bedroom/util/Settings.java b/src/main/java/me/soggysandwich/bedroom/util/Settings.java index 7063253..f91388b 100644 --- a/src/main/java/me/soggysandwich/bedroom/util/Settings.java +++ b/src/main/java/me/soggysandwich/bedroom/util/Settings.java @@ -176,7 +176,7 @@ public static boolean isDoneLoadingShiftHistory() { * * @return an ArrayList of the String's items */ - public static ArrayList getStartupItemsList() { + public static String[] getStartupItemsList() { ArrayList list = new ArrayList<>(); String str = Main.userPrefs.get("workApps", "[]"); @@ -188,15 +188,15 @@ public static ArrayList getStartupItemsList() { if (str.charAt(i) != 44) { // If it is not a comma, extend end point end++; } else { // Else return the string we have - list.add(str.substring(start, end)); + if (!str.substring(start,end).isBlank()) list.add(str.substring(start, end)); start = i+2; // Go 2 characters ahead to avoid the space in between items. end = i+1; } } - list.add(str.substring(start, end)); + if (!str.substring(start, end).isBlank()) list.add(str.substring(start, end)); - return list; + return list.toArray(new String[0]); }