1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-17 22:23:10 +03:00

see #1211: formatted code, moved RelativePath code into FileUtils

This commit is contained in:
Federico Fissore
2013-01-16 14:04:30 +01:00
parent 093483602f
commit 73c8f1c4ab
4 changed files with 100 additions and 110 deletions

View File

@ -259,16 +259,16 @@ public class Base {
if (sketchbookPath != null) { if (sketchbookPath != null) {
File sketchbookFolder; File sketchbookFolder;
if (portableFolder != null) if (portableFolder != null)
sketchbookFolder = new File(portableFolder, sketchbookPath); sketchbookFolder = new File(portableFolder, sketchbookPath);
else else
sketchbookFolder = new File(sketchbookPath); sketchbookFolder = new File(sketchbookPath);
if (!sketchbookFolder.exists()) { if (!sketchbookFolder.exists()) {
Base.showWarning(_("Sketchbook folder disappeared"), Base.showWarning(_("Sketchbook folder disappeared"),
_("The sketchbook folder no longer exists.\n" + _("The sketchbook folder no longer exists.\n" +
"Arduino will switch to the default sketchbook\n" + "Arduino will switch to the default sketchbook\n" +
"location, and create a new sketchbook folder if\n" + "location, and create a new sketchbook folder if\n" +
"necessary. Arduino will then stop talking about\n" + "necessary. Arduino will then stop talking about\n" +
"himself in the third person."), null); "himself in the third person."), null);
sketchbookPath = null; sketchbookPath = null;
} }
} }
@ -277,9 +277,9 @@ public class Base {
if (sketchbookPath == null) { if (sketchbookPath == null) {
File defaultFolder = getDefaultSketchbookFolder(); File defaultFolder = getDefaultSketchbookFolder();
if (portableFolder != null) if (portableFolder != null)
Preferences.set("sketchbook.path", portableSketchbookFolder); Preferences.set("sketchbook.path", portableSketchbookFolder);
else else
Preferences.set("sketchbook.path", defaultFolder.getAbsolutePath()); Preferences.set("sketchbook.path", defaultFolder.getAbsolutePath());
if (!defaultFolder.exists()) { if (!defaultFolder.exists()) {
defaultFolder.mkdirs(); defaultFolder.mkdirs();
} }
@ -438,13 +438,12 @@ public class Base {
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
String path = Preferences.get("last.sketch" + i + ".path"); String path = Preferences.get("last.sketch" + i + ".path");
if (portableFolder != null) { if (portableFolder != null) {
File absolute = new File(portableFolder, path); File absolute = new File(portableFolder, path);
try { try {
path = absolute.getCanonicalPath(); path = absolute.getCanonicalPath();
} } catch (IOException e) {
catch (IOException e) { // path unchanged.
// path unchanged. }
}
} }
int[] location; int[] location;
if (windowPositionValid) { if (windowPositionValid) {
@ -485,9 +484,9 @@ public class Base {
continue; continue;
} }
if (portableFolder != null) { if (portableFolder != null) {
path = RelativePath.relativePath(portableFolder.toString(), path); path = FileUtils.relativePath(portableFolder.toString(), path);
if (path == null) if (path == null)
continue; continue;
} }
Preferences.set("last.sketch" + index + ".path", path); Preferences.set("last.sketch" + index + ".path", path);
@ -509,9 +508,9 @@ public class Base {
path = ""; path = "";
} else } else
if (portableFolder != null) { if (portableFolder != null) {
path = RelativePath.relativePath(portableFolder.toString(), path); path = FileUtils.relativePath(portableFolder.toString(), path);
if (path == null) if (path == null)
path = ""; path = "";
} }
Preferences.set("last.sketch" + index + ".path", path); Preferences.set("last.sketch" + index + ".path", path);
} }
@ -1815,8 +1814,8 @@ public class Base {
if (!settingsFolder.exists()) { if (!settingsFolder.exists()) {
if (!settingsFolder.mkdirs()) { if (!settingsFolder.mkdirs()) {
showError(_("Settings issues"), showError(_("Settings issues"),
_("Arduino cannot run because it could not\n" + _("Arduino cannot run because it could not\n" +
"create a folder to store your settings."), null); "create a folder to store your settings."), null);
} }
} }
return settingsFolder; return settingsFolder;

View File

@ -23,6 +23,7 @@
package processing.app; package processing.app;
import processing.app.helpers.FileUtils;
import processing.app.syntax.SyntaxStyle; import processing.app.syntax.SyntaxStyle;
import processing.core.PApplet; import processing.core.PApplet;
import processing.core.PConstants; import processing.core.PConstants;
@ -324,16 +325,16 @@ public class Preferences {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
File dflt = new File(sketchbookLocationField.getText()); File dflt = new File(sketchbookLocationField.getText());
File file = File file =
Base.selectFolder(_("Select new sketchbook location"), dflt, dialog); Base.selectFolder(_("Select new sketchbook location"), dflt, dialog);
if (file != null) { if (file != null) {
String path = file.getAbsolutePath(); String path = file.getAbsolutePath();
if (Base.getPortableFolder() != null) { if (Base.getPortableFolder() != null) {
path = RelativePath.relativePath(Base.getPortableFolder().toString(), path); path = FileUtils.relativePath(Base.getPortableFolder().toString(), path);
if (path == null) { if (path == null) {
path = Base.getPortableSketchbookFolder(); path = Base.getPortableSketchbookFolder();
} }
} }
sketchbookLocationField.setText(path); sketchbookLocationField.setText(path);
} }
} }
}); });
@ -449,7 +450,7 @@ public class Preferences {
// If using portable mode, it's bad manner to change PC setting. // If using portable mode, it's bad manner to change PC setting.
if (Base.getPortableFolder() != null) if (Base.getPortableFolder() != null)
autoAssociateBox.setEnabled(false); autoAssociateBox.setEnabled(false);
} }
// More preferences are in the ... // More preferences are in the ...
@ -468,7 +469,7 @@ public class Preferences {
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
Base.openFolder(Base.getSettingsFolder()); Base.openFolder(Base.getSettingsFolder());
} }
public void mouseEntered(MouseEvent e) { public void mouseEntered(MouseEvent e) {
clickable.setForeground(new Color(0, 0, 140)); clickable.setForeground(new Color(0, 0, 140));
} }
@ -604,9 +605,9 @@ public class Preferences {
String newPath = sketchbookLocationField.getText(); String newPath = sketchbookLocationField.getText();
if (newPath.isEmpty()) { if (newPath.isEmpty()) {
if (Base.getPortableFolder() == null) if (Base.getPortableFolder() == null)
newPath = editor.base.getDefaultSketchbookFolder().toString(); newPath = editor.base.getDefaultSketchbookFolder().toString();
else else
newPath = Base.getPortableSketchbookFolder(); newPath = Base.getPortableSketchbookFolder();
} }
if (!newPath.equals(oldPath)) { if (!newPath.equals(oldPath)) {
editor.base.rebuildSketchbookMenus(); editor.base.rebuildSketchbookMenus();

View File

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

View File

@ -90,4 +90,68 @@ public class FileUtils {
return tmpFolder; 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);
}
} }