From e994c527294359b19fafbfa00f14585d80f7ef90 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 8 Jul 2014 15:35:38 +0200 Subject: [PATCH] Don't store the extension in SketchCode Nobody was using it anymore, except for checking against specific extensions, which is easily done against the filename itself. This prepares for some simplification of Sketch.load next. --- app/src/processing/app/Sketch.java | 12 ++++++------ app/src/processing/app/SketchCode.java | 23 ++++++++++------------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/app/src/processing/app/Sketch.java b/app/src/processing/app/Sketch.java index bf31873bd..3663e7322 100644 --- a/app/src/processing/app/Sketch.java +++ b/app/src/processing/app/Sketch.java @@ -192,7 +192,7 @@ public class Sketch { // it would be otherwise possible to sneak in nasty filenames. [0116] if (Sketch.isSanitaryName(base)) { code[codeCount++] = - new SketchCode(new File(folder, filename), extension); + new SketchCode(new File(folder, filename)); } else { editor.console.message(I18n.format("File name {0} is invalid: ignored", filename), true, false); } @@ -487,7 +487,7 @@ public class Sketch { } } - if (!current.renameTo(newFile, newExtension)) { + if (!current.renameTo(newFile)) { Base.showWarning(_("Error"), I18n.format( _("Could not rename \"{0}\" to \"{1}\""), @@ -531,7 +531,7 @@ public class Sketch { editor.base.rebuildSketchbookMenus(); } else { // else if something besides code[0] - if (!current.renameTo(newFile, newExtension)) { + if (!current.renameTo(newFile)) { Base.showWarning(_("Error"), I18n.format( _("Could not rename \"{0}\" to \"{1}\""), @@ -557,7 +557,7 @@ public class Sketch { ), e); return; } - SketchCode newCode = new SketchCode(newFile, newExtension); + SketchCode newCode = new SketchCode(newFile); //System.out.println("new code is named " + newCode.getPrettyName() + " " + newCode.getFile()); insertCode(newCode); } @@ -788,7 +788,7 @@ public class Sketch { String pdeName = pdeFile.getPath(); pdeName = pdeName.substring(0, pdeName.length() - 4) + ".ino"; - return c.renameTo(new File(pdeName), "ino"); + return c.renameTo(new File(pdeName)); } return false; } @@ -1075,7 +1075,7 @@ public class Sketch { } if (codeExtension != null) { - SketchCode newCode = new SketchCode(destFile, codeExtension); + SketchCode newCode = new SketchCode(destFile); if (replacement) { replaceCode(newCode); diff --git a/app/src/processing/app/SketchCode.java b/app/src/processing/app/SketchCode.java index 096d37875..91a8232e8 100644 --- a/app/src/processing/app/SketchCode.java +++ b/app/src/processing/app/SketchCode.java @@ -25,10 +25,13 @@ package processing.app; import java.io.*; +import java.util.List; +import java.util.Arrays; import javax.swing.text.Document; import static processing.app.I18n._; +import processing.app.helpers.FileUtils; /** @@ -41,9 +44,6 @@ public class SketchCode { /** File object for where this code is located */ private File file; - /** Extension for this file (no dots, and in lowercase). */ - private String extension; - /** Text of the program text for this tab */ private String program; @@ -70,9 +70,8 @@ public class SketchCode { private int preprocOffset; - public SketchCode(File file, String extension) { + public SketchCode(File file) { this.file = file; - this.extension = extension; makePrettyName(); @@ -125,11 +124,10 @@ public class SketchCode { } - protected boolean renameTo(File what, String ext) { + protected boolean renameTo(File what) { boolean success = file.renameTo(what); if (success) { file = what; - extension = ext; makePrettyName(); } return success; @@ -151,13 +149,12 @@ public class SketchCode { } - public String getExtension() { - return extension; + public boolean isExtension(String... extensions) { + return isExtension(Arrays.asList(extensions)); } - - - public boolean isExtension(String what) { - return extension.equals(what); + + public boolean isExtension(List extensions) { + return FileUtils.hasExtension(file, extensions); }