function [dat,dat_n,dat_i,outarg]=iduiedit(arg,wi_no)
%IDUIEDIT Handles various edit functions.
%      Arguments: 
%	new            Clears (deletes) all models and data from the summary board
%	close_all      Closes all ident windows
%	pres           Fills out the Text Info dialog box
%	present        Activates the presentation of the model/data to the command window
%	slide          Handles the slider in the Text Info window
%	present_update Handles the update of info from the Text Info box

%	L. Ljung 4-4-94
%	Copyright (c) 1986-95 by the MathWorks, Inc.
%	$Revision: 1.18 $  $Date: 1995/06/01 21:43:41 $

global  XIDplotw XIDsumb  XIDlayout  XIDcounters  XIDhw
global  XIDsbmen XIDsessions XIDlaypath

sumb=findobj(get(0,'children'),'flat','tag','sitb30');
sumbs=[XIDsumb(1);sumb(:)];
iduistat('');
if strcmp(arg,'new')
   PathFileName=get(XIDsumb(1),'userdata');
   if isempty(PathFileName),
      PathFileName='Untitled.sid';
   end
   quest=['Save to ',PathFileName,' before closing session?'];
   if XIDcounters(5)
      click=questdlg(quest,'Yes','No','Cancel');
   else
      click='No';
   end
   lasterr('')
   if strcmp(click,'Cancel')
      return
   elseif strcmp(click,'Yes')
      iduisess('save',PathFileName);
   end
   if ~isempty(lasterr),
      % Saving errored, so return
      return
   end
%   if strcmp(click,'Yes')|strcmp(click,'No')
   iduiwast('kill');
   set(XIDsumb(1),'userdata',[]);
   set(XIDsumb(1),'name',idlaytab('figname',16));
   mod=findobj(XIDsumb(1),'tag','modelline');
   dat=findobj(XIDsumb(1),'tag','dataline');
   dat1=findobj(XIDsumb(1),'tag','seles');
   dat2=findobj(XIDsumb(1),'tag','selva');
   set([get(dat1,'children');get(dat2,'children')],'vis','off');
   set(dat1,'userdata',[]);
   set(get(dat1,'zlabel'),'userdata','');
   set(get(dat2,'zlabel'),'userdata','');
   for ka=[mod(:)',dat(:)']
       eval('nam=findobj(get(ka,''parent''),''tag'',''name'');','nam=[];')
       set([ka,nam],'vis','off','userdata',[]);
       set(get(ka,'parent'),'userdata',[],'color',get(XIDsumb(1),'color'));
       tag=get(ka,'tag');
     if strcmp(tag,'modelline'),set(ka,'tag','modelline0','linewidth',0.5),end
       if strcmp(tag,'dataline'),set(ka,'tag','dataline0','linewidth',0.5),end
   end
   XIDcounters(3:4)=[0 0];
   close(sumb)
   for kwin=1:15
       if iduiwok(kwin),iduiclpw(kwin,0);end
   end
   close(iduiwok(20));close(iduiwok(21));close(iduiwok(22));
   XIDcounters(5)=0;
   set(XIDsbmen([3 4 5]),'enable','off')
   [label,acc]=menulabel('&Open session... ^o');
   set(XIDsbmen(1),'label',label,'tag','open');
   iduistat('Ready to open new session or import data.')
%   end % if click etc
elseif strcmp(arg,'close_all')
   PathFileName=get(XIDsumb(1),'userdata');
   if isempty(PathFileName),
      PathFileName='Untitled.sid';
   end
   if XIDcounters(5)
      quest=['Save to ',PathFileName,' before exiting?'];
      click=questdlg(quest,'Yes','No','Cancel');
   else
      click='No';
   end
   lasterr('')
   if strcmp(click,'Cancel')
      return
   elseif strcmp(click,'Yes')
      iduisess('save',PathFileName);
   end
   if ~isempty(lasterr),
      % Saving errored, so return
      return
   end
