Skip to content

Commit

Permalink
Merge pull request #166 from TonyCallear/Warn-on-close
Browse files Browse the repository at this point in the history
Warn on close
Not a complete fix for #151 but HTH
  • Loading branch information
TonyCallear authored Oct 28, 2017
2 parents e44934f + b3c92d1 commit a5efea5
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 29 deletions.
Binary file modified lib/arduino-core.jar
Binary file not shown.
Binary file modified lib/pde.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<repositoryLayout>default</repositoryLayout>
<groupId>arduino</groupId>
<artifactId>arduino-core</artifactId>
<version>1.6.12</version>
<version>1.8.5</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
Expand Down Expand Up @@ -111,7 +111,7 @@
<groupId>arduino</groupId>
<artifactId>arduino-core</artifactId>
<scope>provided</scope>
<version>1.6.12</version>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/ardublock/ArduBlockTool.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ardublock;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -11,6 +13,7 @@

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JFrame;

import processing.app.Editor;
import processing.app.EditorTab;
Expand Down Expand Up @@ -38,6 +41,18 @@ public void init(Editor editor) {
context.setArduinoVersionString(arduinoVersion);
context.setEditor(editor);
System.out.println("Arduino Version: " + arduinoVersion);

// Don't just "close" Ardublock, see if there's something to save first.
// Note to self: Code here only affects behaviour when we're an Arduino Tool,
// not when run directly - See Main.java for that.
//ArduBlockTool.openblocksFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
ArduBlockTool.openblocksFrame.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
ArduBlockTool.openblocksFrame.doCloseArduBlockFile();
}
});
}
}

Expand All @@ -46,6 +61,7 @@ public void run() {
ArduBlockTool.editor.toFront();
ArduBlockTool.openblocksFrame.setVisible(true);
ArduBlockTool.openblocksFrame.toFront();
ArduBlockTool.openblocksFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
} catch (Exception e) {

}
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/com/ardublock/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.ardublock;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;
Expand Down Expand Up @@ -31,7 +33,21 @@ public void startArdublock() throws SAXException, IOException, ParserConfigurati
private void startOpenblocksFrame() throws SAXException, IOException, ParserConfigurationException
{
openblocksFrame = new OpenblocksFrame();
openblocksFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Don't just "close" Ardublock, see if there's something to save first.
// Note to self: This only affects behaviour when we're run directly,
// not when we're an Arduino Tool - See ArduBlockTool.java for that.

//openblocksFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

openblocksFrame.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
openblocksFrame.doCloseArduBlockFile();
}
});

Context context = Context.getContext();
context.setInArduino(false);
openblocksFrame.setVisible(true);
Expand Down
77 changes: 63 additions & 14 deletions src/main/java/com/ardublock/ui/OpenblocksFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public OpenblocksFrame()
this.setLayout(new BorderLayout());
//put the frame to the center of screen
this.setLocationRelativeTo(null);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

uiMessageBundle = ResourceBundle.getBundle("com/ardublock/block/ardublock");

Expand Down Expand Up @@ -235,23 +234,24 @@ private void loadFile()
}
}

public void doSaveArduBlockFile()
public boolean doSaveArduBlockFile()
{
if (!context.isWorkspaceChanged())
{
return ;
return true;
}

String saveString = getArduBlockString();

if (context.getSaveFilePath() == null)
{
chooseFileAndSave(saveString);
return chooseFileAndSave(saveString);
}
else
{
File saveFile = new File(context.getSaveFilePath());
writeFileAndUpdateFrame(saveString, saveFile);
return true;
}
}

Expand All @@ -269,21 +269,22 @@ public void doSaveAsArduBlockFile()

}

private void chooseFileAndSave(String ardublockString)
private boolean chooseFileAndSave(String ardublockString)
{
File saveFile = letUserChooseSaveFile();
saveFile = checkFileSuffix(saveFile);
if (saveFile == null)
{
return ;
return false;
}

if (saveFile.exists() && !askUserOverwriteExistedFile())
{
return ;
return false;
}

writeFileAndUpdateFrame(ardublockString, saveFile);
return true;
}

private String getArduBlockString()
Expand Down Expand Up @@ -336,18 +337,66 @@ public void doNewArduBlockFile()
if (context.isWorkspaceChanged())
{
int optionValue = JOptionPane.showOptionDialog(this, uiMessageBundle.getString("message.question.newfile_on_workspace_changed"), uiMessageBundle.getString("message.title.question"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, JOptionPane.YES_OPTION);
if (optionValue != JOptionPane.YES_OPTION)

switch (optionValue)
{
return ;
case JOptionPane.YES_OPTION:
doSaveArduBlockFile();
//break;
case JOptionPane.NO_OPTION:
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
context.resetWorksapce();
context.setWorkspaceChanged(false);
this.setTitle(this.makeFrameTitle());
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
break;
case JOptionPane.CANCEL_OPTION:
break;
}
}
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
context.resetWorksapce();
context.setWorkspaceChanged(false);
this.setTitle(this.makeFrameTitle());
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
else
{
// If workspace unchanged just start a new Ardublock
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
context.resetWorksapce();
context.setWorkspaceChanged(false);
this.setTitle(this.makeFrameTitle());
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}

}

