Move `isModified()` from SketchController to Sketch

Also, update any code that uses it, removing the dependency on
SketchController entirely if possible.
This commit is contained in:
Matthijs Kooijman 2015-12-17 11:30:44 +01:00 committed by Martino Facchin
parent 629953e20d
commit dd5c1787fd
5 changed files with 34 additions and 20 deletions

View File

@ -542,8 +542,8 @@ public class Base {
Collections.reverse(reversedEditors);
int index = 0;
for (Editor editor : reversedEditors) {
SketchController sketch = editor.getSketchController();
String path = sketch.getSketch().getMainFilePath();
Sketch sketch = editor.getSketch();
String path = sketch.getMainFilePath();
// Skip untitled sketches if they do not contains changes.
if (path.startsWith(untitledPath) && !sketch.isModified()) {
continue;

View File

@ -85,11 +85,17 @@ public class Editor extends JFrame implements RunnerListener {
private ArrayList<EditorTab> tabs = new ArrayList<>();
private int currentTabIndex = -1;
private static class ShouldSaveIfModified implements Predicate<SketchController> {
private static class ShouldSaveIfModified
implements Predicate<SketchController> {
@Override
public boolean test(SketchController sketch) {
return PreferencesData.getBoolean("editor.save_on_verify") && sketch.isModified() && !sketch.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath());
public boolean test(SketchController controller) {
return PreferencesData.getBoolean("editor.save_on_verify")
&& controller.getSketch().isModified()
&& !controller.isReadOnly(
BaseNoGui.librariesIndexer
.getInstalledLibraries(),
BaseNoGui.getExamplesPath());
}
}
@ -1838,7 +1844,8 @@ public class Editor extends JFrame implements RunnerListener {
* @return false if canceling the close/quit operation
*/
protected boolean checkModified() {
if (!sketchController.isModified()) return true;
if (!sketch.isModified())
return true;
// As of Processing 1.0.10, this always happens immediately.
// http://dev.processing.org/bugs/show_bug.cgi?id=1456
@ -2169,7 +2176,11 @@ public class Editor extends JFrame implements RunnerListener {
*/
synchronized public void handleExport(final boolean usingProgrammer) {
if (PreferencesData.getBoolean("editor.save_on_verify")) {
if (sketchController.isModified() && !sketchController.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) {
if (sketch.isModified()
&& !sketchController.isReadOnly(
BaseNoGui.librariesIndexer
.getInstalledLibraries(),
BaseNoGui.getExamplesPath())) {
handleSave(true);
}
}

View File

@ -456,21 +456,13 @@ public class SketchController {
if (OSUtils.isMacOS()) {
// http://developer.apple.com/qa/qa2001/qa1146.html
Object modifiedParam = isModified() ? Boolean.TRUE : Boolean.FALSE;
Object modifiedParam = sketch.isModified() ? Boolean.TRUE : Boolean.FALSE;
editor.getRootPane().putClientProperty("windowModified", modifiedParam);
editor.getRootPane().putClientProperty("Window.documentModified", modifiedParam);
}
}
public boolean isModified() {
for (SketchCode code : sketch.getCodes()) {
if (code.isModified())
return true;
}
return false;
}
/**
* Save all code in the current sketch.
@ -896,7 +888,7 @@ public class SketchController {
boolean deleteTemp = false;
String pathToSketch = sketch.getMainFilePath();
if (isModified()) {
if (sketch.isModified()) {
// If any files are modified, make a copy of the sketch with the changes
// saved, so arduino-builder will see the modifications.
pathToSketch = saveSketchInTempFolder();

View File

@ -49,7 +49,7 @@ public class FixEncoding implements Tool {
public void run() {
SketchController sketch = editor.getSketchController();
Sketch sketch = editor.getSketch();
//SketchCode code = sketch.current;
if (sketch.isModified()) {
@ -65,8 +65,8 @@ public class FixEncoding implements Tool {
}
}
try {
for (int i = 0; i < sketch.getSketch().getCodeCount(); i++) {
SketchCode code = sketch.getSketch().getCode(i);
for (int i = 0; i < sketch.getCodeCount(); i++) {
SketchCode code = sketch.getCode(i);
editor.findTab(code).setText(loadWithLocalEncoding(code.getFile()));
}
} catch (IOException e) {

View File

@ -214,4 +214,15 @@ public class Sketch {
public File getDataFolder() {
return dataFolder;
}
/**
* Is any of the files in this sketch modified?
*/
public boolean isModified() {
for (SketchCode code : codes) {
if (code.isModified())
return true;
}
return false;
}
}