Skip to content

Commit 81684e9

Browse files
committed
fix: bug fixes
1 parent 2e43592 commit 81684e9

File tree

11 files changed

+731
-136
lines changed

11 files changed

+731
-136
lines changed

src/main/java/com/mycompany/autobackupprogram/BackupManagerGUI.java

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.awt.Color;
66
import java.awt.Component;
77
import java.awt.Desktop;
8+
import java.awt.Dimension;
89
import java.awt.HeadlessException;
910
import java.awt.Image;
1011
import java.awt.Toolkit;
@@ -23,7 +24,9 @@
2324
import javax.swing.JCheckBox;
2425
import javax.swing.JFileChooser;
2526
import javax.swing.JOptionPane;
27+
import javax.swing.JScrollPane;
2628
import javax.swing.JTable;
29+
import javax.swing.JTextArea;
2730
import javax.swing.JTextField;
2831
import javax.swing.SwingUtilities;
2932
import javax.swing.UIManager;
@@ -78,7 +81,7 @@ public BackupManagerGUI() {
7881
displayBackupList(backups);
7982
} catch (IOException ex) {
8083
backups = null;
81-
Logger.logMessage(ex.getMessage());
84+
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
8285
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
8386
}
8487

@@ -154,7 +157,7 @@ private void OpenFolder(String path) {
154157
try {
155158
desktop.open(folder);
156159
} catch (IOException ex) {
157-
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
160+
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
158161
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
159162
}
160163
} else {
@@ -317,7 +320,7 @@ private void SaveWithName() {
317320
JOptionPane.showMessageDialog(this, "Backup '" + currentBackup.getBackupName() + "' saved successfully!", "Backup saved", JOptionPane.INFORMATION_MESSAGE);
318321
savedChanges(true);
319322
} catch (IllegalArgumentException ex) {
320-
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
323+
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
321324
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
322325
} catch (HeadlessException ex) {
323326
Logger.logMessage("Error saving backup", Logger.LogLevel.WARN);
@@ -413,7 +416,7 @@ public void setStringToText() {
413416
String last_date = LocalDateTime.now().format(formatter);
414417
lastBackupLabel.setText("last backup: " + last_date);
415418
} catch(Exception ex) {
416-
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
419+
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
417420
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
418421
}
419422
}
@@ -422,7 +425,7 @@ public void setTextValues() {
422425
try {
423426
updateCurrentFiedsByBackup(currentBackup);
424427
} catch (IllegalArgumentException ex) {
425-
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
428+
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
426429
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
427430
}
428431
setAutoBackupPreference(currentBackup.isAutoBackup());
@@ -499,32 +502,50 @@ public void SetLastBackupLabel(LocalDateTime date) {
499502
public static void OpenExceptionMessage(String errorMessage, String stackTrace) {
500503
Object[] options = {"Close", "Copy to clipboard", "Report the Problem"};
501504

502-
if (errorMessage == null ) {
505+
if (errorMessage == null) {
503506
errorMessage = "";
504507
}
505508
stackTrace = !errorMessage.isEmpty() ? errorMessage + "\n" + stackTrace : errorMessage + stackTrace;
506-
String stackTraceMessage = "Please report this error, either with an image of the screen or by copying the following error text (it is appreciable to provide a description of the operations performed before the error): \n" + stackTrace;
509+
String stackTraceMessage = "Please report this error, either with an image of the screen or by copying the following error text (it is appreciable to provide a description of the operations performed before the error): \n" + stackTrace;
507510

508511
int choice;
509512

513+
// Set a maximum width for the error message
514+
final int MAX_WIDTH = 500;
515+
510516
// Keep displaying the dialog until the "Close" option (index 0) is chosen
511517
do {
512518
if (stackTraceMessage.length() > 1500) {
513-
stackTraceMessage = stackTraceMessage.substring(0, 1500) + "...";
519+
stackTraceMessage = stackTraceMessage.substring(0, 1500) + "...";
514520
}
515-
516-
// Display the option dialog
521+
522+
// Create a JTextArea to hold the error message with line wrapping
523+
JTextArea messageArea = new JTextArea(stackTraceMessage);
524+
messageArea.setLineWrap(true);
525+
messageArea.setWrapStyleWord(true);
526+
messageArea.setEditable(false);
527+
messageArea.setColumns(50); // Approximate width, adjust as necessary
528+
529+
// Limit the maximum width
530+
messageArea.setSize(new Dimension(MAX_WIDTH, Integer.MAX_VALUE));
531+
messageArea.setPreferredSize(new Dimension(MAX_WIDTH, messageArea.getPreferredSize().height));
532+
533+
// Put the JTextArea in a JScrollPane for scrollable display if needed
534+
JScrollPane scrollPane = new JScrollPane(messageArea);
535+
scrollPane.setPreferredSize(new Dimension(MAX_WIDTH, 300));
536+
537+
// Display the option dialog with the JScrollPane
517538
choice = JOptionPane.showOptionDialog(
518539
null,
519-
stackTraceMessage, // The detailed message or stack trace
520-
"Error...", // The error message/title
521-
JOptionPane.DEFAULT_OPTION, // Option type (default option type)
522-
JOptionPane.ERROR_MESSAGE, // Message type (error message icon)
523-
null, // Icon (null means default icon)
524-
options, // The options for the buttons
525-
options[0] // The default option (Close)
540+
scrollPane, // The JScrollPane containing the error message
541+
"Error...", // The error message/title
542+
JOptionPane.DEFAULT_OPTION, // Option type (default option type)
543+
JOptionPane.ERROR_MESSAGE, // Message type (error message icon)
544+
null, // Icon (null means default icon)
545+
options, // The options for the buttons
546+
options[0] // The default option (Close)
526547
);
527-
548+
528549
if (choice == 1) {
529550
StringSelection selection = new StringSelection(stackTrace);
530551
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, null);
@@ -657,12 +678,14 @@ public void Clear() {
657678
Logger.logMessage("Event --> clear", Logger.LogLevel.INFO);
658679

659680
if ((!saveChanged && !currentBackup.getBackupName().isEmpty()) || (!startPathField.getText().isEmpty() || !destinationPathField.getText().isEmpty() || !backupNoteTextArea.getText().isEmpty())) {
660-
int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to clean the fields?", "Confimation required", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
681+
int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to clean the fields?", "Confimation required", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
661682
if (response != JOptionPane.YES_OPTION) {
662683
return;
663684
}
664685
}
665686

687+
saveChanged = false;
688+
setCurrentBackupName("");
666689
startPathField.setText("");
667690
destinationPathField.setText("");
668691
lastBackupLabel.setText("");
@@ -713,15 +736,15 @@ private void saveFile() {
713736
BackupOperations.updateBackupList(backups);
714737
savedChanges(true);
715738
} catch (IllegalArgumentException ex) {
716-
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
739+
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
717740
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
718741
}
719742
}
720743

721744
private void OpenBackup(String backupName) {
722745
Logger.logMessage("Event --> opening backup", Logger.LogLevel.INFO);
723746

724-
if ((!saveChanged && !currentBackup.getBackupName().isEmpty()) || (!startPathField.getText().isEmpty() || !destinationPathField.getText().isEmpty() || !backupNoteTextArea.getText().isEmpty())) {
747+
if (!saveChanged) {
725748
int response = JOptionPane.showConfirmDialog(null, "There are unsaved changes, do you want to save them before moving to another file?", "Confimation required", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
726749
if (response == JOptionPane.YES_OPTION) {
727750
saveFile();
@@ -742,7 +765,7 @@ private void OpenBackup(String backupName) {
742765
backupNoteTextArea.setEnabled(true);
743766
savedChanges(true);
744767
} catch (IllegalArgumentException ex) {
745-
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
768+
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
746769
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
747770
}
748771
}
@@ -811,7 +834,7 @@ private void updateCurrentFiedsByBackup(Backup backup) {
811834
private void NewBackup() {
812835
Logger.logMessage("Event --> new backup", Logger.LogLevel.INFO);
813836

814-
if ((!saveChanged && !currentBackup.getBackupName().isEmpty()) || (startPathField.getText().length() != 0 || destinationPathField.getText().length() != 0 || backupNoteTextArea.getText().length() != 0)) {
837+
if (!saveChanged) {
815838
int response = JOptionPane.showConfirmDialog(null, "There are unsaved changes, do you want to save them before moving to another backup?", "Confimation required", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
816839
if (response == JOptionPane.YES_OPTION) {
817840
saveFile();
@@ -1819,12 +1842,6 @@ private void btnTimePickerActionPerformed(java.awt.event.ActionEvent evt) {//GEN
18191842
JOptionPane.showMessageDialog(null, "Auto Backup has been activated\n\tFrom: " + startPathField.getText() + "\n\tTo: " + destinationPathField.getText() + "\nIs setted every " + timeInterval.toString() + " days", "AutoBackup", 1);
18201843
}//GEN-LAST:event_btnTimePickerActionPerformed
18211844

1822-
public static void main(String args[]) {
1823-
java.awt.EventQueue.invokeLater(() -> {
1824-
new BackupManagerGUI().setVisible(true);
1825-
});
1826-
}
1827-
18281845
// Variables declaration - do not modify//GEN-BEGIN:variables
18291846
private javax.swing.JCheckBoxMenuItem AutoBackupMenuItem;
18301847
private javax.swing.JMenu Backup;

src/main/java/com/mycompany/autobackupprogram/BackupOperations.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121
import java.util.zip.ZipEntry;
2222
import java.util.zip.ZipOutputStream;
2323

24+
import javax.management.OperationsException;
2425
import javax.swing.JButton;
2526
import javax.swing.JOptionPane;
2627
import javax.swing.JToggleButton;
2728
import javax.swing.SwingUtilities;
2829

30+
import com.mycompany.autobackupprogram.Logger.LogLevel;
31+
2932
public class BackupOperations{
3033

3134
private static final JSONAutoBackup JSON = new JSONAutoBackup();
@@ -38,34 +41,37 @@ public static void SingleBackup(Backup backup, TrayIcon trayIcon, BackupProgress
3841

3942
if (singleBackupBtn != null) singleBackupBtn.setEnabled(false);
4043
if (autoBackupBtn != null) autoBackupBtn.setEnabled(false);
44+
45+
try {
46+
String temp = "\\";
47+
String path1 = backup.getInitialPath();
48+
String path2 = backup.getDestinationPath();
4149

42-
System.out.println(singleBackupBtn.isEnabled());
43-
44-
String temp = "\\";
45-
String path1 = backup.getInitialPath();
46-
String path2 = backup.getDestinationPath();
50+
if(!CheckInputCorrect(backup.getBackupName(), path1, path2, trayIcon)) return;
4751

48-
if(!CheckInputCorrect(backup.getBackupName(), path1, path2, trayIcon)) return;
52+
LocalDateTime dateNow = LocalDateTime.now();
53+
String date = dateNow.format(dateForfolderNameFormatter);
54+
String name1 = path1.substring(path1.length() - 1);
4955

50-
LocalDateTime dateNow = LocalDateTime.now();
51-
String date = dateNow.format(dateForfolderNameFormatter);
52-
String name1 = path1.substring(path1.length() - 1);
56+
for(int i = path1.length() - 1; i >= 0; i--) {
57+
if(path1.charAt(i) != temp.charAt(0)) name1 = path1.charAt(i) + name1;
58+
else break;
59+
}
5360

54-
for(int i = path1.length() - 1; i >= 0; i--) {
55-
if(path1.charAt(i) != temp.charAt(0)) name1 = path1.charAt(i) + name1;
56-
else break;
57-
}
61+
path2 = path2 + "\\" + name1 + " (Backup " + date + ")";
5862

59-
path2 = path2 + "\\" + name1 + " (Backup " + date + ")";
60-
61-
try {
6263
zipDirectory(path1, path2+".zip", backup, trayIcon, progressBar, singleBackupBtn, autoBackupBtn);
6364
} catch (IOException e) {
6465
Logger.logMessage("Error during the backup operation: the initial path is incorrect!", Logger.LogLevel.WARN);
6566
JOptionPane.showMessageDialog(null, "Error during the backup operation: the initial path is incorrect!", "Error", JOptionPane.ERROR_MESSAGE);
6667
if (singleBackupBtn != null) singleBackupBtn.setEnabled(true);
6768
if (autoBackupBtn != null) autoBackupBtn.setEnabled(true);
68-
}
69+
} catch (Exception ex) {
70+
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
71+
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
72+
if (singleBackupBtn != null) singleBackupBtn.setEnabled(true);
73+
if (autoBackupBtn != null) autoBackupBtn.setEnabled(true);
74+
}
6975
}
7076

7177
private static void updateAfterBackup(String path1, String path2, Backup backup, TrayIcon trayIcon, JButton singleBackupBtn, JToggleButton autoBackupBtn) {
@@ -108,7 +114,7 @@ private static void updateAfterBackup(String path1, String path2, Backup backup,
108114
trayIcon.displayMessage("Backup Manager", "Backup: "+ backup.getBackupName() +"\nThe backup was successfully completed:\nFrom: " + path1 + "\nTo: " + path2, TrayIcon.MessageType.INFO);
109115
}
110116
} catch (IllegalArgumentException ex) {
111-
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
117+
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
112118
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
113119
} catch (Exception e) {
114120
Logger.logMessage("Error saving file", Logger.LogLevel.WARN);
@@ -155,6 +161,8 @@ public static boolean CheckInputCorrect(String backupName, String path1, String
155161
}
156162

157163
public static void zipDirectory(String sourceDirectoryPath, String targetZipPath, Backup backup, TrayIcon trayIcon, BackupProgressGUI progressBar, JButton singleBackupBtn, JToggleButton autoBackupBtn) throws IOException { // Track copied files
164+
Logger.logMessage("Starting zipping process", LogLevel.INFO);
165+
158166
File file = new File(sourceDirectoryPath);
159167
int totalFilesCount = file.isDirectory() ? countFilesInDirectory(file) : 1;
160168

@@ -222,8 +230,7 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
222230
});
223231
}
224232
} catch (IOException ex) {
225-
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
226-
ex.printStackTrace(); // Handle the exception as necessary
233+
Logger.logMessage("An error occurred: " + ex.getMessage() , Logger.LogLevel.ERROR, ex);
227234
if (singleBackupBtn != null) singleBackupBtn.setEnabled(true);
228235
if (autoBackupBtn != null) autoBackupBtn.setEnabled(true);
229236
}

src/main/java/com/mycompany/autobackupprogram/BackupService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void run() {
117117
Logger.logMessage("No backup needed at this time.", Logger.LogLevel.INFO);
118118
}
119119
} catch (IOException ex) {
120-
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
120+
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
121121
ex.printStackTrace();
122122
}
123123
}

src/main/java/com/mycompany/autobackupprogram/JSONAutoBackup.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public List<Backup> ReadBackupListFromJSON(String filename, String directoryPath
7575
}
7676

7777
} catch (IOException | ParseException ex) {
78-
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
78+
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
7979
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
8080
ex.printStackTrace();
8181
}
@@ -108,7 +108,7 @@ public void UpdateBackupListJSON(String filename, String directoryPath, List<Bac
108108
file.write(updatedBackupArray.toJSONString());
109109
file.flush();
110110
} catch (IOException ex) {
111-
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
111+
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
112112
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
113113
}
114114
}
@@ -148,7 +148,7 @@ public void UpdateSingleBackupInJSON(String filename, String directoryPath, Back
148148
}
149149

150150
} catch (IOException | ParseException ex) {
151-
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
151+
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
152152
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
153153
}
154154
}

src/main/java/com/mycompany/autobackupprogram/JSONConfigReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public JSONConfigReader(String filename, String directoryPath) {
2020

2121
public boolean isLogLevelEnabled(String level) {
2222
if (config == null) {
23-
Logger.logMessage("Configuration not loaded. Cannot check log level.", Logger.LogLevel.ERROR);
23+
Logger.logMessage("Configuration not loaded. Cannot check log level", Logger.LogLevel.ERROR);
2424
return false;
2525
}
2626

@@ -34,7 +34,7 @@ public boolean isLogLevelEnabled(String level) {
3434

3535
public boolean isMenuItemEnabled(String menuItem) {
3636
if (config == null) {
37-
Logger.logMessage("Configuration not loaded. Cannot check menu items.", Logger.LogLevel.ERROR);
37+
Logger.logMessage("Configuration not loaded. Cannot check menu items", Logger.LogLevel.ERROR);
3838
return false;
3939
}
4040

src/main/java/com/mycompany/autobackupprogram/MainApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static void main(String[] args) {
2828
try {
2929
service.startService();
3030
} catch (IOException ex) {
31-
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
31+
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
3232
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
3333
}
3434
}

0 commit comments

Comments
 (0)