Skip to content

Commit

Permalink
Bugfixes for opening new Files and worked time (#197)
Browse files Browse the repository at this point in the history
* Fixed Make Entry dialog disposing every time

* Fixed required break time and added source

* Cleared File Open issues
- Swapped Operation order to clear workspace properly when opening a new file
- Swapped operation order to properly close the previous file when making a new, blank file

* spotless:apply

* Fixed time message
- Also made variables for the minimum break times
  • Loading branch information
Just1Developer authored Dec 4, 2024
1 parent bb5754d commit 0a54c0c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
33 changes: 25 additions & 8 deletions src/main/java/ui/DialogHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ private DialogHelper() {
static final Pattern TIME_PATTERN_SEMI_SMALL = Pattern.compile("^(\\d{1,2}):(\\d)$");

private static final int MAX_TEXT_LENGTH_ACTIVITY = 27;
private static final int MIN_BREAK_SIX_HOURS = 30;
private static final int MIN_BREAK_NINE_HOURS = 45;

private static final String ACTIVITY_MESSAGE = "You need to enter an activity!";

Expand Down Expand Up @@ -217,8 +219,8 @@ public void focusLost(FocusEvent e) {

// Action listeners for buttons
makeEntryButton.addActionListener(e -> {
makeEntryAction(parentUi, durationWarningLabel, actionTextArea, timeFields, vacationCheckBox);
dialog.dispose();
if (makeEntryAction(parentUi, durationWarningLabel, actionTextArea, timeFields, vacationCheckBox))
dialog.dispose();
});

cancelButton.addActionListener(e -> {
Expand Down Expand Up @@ -261,7 +263,19 @@ private void update() {
dialog.setVisible(true);
}

private static void makeEntryAction(UserInterface parentUI, JLabel durationWarningLabel, JTextArea actionTextArea, JTextField[] timeFields,
/**
* The Action for the "Make Entry" Button in the "Add/Edit Entry" Dialog.
* Returns if the dialog should be disposed (success) or if it should be kept
* open (failure).
*
* @param parentUI The parent UserInterface.
* @param durationWarningLabel The warning label for the work time message.
* @param actionTextArea The text area for the activity.
* @param timeFields The array of fields for the times.
* @param vacationCheckBox The vacation checkbox.
* @return True if the dialog should be disposed, false if not.
*/
private static boolean makeEntryAction(UserInterface parentUI, JLabel durationWarningLabel, JTextArea actionTextArea, JTextField[] timeFields,
JCheckBox vacationCheckBox) {
if (durationWarningLabel.getText().isBlank()) {
if (actionTextArea.getText().isBlank()) {
Expand Down Expand Up @@ -293,7 +307,7 @@ private static void makeEntryAction(UserInterface parentUI, JLabel durationWarni
timer.setRepeats(false); // Only execute once
timer.start();

return;
return false;
}

// No break
Expand All @@ -306,6 +320,7 @@ private static void makeEntryAction(UserInterface parentUI, JLabel durationWarni
vacationCheckBox.isSelected());
parentUI.addEntry(newEntry);
parentUI.setHasUnsavedChanges(true);
return true;
}

private static void updateTimeFieldView(int index, int breakFieldIndex, JTextField[] timeFields, JLabel[] errorLabels, JLabel durationSummaryValue,
Expand Down Expand Up @@ -483,10 +498,12 @@ else if (minutes == 0)
if (!isVacation.isSelected()) {
long totalMinutes = workDuration.toMinutes();
long breakMinutes = breakTime.getHour() * 60L + breakTime.getMinute();
if (totalMinutes >= 540 && breakMinutes < 60) {
durationWarningLabel.setText("Break must be at least 1 hour for work of 9 hours or more");
} else if (totalMinutes >= 360 && breakMinutes < 30) {
durationWarningLabel.setText("Break must be at least 30 minutes for work over 6 hours");
// FROM:
// https://www.gesetze-im-internet.de/arbzg/__4.html#:~:text=Arbeitszeitgesetz%20(ArbZG),neun%20Stunden%20insgesamt%20zu%20unterbrechen.
if (totalMinutes > 540 && breakMinutes < MIN_BREAK_NINE_HOURS) {
durationWarningLabel.setText("Break must be at least %d minutes for work of 9 hours or more".formatted(MIN_BREAK_NINE_HOURS));
} else if (totalMinutes > 360 && breakMinutes < MIN_BREAK_SIX_HOURS) {
durationWarningLabel.setText("Break must be at least %d minutes for work over 6 hours".formatted(MIN_BREAK_SIX_HOURS));
} else {
durationWarningLabel.setText(" ");
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/ui/UserInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ public boolean clearWorkspace() {
if (!closeCurrentOpenFile())
return false;
// Delete all content
monthSettingsBar.reset();
currentOpenFile = null;
listModel.clear();
monthSettingsBar.reset();
setHasUnsavedChanges(false);
return true;
}
Expand All @@ -224,7 +225,7 @@ public boolean clearWorkspace() {
*
* @return If proceed or not.
*/
public boolean closeCurrentOpenFile() {
private boolean closeCurrentOpenFile() {
if (!hasUnsavedChanges)
return true;
// Prompt to save
Expand Down Expand Up @@ -264,10 +265,9 @@ public void openFile() {
public void openFile(File openFile) {
if (openFile == null)
return;
if (!setEditorFile(openFile))
if (!clearWorkspace()) // Don't proceed: Clearing was cancelled
return;
boolean proceed = clearWorkspace();
if (!proceed)
if (!setEditorFile(openFile))
return;

// Open the file
Expand Down

0 comments on commit 0a54c0c

Please sign in to comment.