public void doCloseArduBlockFile()
{
if (context.isWorkspaceChanged())
{
int optionValue = JOptionPane.showOptionDialog(this, uiMessageBundle.getString("message.question.close_on_workspace_changed"), uiMessageBundle.getString("message.title.question"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, JOptionPane.YES_OPTION);
switch (optionValue)
{
case JOptionPane.YES_OPTION:
if (doSaveArduBlockFile())
{
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
else
{
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
break;
case JOptionPane.NO_OPTION:
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
break;
case JOptionPane.CANCEL_OPTION:
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
break;
}
}
else
{
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}

}


private File checkFileSuffix(File saveFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,11 @@ ardublock.translator.exception.subroutineNotDeclared=subroutine not declared

message.title.error=Error
message.title.question=Question
message.content.open_unsaved=You have unsaved work which may be lost!\n Do you want to save your work before opening a new file?
message.content.open_unsaved=You have unsaved work which will be lost!\n Do you want to save your work before opening a different Ardublock program?
message.content.overwrite=Do you want overwrite existing file?
message.file_not_found=File Not Found or Permission Denied
message.question.newfile_on_workspace_changed=You have unsaved work which will be lost if you click 'YES' !\n Do you want to start a new Ardublock program?
message.question.newfile_on_workspace_changed=You have unsaved work which will be lost!\n Do you want to save your work before starting a new Ardublock program?
message.question.close_on_workspace_changed=You have unsaved work which may be lost!\n Do you want to save your work before before closing the Ardublock program?

bc.repeat_times=Repetitions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,11 @@ ardublock.translator.exception.subroutineNotDeclared=subroutine not declared

message.title.error=Error
message.title.question=Question
message.content.open_unsaved=You have unsaved work which may be lost!\n Do you want to save your work before opening a new file?
message.content.open_unsaved=You have unsaved work which will be lost!\n Do you want to save your work before opening a different Ardublock program?
message.content.overwrite=Do you want overwrite existing file?
message.file_not_found=File Not Found or Permission Denied
message.question.newfile_on_workspace_changed=You have unsaved work which will be lost if you click 'YES' !\n Do you want to start a new Ardublock program?
message.question.newfile_on_workspace_changed=You have unsaved work which will be lost!\n Do you want to save your work before starting a new Ardublock program?
message.question.close_on_workspace_changed=You have unsaved work which may be lost!\n Do you want to save your work before before closing the Ardublock program?

bc.repeat_times=Repetitions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,11 @@ ardublock.translator.exception.subroutineNotDeclared=subroutine not declared

message.title.error=Error
message.title.question=Question
message.content.open_unsaved=Ardublock program has changed, do you want to save?
message.content.open_unsaved=You have unsaved work which will be lost!\n Do you want to save your work before opening a different Ardublock program?
message.content.overwrite=Do you want overwrite existing file?
message.file_not_found=File Not Found or Permission Denied
message.question.newfile_on_workspace_changed=The program has changed, do you want to create a new Ardublock file?

message.question.newfile_on_workspace_changed=You have unsaved work which will be lost!\n Do you want to save your work before starting a new Ardublock program?
message.question.close_on_workspace_changed=You have unsaved work which may be lost!\n Do you want to save your work before before closing the Ardublock program?
bc.repeat_times=times

# Descriptions of all Block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,11 @@ ardublock.translator.exception.subroutineNotDeclared=subroutine not declared

message.title.error=Error
message.title.question=Question
message.content.open_unsaved=You have unsaved work which may be lost!\n Do you want to save your work before opening a new file?
message.content.open_unsaved=You have unsaved work which will be lost!\n Do you want to save your work before opening a different Ardublock program?
message.content.overwrite=Do you want overwrite existing file?
message.file_not_found=File Not Found or Permission Denied
message.question.newfile_on_workspace_changed=You have unsaved work which will be lost if you click 'YES' !\n Do you want to start a new Ardublock program?
message.question.newfile_on_workspace_changed=You have unsaved work which will be lost!\n Do you want to save your work before starting a new Ardublock program?
message.question.close_on_workspace_changed=You have unsaved work which may be lost!\n Do you want to save your work before before closing the Ardublock program?

bc.repeat_times=Repetitions

Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/com/ardublock/block/ardublock.properties
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,11 @@ ardublock.translator.exception.subroutineNotDeclared=subroutine not declared

message.title.error=Error
message.title.question=Question
message.content.open_unsaved=Ardublock program has changed, do you want to save?
message.content.open_unsaved=You have unsaved work which will be lost!\n Do you want to save your work before opening a different Ardublock program?
message.content.overwrite=Do you want overwrite existing file?
message.file_not_found=File Not Found or Permission Denied
message.question.newfile_on_workspace_changed=The program has changed, do you want to create a new Ardublock file?

message.question.newfile_on_workspace_changed=You have unsaved work which will be lost!\n Do you want to save your work before starting a new Ardublock program?
message.question.close_on_workspace_changed=You have unsaved work which may be lost!\n Do you want to save your work before before closing the Ardublock program?
bc.repeat_times=times

# Descriptions of all Block
Expand Down

0 comments on commit a5efea5

Please sign in to comment.