1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +01:00

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.
This commit is contained in:
Cristian Maglie 2017-12-01 16:02:32 +01:00
parent bf1b523854
commit 27b99860b4
2 changed files with 13 additions and 18 deletions

View File

@ -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);
}
}

View File

@ -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<UserLibrary> libraryThatIncludesSketch = libraries.stream().filter(lib -> apath.startsWith(lib.getInstalledFolder().getAbsolutePath())).findFirst();