function methName = g_setmethod(varargin)
% GUI callback, to support the hollow block - ie. the block with no
% function name. This function will popup the small figure to set it.
%
%  $Id: g_setmethod.m,v 1.3 2009/02/01 17:20:03 nicola Exp $

   global selBlocks

   guiFontSize = getappdata(0, 'ltpda_gui_fontsize');
   screenSize = get(0,'ScreenSize');
   backColor   = [1 1 1];
   close(findobj('Tag','setMethod'))
   [meth,clas] = strtok(get(gcbh,'Tag'));
   if ~strcmp(meth,'method'), warning('*** This block didn''t contain a valid LTPDA method.'); delete_block(gcbh); return; end %#ok<WNTAG>
   clas = strtrim(clas);
   methName = '';

   position   = [(screenSize(3)-250)/2   (screenSize(4)-70)/2   250   70];
   setMethFig = figure('Position',position,'Name','Which function?','Tag','setMethod','Resize','off','NumberTitle','off','Toolbar','none','Menubar','none');
   set(setMethFig,'CloseRequestFcn','delete_block(gcbh);uiresume;closereq');
 % Class text
   uicontrol('Parent',setMethFig,'BackgroundColor',backColor,'HorizontalAlignment','left','Position',[10 position(4)-25 100 20],'String','Class:','FontName','Times New Roman','FontSize',guiFontSize,'FontWeight','normal','Visible','on','Style','text');
 % Method text
   uicontrol('Parent',setMethFig,'BackgroundColor',backColor,'HorizontalAlignment','left','Position',[120 position(4)-25 140 20],'String','Method:','FontName','Times New Roman','FontSize',guiFontSize,'FontWeight','normal','Visible','on','Style','text');
 % Class edit field
   clasEdit = uicontrol('Parent',setMethFig,'Tag','clasEdit','BackgroundColor',backColor,'HorizontalAlignment','left','Position',[10 position(4)-45 100 20],'String',clas,'FontName','Times New Roman','FontSize',guiFontSize+1,'FontWeight','normal','Visible','on','Callback',@editMethodCbk,'Style','edit');
 % Method edit field
   methEdit = uicontrol('Parent',setMethFig,'Tag','methEdit','BackgroundColor',backColor,'HorizontalAlignment','left','Position',[120 position(4)-45 120 20],'String','','FontName','Times New Roman','FontSize',guiFontSize+1,'FontWeight','normal','Visible','on','Callback',@editMethodCbk,'Style','edit');

   uicontrol(methEdit)
   uiwait

   %----------------------------------------------------------------------
   function editMethodCbk(varargin)
      
      clasName = get(clasEdit,'String');
      methName = get(methEdit,'String');
      
      if isempty(clasName) || ~ismember(clasName,utils.helper.ltpda_classes) % empty or not a valid LTPDA class
         set(clasEdit,'String','')
         delete(findobj(gcf,'Tag','invalidmethod'))
         uicontrol('Parent',gcf,'Tag','invalidclass','BackgroundColor',backColor,'HorizontalAlignment','left','Position',[10 3 140 15],'String','* Invalid class','FontName','Times New Roman','FontSize',guiFontSize-1,'FontWeight','normal','Visible','on','Style','text');
         uicontrol(clasEdit);
         return;
      end
      if isempty(methName) || ~ismethod(clasName,methName)
         set(methEdit,'String','')
         delete(findobj(gcf,'Tag','invalidclass'))
         uicontrol('Parent',gcf,'Tag','invalidmethod','BackgroundColor',backColor,'HorizontalAlignment','left','Position',[120 3 140 15],'String','* Invalid method','FontName','Times New Roman','FontSize',guiFontSize-1,'FontWeight','normal','Visible','on','Style','text');
         uicontrol(methEdit);
         return;
      end
      
      blockName = methName;
      ii = 1;
      while ~isempty(find_system(gcs,'SearchDepth',1,'Name',[blockName,num2str(ii)])), ii=ii+1; end
      blockName = [blockName,num2str(ii)];
      
      set(gcbh,'LinkStatus','inactive','Name',blockName,'MaskDisplay',['disp(''',methName,''')'],'Tag',['method ',clasName],'AncestorBlock','')
      set(utils.prog.find_in_models(gcbh,'LookUnderMasks','all','BlockType','M-S-Function','FunctionName','ltpdasim'),'Tag',methName,'Name',methName);
      
    % Check if it's a copied block:
      prevMeth = get(gcbh,'UserData');
      if ~isempty(prevMeth)
         set(gcbh,'UserData','');
         [funcName,blkHdl] = strtok(prevMeth,'-');
         if strcmp(funcName,methName)
            blkHdl = str2double(blkHdl(2:end));
            newBlk = replace_block(gcs,'Name',get(gcbh,'Name'),getfullname(blkHdl),'noprompt');
            GUIprefs = getappdata(0, 'GUIpreferences');
            GUIprefs.Copied = 0;
            setappdata(0, 'GUIpreferences',GUIprefs);
            set_param(newBlk{1},'Selected','on');
            selBlocks = get_param(newBlk{1},'Handle');
         end
      end
            
      minfoObj = eval([clasName,'.getInfo(''',methName,''');']);
      category = minfoObj.mcategory;
      minInput = minfoObj.argsmin;
      if strcmp(category,'Constructor')
         lineHandles = get(gcbh,'LineHandles');
         if lineHandles.Inport~=-1, delete_line(lineHandles.Inport); end
         setInports(gcbh,0)
         set(gcbh,'BackgroundColor','green')
      elseif strcmp(category,'Output')
         replace_block(gcb,'Outport','Terminator','noprompt');
         set(gcbh,'BackgroundColor','lightBlue')
      elseif minInput>1
         setInports(gcbh,minInput)
      end
      
      closereq
      uiresume
      
   end
   %----------------------------------------------------------------------
   
   %----------------------------------------------------------------------
   function setInports(varargin)
      % Called whenever the block to be added has argsmin>1, so it needs
      % multiple data inputs:

      load_system('simulink')
      currBlock = varargin{1};
      newInports = varargin{2};
      prevInport  = find_system(currBlock,'SearchDepth',1,'LookUnderMasks','all','BlockType','Inport');

      % To remove previous data inport and line:
      try %#ok<ALIGN>
         blockLines   = get_param(prevInport,'LineHandles');
         if (blockLines.Outport(1)~=-1 && ~isempty(blockLines.Outport(1))),   delete_line(blockLines.Outport(1)); end
         delete_block(prevInport);
      catch end

      % To add new inports, mux and lines:
      if newInports>1
         muxblock = add_block('built-in/Mux', [getfullname(currBlock),'/Mux']);
         set(muxblock,'Position',[70 , 10 , 73 , 150])
         set(muxblock,'Inputs',num2str(newInports))
         for ii=1:newInports
            newBlock = add_block('built-in/Inport', [getfullname(currBlock),'/Inport1'],'MakeNameUnique','on');
            set_param(newBlock,'Port',num2str(ii));
            newBlock = get_param(newBlock,'Handle');
            set(newBlock,'Position',[10 , 10+30*(ii-1) , 30 , 30+30*(ii-1)])
            set(newBlock,'Tag',num2str(ii))
            add_line(currBlock,[get(newBlock,'Name'),'/1'],[get(muxblock,'Name'),'/',num2str(ii)]);
         end

         funcBlock = find_system(currBlock,'SearchDepth',1,'LookUnderMasks','all','BlockType','M-S-Function');
         funcBlock = get_param(funcBlock,'Name');
         add_line(currBlock,[get(muxblock,'Name'),'/1'],[funcBlock,'/1']);
         
      else % no inputs
         groundBlk = add_block('simulink/Sources/Ground', [gcb,'/Ground'],'MakeNameUnique','on');
         funcBlock = find_system(currBlock,'SearchDepth',1,'LookUnderMasks','all','BlockType','M-S-Function');
         funcBlock = get_param(funcBlock,'Name');
         add_line(currBlock,[get(groundBlk,'Name'),'/1'],[funcBlock,'/1']);
      end

   end
   %----------------------------------------------------------------------
   
end