Skip to content

Commit ccd7fdc

Browse files
author
Federico Fissore
committed
By using syntax like file://./docs/index.html, editor will open file index.html stored in folder SKETCH_FOLDER/docs/. Fixes esp8266#224
1 parent 8465202 commit ccd7fdc

File tree

4 files changed

+59
-75
lines changed

4 files changed

+59
-75
lines changed

app/src/processing/app/Editor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ private SketchTextArea createTextArea() throws IOException {
10061006
@Override
10071007
public void hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) {
10081008
try {
1009-
platform.openURL(hyperlinkEvent.getURL().toExternalForm());
1009+
platform.openURL(sketch.getFolder(), hyperlinkEvent.getURL().toExternalForm());
10101010
} catch (Exception e) {
10111011
Base.showWarning(e.getMessage(), e.getMessage(), e);
10121012
}

arduino-core/src/processing/app/Platform.java

+46-33
Original file line numberDiff line numberDiff line change
@@ -29,51 +29,55 @@
2929
import processing.app.legacy.PConstants;
3030

3131
import javax.swing.*;
32-
import java.io.*;
33-
import java.util.*;
32+
import java.io.File;
33+
import java.io.IOException;
34+
import java.util.HashMap;
35+
import java.util.LinkedList;
36+
import java.util.List;
37+
import java.util.Map;
3438

3539
import static processing.app.I18n._;
3640

3741

3842
/**
3943
* Used by Base for platform-specific tweaking, for instance finding the
4044
* sketchbook location using the Windows registry, or OS X event handling.
41-
*
42-
* The methods in this implementation are used by default, and can be
43-
* overridden by a subclass, if loaded by Base.main().
44-
*
45+
* <p/>
46+
* The methods in this implementation are used by default, and can be
47+
* overridden by a subclass, if loaded by Base.main().
48+
* <p/>
4549
* These methods throw vanilla-flavored Exceptions, so that error handling
46-
* occurs inside Base.
47-
*
48-
* There is currently no mechanism for adding new platforms, as the setup is
49-
* not automated. We could use getProperty("os.arch") perhaps, but that's
50-
* debatable (could be upper/lowercase, have spaces, etc.. basically we don't
50+
* occurs inside Base.
51+
* <p/>
52+
* There is currently no mechanism for adding new platforms, as the setup is
53+
* not automated. We could use getProperty("os.arch") perhaps, but that's
54+
* debatable (could be upper/lowercase, have spaces, etc.. basically we don't
5155
* know if name is proper Java package syntax.)
5256
*/
5357
public class Platform {
54-
55-
58+
59+
5660
/**
5761
* Set the default L & F. While I enjoy the bounty of the sixteen possible
58-
* exception types that this UIManager method might throw, I feel that in
62+
* exception types that this UIManager method might throw, I feel that in
5963
* just this one particular case, I'm being spoiled by those engineers
6064
* at Sun, those Masters of the Abstractionverse. It leaves me feeling sad
6165
* and overweight. So instead, I'll pretend that I'm not offered eleven dozen
6266
* ways to report to the user exactly what went wrong, and I'll bundle them
63-
* all into a single catch-all "Exception". Because in the end, all I really
67+
* all into a single catch-all "Exception". Because in the end, all I really
6468
* care about is whether things worked or not. And even then, I don't care.
65-
*
69+
*
6670
* @throws Exception Just like I said.
6771
*/
6872
public void setLookAndFeel() throws Exception {
6973
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
7074
}
71-
72-
75+
76+
7377
public void init() throws IOException {
7478
}
75-
76-
79+
80+
7781
public File getSettingsFolder() throws Exception {
7882
// otherwise make a .processing directory int the user's home dir
7983
File home = new File(System.getProperty("user.home"));
@@ -95,37 +99,46 @@ public File getSettingsFolder() throws Exception {
9599
}
96100
*/
97101
}
98-
102+
99103

100104
/**
101-
* @return null if not overridden, which will cause a prompt to show instead.
105+
* @return null if not overridden, which will cause a prompt to show instead.
102106
* @throws Exception
103107
*/
104108
public File getDefaultSketchbookFolder() throws Exception {
105109
return null;
106110
}
107-
108-
111+
112+
public void openURL(File folder, String url) throws Exception {
113+
if (!url.startsWith("file://./")) {
114+
openURL(url);
115+
return;
116+
}
117+
118+
url = url.replaceAll("file://./", folder.getCanonicalFile().toURI().toASCIIString());
119+
openURL(url);
120+
}
121+
109122
public void openURL(String url) throws Exception {
110123
String launcher = PreferencesData.get("launcher");
111124
if (launcher != null) {
112-
Runtime.getRuntime().exec(new String[] { launcher, url });
125+
Runtime.getRuntime().exec(new String[]{launcher, url});
113126
} else {
114127
showLauncherWarning();
115-
}
128+
}
116129
}
117130

118131

119132
public boolean openFolderAvailable() {
120133
return PreferencesData.get("launcher") != null;
121134
}
122-
123-
135+
136+
124137
public void openFolder(File file) throws Exception {
125138
String launcher = PreferencesData.get("launcher");
126139
if (launcher != null) {
127140
String folder = file.getAbsolutePath();
128-
Runtime.getRuntime().exec(new String[] { launcher, folder });
141+
Runtime.getRuntime().exec(new String[]{launcher, folder});
129142
} else {
130143
showLauncherWarning();
131144
}
@@ -184,14 +197,14 @@ public String getName() {
184197
return PConstants.platformNames[PConstants.OTHER];
185198
}
186199

187-
200+
188201
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
189202

190203

191204
protected void showLauncherWarning() {
192-
BaseNoGui.showWarning(_("No launcher available"),
193-
_("Unspecified platform, no launcher available.\nTo enable opening URLs or folders, add a \n\"launcher=/path/to/app\" line to preferences.txt"),
194-
null);
205+
BaseNoGui.showWarning(_("No launcher available"),
206+
_("Unspecified platform, no launcher available.\nTo enable opening URLs or folders, add a \n\"launcher=/path/to/app\" line to preferences.txt"),
207+
null);
195208
}
196209

197210
public List<BoardPort> filterPorts(List<BoardPort> ports, boolean aBoolean) {

arduino-core/src/processing/app/macosx/Platform.java

+11-40
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@
3434
import processing.app.legacy.PConstants;
3535

3636
import java.awt.*;
37-
import java.io.*;
38-
import java.lang.reflect.Method;
37+
import java.io.ByteArrayOutputStream;
38+
import java.io.File;
39+
import java.io.FileNotFoundException;
40+
import java.io.IOException;
3941
import java.net.URI;
40-
import java.util.*;
42+
import java.util.LinkedList;
4143
import java.util.List;
44+
import java.util.Map;
4245

4346

4447
/**
@@ -98,45 +101,13 @@ public File getDefaultSketchbookFolder() throws Exception {
98101

99102

100103
public void openURL(String url) throws Exception {
101-
if (PApplet.javaVersion < 1.6f) {
102-
if (url.startsWith("http")) {
103-
// formerly com.apple.eio.FileManager.openURL(url);
104-
// but due to deprecation, instead loading dynamically
105-
try {
106-
Class<?> eieio = Class.forName("com.apple.eio.FileManager");
107-
Method openMethod =
108-
eieio.getMethod("openURL", new Class[] { String.class });
109-
openMethod.invoke(null, new Object[] { url });
110-
} catch (Exception e) {
111-
e.printStackTrace();
112-
}
113-
} else {
114-
// Assume this is a file instead, and just open it.
115-
// Extension of http://dev.processing.org/bugs/show_bug.cgi?id=1010
116-
PApplet.open(url);
117-
}
104+
Desktop desktop = Desktop.getDesktop();
105+
if (url.startsWith("http") || url.startsWith("file:")) {
106+
desktop.browse(new URI(url));
118107
} else {
119-
try {
120-
Class<?> desktopClass = Class.forName("java.awt.Desktop");
121-
Method getMethod = desktopClass.getMethod("getDesktop");
122-
Object desktop = getMethod.invoke(null, new Object[] { });
123-
124-
// for Java 1.6, replacing with java.awt.Desktop.browse()
125-
// and java.awt.Desktop.open()
126-
if (url.startsWith("http")) { // browse to a location
127-
Method browseMethod =
128-
desktopClass.getMethod("browse", new Class[] { URI.class });
129-
browseMethod.invoke(desktop, new Object[] { new URI(url) });
130-
} else { // open a file
131-
Method openMethod =
132-
desktopClass.getMethod("open", new Class[] { File.class });
133-
openMethod.invoke(desktop, new Object[] { new File(url) });
134-
}
135-
} catch (Exception e) {
136-
e.printStackTrace();
137-
}
138-
}
108+
desktop.open(new File(url));
139109
}
110+
}
140111

141112

142113
public boolean openFolderAvailable() {

arduino-core/src/processing/app/windows/Platform.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void openURL(String url) throws Exception {
130130
// "Access is denied" in both cygwin and the "dos" prompt.
131131
//Runtime.getRuntime().exec("cmd /c " + currentDir + "\\reference\\" +
132132
// referenceFile + ".html");
133-
if (url.startsWith("http")) {
133+
if (url.startsWith("http") || url.startsWith("file:")) {
134134
// open dos prompt, give it 'start' command, which will
135135
// open the url properly. start by itself won't work since
136136
// it appears to need cmd

0 commit comments

Comments
 (0)