Skip to content

Commit 73c8f1c

Browse files
committedJan 16, 2013
see #1211: formatted code, moved RelativePath code into FileUtils
1 parent 0934836 commit 73c8f1c

File tree

4 files changed

+100
-110
lines changed

4 files changed

+100
-110
lines changed
 

‎app/src/processing/app/Base.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -259,16 +259,16 @@ public Base(String[] args) throws Exception {
259259
if (sketchbookPath != null) {
260260
File sketchbookFolder;
261261
if (portableFolder != null)
262-
sketchbookFolder = new File(portableFolder, sketchbookPath);
262+
sketchbookFolder = new File(portableFolder, sketchbookPath);
263263
else
264-
sketchbookFolder = new File(sketchbookPath);
264+
sketchbookFolder = new File(sketchbookPath);
265265
if (!sketchbookFolder.exists()) {
266266
Base.showWarning(_("Sketchbook folder disappeared"),
267-
_("The sketchbook folder no longer exists.\n" +
268-
"Arduino will switch to the default sketchbook\n" +
269-
"location, and create a new sketchbook folder if\n" +
270-
"necessary. Arduino will then stop talking about\n" +
271-
"himself in the third person."), null);
267+
_("The sketchbook folder no longer exists.\n" +
268+
"Arduino will switch to the default sketchbook\n" +
269+
"location, and create a new sketchbook folder if\n" +
270+
"necessary. Arduino will then stop talking about\n" +
271+
"himself in the third person."), null);
272272
sketchbookPath = null;
273273
}
274274
}
@@ -277,9 +277,9 @@ public Base(String[] args) throws Exception {
277277
if (sketchbookPath == null) {
278278
File defaultFolder = getDefaultSketchbookFolder();
279279
if (portableFolder != null)
280-
Preferences.set("sketchbook.path", portableSketchbookFolder);
280+
Preferences.set("sketchbook.path", portableSketchbookFolder);
281281
else
282-
Preferences.set("sketchbook.path", defaultFolder.getAbsolutePath());
282+
Preferences.set("sketchbook.path", defaultFolder.getAbsolutePath());
283283
if (!defaultFolder.exists()) {
284284
defaultFolder.mkdirs();
285285
}
@@ -438,13 +438,12 @@ protected boolean restoreSketches() {
438438
for (int i = 0; i < count; i++) {
439439
String path = Preferences.get("last.sketch" + i + ".path");
440440
if (portableFolder != null) {
441-
File absolute = new File(portableFolder, path);
442-
try {
443-
path = absolute.getCanonicalPath();
444-
}
445-
catch (IOException e) {
446-
// path unchanged.
447-
}
441+
File absolute = new File(portableFolder, path);
442+
try {
443+
path = absolute.getCanonicalPath();
444+
} catch (IOException e) {
445+
// path unchanged.
446+
}
448447
}
449448
int[] location;
450449
if (windowPositionValid) {
@@ -485,9 +484,9 @@ protected void storeSketches() {
485484
continue;
486485
}
487486
if (portableFolder != null) {
488-
path = RelativePath.relativePath(portableFolder.toString(), path);
489-
if (path == null)
490-
continue;
487+
path = FileUtils.relativePath(portableFolder.toString(), path);
488+
if (path == null)
489+
continue;
491490
}
492491
Preferences.set("last.sketch" + index + ".path", path);
493492

@@ -509,9 +508,9 @@ protected void storeSketchPath(Editor editor, int index) {
509508
path = "";
510509
} else
511510
if (portableFolder != null) {
512-
path = RelativePath.relativePath(portableFolder.toString(), path);
511+
path = FileUtils.relativePath(portableFolder.toString(), path);
513512
if (path == null)
514-
path = "";
513+
path = "";
515514
}
516515
Preferences.set("last.sketch" + index + ".path", path);
517516
}
@@ -1815,8 +1814,8 @@ static public File getSettingsFolder() {
18151814
if (!settingsFolder.exists()) {
18161815
if (!settingsFolder.mkdirs()) {
18171816
showError(_("Settings issues"),
1818-
_("Arduino cannot run because it could not\n" +
1819-
"create a folder to store your settings."), null);
1817+
_("Arduino cannot run because it could not\n" +
1818+
"create a folder to store your settings."), null);
18201819
}
18211820
}
18221821
return settingsFolder;

‎app/src/processing/app/Preferences.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
package processing.app;
2525

26+
import processing.app.helpers.FileUtils;
2627
import processing.app.syntax.SyntaxStyle;
2728
import processing.core.PApplet;
2829
import processing.core.PConstants;
@@ -324,16 +325,16 @@ public Preferences() {
324325
public void actionPerformed(ActionEvent e) {
325326
File dflt = new File(sketchbookLocationField.getText());
326327
File file =
327-
Base.selectFolder(_("Select new sketchbook location"), dflt, dialog);
328+
Base.selectFolder(_("Select new sketchbook location"), dflt, dialog);
328329
if (file != null) {
329-
String path = file.getAbsolutePath();
330-
if (Base.getPortableFolder() != null) {
331-
path = RelativePath.relativePath(Base.getPortableFolder().toString(), path);
332-
if (path == null) {
333-
path = Base.getPortableSketchbookFolder();
334-
}
335-
}
336-
sketchbookLocationField.setText(path);
330+
String path = file.getAbsolutePath();
331+
if (Base.getPortableFolder() != null) {
332+
path = FileUtils.relativePath(Base.getPortableFolder().toString(), path);
333+
if (path == null) {
334+
path = Base.getPortableSketchbookFolder();
335+
}
336+
}
337+
sketchbookLocationField.setText(path);
337338
}
338339
}
339340
});
@@ -449,7 +450,7 @@ public void actionPerformed(ActionEvent e) {
449450

450451
// If using portable mode, it's bad manner to change PC setting.
451452
if (Base.getPortableFolder() != null)
452-
autoAssociateBox.setEnabled(false);
453+
autoAssociateBox.setEnabled(false);
453454
}
454455

455456
// More preferences are in the ...
@@ -468,7 +469,7 @@ public void actionPerformed(ActionEvent e) {
468469
public void mousePressed(MouseEvent e) {
469470
Base.openFolder(Base.getSettingsFolder());
470471
}
471-
472+
472473
public void mouseEntered(MouseEvent e) {
473474
clickable.setForeground(new Color(0, 0, 140));
474475
}
@@ -604,9 +605,9 @@ protected void applyFrame() {
604605
String newPath = sketchbookLocationField.getText();
605606
if (newPath.isEmpty()) {
606607
if (Base.getPortableFolder() == null)
607-
newPath = editor.base.getDefaultSketchbookFolder().toString();
608+
newPath = editor.base.getDefaultSketchbookFolder().toString();
608609
else
609-
newPath = Base.getPortableSketchbookFolder();
610+
newPath = Base.getPortableSketchbookFolder();
610611
}
611612
if (!newPath.equals(oldPath)) {
612613
editor.base.rebuildSketchbookMenus();

‎app/src/processing/app/RelativePath.java

Lines changed: 0 additions & 74 deletions
This file was deleted.

‎app/src/processing/app/helpers/FileUtils.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,68 @@ public static File createTempFolder() throws IOException {
9090
return tmpFolder;
9191
}
9292

93+
//
94+
// Compute relative path to "target" from a directory "origin".
95+
//
96+
// If "origin" is not absolute, it is relative from the current directory.
97+
// If "target" is not absolute, it is relative from "origin".
98+
//
99+
// by Shigeru KANEMOTO at SWITCHSCIENCE.
100+
//
101+
public static String relativePath(String origin, String target) {
102+
try {
103+
origin = (new File(origin)).getCanonicalPath();
104+
File targetFile = new File(target);
105+
if (targetFile.isAbsolute())
106+
target = targetFile.getCanonicalPath();
107+
else
108+
target = (new File(origin, target)).getCanonicalPath();
109+
} catch (IOException e) {
110+
return null;
111+
}
112+
113+
if (origin.equals(target)) {
114+
// origin and target is identical.
115+
return ".";
116+
}
117+
118+
if (origin.equals(File.separator)) {
119+
// origin is root.
120+
return "." + target;
121+
}
122+
123+
String prefix = "";
124+
String root = File.separator;
125+
126+
if (System.getProperty("os.name").indexOf("Windows") != -1) {
127+
if (origin.startsWith("\\\\") || target.startsWith("\\\\")) {
128+
// Windows UNC path not supported.
129+
return null;
130+
}
131+
132+
char originLetter = origin.charAt(0);
133+
char targetLetter = target.charAt(0);
134+
if (Character.isLetter(originLetter) && Character.isLetter(targetLetter)) {
135+
// Windows only
136+
if (originLetter != targetLetter) {
137+
// Drive letters differ
138+
return null;
139+
}
140+
}
141+
142+
prefix = "" + originLetter + ':';
143+
root = prefix + File.separator;
144+
}
145+
146+
String relative = "";
147+
while (!target.startsWith(origin + File.separator)) {
148+
origin = (new File(origin)).getParent();
149+
if (origin.equals(root))
150+
origin = prefix;
151+
relative += "..";
152+
relative += File.separator;
153+
}
154+
155+
return relative + target.substring(origin.length() + 1);
156+
}
93157
}

2 commit comments

Comments
 (2)

sgk commented on Jan 16, 2013

@sgk
Contributor

Sorry. I mixed tab and space.

ffissore commented on Jan 17, 2013

@ffissore
ContributorAuthor

not a problem, really :)

Please sign in to comment.