%   if strcmp(click,'Yes')|strcmp(click,'No')
   figs=get(0,'children');
   idtoolw=findobj(figs,'flat','tag','sitb16');
   iduistat('Closing all ident windows.')
   for kf=figs(:)'
     tag=get(kf,'tag');
     if length(tag)>=4,
        if strcmp(tag(1:4),'sitb')&~strcmp(tag,'sitb16')
           delete(kf)
        end
     end
   end
   delete(idtoolw),XIDcounters(5)=0;
%   end % if strcmp(click,...
end

if strcmp(arg,'pres')
  cura=wi_no;   %This is the handle of the desired axes
  ismodel=0;

  hand1=findobj(cura,'tag','modelline');ismodel=1;
  if isempty(hand1);hand1=findobj(cura,'tag','dataline');ismodel=0;end
  if isempty(hand1)
     iduistat('This object cannot be opened')
     return
  end
  iduipw(8);iduistat('');
  fig=XIDplotw(8,1);h=get(fig,'userdata');
  hand2=findobj(cura,'tag','name');
  hand3=hand2;
  dat=get(hand1,'UserData');dat_n=get(hand2,'String');
  dat_i=get(hand2,'UserData');
  set(fig,'name',['Data/model Info: ',dat_n])
  [Ndat,Nz]=size(dat);
  [sl1,sl2]=size(dat_i);
  isparmod=0;
  if ismodel,
      set(h(4),'string','Model name:','userdata',...
                         [hand1;hand2]);
      set(h(3),'userdata',dat_i(1:8,:)) % 8 var 6
      Method=deblank(dat_i(1,1:3));
      if ~(strcmp(Method,'spa')|strcmp(Method,'cra')),isparmod=1;end
      dat_i=dat_i(2:sl1,:);sl1=sl1-1;
  else
     set(h(4),'string','Data name:','userdata',...
                         [hand1;hand2]);
      set(h(3),'userdata',dat_i(1:7,:))  % 7 var 5
  end
  set(h(5),'string',dat_n,'userdata',dat_n);
  col=get(hand1,'color');
  cols=['[',num2str(col(1)),',',num2str(col(2)),',',num2str(col(3)),']'];
  set(h(6),'string',cols,...
           'userdata',[hand1]);
  [ny,nu,ky,ku]=iduiiono(dat_i,'dat');

  infostr=str2mat(['The data set has ',deblank(dat_i(5,:)),...
       ' inputs and ',deblank(dat_i(2,:)),' outputs'],...
       ['The number of samples is ',deblank(dat_i(4,:))],...
       ['Sampling interval: ',deblank(dat_i(1,:)),...
        '   Starting time: ',deblank(dat_i(3,:))]);
   if isparmod,
      dat=iduicalc('unpack',dat,1);
      infostr=str2mat(infostr,...
        ['Loss function: ',num2str(dat(1,1)),'  FPE: ',num2str(dat(2,1))]);
   end
  set(h(3),'string',infostr);
  set(h(1),'userdata',dat_i(8:sl1,:),...
                  'string',dat_i(8:sl1,:)) % 8  var 6
  set(h(2),'max',-1,'min',-sl1+8,'userdata',1,'value',-1);  
figure(XIDplotw(8,1));
elseif strcmp(arg,'slide')
  h=get(gcf,'userdata');
  text=get(h(1),'userdata');
  [sl1,dum]=size(text);
  first_row=-floor(get(h(2),'value'));
  first_row=max(first_row,1);
  set(h(1),'string',text(first_row:sl1,:));
  set(h(2),'userdata',first_row);  
elseif strcmp(arg,'present')
    h=get(gcf,'userdata');
    usd=get(h(4),'userdata');
    dat=get(usd(1),'userdata');
    str=get(h(4),'string');
    if strcmp(str(1:3),'Dat')
        iduistat('The data are displayed at MATLAB command line.')
        dat
    else
        usd1=get(h(3),'userdata');
        [rusd1,cusd1]=size(usd1);
        if cusd1>2,Method=usd1(1,1:3);else Method=[];end
        if ~strcmp(Method,'spa')&~strcmp(Method,'cra')
           iduistat('Information on the model is presented at MATLAB command line.');
           present(iduicalc('unpack',dat,1))
        else
           iduistat('The response is displayed at MATLAB command line.')
           dat
        end 
    end
    
elseif strcmp(arg,'update_name')
  XIDcounters(5)=1;
    h=get(gcf,'userdata');
  handles=get(h(4),'userdata');
  oldnam=get(h(5),'userdata');
  newnam=deblank(get(h(5),'string'));
  if strcmp(oldnam,newnam),return,end
  add=[' ',newnam,' = ',oldnam,'  % Rename'];
  set(handles(2),'string',newnam)
  set(h(5),'userdata',newnam)
  inf=get(h(1),'string');set(h(1),'string',str2mat(inf,add));
  usd=get(h(3),'UserData');
  flag=0;
  str=get(h(4),'string');
  if strcmp(str(1:3),'Dat')
    ismodel=0;adder=0;
  else
    ismodel=1;adder=1;
  end

  newstr=get(h(1),'string');
  first_row=get(h(2),'userdata');
  begstr=get(h(1),'userdata');
  if first_row>1
    begstr=begstr(1:first_row-1,:);
    sl=str2mat(usd,begstr,newstr);
  else
    sl=str2mat(usd,newstr);
  end
  if ~isempty(add),sl=str2mat(sl,add);end
  set(handles(2),'UserData',sl);[slend,dum]=size(sl);
  axpar=get(handles(2),'parent');
  if ismodel
     plots=get(axpar,'userdata');
     fittext=findobj(plots,'flat','tag','fits');
     if ~isempty(fittext)
        set(fittext,'string',[newnam,': ',num2str(get(fittext,'userdata'))])
     end
   end
  if strcmp(get(axpar,'tag'),get(get(XIDhw(3,1),'zlabel'),'userdata'))
     % then we are changing the color of the working data
     set(findobj(XIDhw(3,1),'tag','name'),'string',newnam)
  end
  if strcmp(get(axpar,'tag'),get(get(XIDhw(4,1),'zlabel'),'userdata'))
     % then we are changing the color of the validation data
     set(findobj(XIDhw(4,1),'tag','name'),'string',newnam)
  end
     
elseif strcmp(arg,'update_info')
  XIDcounters(5)=1;
  h=get(gcf,'userdata');
  handles=get(h(4),'userdata');
  usd=get(h(3),'UserData');
  flag=0;
  str=get(h(4),'string');
  if strcmp(str(1:3),'Dat')
    ismodel=0;adder=0;
  else
    ismodel=1;adder=1;
  end

  newstr=get(h(1),'string');
  first_row=get(h(2),'userdata');
  begstr=get(h(1),'userdata');
  if first_row>1
    begstr=begstr(1:first_row-1,:);
    sl=str2mat(usd,begstr,newstr);
  else
    sl=str2mat(usd,newstr);
  end
  set(handles(2),'UserData',sl);[slend,dum]=size(sl);
elseif strcmp(arg,'update_color');
  XIDcounters(5)=1;
  err=0;
  eval('col=eval(get(gco,''string''));','err=1;')
  obj=get(gco,'userdata');   % The handle to the icon line
  axh=get(obj,'parent');     % The handle of the icon axes
  lines=get(get(obj,'parent'),'userdata');
  eval('set(idnonzer([obj;lines(:)]),''color'',col);','err=1;')
  if err
     errordlg(str2mat('Invalid color specification.',...
                      'Please enter either a color name within quotes (e.g. ''y'')',...
                      'or an RGB triple within brackets (e.g. [0.3 0.6 0.7]).'));return
  end
  if strcmp(get(axh,'tag'),get(get(XIDhw(3,1),'zlabel'),'userdata'))
     % then we are changing the color of the working data
     set(findobj(XIDhw(3,1),'tag','selline'),'color',col)
     for kn=[14,15]
       hw14=iduiwok(kn);
       if ~isempty(hw14)
         xax=get(hw14,'userdata');
         hl1=get(xax(3,1),'userdata');set(hl1(5,1),'color',col);
         eval('hl1=get(xax(4,1),''userdata'');set(hl1(5,1),''color'',col);')
       end
     end
  end   
end
