From 3e3593d23d8262e74c5f3afefe01ec6713829bee Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Wed, 22 Aug 2018 11:35:10 +0200 Subject: [PATCH] Rename sketch folder only if proper name is slightly different Takes into account Github-like transformations (projectName becomes projectName-branchName) Also changes user prompt to specify that it will move a whole folder. --- app/src/processing/app/Editor.java | 55 +++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index da94f9c99..2bbfb5364 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -1793,6 +1793,7 @@ public class Editor extends JFrame implements RunnerListener { } else { String properParent = fileName.substring(0, fileName.length() - 4); + File properFolder; Object[] options = {tr("OK"), tr("Cancel")}; String prompt = I18n.format(tr("The file \"{0}\" needs to be inside\n" + @@ -1801,36 +1802,61 @@ public class Editor extends JFrame implements RunnerListener { fileName, properParent); - int result = JOptionPane.showOptionDialog(this, prompt, tr("Moving"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); + properFolder = new File(sketchFile.getParent(), properParent); + + int result; + if (parentFolderContainsSketchName(sketchFile, properParent)) { + + // properFolder needs to be created one level above + properFolder = new File(new File(sketchFile.getParent()).getParent(), properParent); + + // ask for different confirmation + prompt = I18n.format(tr("The file \"{0}\" needs to be inside\n" + + "a sketch folder named \"{1}\".\n" + + "Renaming folder \"{2}\" into \"{3}\"\n" + + "Continue?"), + fileName, + properParent, + sketchFile.getParent(), + properFolder); + + result = JOptionPane.showOptionDialog(this, prompt, tr("Renaming"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); + } else { + result = JOptionPane.showOptionDialog(this, prompt, tr("Moving"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); + } if (result != JOptionPane.YES_OPTION) { return false; } - // create properly named folder - File properFolder = new File(sketchFile.getParent(), properParent); if (properFolder.exists()) { Base.showWarning(tr("Error"), I18n.format(tr("A folder named \"{0}\" already exists. " + "Can't open sketch."), properParent), null); return false; } - if (!properFolder.mkdirs()) { - //throw new IOException("Couldn't create sketch folder"); - Base.showWarning(tr("Error"), tr("Could not create the sketch folder."), null); - return false; - } + // copy the sketch inside File properPdeFile = new File(properFolder, sketchFile.getName()); try { - Base.copyFile(sketchFile, properPdeFile); + if (parentFolderContainsSketchName(sketchFile, properParent)) { + File dir = new File(sketchFile.getParent()); + dir.renameTo(properFolder); + } else { + // Create folder + if (!properFolder.mkdirs()) { + //throw new IOException("Couldn't create sketch folder"); + Base.showWarning(tr("Error"), tr("Could not create the sketch folder."), null); + return false; + } + Base.copyFile(sketchFile, properPdeFile); + // remove the original file, so user doesn't get confused + sketchFile.delete(); + } } catch (IOException e) { Base.showWarning(tr("Error"), tr("Could not copy to a proper location."), e); return false; } - // remove the original file, so user doesn't get confused - sketchFile.delete(); - // update with the new path file = properPdeFile; @@ -1853,6 +1879,11 @@ public class Editor extends JFrame implements RunnerListener { return true; } + private boolean parentFolderContainsSketchName(File sketchFile, String sketchName) { + String dir = sketchFile.getParent().toLowerCase(); + return dir.contains(sketchName.toLowerCase()); + } + public void updateTitle() { if (sketchController == null) { return;