3 import java.io.FileNotFoundException;
4 import java.io.IOException;
5 import java.lang.reflect.Constructor;
6 import java.lang.reflect.Field;
7 import java.lang.reflect.InvocationTargetException;
8 import java.lang.reflect.Method;
10 import java.net.URLClassLoader;
11 import java.applet.Applet;
63 public void run(String theURL) {
64 if (theURL==null || theURL.equals(
""))
65 theURL =
"http://rsb.info.nih.gov/ij/";
66 else if (theURL.equals(
"online"))
67 theURL =
"http://rsb.info.nih.gov/ij/docs";
68 Applet applet = ij.IJ.getApplet();
71 applet.getAppletContext().showDocument(
new URL(theURL),
"_blank" );
72 }
catch (Exception e) {}
75 try {openURL(theURL);}
76 catch (IOException e) {}
82 private static int jvm;
85 private static Object browser;
94 private static boolean loadedWithoutErrors;
97 private static Class mrjFileUtilsClass;
100 private static Class mrjOSTypeClass;
103 private static Class aeDescClass;
106 private static Constructor aeTargetConstructor;
109 private static Constructor appleEventConstructor;
112 private static Constructor aeDescConstructor;
115 private static Method findFolder;
118 private static Method getFileCreator;
121 private static Method getFileType;
124 private static Method openURL;
127 private static Method makeOSType;
130 private static Method putParameter;
133 private static Method sendNoReply;
136 private static Object kSystemFolderType;
139 private static Integer keyDirectObject;
142 private static Integer kAutoGenerateReturnID;
145 private static Integer kAnyTransactionID;
148 private static Object linkage;
151 private static final String JDirect_MacOSX =
"/System/Library/Frameworks/Carbon.framework/Frameworks/HIToolbox.framework/HIToolbox";
154 private static final int MRJ_2_0 = 0;
157 private static final int MRJ_2_1 = 1;
160 private static final int MRJ_3_0 = 3;
163 private static final int MRJ_3_1 = 4;
166 private static final int WINDOWS = 5;
169 private static final int MACOSX = 6;
172 private static final int OTHER = -1;
178 private static final String FINDER_TYPE =
"FNDR";
184 private static final String FINDER_CREATOR =
"MACS";
187 private static final String GURL_EVENT =
"GURL";
193 private static final String NETSCAPE_REMOTE_PARAMETER =
"-remote";
194 private static final String NETSCAPE_OPEN_PARAMETER_START =
"'openURL(";
195 private static final String NETSCAPE_OPEN_PARAMETER_END =
")'";
200 private static String errorMessage;
207 loadedWithoutErrors =
true;
208 String osName = System.getProperty(
"os.name");
209 if (osName.startsWith(
"Mac OS X") )
211 else if (osName.startsWith(
"Mac OS")) {
212 String mrjVersion = System.getProperty(
"mrj.version");
213 String majorMRJVersion = mrjVersion.substring(0, 3);
215 double version = Double.valueOf(majorMRJVersion).doubleValue();
218 }
else if (version >= 2.1 && version < 3) {
223 }
else if (version == 3.0) {
225 }
else if (version >= 3.1) {
229 loadedWithoutErrors =
false;
230 errorMessage =
"Unsupported MRJ version: " + version;
232 }
catch (NumberFormatException nfe) {
233 loadedWithoutErrors =
false;
234 errorMessage =
"Invalid MRJ version: " + mrjVersion;
236 }
else if (osName.startsWith(
"Windows"))
241 if (loadedWithoutErrors) {
242 loadedWithoutErrors = loadClasses();
257 private static boolean loadClasses() {
261 mrjFileUtilsClass = Class.forName(
"com.apple.mrj.MRJFileUtils");
262 mrjOSTypeClass = Class.forName(
"com.apple.mrj.MRJOSType");
263 Field systemFolderField = mrjFileUtilsClass.getDeclaredField(
"kSystemFolderType");
264 kSystemFolderType = systemFolderField.get(null);
265 findFolder = mrjFileUtilsClass.getDeclaredMethod(
"findFolder",
new Class[] { mrjOSTypeClass });
266 getFileCreator = mrjFileUtilsClass.getDeclaredMethod(
"getFileCreator",
new Class[] {
File.class });
267 getFileType = mrjFileUtilsClass.getDeclaredMethod(
"getFileType",
new Class[] {
File.class });
268 }
catch (ClassNotFoundException cnfe) {
269 errorMessage = cnfe.getMessage();
271 }
catch (NoSuchFieldException nsfe) {
272 errorMessage = nsfe.getMessage();
274 }
catch (NoSuchMethodException nsme) {
275 errorMessage = nsme.getMessage();
277 }
catch (SecurityException se) {
278 errorMessage = se.getMessage();
280 }
catch (IllegalAccessException iae) {
281 errorMessage = iae.getMessage();
287 if (
new File(
"/System/Library/Java/com/apple/cocoa/application/NSWorkspace.class").exists()){
288 ClassLoader classLoader =
new URLClassLoader(
new URL[]{
new File(
"/System/Library/Java").toURL()});
289 mrjFileUtilsClass = Class.forName(
"com.apple.cocoa.application.NSWorkspace",
true, classLoader);
291 mrjFileUtilsClass = Class.forName(
"com.apple.cocoa.application.NSWorkspace");
293 openURL = mrjFileUtilsClass.getDeclaredMethod(
"openURL",
new Class[] { java.net.URL.class });
294 }
catch (Exception e) {
295 ij.IJ.log(
"MaxOSX: "+e);
296 errorMessage = e.getMessage();
313 private static Object locateBrowser() {
314 if (browser != null) {
323 systemFolder = (
File) findFolder.invoke(null,
new Object[] { kSystemFolderType });
324 }
catch (IllegalArgumentException iare) {
326 errorMessage = iare.getMessage();
328 }
catch (IllegalAccessException iae) {
330 errorMessage = iae.getMessage();
332 }
catch (InvocationTargetException ite) {
334 errorMessage = ite.getTargetException().getClass() +
": " + ite.getTargetException().getMessage();
337 String[] systemFolderFiles = systemFolder.list();
339 for(
int i = 0; i < systemFolderFiles.length; i++) {
341 File file =
new File(systemFolder, systemFolderFiles[i]);
342 if (!file.isFile()) {
350 Object fileType = getFileType.invoke(null,
new Object[] { file });
351 if (FINDER_TYPE.equals(fileType.toString())) {
352 Object fileCreator = getFileCreator.invoke(null,
new Object[] { file });
353 if (FINDER_CREATOR.equals(fileCreator.toString())) {
354 browser = file.toString();
358 }
catch (IllegalArgumentException iare) {
360 errorMessage = iare.getMessage();
362 }
catch (IllegalAccessException iae) {
364 errorMessage = iae.getMessage();
366 }
catch (InvocationTargetException ite) {
368 errorMessage = ite.getTargetException().getClass() +
": " + ite.getTargetException().getMessage();
381 browser =
"netscape";
392 public static void openURL(String url)
throws IOException {
393 if (!loadedWithoutErrors) {
394 throw new IOException(
"Exception in finding browser: " + errorMessage);
396 Object browser = locateBrowser();
397 if (browser == null) {
398 throw new IOException(
"Unable to locate browser: " + errorMessage);
403 Runtime.getRuntime().exec(
new String[] { (String) browser, url } );
407 Method aMethod = mrjFileUtilsClass.getDeclaredMethod(
"sharedWorkspace",
new Class[] {});
408 Object aTarget = aMethod.invoke( mrjFileUtilsClass,
new Object[] {});
409 openURL.invoke(aTarget,
new Object[] {
new java.net.URL( url )});
410 }
catch (Exception e) {
415 String cmd =
"rundll32 url.dll,FileProtocolHandler " + url;
416 if (ij.IJ.debugMode) {ij.IJ.log(
"Exec: "+cmd);}
417 Process process = Runtime.getRuntime().exec(cmd);
423 }
catch (InterruptedException ie) {
424 throw new IOException(
"InterruptedException while launching browser: " + ie.getMessage());
431 process = Runtime.getRuntime().exec(
new String[] { (String) browser,
432 NETSCAPE_REMOTE_PARAMETER,
433 NETSCAPE_OPEN_PARAMETER_START +
435 NETSCAPE_OPEN_PARAMETER_END });
437 int exitCode = process.waitFor();
439 Runtime.getRuntime().exec(
new String[] { (String) browser, url });
441 }
catch (InterruptedException ie) {
442 throw new IOException(
"InterruptedException while launching browser: " + ie.getMessage());
447 Runtime.getRuntime().exec(
new String[] { (String) browser, url });
456 private native
static int ICStart(
int[] instance,
int signature);
457 private native
static int ICStop(
int[] instance);
458 private native
static int ICLaunchURL(
int instance, byte[] hint, byte[] data,
int len,
459 int[] selectionStart,
int[] selectionEnd);