reworking open()

This commit is contained in:
benfry
2007-07-14 13:12:42 +00:00
parent 216d1a734e
commit 92e79850a6
2 changed files with 110 additions and 63 deletions

View File

@@ -2235,7 +2235,9 @@ public class PApplet extends Applet
e.printStackTrace();
}
} else {
throw new RuntimeException("Can't open URLs for this platform");
//throw new RuntimeException("Can't open URLs for this platform");
// Just pass it off to open() and hope for the best
open(url);
}
} catch (IOException e) {
e.printStackTrace();
@@ -2249,70 +2251,12 @@ public class PApplet extends Applet
* Attempt to open a file using the platform's shell.
*/
public void open(String filename) {
if (platform == WINDOWS) {
// just launching the .html file via the shell works
// but make sure to chmod +x the .html files first
// also place quotes around it in case there's a space
// in the user.dir part of the url
try {
Runtime.getRuntime().exec("cmd /c \"" + filename + "\"");
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Could not open " + filename);
}
} else if (platform == MACOSX) {
// osx fix contributed by chandler for rev 0113
try {
// Java on OS X doesn't like to exec commands inside quotes
// for some reason.. escape spaces with slashes just in case
if (filename.indexOf(' ') != -1) {
StringBuffer sb = new StringBuffer();
char c[] = filename.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == ' ') {
sb.append("\\\\ ");
} else {
sb.append(c[i]);
}
}
filename = sb.toString();
}
Runtime.getRuntime().exec("open " + filename);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Could not open " + filename);
}
} else if (platform == MACOS9) {
// prepend file:// on this guy since it's a file
String url = "file://" + filename;
// replace spaces with %20 for the file url
// otherwise the mac doesn't like to open it
// can't just use URLEncoder, since that makes slashes into
// %2F characters, which is no good. some might say "useless"
if (url.indexOf(' ') != -1) {
StringBuffer sb = new StringBuffer();
char c[] = url.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == ' ') {
sb.append("%20");
} else {
sb.append(c[i]);
}
}
url = sb.toString();
}
link(url);
} else { // give up and just pass it to Runtime.exec()
open(new String[] { filename });
}
open(new String[] { filename });
}
static String openLauncher;
/**
* Launch a process using a platforms shell. This version uses an array
* to make it easier to deal with spaces in the individual elements.
@@ -2320,6 +2264,63 @@ public class PApplet extends Applet
* around different bits).
*/
static public Process open(String argv[]) {
String[] params = null;
if (platform == WINDOWS) {
// just launching the .html file via the shell works
// but make sure to chmod +x the .html files first
// also place quotes around it in case there's a space
// in the user.dir part of the url
params = new String[] { "cmd", "/c" };
} else if (platform == MACOSX) {
params = new String[] { "open" };
} else if (platform == LINUX) {
if (openLauncher == null) {
// Attempt to use gnome-open
try {
Process p = Runtime.getRuntime().exec(new String[] { "gnome-open" });
/*int result =*/ p.waitFor();
// Not installed will throw an IOException (JDK 1.4.2, Ubuntu 7.04)
openLauncher = "gnome-open";
} catch (Exception e) { }
}
if (openLauncher == null) {
// Attempt with kde-open
try {
Process p = Runtime.getRuntime().exec(new String[] { "kde-open" });
/*int result =*/ p.waitFor();
openLauncher = "kde-open";
} catch (Exception e) { }
}
if (openLauncher == null) {
System.err.println("Could not find gnome-open or kde-open, " +
"the open() command may not work.");
}
if (openLauncher != null) {
params = new String[] { openLauncher };
}
//} else { // give up and just pass it to Runtime.exec()
//open(new String[] { filename });
//params = new String[] { filename };
}
if (params != null) {
// If the 'open', 'gnome-open' or 'cmd' are already included
if (params[0].equals(argv[0])) {
// then don't prepend those params again
return exec(argv);
} else {
params = concat(params, argv);
return exec(params);
}
} else {
return exec(argv);
}
}
static public Process exec(String[] argv) {
try {
return Runtime.getRuntime().exec(argv);
} catch (Exception e) {
@@ -2328,6 +2329,47 @@ public class PApplet extends Applet
}
}
/*
try {
Runtime.getRuntime().exec("cmd /c \"" + filename + "\"");
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Could not open " + filename);
}
try {
return Runtime.getRuntime().exec(argv);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Could not open " + join(argv, ' '));
}
}
/*
static protected String findLinuxLauncher() {
if (linuxLauncher == null) {
// Attempt to use gnome-open
try {
Process p = Runtime.getRuntime().exec(new String[] { "gnome-open" });
int result = p.waitFor();
// Not installed will throw an IOException (JDK 1.4.2, Ubuntu 7.04)
linuxLauncher = "gnome-open";
} catch (Exception e) { }
// Attempt with kde-open
try {
Process p = Runtime.getRuntime().exec(new String[] { "kde-open" });
int result = p.waitFor();
linuxLauncher = "kde-open";
} catch (Exception e) { }
}
if (linuxLauncher == null) {
System.err.println("Could not find gnome-open or kde-open, " +
"the open() command may not work.");
}
return linuxLauncher;
}
*/
//////////////////////////////////////////////////////////////
@@ -6721,7 +6763,7 @@ public class PApplet extends Applet
Dimension windowSize = farm.getSize();
int usableW = windowSize.width - insets.left - insets.right;
int usableH = windowSize.height - insets.top - insets.bottom;
// the ComponentListener in PApplet will handle calling size()
setBounds(insets.left, insets.top, usableW, usableH);
}

View File

@@ -108,6 +108,11 @@ X problem with resizing the component when the frame wasn't resizable
X http://dev.processing.org/bugs/show_bug.cgi?id=341
_ unregisterXxxx() calls to remove methods from libs
_ http://dev.processing.org/bugs/show_bug.cgi?id=312
X major rework of the open() command
X add gnome-open/kde-open for with PApplet.open()
X add open (-a?) on osx to the open() command
_ make changes in the javadoc and reference
_ offscreen buffers fail with texture mapping
_ http://dev.processing.org/bugs/show_bug.cgi?id=594