% LTPDA_EXPLORER for exploring analysis objects and plotting/displaying their fields
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: LTPDA_EXPLORER for exploring analysis objects and plotting/displaying
%              their fields. It is possible to browse througth the history tree
%              to see only a part of this tree. If the user will use the plot
%              function the plot will be printed in a new figure.
%
% CALL:    ltpda_explorer;              % Read the ao's from the'base' workspace
%          ltpda_explorer(ao);
%          ltpda_explorer(ao_vector);
%          ltpda_explorer(ao_matrix);
%
%
% VERSION: $Id: ltpda_explorer.m,v 1.13 2011/05/10 04:50:58 hewitson Exp $
%
% HISTORY: 10-06-07 Diepholz
%             Creation
%
% NOTE:      The idea and the core source code are taken from:
%            Hassan Lahdili (hassan.lahdili@crc.ca)
%            Communications Research Centre (CRC) | Advanced Audio Systems (AAS)
%            www.crc.ca | www.crc.ca/aas
%            Ottawa. Canada
%            CRC Advanced Audio Systems - Ottawa 16/02/2005 2004-2005
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function ltpda_explorer(varargin)

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %        Define the Positions         %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  % figure position
  % FIG_X   = .150;
  % FIG_Y   = .150;
  % FIG_dX  = .700;
  % FIG_dY = .600;

  FONT_SIZE = 13;

  FIG_X   = 150;
  FIG_Y   = 150;
  FIG_dX  = 700;
  FIG_dY = 600;


  % tree position
  TREE_X  = .000;
  TREE_Y  = .000;
  TREE_dX = .350;
  TREE_dY = 1.000;

  % plot field position
  PLOT_X  = .45;
  PLOT_Y  = .15;
  PLOT_dX = .5;
  PLOT_dY = .7;

  % display field position
  DISP_X  = TREE_X;
  DISP_Y  = .750;
  DISP_dX = TREE_dX;
  DISP_dY = 1-DISP_Y;

  % explorer name position
  EXPL_NAME_dX = .370;
  EXPL_NAME_dY = .045;
  EXPL_NAME_X  = PLOT_X+(PLOT_dX-EXPL_NAME_dX)/2;
  EXPL_NAME_Y  = .030;
  % EXPL_NAME_Y  = .065;

  % info fields position
  N_INFOS = 3;

  INFO_X  = PLOT_X;
  INFO_Y  = .915;
  INFO_dX = PLOT_dX / N_INFOS;
  INFO_dY = .06;

  BG_COLOR = [.925 .914 .847];

  MAX_HIST_DEPTH = 4;

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %           Check the input           %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


  tree_name = 'objects';
  obj       = [];
  obj_value = {};
  obj_name  = {};

  % Read the objects from the 'base' workspace

  if nargin == 0
    ws_vars   = evalin('base','whos');

  elseif nargin == 1
    tmp       = varargin{1};

    if iscell(tmp)
      ws_vars = [];
      for jj = 1:length(tmp)
        ws_vars(jj).name  = [inputname(1) '{' num2str(jj) '}'];
        ws_vars(jj).class = class(tmp{jj});
        ws_vars(jj).obj   = tmp{jj};
      end
      
    elseif numel(tmp)>1
       ws_vars = [];
       for jj=1:numel(tmp)
          ws_vars(jj).name  = [inputname(1) '(' num2str(jj) ')'];
          ws_vars(jj).class = class(tmp(jj));
          ws_vars(jj).obj   = tmp(jj);
       end
      
    else
      ws_vars      = whos('tmp');
      ws_vars.name = inputname(1);
      ws_vars.obj  = tmp;
    end

  else
    error ('##########');
  end
  

  for mm=1:length(ws_vars)

    if nargin == 0
      obj = evalin('base', ws_vars(mm).name);
    elseif nargin == 1
      obj = ws_vars(mm).obj;
    else
      error('#####');
    end

    if utils.helper.isobject(obj)

      % the object in the workspace is a single value
      if numel(obj) == 1
        obj_value{end+1} = obj;
        obj_name{end+1}  = [ws_vars(mm).class ':' ws_vars(mm).name];
      else

        [n,m] = size(obj);

        % the ao in the workspace is a vector
        if n == 1 || m == 1

          for jj=1:length(obj)
            obj_value{end+1} = obj(jj);
            obj_name{end+1}  = [ws_vars(mm).class ':' ws_vars(mm).name '(' num2str(jj) ')'];
          end

          % the ao in the workspace is a matrix
        elseif n > 1 && m > 1

          for gg = 1:n
            for hh = 1:m
              obj_value{end+1} = obj(gg,hh);
              obj_name{end+1}  = [ws_vars(mm).class ':' ws_vars(mm).name '(' num2str(gg) ',' num2str(hh) ')'];
            end
          end

        else
          error ('### this should not happen.');
        end

      end

    end
  end


  fig_name = 'Exploring objects';

  % Define figure
  fig = figure('NextPlot',    'add',            ...
    'NumberTitle', 'off',            ...
    'Toolbar',     'none',           ...
    'name',         fig_name,        ...
    'Color',        BG_COLOR,        ...
    'ToolBar',      'none',           ...
    'NextPlot',     'new',            ...
    'MenuBar',      'none',...
    'Position',    [FIG_X  FIG_Y     ...
    FIG_dX FIG_dY]);

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %           Define the tree           %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  root = uitreenode('v0', tree_name, tree_name, [], false);
  tree = uitree('v0',  fig, 'Root',root, 'ExpandFcn', @myExpfcn4);
  set(tree, 'Units', 'normalized')
  drawnow;
  set(tree, 'position', [TREE_X TREE_Y TREE_dX TREE_dY]); %,   ...
	set(tree, 'NodeWillExpandCallback', @nodeWillExpand_cb4, ...
    'NodeSelectedCallback',   @nodeSelected_cb4);

  tmp = tree.FigureComponent;
  cell_Data = cell(3,1);

  % cell_Data{1} = varargin{:};
  cell_Data{1} = obj_value;
  cell_Data{3} = obj_name;

  warning('off','MATLAB:hg:JavaSetHGProperty');
  set(tmp, 'UserData', cell_Data);
  warning('on','MATLAB:hg:JavaSetHGProperty');
  
  % Define the plot field
  haxes = axes('Units',   'normalized',       ...
    'Position', [PLOT_X  PLOT_Y    ...
    PLOT_dX PLOT_dY], ...
    'Box',     'on',               ...
    'XTick',    [],                ...
    'YTick',    []);

  box  off;
  axis off;

  % Define the info fields 'name'
  txt1 = uicontrol('String',          '',                 ...
    'Units',           'normalized',       ...
    'Style',           'Edit',             ...
    'FontSize',          FONT_SIZE,        ...
    'Position',         [INFO_X  INFO_Y    ...
    INFO_dX INFO_dY], ...
    'BackgroundColor',   BG_COLOR);
  % Define the info fields 'size'
  txt2 = uicontrol('String',         '',                          ...
    'Units',          'normalized',                ...
    'Style',          'Edit',                      ...
    'FontSize',         FONT_SIZE,                 ...
    'Position',        [(INFO_X+INFO_dX) INFO_Y    ...
    INFO_dX          INFO_dY],...
    'BackgroundColor',   BG_COLOR);
  % Define the info fields 'class'
  txt3 = uicontrol('String',         '',                          ...
    'Units',          'normalized',                ...
    'Style',          'Edit',                      ...
    'FontSize',         FONT_SIZE,                 ...
    'Position',        [(INFO_X+2*INFO_dX) INFO_Y  ...
    INFO_dX          INFO_dY],...
    'BackgroundColor',   BG_COLOR);
  % Define the info fields 'value'
  txt4 = uicontrol('String',         '',                          ...
    'Units',          'normalized',                ...
    'Style',          'Edit',                      ...
    'FontSize',         FONT_SIZE,                 ...
    'Position',        [  INFO_X  (INFO_Y-INFO_dY) ...
    3*INFO_dX  INFO_dY],       ...
    'BackgroundColor',   BG_COLOR);

  % Define the info fields 'value'
  txt5 = uicontrol('String',          '',                  ...
    'Units',           'normalized',        ...
    'Style',           'listbox',           ...
    'Visible',         'off',               ...
    'Fontsize',         8,                  ...
    'FontSize',         FONT_SIZE,          ...
    'Position',        [DISP_X   DISP_Y     ...
    DISP_dX  DISP_dY],  ...
    'BackgroundColor',  BG_COLOR);



  % Define the decription of the info fields
  col1 = uicontrol('String',         'Name',                   ...
    'Units',          'normalized',             ...
    'Style',          'Text',                   ...
    'FontSize',         FONT_SIZE,              ...
    'Position',        [INFO_X  (INFO_Y+INFO_dY)...
    INFO_dX  INFO_dY],      ...
    'BackgroundColor',  BG_COLOR);
  col2 = uicontrol('String',         'Size',                            ...
    'Units',          'normalized',                      ...
    'Style',          'Text',                            ...
    'FontSize',         FONT_SIZE,                       ...
    'Position',        [(INFO_X+INFO_dX) (INFO_Y+INFO_dY)...
    INFO_dX          INFO_dY],      ...
    'BackgroundColor',   BG_COLOR);
  col3 = uicontrol('String',         'Class',                             ...
    'Units',          'normalized',                        ...
    'Style',          'Text',                              ...
    'FontSize',          FONT_SIZE,                        ...
    'Position',        [(INFO_X+2*INFO_dX) (INFO_Y+INFO_dY)...
    INFO_dX            INFO_dY],      ...
    'BackgroundColor',   BG_COLOR);

  % Define the name of the explorer
  expl_name = uicontrol('String',         'LTPDA Object explorer',  ...
    'Units',          'normalized',                ...
    'Style',          'text',                      ...
    'Position',        [EXPL_NAME_X-.05  EXPL_NAME_Y   ...
    EXPL_NAME_dX+.1 EXPL_NAME_dY],...
    'ForeGroundColor', [0.2 0.4 1],                ...
    'BackGroundColor', BG_COLOR,                   ...
    'FontSize',         18,                        ...
    'FontWeight',     'bold',                      ...
    'FontAngle',      'italic');

  tree_menu  = uicontextmenu();
  tree_menu1 = uimenu(tree_menu, 'Label',   'Plot', ...
    'Callback', @f_tree_menu1);
  tree_menu2 = uimenu(tree_menu, 'Label',   'Display', ...
    'Callback', @f_tree_menu2);

  disp_menu  = uicontextmenu;
  disp_menu1 = uimenu(disp_menu, 'Label',   'close', ...
    'Callback', @f_disp_menu1);

  warning('off','MATLAB:hg:JavaSetHGProperty');
  set(tree.Tree, 'MousePressedCallback', @mouse_cb);
  set(tree.Tree, 'UIContextMenu', tree_menu);
  set(tree.Tree, 'Font', javax.swing.plaf.FontUIResource('Dialog', 0, FONT_SIZE))
  warning('on','MATLAB:hg:JavaSetHGProperty');

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % Mouse Pressed Handler
  function mouse_cb(h, ev)
    if ev.getModifiers()== ev.META_MASK
      % Workaround to set the y position
      % Workaround to set the x position
      vis = get(get(ev, 'Component'), 'VisibleRect');
      x_width  = get(get(ev, 'Component'), 'Width');
      y_height = get(get(ev, 'Component'), 'Height');

      new_x =  ev.getX-vis(1);
      new_y = -ev.getY+y_height-(y_height-vis(4));
      set(tree_menu, 'Position',  [new_x new_y], ...
        'Visible',  'on');
    end

  end
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  function f_disp_menu1(h, ev)
    set(txt5, 'Visible', 'off')
    set(tree, 'position', [TREE_X  TREE_Y    ...
      TREE_dX TREE_dY])
  end
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  function f_tree_menu1(h,ev)
    plotselected_cb;
  end
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  function f_tree_menu2(h,ev)
    displayselected_cb;
  end
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  function plotselected_cb(h, ev)

    tmp =  tree.FigureComponent;
    S = get(tmp, 'UserData');
    s = S{1};
    cNode = S{2};
    [val, plotted, cNode] = getcNodevalue(cNode, s);

    cla(haxes)
    box off;
    axis off;

    %%%%%   Plot history object   %%%%%
    if (isa(val,'history'))
      if length(val) == 1
        figure;
        plot(val);
        ii = strfind(plotted, 'inhists');
        title(haxes, sprintf('History-Level: %d', length(ii)+1))
      else
        na = text(0.5,0.5,'Select the left or right branch.');
        set(na, 'HorizontalAlignment', 'center', ...
          'Color',               'r',      ...
          'FontWeight',          'bold',   ...
          'EdgeColor',           'k',      ...
          'BackgroundColor',     'w',      ...
          'Fontsize',             10,      ...
          'Margin',               5);
      end

      %%%%%   Plot data object   %%%%%
    elseif isa(val,'fsdata') || isa(val,'tsdata') || ...
        isa(val,'xydata') || isa(val,'cdata')
      aoin = getcNodevalue(S{2}.getParent, s);
      iplot(aoin)

      %%%%%   Plot the AO object   %%%%%
    elseif isa(val, 'ao')
      iplot(val)

      %%%%%   Plot mfir and miir object   %%%%%
    elseif isa(val, 'mfir') || isa(val, 'miir')
      resp(val)

      %%%%%   Plot pzmodel object   %%%%%
    elseif isa(val, 'pzmodel')
      resp(val)

      %%%%%   Is the parent node == 'data' so plot data   %%%%%
    else

      cNode  = S{2};

      if cNode.getLevel >= 1
        obj = getcNodevalue(S{2}.getParent, s);
        
        if isa(obj, 'ltpda_data')
          aoin = getcNodevalue(S{2}.getParent.getParent, s);
          iplot(aoin);
        elseif isa(obj, 'ao')
          iplot(obj);
        end
      end

    end

  end
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  function displayselected_cb(h, ev)
    tmp =  tree.FigureComponent;
    S = get(tmp, 'UserData');
    s = S{1};
    cNode = S{2};
    [val, displayed, cNode] = getcNodevalue(cNode, s);

    text1 = '';
    if isobject(val)
      text1 = display(val);
    else
      disp(val)
    end

    % some sisplay outputs contains '\n' <-> char(10)
    % text can not display this character so replace it with '   '
    text1 = strrep(text1, char(10), '  ');

    set(tree, 'position', [TREE_X  TREE_Y      ...
      TREE_dX TREE_dY-DISP_dY]);
    set(txt5, 'string', text1);
    set(txt5, 'Visible', 'on');
    set(txt5, 'UIContextMenu', disp_menu);
  end
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  function cNode = nodeSelected_cb4(tree,ev)
    cNode = ev.getCurrentNode;
    tmp = tree.FigureComponent;
    warning('off','MATLAB:hg:JavaSetHGProperty');
    cell_Data = get(tmp, 'UserData');
    warning('on','MATLAB:hg:JavaSetHGProperty');
    cell_Data{2} = cNode;
    s = cell_Data{1};
    val = s;
    plotted = cNode.getValue;
    selected = plotted;
    [val, plotted, cNode] = getcNodevalue(cNode, val);
    set(txt1, 'string', selected)
    set(txt2, 'string', strcat(num2str(size(val,1)),'x',num2str(size(val,2))) )
    set(txt3, 'string', class(val))

    str = ' ';
    c_str = {};
    cla(haxes)
    axesSize = get(haxes,'Position');
    axesSize = axesSize + [-.07 -.05 .09 0.05];
    plotPanel = findobj(gcf,'Tag','plotPanel');
    if isempty(plotPanel), plotPanel = uipanel('Position',axesSize,'Tag','plotPanel','BorderType','none','BackgroundColor',[.925 .914 .847]); end
    delete(get(plotPanel,'Children'))
    box off;
    axis off;

    if ~isempty(val)

      if isnumeric(val)
        % normalize the vector
        si = size(val);
        if si(1) > si(2)
          val = val.';
        else
          val = val;
        end
        if size(val,1) == 1 || size(val,2) == 1
          if length(val) > 3
            str = strcat(num2str(val(1:3)), '  ...');
          else
            str = num2str(val);
          end
        else
          str = 'Matrix';
        end

      elseif ischar(val)
        str = val;

      elseif islogical(val)
        if val
          str = 'true';
        else
          str = 'false';
        end

      elseif isobject(val)
        if isa(val, 'ao')
          str = 'Analysis Object';
          if ~isa(val.data, 'cdata')
            iplot(val,plist('Figure',plotPanel));
          else
            iplot(val,plist('Figure',plotPanel));
          end
        elseif isa(val, 'cdata')
          str = 'C-Data Object';
          aoin = getcNodevalue(ev.getCurrentNode.getParent, s);
        elseif isa(val, 'fsdata')
          str = 'Frequency-Series Object';
          aoin = getcNodevalue(ev.getCurrentNode.getParent, s);
          iplot(aoin,plist('Figure',plotPanel));
        elseif isa(val, 'tsdata')
          str = 'Time-Series Object';
          aoin = getcNodevalue(ev.getCurrentNode.getParent, s);
          iplot(aoin,plist('Figure',plotPanel));
        elseif isa(val, 'xydata')
          str = 'X-Y Data Object';
          aoin = getcNodevalue(ev.getCurrentNode.getParent, s);
          iplot(aoin,plist('Figure',plotPanel));
        elseif isa(val, 'xyzdata')
          str = 'X-Y-Z Data Object';
        elseif isa(val, 'history')
          str = 'History Object';
          if length(val) == 1
            pl = plist(param('stop_option', MAX_HIST_DEPTH));
            plot(haxes, val, pl);
            ii = strfind(plotted, 'inhists');
            title(haxes, sprintf('History-Level: %d', length(ii)+1))
          else
            c_str{1} = 'Select the left or right branch.';
          end

        elseif isa(val, 'param')
          if length(val) == 1
            str   = char(val);
            c_str = split_by_comma(str);
          else
            for ii = 1:length(val)
              str = char(val(ii));
              c_str1 = split_by_comma(str, '- ');
              c_str(end+1:end+length(c_str1)) = c_str1;
            end
          end
          str = 'Parameter Object';

        elseif isa(val, 'plist')
          if numel(val) > 1
            c_str{1} = 'select a plist';
          else
            for ii=1:length(val.params)

              str = char(val.params(ii));
              c_str1 = split_by_comma(str, '- ');
              c_str(end+1:end+length(c_str1)) = strrep(c_str1, '_', '\_');
            end
          end
          str = 'Parameter List Object';

        elseif isa(val, 'mfir')
          str = 'FIR Filter Object';
        elseif isa(val, 'miir')
          str = 'IIR Filter Object';
        elseif isa(val, 'provenance')
          str = 'Provenance Object';
        elseif isa(val, 'pzmodel')
          str = 'Pole Zero Object';
        elseif isa(val, 'specwin')
          str = 'Spectral Window Object';
        elseif isa(val, 'pz')
          str = 'Pole/Zero Object';
        elseif isa(val, 'time')
          str = 'Time Object';
        elseif isa(val, 'timespan')
          str = 'Time span Object';
        elseif isa(val, 'ssm')
          str = 'statespace model Object';
        elseif isa(val, 'unit')
          str = 'Unit Object';
        elseif isa(val, 'minfo')
          str = 'Method info Object';
        else
          cl  = class(val);
          str = sprintf('%s%s Object', upper(cl(1)), lower(cl(2:end)));
        end

      elseif iscell(val)
        for i = 1:min(length(val),3)
          if ischar(val{i})
            str = strcat(str, val{i});
          elseif isnumeric(val)
            str = strcat(str, num2str(val{i}));
          end
          if i < min(length(val),3)
            str = strcat(str, ',');
          end
        end
        if length(val) > 3
          str = strcat(str,'...');
        end

      end

      if ~isempty(c_str)
        c_str = strtrim(c_str);
        na = text(0.5,0.5,c_str);
        txt_extent = get(na, 'Extent');
        set(na, 'Position',            [0.5-txt_extent(3)/2, 0.5], ...
          'HorizontalAlignment', 'left', ...
          'Color',               'k',    ...
          'FontWeight',          'bold', ...
          'EdgeColor',           'k',    ...
          'BackgroundColor',     'w',    ...
          'Fontsize',            10,     ...
          'Margin',              5);
      end

    else % ~isempty(val)
      str = 'The field is empty';
    end
    set(txt4, 'string', str)
    warning('off','MATLAB:hg:JavaSetHGProperty');
    set(tmp, 'UserData', cell_Data);
    warning('on','MATLAB:hg:JavaSetHGProperty');
  end


  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function nodes = myExpfcn4(tree,value)

  tmp = tree.FigureComponent;
  warning('off','MATLAB:hg:JavaSetHGProperty');
  S = get(tmp, 'UserData');
  warning('on','MATLAB:hg:JavaSetHGProperty');
  s = S{1};
  cNode = S{2};
  [val, cNode] = getcNodevalue(cNode, s);

  % Set the path to the *.gif files
  % This tis the current path of this function + 'exp_struct_icons'
  pth = '';
  eval (sprintf('pth = which(''%s'');',mfilename))
  index = find(pth==filesep, 1, 'last');
  pth = pth(1:index);
  pth = [pth 'exp_struct_icons' filesep];

  [n,m] = size(val);
  count = 0;

  %%% Vector or Matrix
  if m>1 || n>1

    if isa(val, 'ao')
      iconpath =[pth,'analysis_object.gif'];
    else
      iconpath =[pth,'struct_icon.gif'];
    end

    %%% Vector
    if m==1 || n==1
      L = length(val);

      for J = 1:L
        count = count + 1;
        cNode = S{2};

        level = cNode.getLevel;
        fname = strcat(cNode.getValue, '(', num2str(J),')');

        if level==0 && ~isempty(S{3}) && numel(S{3}) == numel(S{1})
          node_str = S{3}(J);
        else
          node_str = fname;
        end

        nodes(count) =  uitreenode('v0',fname, node_str, iconpath, 0);
      end
      %%% Matrix
    else

      for ii=1:n
        for jj=1:m
          count = count + 1;
          cNode = S{2};
          fname = [cNode.getValue '(' num2str(ii) ',' num2str(jj) ')'];
          nodes(count) =  uitreenode('v0',fname, fname, iconpath, 0);
        end
      end

    end
    %%% Struct, Object or single value
  else
    %%%
    val = val;
    if ~isempty(val)
      fnames = fieldnames(val);
    else
      fnames = {};
    end

    for i=1:length(fnames)
      count = count + 1;
      x = getfield(val,fnames{i});

      if isa(x, 'ao')
        iconpath =[pth,'analysis_object.gif'];
      elseif isa(x, 'tsdata')
        iconpath =[pth,'ts_data.gif'];
      elseif isa(x, 'fsdata')
        iconpath =[pth,'fs_data.gif'];
      elseif isa(x, 'xydata')
        iconpath =[pth,'xy_data.gif'];
      elseif isa(x, 'cdata')
        iconpath =[pth,'c_data.gif'];
      elseif isa(x, 'history')
        iconpath =[pth,'history.gif'];
      elseif isa(x, 'plist')
        iconpath =[pth,'plist.gif'];

      elseif isstruct(x)
        if length(x) > 1
          iconpath =[pth,'structarray_icon.gif'];
        else
          iconpath =[pth,'struct_icon.gif'];
        end
      elseif isnumeric(x)
        iconpath =[pth,'double_icon.gif'];
      elseif iscell(x)
        iconpath =[pth,'cell_icon.gif'];
      elseif ischar(x)
        iconpath =[pth,'char_icon.gif'];
      elseif islogical(x)
        iconpath =[pth,'logic_icon.gif'];
      elseif isobject(x)
        iconpath =[pth,'obj_icon.gif'];
      else
        iconpath =[pth,'unknown_icon.gif'];
      end

      if isstruct(x) || isobject(x)
        isLeaf = 0;
      else
        isLeaf = 1;
      end

      nodes(count) = uitreenode('v0',fnames{i}, fnames{i}, iconpath, isLeaf);
    end
  end

  if (count == 0)
    nodes = [];
  end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function cNode = nodeWillExpand_cb4(tree,ev)
  cNode = ev.getCurrentNode;
  tmp = tree.FigureComponent;
  warning('off','MATLAB:hg:JavaSetHGProperty');
  cell_Data = get(tmp, 'UserData');
  cell_Data{2} = cNode;
  set(tmp, 'UserData', cell_Data);
  warning('on','MATLAB:hg:JavaSetHGProperty');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [val, displayed, cNode] = getcNodevalue(cNode, s)

  fields = {};
  while cNode.getLevel ~=0
    fields = [fields; cNode.getValue];
    c = findstr(cNode.getValue, '(');
    if ~isempty(c) && cNode.getLevel ~=0
      cNode = cNode.getParent;
    end

    if  cNode.getLevel ==0, break; end
    cNode = cNode.getParent;
  end

  val = s;

  if ~isempty(fields)
    L=length(fields);
    displayed = fields{L};
    % create the variable: displayed
    for j = L-1:-1:1
      displayed = strcat(displayed, '.', fields{j});
    end

    for i = L:-1:1
      field = fields{i};
      von = findstr(field,'(');
      bis = findstr(field,')');
      if ~isempty(von)

        idx = field(von+1:bis-1);
        field = field(1:von-1);
        if (strcmp(field, cNode.getValue))
          cmd = sprintf('val = val(%s);',idx);
          eval(cmd);
          if iscell(val) && numel(val) == 1
            val = val{1};
          else
            error('################ MAch mich neu');
          end
        else
          cmd = sprintf('val = getfield(val, field, {%s});',idx);
          eval(cmd);
          if iscell(val) && numel(val) == 1
            val = val{1};
          end

        end

      else
        if iscell(val) && numel(val) == 1
          val = val{1};
        elseif numel(val) ~= 1
          error('################ MAch mich neu');
        end
        val = getfield(val, field);
      end
    end
  else
    displayed = cNode.getValue;
    if iscell(val) && numel(val) == 1
      val = val{1};
    else
    end
    return;
  end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function c_str = split_by_comma(str, pref)

  if nargin < 2
    pref = '';
  end

  c_str = {};
  c_str{1} = str;
  index    = find(str==',');
  von      = 1;
  for ii = 1:length(index)
    bis = index(ii)-1;
    c_str{ii} = [pref str(von:bis)];
    von = bis + 2;
  end
  if ~isempty(index)
    c_str{ii+1} = [pref str(von:end)];
  end

  c_str = strtrim(c_str);
end
