From 27b99860b43582f442b893b171a04a34907f9ea4 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 1 Dec 2017 16:02:32 +0100 Subject: [PATCH] Simplified SketchController.isReadOnly(..) method Since the method is called everywhere with the following parameters isReadOnly( BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath()) ) the static calls to BaseNoGui have been inlined into isReadOnly() removing all the duplications around. --- app/src/processing/app/Editor.java | 15 ++++----------- app/src/processing/app/SketchController.java | 16 +++++++++------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index e2918efcd..186f66192 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -96,10 +96,7 @@ public class Editor extends JFrame implements RunnerListener { public boolean test(SketchController controller) { return PreferencesData.getBoolean("editor.save_on_verify") && controller.getSketch().isModified() - && !controller.isReadOnly( - BaseNoGui.librariesIndexer - .getInstalledLibraries(), - BaseNoGui.getExamplesPath()); + && !controller.isReadOnly(); } } @@ -107,7 +104,7 @@ public class Editor extends JFrame implements RunnerListener { @Override public boolean test(SketchController sketch) { - return sketch.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath()); + return sketch.isReadOnly(); } } @@ -2043,7 +2040,7 @@ public class Editor extends JFrame implements RunnerListener { formatTool.run(); } - boolean wasReadOnly = sketchController.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath()); + boolean wasReadOnly = sketchController.isReadOnly(); String previousMainFilePath = sketch.getMainFilePath(); saved = sketchController.save(); if (saved) { @@ -2161,11 +2158,7 @@ public class Editor extends JFrame implements RunnerListener { */ synchronized public void handleExport(final boolean usingProgrammer) { if (PreferencesData.getBoolean("editor.save_on_verify")) { - if (sketch.isModified() - && !sketchController.isReadOnly( - BaseNoGui.librariesIndexer - .getInstalledLibraries(), - BaseNoGui.getExamplesPath())) { + if (sketch.isModified() && !sketchController.isReadOnly()) { handleSave(true); } } diff --git a/app/src/processing/app/SketchController.java b/app/src/processing/app/SketchController.java index 1fc7461bc..e66f138f2 100644 --- a/app/src/processing/app/SketchController.java +++ b/app/src/processing/app/SketchController.java @@ -75,7 +75,7 @@ public class SketchController { ensureExistence(); // if read-only, give an error - if (isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) { + if (isReadOnly()) { // if the files are read-only, need to first do a "save as". Base.showMessage(tr("Sketch is Read-Only"), tr("Some files are marked \"read-only\", so you'll\n" + @@ -107,7 +107,7 @@ public class SketchController { } // if read-only, give an error - if (isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) { + if (isReadOnly()) { // if the files are read-only, need to first do a "save as". Base.showMessage(tr("Sketch is Read-Only"), tr("Some files are marked \"read-only\", so you'll\n" + @@ -225,7 +225,7 @@ public class SketchController { ensureExistence(); // if read-only, give an error - if (isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) { + if (isReadOnly()) { // if the files are read-only, need to first do a "save as". Base.showMessage(tr("Sketch is Read-Only"), tr("Some files are marked \"read-only\", so you'll\n" + @@ -303,7 +303,7 @@ public class SketchController { // make sure the user didn't hide the sketch folder ensureExistence(); - if (isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) { + if (isReadOnly()) { Base.showMessage(tr("Sketch is read-only"), tr("Some files are marked \"read-only\", so you'll\n" + "need to re-save this sketch to another location.")); @@ -367,7 +367,7 @@ public class SketchController { protected boolean saveAs() throws IOException { // get new name for folder FileDialog fd = new FileDialog(editor, tr("Save sketch folder as..."), FileDialog.SAVE); - if (isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath()) || isUntitled()) { + if (isReadOnly() || isUntitled()) { // default to the sketchbook folder fd.setDirectory(BaseNoGui.getSketchbookFolder().getAbsolutePath()); } else { @@ -456,7 +456,7 @@ public class SketchController { ensureExistence(); // if read-only, give an error - if (isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) { + if (isReadOnly()) { // if the files are read-only, need to first do a "save as". Base.showMessage(tr("Sketch is Read-Only"), tr("Some files are marked \"read-only\", so you'll\n" + @@ -791,7 +791,9 @@ public class SketchController { * examples directory, or when sketches are loaded from read-only * volumes or folders without appropriate permissions. */ - public boolean isReadOnly(LibraryList libraries, String examplesPath) { + public boolean isReadOnly() { + LibraryList libraries = BaseNoGui.librariesIndexer.getInstalledLibraries(); + String examplesPath = BaseNoGui.getExamplesPath(); String apath = sketch.getFolder().getAbsolutePath(); Optional libraryThatIncludesSketch = libraries.stream().filter(lib -> apath.startsWith(lib.getInstalledFolder().getAbsolutePath())).findFirst();