## Copyright (C) 2014 Philip Nienhuis
## 
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*- 
## @deftypefn {Function File} {@var{msg} =} javachk (@var{feature})
## @deftypefnx {Function File} {@var{msg} =} javachk (@var{feature}, @var{component})
## Check for presence of a Java @var{feature}, optionally mention @var{component}
## if @var{feature} isn't supported.
##
## If @var{feature} is supported and:
## @itemize @bullet
## @item
## An output argument is requested:  Return a struct with fields 'feature'
## and 'identifier' both empty
## @item
## No output argument is requested:  Return an empty string
## @end itemize
##
## If @var{feature} is not supported and:
## @itemize @bullet
## @item
## An output argument is requested:  Return a struct with field 'feature' set
## to @var{feature} and field 'identifier' set to @var{component}
## @item
## No output argument is requested:  Emit an error message.
## @end itemize
##
## @seealso{error, usejava}
## @end deftypefn

## Author: Philip Nienhuis <prnienhuis at users.sf.net>
## Created: 2014-04-19

function [msg] = javachk (feature, compnt="")

  msg = "";
  chk = false;
  switch feature
    ## For each feature, try methods() on a Java class of a feature
    case "awt"
      try
        dum = methods ("java.awt.Frame");
        chk = true;
      end_try_catch
    case "desktop"
      ## Octave has no Java based GUI/desktop, leave chk = false
    case "jvm"
      try
        dum = methods ("java.lang.Runtime");
        chk = true;
      end_try_catch
    case "swing"
      try
        dum = methods ("javax.swing.Popup");
        chk = true;
      end_try_catch
    otherwise
      error ("javachk: unrecognized feature '%s', can be one of 'awt'|'jvm'|'swing'\n", ...
             feature);
  endswitch

  if (! chk)
    ## Desired feature not present
    if (nargout >= 1)
      msg.message = sprintf ("javachk: %s is not supported", feature);
      msg.identifier = compnt;
    else
      if (! isempty (compnt))
        err = sprintf ("javachk: %s is not supported\n", compnt);
      else
        err = sprintf ("javachk: %s is not supported\n", feature);
      endif
      error (err);
    endif
  endif

endfunction
