changeset 14353:937035390ec0

usejava.m: Update to use try/catch architecture * usejava.m: Update to use try/catch architecture
author Philip Nienhuis <prnienhuis@users.sf.net>
date Thu, 09 Feb 2012 12:41:37 -0800
parents 3002986df93c
children 55bb8c902a4d
files scripts/miscellaneous/usejava.m
diffstat 1 files changed, 32 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/miscellaneous/usejava.m	Thu Feb 09 12:39:55 2012 -0800
+++ b/scripts/miscellaneous/usejava.m	Thu Feb 09 12:41:37 2012 -0800
@@ -1,4 +1,5 @@
 ## Copyright (C) 2012 Rik Wehbring
+## Parts Copyright (C) 2012 Philip Nienhuis <prnienhuis@users.sf.net>
 ##
 ## This file is part of Octave.
 ##
@@ -18,7 +19,7 @@
 
 ## -*- texinfo -*-
 ## @deftypefn {Function File} {} usejava (@var{feature})
-## Return true if the specific Sun Java element @var{feature} is available.
+## Return true if the Java element @var{feature} is available.
 ##
 ## Possible features are:
 ##
@@ -36,10 +37,12 @@
 ## Swing components for lightweight GUIs.
 ## @end table
 ##
-## This function is provided for compatibility with @sc{matlab} scripts which
-## may alter their behavior based on the availability of Java.  Octave does
-## not implement an interface to Java and this function always returns
-## @code{false}.
+## @code{usejava} determines if specific Java features are available in an
+## Octave session.  This function is provided for compatibility with scripts
+## which may alter their behavior based on the availability of Java.  The
+## feature "desktop" always returns @code{false} as Octave has no Java-based
+## desktop.  Other features may be available if the Octave-Forge Java package
+## has been installed.
 ## @end deftypefn
 
 function retval = usejava (feature)
@@ -48,16 +51,35 @@
     print_usage ();
   endif
 
-  if (! any (strcmp (feature, {"awt", "desktop", "jvm", "swing"})))
-    error ("usejava: unrecognized feature '%s'", feature);
-  endif
+  retval = false;
 
-  retval = false;
+  switch feature
+    ## For each feature, try javamethods() on a Java class of a feature
+    case "awt"
+      try
+        dum = javamethods ("java.awt.Frame");
+        retval = true;
+      end_try_catch
+    case "desktop"
+      ## Octave has no Java based GUI/desktop, leave retval = false
+    case "jvm"
+      try
+        dum = javamethods ("java.lang.Runtime");
+        retval = true;
+      end_try_catch
+    case "swing"
+      try
+        dum = javamethods ("javax.swing.Popup");
+        retval = true;
+      end_try_catch
+    otherwise
+      error ("usejava: unrecognized feature '%s'", feature);
+  endswitch
 
 endfunction
 
 
-%!assert (usejava ("awt"), false)
+%!assert (usejava ("desktop"), false)
 
 %% Test input validation
 %!error usejava ()