Skip to content

Commit

Permalink
Merge pull request #26 from BradF-99/merge-gui+file
Browse files Browse the repository at this point in the history
Various & miscellaneous bugfixes
  • Loading branch information
Assasindie authored Jun 2, 2019
2 parents ef6fac4 + cb4a3e3 commit bf8d115
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/main/java/filehandler/FileWrite.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class FileWrite {
* @throws IOException
*/
public void writeFile(List<String> 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);
Expand Down
24 changes: 20 additions & 4 deletions src/main/java/gui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,13 @@ 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) {
ex.printStackTrace();
JOptionPane.showMessageDialog(frame, ex.getMessage());
} catch (FileInvalidArgumentException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(frame, ex.getMessage());
}
}
else if (pressedComp == fileOpt.getMenuComponent(2)){
Expand Down Expand Up @@ -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);
Expand Down
48 changes: 42 additions & 6 deletions src/main/java/gui/MenuCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -97,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?",
Expand All @@ -108,13 +114,17 @@ public static String saveFile(JFrame frame, Boolean undoHistoryActive){
options,
options[0]);
if(responseInt == 0) {
selectedFile.delete();
return selectedFile.getAbsolutePath();
actualFile.delete();
return path;
}
else if (responseInt == 1 || responseInt == 2){
return "";
}
} else return selectedFile.getAbsolutePath();
} else {
return path;
}
} else {
return "";
}
return ""; // if the user selects nothing
}
Expand Down Expand Up @@ -169,6 +179,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;
Expand Down Expand Up @@ -209,12 +222,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
Expand All @@ -236,6 +271,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");
}
}

Expand Down

0 comments on commit bf8d115

Please sign in to comment.