function varargout = bfit(figHandle, cmd)
% BFIT
%   GUIHANDLE = BFIT(FIGHANDLE, 'bf') opens the Basic Fitting GUI and stores the
%   handle of the GUI in the appdata of the figure.
%   GUIHANDLE = BFIT(FIGHANDLE, 'ds') opens the Data Stats GUI and stores the
%   handle of the GUI in the appdata of the figure.

%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.7 $  $Date: 2000/09/15 14:15:37 $

ptr = get(figHandle,'pointer');
set(figHandle,'pointer','watch');
switch cmd
case 'bf'
	% Was it loaded from a figfile: if so, then no pointer to GUI but has Basic_Fit_Fig_Tag
	% BEFORE opening GUI.
	if isempty(findprop(handle(figHandle),'_Basic_Fit_GUI_Object')) ...
			& ~isempty(findprop(handle(figHandle),'Basic_Fit_Fig_Tag')) & ...
			isappdata(figHandle,'Basic_Fit_Data_Counter')  % not a residual figure
		updatetags(figHandle);
	end
	bf = javaMethod('showBasicFit', 'com.mathworks.page.basicfit.BasicFit', figHandle);
	if isempty(findprop(handle(figHandle),'_Basic_Fit_GUI_Object'))
		p = schema.prop(handle(figHandle), '_Basic_Fit_GUI_Object', ...
			'MATLAB array');
		p.AccessFlags.Serialize = 'off';
	end
	set(handle(figHandle), '_Basic_Fit_GUI_Object', bf);
case 'ds'
	ds = javaMethod('showDataStats', 'com.mathworks.page.basicfit.DataStatistics', figHandle);
	if isempty(findprop(handle(figHandle),'_Data_Stats_GUI_Object'))
		p = schema.prop(handle(figHandle), '_Data_Stats_GUI_Object', ...
			'MATLAB array');
		p.AccessFlags.Serialize = 'off';
	end
	set(handle(figHandle), '_Data_Stats_GUI_Object', ds);
end
set(figHandle,'pointer',ptr);

%------------------------------------------------------------------------------------------------
function updatetags(figHandle)
% Recreate a Tag in case the figure it was created from is open (otherwise
% they will have the same Tags).

oldTag = get(handle(figHandle),'Basic_Fit_Fig_Tag');
% Create new tag
figureTag = datenum(clock);
set(handle(figHandle), 'Basic_Fit_Fig_Tag', figureTag);

% for current data, check is resid is separate since we need to create it if it is
datahandle = getappdata(figHandle,'_Basic_Fit_Current_Data');
residinfo = getappdata(datahandle,'_Basic_Fit_Resid_Info');
if isequal(residinfo.figuretag,oldTag) % subplot		
	residinfo.figuretag = figureTag;
	setappdata(datahandle,'_Basic_Fit_Resid_Info',residinfo);
else % separate figure
	% No residfigure, so set to empty for bfitcheckplotresiduals call
	residinfo.figuretag = [];
	residinfo.axes = [];
	setappdata(datahandle,'_Basic_Fit_Resid_Info',residinfo);
	checkon = 1;
	guistate = getappdata(datahandle,'_Basic_Fit_Gui_State');
	% need to draw a new resid figure
	bfitcheckplotresiduals(checkon,datahandle,guistate.plottype,~guistate.subplot,guistate.showresid);
	residinfo = getappdata(datahandle,'_Basic_Fit_Resid_Info');
end
currentresidtag = residinfo.figuretag; 

% update other tags: residinfo.figuretag on each dataset
% for each data, check residinfo.figuretag and update
datasethandles = getappdata(figHandle,'_Basic_Fit_Data_Handles'); 
datasethandles(datasethandles==datahandle) = [];      % delete current
for i=1:length(datasethandles)
	datahandle = datasethandles(i);
	residinfo = getappdata(datahandle,'_Basic_Fit_Resid_Info');
	if isequal(residinfo.figuretag,oldTag)
		residinfo.figuretag = figureTag;
	else
		residinfo.figuretag = currentresidtag;
	end
	setappdata(datahandle,'_Basic_Fit_Resid_Info',residinfo);
end

