1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-29 10:24:12 +01: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) {
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;

View File

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

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