Skip to content
This repository has been archived by the owner on Jul 20, 2022. It is now read-only.

Commit

Permalink
Merge pull request #22 from NoahOrtega/dev
Browse files Browse the repository at this point in the history
improved popup behavior + added warning for replacing originals
  • Loading branch information
MerryPlace authored Aug 24, 2020
2 parents 174d882 + d52994b commit 5f26d21
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
34 changes: 31 additions & 3 deletions NMSVRScreenshotFix.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class LogicController {
private boolean isExecuting = false;
private boolean canceled = false;

private boolean triggerFileErrorPopups = true;

// File handling variables
private int totalFiles;
private int filesConverted;
Expand Down Expand Up @@ -131,6 +133,10 @@ public void execute() {
//disables UI
myUI.toggleUI();

if(!shouldRename && (sourcePath.equals(resultPath))) {
myUI.warningReplacingFiles();
}

for (int fileIndex = 0; fileIndex < totalFiles && !canceled; fileIndex++) {
curFile = folderContents[fileIndex];
if (!folderContents[fileIndex].isDirectory() && isImage(curFile.getPath())) {
Expand All @@ -143,22 +149,29 @@ public void execute() {
}
}
catch (IOException ex) {
if(triggerFileErrorPopups){
myUI.errorReading(curFile.getName());
}
System.err.println(">> Caught IOException on '" + curFile.getPath() + "':\n " + ex.getMessage());
}
catch (NullPointerException ex) {
if(triggerFileErrorPopups){
myUI.errorCorruptImage(curFile.getName());
}
System.out.println(">> ImageIO.read returned null (likely corrupt): " + ex);
myUI.errorCorruptImage(curFile.getName());
}
}
myUI.updateProgressBar((fileIndex + 1)*100/totalFiles);
}
if(canceled) {
myUI.cancelPopup(filesConverted);
canceled = false;
myUI.updateProgressBar(0);
} else {
myUI.completePopup(filesConverted);
}

triggerFileErrorPopups = true;
isExecuting = false;
myUI.toggleUI();
System.out.println("Finished Execution");
Expand Down Expand Up @@ -202,9 +215,10 @@ private void squish(File originalFile) throws IOException {
System.out.println(">> Converted");
}
catch(Exception ex) {
myUI.errorWriting();
if(triggerFileErrorPopups){
myUI.errorWriting(newFile.getName());
}
System.err.print(">> Error writing to result folder " + ex.getClass());
canceled = true;
}
}

Expand Down Expand Up @@ -369,4 +383,18 @@ public boolean isValidTextAddition(String phrase) {
public boolean hasValidDirectoryPaths() {
return((new File(sourcePath)).isDirectory() && (new File(resultPath)).isDirectory());
}

/**
* File errors during execution trigger warning popups in ProgramUI. The user makes a choice
* about how the error should be handled via this method.
* @see ProgramUI.fileErrorOptions for the text options these indexes correspond to
* @param userChoice the index of the choice the user selected
*/
public void userErrorResponse(int userChoice) {
if (userChoice == 1) {
triggerFileErrorPopups = false;
} else if (userChoice == 2) {
canceled = true;
}
}
}
53 changes: 46 additions & 7 deletions ProgramUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ public class ProgramUI extends javax.swing.JFrame {

LogicController controller = LogicController.getInstance();

String[] fileErrorOptions = {"Okay","Okay, Skip Remaining Errors","Cancel Execution"};

public ProgramUI() {
initComponents();
resultFolderField.setText(controller.resultPath);
sourceFolderField.setText(controller.sourcePath);
behaviorTextArea.setText(controller.getCurrentBehaviorString());
}


/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
Expand Down Expand Up @@ -277,7 +280,7 @@ public void updateProgressBar(int value) {
public void completePopup(int converted) {
if(converted != 0) {
JOptionPane.showMessageDialog(this,
("All " + converted + " images were converted successfully."),
("" + converted + " images were converted successfully."),
"Complete", JOptionPane.INFORMATION_MESSAGE);
progressBar.setValue(0);
} else {
Expand All @@ -299,6 +302,22 @@ public void cancelPopup(int converted) {
"Canceled", JOptionPane.ERROR_MESSAGE);
}

/**
* If the user chooses a settings combination which will result in replacing
* their original screenshots this warning will give them one last chance to
* change their mind by canceling execution.
*/
public void warningReplacingFiles(){
int choice = JOptionPane.showOptionDialog(this,
"By continuing you will be replacing your original screenshots. This cannot be undone.\n"
+ "Please press Cancel and change the behavior settings if this was not intended.",
"Replacing Files",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE,
null,null,null);
if(choice != 0) {
controller.cancelExecution();
}
}

/**
* Popup triggers on attempted execution when the source or destination
* folder path is invalid.
Expand All @@ -312,23 +331,43 @@ public void errorInvalidPath() {

/**
* Popup triggers during execution when an image cannot be read due to corruption.
* Gives user the option to skip remaining errors or cancel the operation.
* @param fileName the name of the corrupt image file
*/
public void errorCorruptImage(String fileName) {
JOptionPane.showMessageDialog(this,
int choice = JOptionPane.showOptionDialog(this,
"The image file '" + fileName + "' cannot be read and may be corrupt.",
"Error: Corrupt File",JOptionPane.ERROR_MESSAGE);
"Corrupt File Error: Cancel Execution?",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE,
null,fileErrorOptions,fileErrorOptions[0]);
controller.userErrorResponse(choice);
}

/**
* Popup triggers if an image cannot be read from the source folder.
* Gives user the option to skip remaining errors or cancel the operation.
*/
public void errorReading(String fileName) {
int choice = JOptionPane.showOptionDialog(this,
"There was a problem reading the file, '"+fileName+"' from the source folder.",
"Read Error: Cancel Execution?",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE,
null,fileErrorOptions,fileErrorOptions[0]);
controller.userErrorResponse(choice);
}

/**
* Popup triggers if an image cannot be written to the destination folder
* Gives user the option to skip remaining errors or cancel the operation.
*/
public void errorWriting() {
JOptionPane.showMessageDialog(this,
"There was a problem writing to the destination folder. Canceling operation.",
"Write Error: Aborting",JOptionPane.ERROR_MESSAGE);
public void errorWriting(String fileName) {
int choice = JOptionPane.showOptionDialog(this,
"There was a problem writing the file '"+fileName+"' to the destination folder.",
"Write Error: Cancel Execution?",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE,
null,fileErrorOptions,fileErrorOptions[0]);
controller.userErrorResponse(choice);
}



//////////////////////////////////////////////////////////////////////
// Settings Menu: text addition issue warning popups
/////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 5f26d21

Please sign in to comment.