From 73c8f1c4abc2a5d5789d35ea25b0ffaa773e90dc Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Wed, 16 Jan 2013 14:04:30 +0100 Subject: [PATCH] see #1211: formatted code, moved RelativePath code into FileUtils --- app/src/processing/app/Base.java | 45 ++++++----- app/src/processing/app/Preferences.java | 27 +++---- app/src/processing/app/RelativePath.java | 74 ------------------- app/src/processing/app/helpers/FileUtils.java | 64 ++++++++++++++++ 4 files changed, 100 insertions(+), 110 deletions(-) delete mode 100644 app/src/processing/app/RelativePath.java diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index eb647c707..860e62bf7 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -259,16 +259,16 @@ public class Base { if (sketchbookPath != null) { File sketchbookFolder; if (portableFolder != null) - sketchbookFolder = new File(portableFolder, sketchbookPath); + sketchbookFolder = new File(portableFolder, sketchbookPath); else - sketchbookFolder = new File(sketchbookPath); + sketchbookFolder = new File(sketchbookPath); if (!sketchbookFolder.exists()) { Base.showWarning(_("Sketchbook folder disappeared"), - _("The sketchbook folder no longer exists.\n" + - "Arduino will switch to the default sketchbook\n" + - "location, and create a new sketchbook folder if\n" + - "necessary. Arduino will then stop talking about\n" + - "himself in the third person."), null); + _("The sketchbook folder no longer exists.\n" + + "Arduino will switch to the default sketchbook\n" + + "location, and create a new sketchbook folder if\n" + + "necessary. Arduino will then stop talking about\n" + + "himself in the third person."), null); sketchbookPath = null; } } @@ -277,9 +277,9 @@ public class Base { if (sketchbookPath == null) { File defaultFolder = getDefaultSketchbookFolder(); if (portableFolder != null) - Preferences.set("sketchbook.path", portableSketchbookFolder); + Preferences.set("sketchbook.path", portableSketchbookFolder); else - Preferences.set("sketchbook.path", defaultFolder.getAbsolutePath()); + Preferences.set("sketchbook.path", defaultFolder.getAbsolutePath()); if (!defaultFolder.exists()) { defaultFolder.mkdirs(); } @@ -438,13 +438,12 @@ public class Base { for (int i = 0; i < count; i++) { String path = Preferences.get("last.sketch" + i + ".path"); if (portableFolder != null) { - File absolute = new File(portableFolder, path); - try { - path = absolute.getCanonicalPath(); - } - catch (IOException e) { - // path unchanged. - } + File absolute = new File(portableFolder, path); + try { + path = absolute.getCanonicalPath(); + } catch (IOException e) { + // path unchanged. + } } int[] location; if (windowPositionValid) { @@ -485,9 +484,9 @@ public class Base { continue; } if (portableFolder != null) { - path = RelativePath.relativePath(portableFolder.toString(), path); - if (path == null) - continue; + path = FileUtils.relativePath(portableFolder.toString(), path); + if (path == null) + continue; } Preferences.set("last.sketch" + index + ".path", path); @@ -509,9 +508,9 @@ public class Base { path = ""; } else if (portableFolder != null) { - path = RelativePath.relativePath(portableFolder.toString(), path); + path = FileUtils.relativePath(portableFolder.toString(), path); if (path == null) - path = ""; + path = ""; } Preferences.set("last.sketch" + index + ".path", path); } @@ -1815,8 +1814,8 @@ public class Base { if (!settingsFolder.exists()) { if (!settingsFolder.mkdirs()) { showError(_("Settings issues"), - _("Arduino cannot run because it could not\n" + - "create a folder to store your settings."), null); + _("Arduino cannot run because it could not\n" + + "create a folder to store your settings."), null); } } return settingsFolder; diff --git a/app/src/processing/app/Preferences.java b/app/src/processing/app/Preferences.java index 0661c08f9..e213753f3 100644 --- a/app/src/processing/app/Preferences.java +++ b/app/src/processing/app/Preferences.java @@ -23,6 +23,7 @@ package processing.app; +import processing.app.helpers.FileUtils; import processing.app.syntax.SyntaxStyle; import processing.core.PApplet; import processing.core.PConstants; @@ -324,16 +325,16 @@ public class Preferences { public void actionPerformed(ActionEvent e) { File dflt = new File(sketchbookLocationField.getText()); File file = - Base.selectFolder(_("Select new sketchbook location"), dflt, dialog); + Base.selectFolder(_("Select new sketchbook location"), dflt, dialog); if (file != null) { - String path = file.getAbsolutePath(); - if (Base.getPortableFolder() != null) { - path = RelativePath.relativePath(Base.getPortableFolder().toString(), path); - if (path == null) { - path = Base.getPortableSketchbookFolder(); - } - } - sketchbookLocationField.setText(path); + String path = file.getAbsolutePath(); + if (Base.getPortableFolder() != null) { + path = FileUtils.relativePath(Base.getPortableFolder().toString(), path); + if (path == null) { + path = Base.getPortableSketchbookFolder(); + } + } + sketchbookLocationField.setText(path); } } }); @@ -449,7 +450,7 @@ public class Preferences { // If using portable mode, it's bad manner to change PC setting. if (Base.getPortableFolder() != null) - autoAssociateBox.setEnabled(false); + autoAssociateBox.setEnabled(false); } // More preferences are in the ... @@ -468,7 +469,7 @@ public class Preferences { public void mousePressed(MouseEvent e) { Base.openFolder(Base.getSettingsFolder()); } - + public void mouseEntered(MouseEvent e) { clickable.setForeground(new Color(0, 0, 140)); } @@ -604,9 +605,9 @@ public class Preferences { String newPath = sketchbookLocationField.getText(); if (newPath.isEmpty()) { if (Base.getPortableFolder() == null) - newPath = editor.base.getDefaultSketchbookFolder().toString(); + newPath = editor.base.getDefaultSketchbookFolder().toString(); else - newPath = Base.getPortableSketchbookFolder(); + newPath = Base.getPortableSketchbookFolder(); } if (!newPath.equals(oldPath)) { editor.base.rebuildSketchbookMenus(); diff --git a/app/src/processing/app/RelativePath.java b/app/src/processing/app/RelativePath.java deleted file mode 100644 index 7daeb990e..000000000 --- a/app/src/processing/app/RelativePath.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * by Shigeru KANEMOTO at SWITCHSCIENCE. - */ - -package processing.app; - -import java.io.File; -import java.io.IOException; - -class RelativePath { - // - // Compute relative path to "target" from a directory "origin". - // - // If "origin" is not absolute, it is relative from the current directory. - // If "target" is not absolute, it is relative from "origin". - // - public static String relativePath(String origin, String target) { - try { - origin = (new File(origin)).getCanonicalPath(); - File targetFile = new File(target); - if (targetFile.isAbsolute()) - target = targetFile.getCanonicalPath(); - else - target = (new File(origin, target)).getCanonicalPath(); - } - catch (IOException e) { - return null; - } - - if (origin.equals(target)) { - // origin and target is identical. - return "."; - } - - if (origin.equals(File.separator)) { - // origin is root. - return "." + target; - } - - String prefix = ""; - String root = File.separator; - - if (System.getProperty("os.name").indexOf("Windows") != -1) { - if (origin.startsWith("\\\\") || target.startsWith("\\\\")) { - // Windows UNC path not supported. - return null; - } - - char originLetter = origin.charAt(0); - char targetLetter = target.charAt(0); - if (Character.isLetter(originLetter) && Character.isLetter(targetLetter)) { - // Windows only - if (originLetter != targetLetter) { - // Drive letters differ - return null; - } - } - - prefix = "" + originLetter + ':'; - root = prefix + File.separator; - } - - String relative = ""; - while (!target.startsWith(origin + File.separator)) { - origin = (new File(origin)).getParent(); - if (origin.equals(root)) - origin = prefix; - relative += ".."; - relative += File.separator; - } - - return relative + target.substring(origin.length() + 1); - } -} diff --git a/app/src/processing/app/helpers/FileUtils.java b/app/src/processing/app/helpers/FileUtils.java index 47c5b0a32..fb80d2e4f 100644 --- a/app/src/processing/app/helpers/FileUtils.java +++ b/app/src/processing/app/helpers/FileUtils.java @@ -90,4 +90,68 @@ public class FileUtils { return tmpFolder; } + // + // Compute relative path to "target" from a directory "origin". + // + // If "origin" is not absolute, it is relative from the current directory. + // If "target" is not absolute, it is relative from "origin". + // + // by Shigeru KANEMOTO at SWITCHSCIENCE. + // + public static String relativePath(String origin, String target) { + try { + origin = (new File(origin)).getCanonicalPath(); + File targetFile = new File(target); + if (targetFile.isAbsolute()) + target = targetFile.getCanonicalPath(); + else + target = (new File(origin, target)).getCanonicalPath(); + } catch (IOException e) { + return null; + } + + if (origin.equals(target)) { + // origin and target is identical. + return "."; + } + + if (origin.equals(File.separator)) { + // origin is root. + return "." + target; + } + + String prefix = ""; + String root = File.separator; + + if (System.getProperty("os.name").indexOf("Windows") != -1) { + if (origin.startsWith("\\\\") || target.startsWith("\\\\")) { + // Windows UNC path not supported. + return null; + } + + char originLetter = origin.charAt(0); + char targetLetter = target.charAt(0); + if (Character.isLetter(originLetter) && Character.isLetter(targetLetter)) { + // Windows only + if (originLetter != targetLetter) { + // Drive letters differ + return null; + } + } + + prefix = "" + originLetter + ':'; + root = prefix + File.separator; + } + + String relative = ""; + while (!target.startsWith(origin + File.separator)) { + origin = (new File(origin)).getParent(); + if (origin.equals(root)) + origin = prefix; + relative += ".."; + relative += File.separator; + } + + return relative + target.substring(origin.length() + 1); + } }