% LTPDAModelBrowser is a graphical user interface for browsing the
% available built-in models.
%
% CALL: LTPDAModelBrowser
%
%
% M Hewitson 18-10-10
%
% $Id: LTPDAModelBrowser.m,v 1.1 2010/10/23 11:14:08 hewitson Exp $
%
classdef LTPDAModelBrowser < handle
  
  properties
    gui = [];
  end
  
  
  methods
    function mainfig = LTPDAModelBrowser(varargin)
            
      % generate model data
      modelClasses = LTPDAModelBrowser.generateModelData();
      
      % make a gui
      mainfig.gui = javaObjectEDT('mpipeline.ltpdamodelbrowser.JModelBrowser', [], false, modelClasses);
            
      %--- called when window is closed
      h = handle(mainfig.gui, 'callbackproperties');
      set(h, 'WindowClosedCallback', {@mainfig.cb_guiClosed});
            
      %--- Run button
      h = handle(mainfig.gui.getDocBtn, 'callbackproperties');
      set(h, 'ActionPerformedCallback', {@mainfig.cb_getDoc});

      % Make gui visible
      mainfig.gui.setVisible(true);
      
      
    end % End constructor
    
    function display(varargin)
    end
    
  end % End public methods
  
  methods (Access = private)
   
    function cb_getDoc(varargin)
      browser = varargin{1};
      jbrowser = browser.gui;
      model = jbrowser.getSelectedModel();
      if ~isempty(model)
        mdl = char(model.fullname());
        cmd = [ mdl '(''info'')'];
        info = eval(cmd);
        web(modelOverview(info),'-helpbrowser');
      end
    end
    
  end
  
  methods (Access = private, Static=true)
    
    function modelClasses = generateModelData()
      
      classes = utils.helper.ltpda_userclasses;
      modelClasses = java.util.ArrayList();
      for kk=1:numel(classes)
        cl = classes{kk};
        
        % get models for this class
        if ~strcmp(cl, 'plist') && ~strcmp(cl, 'time')
          models = eval([cl '.getBuiltInModels()']);
          models = [{models{:,1}}];
          modelClass = mpipeline.ltpdamodelbrowser.JModelClass(cl);
          
          for jj=1:numel(models)
            model = models{jj};
            try
              cmd = [cl '_model_' model '(''describe'')'];
              info = eval(cmd);
              cmd = [cl '_model_' model '(''doc'')'];
              doc = eval(cmd);
              html = ['<html><body>' info sprintf('\n\n') doc '</body></html>'];
              jmodel = mpipeline.ltpdamodelbrowser.JModel(modelClass, model, html);
              modelClass.addModel(jmodel);
            catch
              warning('Model %s does not respond to the <info> call', model)
            end
          end
          
          if modelClass.getModels.size>0
            modelClasses.add(modelClass);
          end
        end
      end
    end
    
  end
  
  methods (Access = public)
    
    function cb_guiClosed(varargin)
      disp('*** Goodbye from LTPDAModelBrowser');
      browser = varargin{1};
      
      %--- called when window is closed
      h = handle(browser.gui, 'callbackproperties');
      set(h, 'WindowClosedCallback', []);
      
      h = handle(browser.gui.getDocBtn, 'callbackproperties');
      set(h, 'ActionPerformedCallback', []);
      
    end
    
  end
  
end

% END
