1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-16 11:21:18 +03:00

By using syntax like file://./docs/index.html, editor will open file index.html stored in folder SKETCH_FOLDER/docs/. Fixes #224

This commit is contained in:
Federico Fissore
2015-06-08 14:39:11 +02:00
parent 8465202264
commit ccd7fdc0a3
4 changed files with 59 additions and 75 deletions

View File

@ -1006,7 +1006,7 @@ public class Editor extends JFrame implements RunnerListener {
@Override @Override
public void hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) { public void hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) {
try { try {
platform.openURL(hyperlinkEvent.getURL().toExternalForm()); platform.openURL(sketch.getFolder(), hyperlinkEvent.getURL().toExternalForm());
} catch (Exception e) { } catch (Exception e) {
Base.showWarning(e.getMessage(), e.getMessage(), e); Base.showWarning(e.getMessage(), e.getMessage(), e);
} }

View File

@ -29,8 +29,12 @@ import processing.app.debug.TargetPlatform;
import processing.app.legacy.PConstants; import processing.app.legacy.PConstants;
import javax.swing.*; import javax.swing.*;
import java.io.*; import java.io.File;
import java.util.*; import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import static processing.app.I18n._; import static processing.app.I18n._;
@ -38,42 +42,42 @@ import static processing.app.I18n._;
/** /**
* Used by Base for platform-specific tweaking, for instance finding the * Used by Base for platform-specific tweaking, for instance finding the
* sketchbook location using the Windows registry, or OS X event handling. * sketchbook location using the Windows registry, or OS X event handling.
* * <p/>
* The methods in this implementation are used by default, and can be * The methods in this implementation are used by default, and can be
* overridden by a subclass, if loaded by Base.main(). * overridden by a subclass, if loaded by Base.main().
* * <p/>
* These methods throw vanilla-flavored Exceptions, so that error handling * These methods throw vanilla-flavored Exceptions, so that error handling
* occurs inside Base. * occurs inside Base.
* * <p/>
* There is currently no mechanism for adding new platforms, as the setup is * There is currently no mechanism for adding new platforms, as the setup is
* not automated. We could use getProperty("os.arch") perhaps, but that's * not automated. We could use getProperty("os.arch") perhaps, but that's
* debatable (could be upper/lowercase, have spaces, etc.. basically we don't * debatable (could be upper/lowercase, have spaces, etc.. basically we don't
* know if name is proper Java package syntax.) * know if name is proper Java package syntax.)
*/ */
public class Platform { public class Platform {
/** /**
* Set the default L & F. While I enjoy the bounty of the sixteen possible * Set the default L & F. While I enjoy the bounty of the sixteen possible
* exception types that this UIManager method might throw, I feel that in * exception types that this UIManager method might throw, I feel that in
* just this one particular case, I'm being spoiled by those engineers * just this one particular case, I'm being spoiled by those engineers
* at Sun, those Masters of the Abstractionverse. It leaves me feeling sad * at Sun, those Masters of the Abstractionverse. It leaves me feeling sad
* and overweight. So instead, I'll pretend that I'm not offered eleven dozen * and overweight. So instead, I'll pretend that I'm not offered eleven dozen
* ways to report to the user exactly what went wrong, and I'll bundle them * ways to report to the user exactly what went wrong, and I'll bundle them
* all into a single catch-all "Exception". Because in the end, all I really * all into a single catch-all "Exception". Because in the end, all I really
* care about is whether things worked or not. And even then, I don't care. * care about is whether things worked or not. And even then, I don't care.
* *
* @throws Exception Just like I said. * @throws Exception Just like I said.
*/ */
public void setLookAndFeel() throws Exception { public void setLookAndFeel() throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} }
public void init() throws IOException { public void init() throws IOException {
} }
public File getSettingsFolder() throws Exception { public File getSettingsFolder() throws Exception {
// otherwise make a .processing directory int the user's home dir // otherwise make a .processing directory int the user's home dir
File home = new File(System.getProperty("user.home")); File home = new File(System.getProperty("user.home"));
@ -95,37 +99,46 @@ public class Platform {
} }
*/ */
} }
/** /**
* @return null if not overridden, which will cause a prompt to show instead. * @return null if not overridden, which will cause a prompt to show instead.
* @throws Exception * @throws Exception
*/ */
public File getDefaultSketchbookFolder() throws Exception { public File getDefaultSketchbookFolder() throws Exception {
return null; return null;
} }
public void openURL(File folder, String url) throws Exception {
if (!url.startsWith("file://./")) {
openURL(url);
return;
}
url = url.replaceAll("file://./", folder.getCanonicalFile().toURI().toASCIIString());
openURL(url);
}
public void openURL(String url) throws Exception { public void openURL(String url) throws Exception {
String launcher = PreferencesData.get("launcher"); String launcher = PreferencesData.get("launcher");
if (launcher != null) { if (launcher != null) {
Runtime.getRuntime().exec(new String[] { launcher, url }); Runtime.getRuntime().exec(new String[]{launcher, url});
} else { } else {
showLauncherWarning(); showLauncherWarning();
} }
} }
public boolean openFolderAvailable() { public boolean openFolderAvailable() {
return PreferencesData.get("launcher") != null; return PreferencesData.get("launcher") != null;
} }
public void openFolder(File file) throws Exception { public void openFolder(File file) throws Exception {
String launcher = PreferencesData.get("launcher"); String launcher = PreferencesData.get("launcher");
if (launcher != null) { if (launcher != null) {
String folder = file.getAbsolutePath(); String folder = file.getAbsolutePath();
Runtime.getRuntime().exec(new String[] { launcher, folder }); Runtime.getRuntime().exec(new String[]{launcher, folder});
} else { } else {
showLauncherWarning(); showLauncherWarning();
} }
@ -184,14 +197,14 @@ public class Platform {
return PConstants.platformNames[PConstants.OTHER]; return PConstants.platformNames[PConstants.OTHER];
} }
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
protected void showLauncherWarning() { protected void showLauncherWarning() {
BaseNoGui.showWarning(_("No launcher available"), BaseNoGui.showWarning(_("No launcher available"),
_("Unspecified platform, no launcher available.\nTo enable opening URLs or folders, add a \n\"launcher=/path/to/app\" line to preferences.txt"), _("Unspecified platform, no launcher available.\nTo enable opening URLs or folders, add a \n\"launcher=/path/to/app\" line to preferences.txt"),
null); null);
} }
public List<BoardPort> filterPorts(List<BoardPort> ports, boolean aBoolean) { public List<BoardPort> filterPorts(List<BoardPort> ports, boolean aBoolean) {

View File

@ -34,11 +34,14 @@ import processing.app.legacy.PApplet;
import processing.app.legacy.PConstants; import processing.app.legacy.PConstants;
import java.awt.*; import java.awt.*;
import java.io.*; import java.io.ByteArrayOutputStream;
import java.lang.reflect.Method; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.*; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
@ -98,45 +101,13 @@ public class Platform extends processing.app.Platform {
public void openURL(String url) throws Exception { public void openURL(String url) throws Exception {
if (PApplet.javaVersion < 1.6f) { Desktop desktop = Desktop.getDesktop();
if (url.startsWith("http")) { if (url.startsWith("http") || url.startsWith("file:")) {
// formerly com.apple.eio.FileManager.openURL(url); desktop.browse(new URI(url));
// but due to deprecation, instead loading dynamically
try {
Class<?> eieio = Class.forName("com.apple.eio.FileManager");
Method openMethod =
eieio.getMethod("openURL", new Class[] { String.class });
openMethod.invoke(null, new Object[] { url });
} catch (Exception e) {
e.printStackTrace();
}
} else {
// Assume this is a file instead, and just open it.
// Extension of http://dev.processing.org/bugs/show_bug.cgi?id=1010
PApplet.open(url);
}
} else { } else {
try { desktop.open(new File(url));
Class<?> desktopClass = Class.forName("java.awt.Desktop");
Method getMethod = desktopClass.getMethod("getDesktop");
Object desktop = getMethod.invoke(null, new Object[] { });
// for Java 1.6, replacing with java.awt.Desktop.browse()
// and java.awt.Desktop.open()
if (url.startsWith("http")) { // browse to a location
Method browseMethod =
desktopClass.getMethod("browse", new Class[] { URI.class });
browseMethod.invoke(desktop, new Object[] { new URI(url) });
} else { // open a file
Method openMethod =
desktopClass.getMethod("open", new Class[] { File.class });
openMethod.invoke(desktop, new Object[] { new File(url) });
}
} catch (Exception e) {
e.printStackTrace();
}
}
} }
}
public boolean openFolderAvailable() { public boolean openFolderAvailable() {

View File

@ -130,7 +130,7 @@ public class Platform extends processing.app.Platform {
// "Access is denied" in both cygwin and the "dos" prompt. // "Access is denied" in both cygwin and the "dos" prompt.
//Runtime.getRuntime().exec("cmd /c " + currentDir + "\\reference\\" + //Runtime.getRuntime().exec("cmd /c " + currentDir + "\\reference\\" +
// referenceFile + ".html"); // referenceFile + ".html");
if (url.startsWith("http")) { if (url.startsWith("http") || url.startsWith("file:")) {
// open dos prompt, give it 'start' command, which will // open dos prompt, give it 'start' command, which will
// open the url properly. start by itself won't work since // open the url properly. start by itself won't work since
// it appears to need cmd // it appears to need cmd