Skip to content

Commit

Permalink
fix: bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisTurco committed Nov 10, 2024
1 parent 2e43592 commit 81684e9
Show file tree
Hide file tree
Showing 11 changed files with 731 additions and 136 deletions.
75 changes: 46 additions & 29 deletions src/main/java/com/mycompany/autobackupprogram/BackupManagerGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.awt.Color;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
Expand All @@ -23,7 +24,9 @@
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
Expand Down Expand Up @@ -78,7 +81,7 @@ public BackupManagerGUI() {
displayBackupList(backups);
} catch (IOException ex) {
backups = null;
Logger.logMessage(ex.getMessage());
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
}

Expand Down Expand Up @@ -154,7 +157,7 @@ private void OpenFolder(String path) {
try {
desktop.open(folder);
} catch (IOException ex) {
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
}
} else {
Expand Down Expand Up @@ -317,7 +320,7 @@ private void SaveWithName() {
JOptionPane.showMessageDialog(this, "Backup '" + currentBackup.getBackupName() + "' saved successfully!", "Backup saved", JOptionPane.INFORMATION_MESSAGE);
savedChanges(true);
} catch (IllegalArgumentException ex) {
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
} catch (HeadlessException ex) {
Logger.logMessage("Error saving backup", Logger.LogLevel.WARN);
Expand Down Expand Up @@ -413,7 +416,7 @@ public void setStringToText() {
String last_date = LocalDateTime.now().format(formatter);
lastBackupLabel.setText("last backup: " + last_date);
} catch(Exception ex) {
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
}
}
Expand All @@ -422,7 +425,7 @@ public void setTextValues() {
try {
updateCurrentFiedsByBackup(currentBackup);
} catch (IllegalArgumentException ex) {
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
}
setAutoBackupPreference(currentBackup.isAutoBackup());
Expand Down Expand Up @@ -499,32 +502,50 @@ public void SetLastBackupLabel(LocalDateTime date) {
public static void OpenExceptionMessage(String errorMessage, String stackTrace) {
Object[] options = {"Close", "Copy to clipboard", "Report the Problem"};

if (errorMessage == null ) {
if (errorMessage == null) {
errorMessage = "";
}
stackTrace = !errorMessage.isEmpty() ? errorMessage + "\n" + stackTrace : errorMessage + stackTrace;
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;
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;

int choice;

// Set a maximum width for the error message
final int MAX_WIDTH = 500;

// Keep displaying the dialog until the "Close" option (index 0) is chosen
do {
if (stackTraceMessage.length() > 1500) {
stackTraceMessage = stackTraceMessage.substring(0, 1500) + "...";
stackTraceMessage = stackTraceMessage.substring(0, 1500) + "...";
}

// Display the option dialog

// Create a JTextArea to hold the error message with line wrapping
JTextArea messageArea = new JTextArea(stackTraceMessage);
messageArea.setLineWrap(true);
messageArea.setWrapStyleWord(true);
messageArea.setEditable(false);
messageArea.setColumns(50); // Approximate width, adjust as necessary

// Limit the maximum width
messageArea.setSize(new Dimension(MAX_WIDTH, Integer.MAX_VALUE));
messageArea.setPreferredSize(new Dimension(MAX_WIDTH, messageArea.getPreferredSize().height));

// Put the JTextArea in a JScrollPane for scrollable display if needed
JScrollPane scrollPane = new JScrollPane(messageArea);
scrollPane.setPreferredSize(new Dimension(MAX_WIDTH, 300));

// Display the option dialog with the JScrollPane
choice = JOptionPane.showOptionDialog(
null,
stackTraceMessage, // The detailed message or stack trace
"Error...", // The error message/title
JOptionPane.DEFAULT_OPTION, // Option type (default option type)
JOptionPane.ERROR_MESSAGE, // Message type (error message icon)
null, // Icon (null means default icon)
options, // The options for the buttons
options[0] // The default option (Close)
scrollPane, // The JScrollPane containing the error message
"Error...", // The error message/title
JOptionPane.DEFAULT_OPTION, // Option type (default option type)
JOptionPane.ERROR_MESSAGE, // Message type (error message icon)
null, // Icon (null means default icon)
options, // The options for the buttons
options[0] // The default option (Close)
);

if (choice == 1) {
StringSelection selection = new StringSelection(stackTrace);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, null);
Expand Down Expand Up @@ -657,12 +678,14 @@ public void Clear() {
Logger.logMessage("Event --> clear", Logger.LogLevel.INFO);

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

saveChanged = false;
setCurrentBackupName("");
startPathField.setText("");
destinationPathField.setText("");
lastBackupLabel.setText("");
Expand Down Expand Up @@ -713,15 +736,15 @@ private void saveFile() {
BackupOperations.updateBackupList(backups);
savedChanges(true);
} catch (IllegalArgumentException ex) {
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
}
}

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

if ((!saveChanged && !currentBackup.getBackupName().isEmpty()) || (!startPathField.getText().isEmpty() || !destinationPathField.getText().isEmpty() || !backupNoteTextArea.getText().isEmpty())) {
if (!saveChanged) {
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);
if (response == JOptionPane.YES_OPTION) {
saveFile();
Expand All @@ -742,7 +765,7 @@ private void OpenBackup(String backupName) {
backupNoteTextArea.setEnabled(true);
savedChanges(true);
} catch (IllegalArgumentException ex) {
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
}
}
Expand Down Expand Up @@ -811,7 +834,7 @@ private void updateCurrentFiedsByBackup(Backup backup) {
private void NewBackup() {
Logger.logMessage("Event --> new backup", Logger.LogLevel.INFO);

if ((!saveChanged && !currentBackup.getBackupName().isEmpty()) || (startPathField.getText().length() != 0 || destinationPathField.getText().length() != 0 || backupNoteTextArea.getText().length() != 0)) {
if (!saveChanged) {
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);
if (response == JOptionPane.YES_OPTION) {
saveFile();
Expand Down Expand Up @@ -1819,12 +1842,6 @@ private void btnTimePickerActionPerformed(java.awt.event.ActionEvent evt) {//GEN
JOptionPane.showMessageDialog(null, "Auto Backup has been activated\n\tFrom: " + startPathField.getText() + "\n\tTo: " + destinationPathField.getText() + "\nIs setted every " + timeInterval.toString() + " days", "AutoBackup", 1);
}//GEN-LAST:event_btnTimePickerActionPerformed

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(() -> {
new BackupManagerGUI().setVisible(true);
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JCheckBoxMenuItem AutoBackupMenuItem;
private javax.swing.JMenu Backup;
Expand Down
47 changes: 27 additions & 20 deletions src/main/java/com/mycompany/autobackupprogram/BackupOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.management.OperationsException;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;

import com.mycompany.autobackupprogram.Logger.LogLevel;

public class BackupOperations{

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

if (singleBackupBtn != null) singleBackupBtn.setEnabled(false);
if (autoBackupBtn != null) autoBackupBtn.setEnabled(false);

try {
String temp = "\\";
String path1 = backup.getInitialPath();
String path2 = backup.getDestinationPath();

System.out.println(singleBackupBtn.isEnabled());

String temp = "\\";
String path1 = backup.getInitialPath();
String path2 = backup.getDestinationPath();
if(!CheckInputCorrect(backup.getBackupName(), path1, path2, trayIcon)) return;

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

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

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

path2 = path2 + "\\" + name1 + " (Backup " + date + ")";

try {
zipDirectory(path1, path2+".zip", backup, trayIcon, progressBar, singleBackupBtn, autoBackupBtn);
} catch (IOException e) {
Logger.logMessage("Error during the backup operation: the initial path is incorrect!", Logger.LogLevel.WARN);
JOptionPane.showMessageDialog(null, "Error during the backup operation: the initial path is incorrect!", "Error", JOptionPane.ERROR_MESSAGE);
if (singleBackupBtn != null) singleBackupBtn.setEnabled(true);
if (autoBackupBtn != null) autoBackupBtn.setEnabled(true);
}
} catch (Exception ex) {
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
if (singleBackupBtn != null) singleBackupBtn.setEnabled(true);
if (autoBackupBtn != null) autoBackupBtn.setEnabled(true);
}
}

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

public static void zipDirectory(String sourceDirectoryPath, String targetZipPath, Backup backup, TrayIcon trayIcon, BackupProgressGUI progressBar, JButton singleBackupBtn, JToggleButton autoBackupBtn) throws IOException { // Track copied files
Logger.logMessage("Starting zipping process", LogLevel.INFO);

File file = new File(sourceDirectoryPath);
int totalFilesCount = file.isDirectory() ? countFilesInDirectory(file) : 1;

Expand Down Expand Up @@ -222,8 +230,7 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
});
}
} catch (IOException ex) {
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
ex.printStackTrace(); // Handle the exception as necessary
Logger.logMessage("An error occurred: " + ex.getMessage() , Logger.LogLevel.ERROR, ex);
if (singleBackupBtn != null) singleBackupBtn.setEnabled(true);
if (autoBackupBtn != null) autoBackupBtn.setEnabled(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void run() {
Logger.logMessage("No backup needed at this time.", Logger.LogLevel.INFO);
}
} catch (IOException ex) {
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
ex.printStackTrace();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public List<Backup> ReadBackupListFromJSON(String filename, String directoryPath
}

} catch (IOException | ParseException ex) {
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
ex.printStackTrace();
}
Expand Down Expand Up @@ -108,7 +108,7 @@ public void UpdateBackupListJSON(String filename, String directoryPath, List<Bac
file.write(updatedBackupArray.toJSONString());
file.flush();
} catch (IOException ex) {
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
}
}
Expand Down Expand Up @@ -148,7 +148,7 @@ public void UpdateSingleBackupInJSON(String filename, String directoryPath, Back
}

} catch (IOException | ParseException ex) {
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public JSONConfigReader(String filename, String directoryPath) {

public boolean isLogLevelEnabled(String level) {
if (config == null) {
Logger.logMessage("Configuration not loaded. Cannot check log level.", Logger.LogLevel.ERROR);
Logger.logMessage("Configuration not loaded. Cannot check log level", Logger.LogLevel.ERROR);
return false;
}

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

public boolean isMenuItemEnabled(String menuItem) {
if (config == null) {
Logger.logMessage("Configuration not loaded. Cannot check menu items.", Logger.LogLevel.ERROR);
Logger.logMessage("Configuration not loaded. Cannot check menu items", Logger.LogLevel.ERROR);
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/mycompany/autobackupprogram/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void main(String[] args) {
try {
service.startService();
} catch (IOException ex) {
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
Logger.logMessage("An error occurred: " + ex.getMessage(), Logger.LogLevel.ERROR, ex);
OpenExceptionMessage(ex.getMessage(), Arrays.toString(ex.getStackTrace()));
}
}
Expand Down
Loading

0 comments on commit 81684e9

Please sign in to comment.