From 165a4d38794d993e55d3dbb7bc1c58a8cb929ee2 Mon Sep 17 00:00:00 2001 From: Joshua O'Riordan n9965319 Date: Sun, 2 Jun 2019 20:16:14 +1000 Subject: [PATCH 1/6] Added error dialogues in exceptions --- src/main/java/gui/MainWindow.java | 5 ++--- src/main/java/gui/MenuCommands.java | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/gui/MainWindow.java b/src/main/java/gui/MainWindow.java index 75291e1..d31edb4 100644 --- a/src/main/java/gui/MainWindow.java +++ b/src/main/java/gui/MainWindow.java @@ -40,7 +40,6 @@ public class MainWindow { private boolean filled = false; private int undoHistoryNum; private boolean undoHistoryActive = false; - private boolean undoPolygon = false; private List argsList = new ArrayList<>(); private FileRead fileReader = new FileRead(); private FileWrite fileWriter = new FileWrite(); @@ -255,9 +254,9 @@ else if (pressedComp == fileOpt.getMenuComponent(1)){ try { fileRead(MenuCommands.openFile(frame)); } catch (IOException ex) { - ex.printStackTrace(); + JOptionPane.showMessageDialog(frame, ex.getMessage()); } catch (FileInvalidArgumentException ex) { - ex.printStackTrace(); + JOptionPane.showMessageDialog(frame, ex.getMessage()); } } else if (pressedComp == fileOpt.getMenuComponent(2)){ diff --git a/src/main/java/gui/MenuCommands.java b/src/main/java/gui/MenuCommands.java index 60ac85f..dee757c 100644 --- a/src/main/java/gui/MenuCommands.java +++ b/src/main/java/gui/MenuCommands.java @@ -219,6 +219,7 @@ public static void exportBMP(JPanel drawingBoard, Boolean undoHistoryActive){ try { ImageIO.write(bufferedImageToWrite, "bmp", new File(filePath)); } catch (IOException e) { + JOptionPane.showMessageDialog(drawingBoard, "Error writing BMP to file. Please try again"); } } From af7393c808d795ef8cdc42c339c3091132f0de6e Mon Sep 17 00:00:00 2001 From: Joshua O'Riordan n9965319 Date: Sun, 2 Jun 2019 20:22:36 +1000 Subject: [PATCH 2/6] Fixed error in BMP where hitting exit on the first prompt would open file saver --- src/main/java/gui/MenuCommands.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/gui/MenuCommands.java b/src/main/java/gui/MenuCommands.java index dee757c..c576ae2 100644 --- a/src/main/java/gui/MenuCommands.java +++ b/src/main/java/gui/MenuCommands.java @@ -152,6 +152,9 @@ public static void exportBMP(JPanel drawingBoard, Boolean undoHistoryActive){ null, options, options[0]); + if (responseInt != 1 && responseInt != 0){ + return; //Cancel execution if the exit cross is pressed + } if (responseInt == 1){ Boolean validData = false; useUserDimensions = true; From 83acce0b7701604d3939b956ce78417fecef4bf9 Mon Sep 17 00:00:00 2001 From: Brad Fuller <37586752+BradF-99@users.noreply.github.com> Date: Sun, 2 Jun 2019 22:23:01 +1000 Subject: [PATCH 3/6] Check for file extension when saving --- src/main/java/gui/MenuCommands.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/gui/MenuCommands.java b/src/main/java/gui/MenuCommands.java index 6dc952c..a199b3c 100644 --- a/src/main/java/gui/MenuCommands.java +++ b/src/main/java/gui/MenuCommands.java @@ -85,6 +85,7 @@ public static void undo(ComponentsClass comp, JFrame frame, JPanel sideBar, Bool * @param undoHistoryActive A Boolean which is true if undo history is active, prevents saving if it is */ public static String saveFile(JFrame frame, Boolean undoHistoryActive){ + String path = ""; if (undoHistoryActive){ JOptionPane.showMessageDialog(frame, "Undo History is active, please disable or save selected state"); return ""; //prevent execution of following code as undo history is active @@ -109,12 +110,24 @@ public static String saveFile(JFrame frame, Boolean undoHistoryActive){ options[0]); if(responseInt == 0) { selectedFile.delete(); - return selectedFile.getAbsolutePath(); + path = selectedFile.getAbsolutePath(); + //Add .vec extension if not already present + if (!(path.matches(".*\\.vec"))){ + path += ".vec"; + return path; + } } else if (responseInt == 1 || responseInt == 2){ return ""; } - } else return selectedFile.getAbsolutePath(); + } else { + //Add .vec extension if not already present + path = selectedFile.getAbsolutePath(); + if (!(path.matches(".*\\.vec"))){ + path += ".vec"; + return path; + } + } } return ""; // if the user selects nothing } From 15e00e4ded7e82d62328f0d662128931d72bb455 Mon Sep 17 00:00:00 2001 From: Brad Fuller <37586752+BradF-99@users.noreply.github.com> Date: Sun, 2 Jun 2019 22:33:43 +1000 Subject: [PATCH 4/6] Added overwrite checks to VEC and BMP save --- src/main/java/gui/MenuCommands.java | 49 +++++++++++++++++++---------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/src/main/java/gui/MenuCommands.java b/src/main/java/gui/MenuCommands.java index a199b3c..c5a62ab 100644 --- a/src/main/java/gui/MenuCommands.java +++ b/src/main/java/gui/MenuCommands.java @@ -98,7 +98,12 @@ public static String saveFile(JFrame frame, Boolean undoHistoryActive){ int status = fileChooser.showSaveDialog(frame); if (status == JFileChooser.APPROVE_OPTION) { // if the user has selected a file File selectedFile = fileChooser.getSelectedFile(); - if(selectedFile.exists()){ + path = selectedFile.getAbsolutePath(); + if (!(path.matches(".*\\.vec"))){ + path += ".vec"; + } + File actualFile = new File(path); // used for checking overwrite + if(actualFile.exists()){ Object[] options = {"Yes", "No", "Cancel"}; int responseInt = JOptionPane.showOptionDialog(frame, "This file already exists. Would you like to over-write it?", @@ -109,24 +114,14 @@ public static String saveFile(JFrame frame, Boolean undoHistoryActive){ options, options[0]); if(responseInt == 0) { - selectedFile.delete(); - path = selectedFile.getAbsolutePath(); - //Add .vec extension if not already present - if (!(path.matches(".*\\.vec"))){ - path += ".vec"; - return path; - } + actualFile.delete(); + return path; } else if (responseInt == 1 || responseInt == 2){ return ""; } } else { - //Add .vec extension if not already present - path = selectedFile.getAbsolutePath(); - if (!(path.matches(".*\\.vec"))){ - path += ".vec"; - return path; - } + return path; } } return ""; // if the user selects nothing @@ -225,12 +220,34 @@ public static void exportBMP(JPanel drawingBoard, Boolean undoHistoryActive){ if (status == JFileChooser.APPROVE_OPTION) { // if the user has selected a file File selectedFile = fileChooser.getSelectedFile(); filePath = selectedFile.getAbsolutePath(); + //Add .bmp extension if not already present if (!(filePath.matches(".*\\.bmp"))){ filePath += ".bmp"; } - } - else{ + + File actualFile = new File(filePath); // used for checking overwrite + if(actualFile.exists()){ + Object[] bmpOverWriteOptions = {"Yes", "No", "Cancel"}; + int bmpResponseInt = JOptionPane.showOptionDialog(drawingBoard, + "This file already exists. Would you like to over-write it?", + "File already exists", + JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE, + null, + bmpOverWriteOptions, + bmpOverWriteOptions[0]); + if(bmpResponseInt == 0) { + actualFile.delete(); + } + else if (bmpResponseInt == 1 || bmpResponseInt == 2){ + return; + } else { + return; + } + } + + } else { return; //cancel following execution } //Create image of the drawingBoard based on its current dimensions From f23799cc02fc5d972d760e85d2a1c7f6597615e1 Mon Sep 17 00:00:00 2001 From: Joshua O'Riordan n9965319 Date: Sun, 2 Jun 2019 22:44:02 +1000 Subject: [PATCH 5/6] Checked that code was the same between keybindings and buttons and fixed open file not clearing undo history --- src/main/java/gui/MainWindow.java | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/gui/MainWindow.java b/src/main/java/gui/MainWindow.java index a01c9d7..1c49f4f 100644 --- a/src/main/java/gui/MainWindow.java +++ b/src/main/java/gui/MainWindow.java @@ -253,6 +253,9 @@ else if (pressedComp == fileOpt.getMenuComponent(0)){ else if (pressedComp == fileOpt.getMenuComponent(1)){ try { fileRead(MenuCommands.openFile(frame)); + undoHistoryActive = MenuCommands.refreshEventListeners(drawingBoard, new MyMouseAdapter(), + new MyMouseAdapter() + ); } catch (IOException ex) { JOptionPane.showMessageDialog(frame, ex.getMessage()); } catch (FileInvalidArgumentException ex) { @@ -316,10 +319,23 @@ public void keyPressed(KeyEvent e) { System.out.println(e.getKeyChar()); if (e.isControlDown()){ if (pressedKey == KeyEvent.VK_S){ - MenuCommands.saveFile(frame, undoHistoryActive); + try { + fileWrite(MenuCommands.saveFile(frame, undoHistoryActive)); + } catch (IOException ex) { + ex.printStackTrace(); + } } else if (pressedKey == KeyEvent.VK_O){ - MenuCommands.openFile(frame); + try { + fileRead(MenuCommands.openFile(frame)); + undoHistoryActive = MenuCommands.refreshEventListeners(drawingBoard, new MyMouseAdapter(), + new MyMouseAdapter() + ); + } catch (IOException ex) { + JOptionPane.showMessageDialog(frame, ex.getMessage()); + } catch (FileInvalidArgumentException ex) { + JOptionPane.showMessageDialog(frame, ex.getMessage()); + } } else if (pressedKey == KeyEvent.VK_B){ MenuCommands.exportBMP(drawingBoard, undoHistoryActive); From cb4a3e3a00432316b957762a33fc0a76a6be0af9 Mon Sep 17 00:00:00 2001 From: Brad Fuller <37586752+BradF-99@users.noreply.github.com> Date: Sun, 2 Jun 2019 22:50:20 +1000 Subject: [PATCH 6/6] Fixed FileNotFound exception if no file is specified --- src/main/java/filehandler/FileWrite.java | 2 ++ src/main/java/gui/MenuCommands.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/main/java/filehandler/FileWrite.java b/src/main/java/filehandler/FileWrite.java index 6403ccc..b412683 100644 --- a/src/main/java/filehandler/FileWrite.java +++ b/src/main/java/filehandler/FileWrite.java @@ -21,6 +21,8 @@ public class FileWrite { * @throws IOException */ public void writeFile(List arguments, String filePath) throws IOException { + if(filePath.isEmpty()||filePath.isBlank()) return; + BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, false)); for (String arg: arguments) { writer.write(arg); diff --git a/src/main/java/gui/MenuCommands.java b/src/main/java/gui/MenuCommands.java index c5a62ab..ac43c2a 100644 --- a/src/main/java/gui/MenuCommands.java +++ b/src/main/java/gui/MenuCommands.java @@ -123,6 +123,8 @@ else if (responseInt == 1 || responseInt == 2){ } else { return path; } + } else { + return ""; } return ""; // if the user selects nothing }