function dummy = respview(sisodb,Contents)
%RESPVIEW  Creates and manages the SISO Tool LTI Viewer.
%
%   See also SISOTOOL.

%   Author(s): K. Gondoly and P. Gahinet
%   Copyright 1986-2000 The MathWorks, Inc.
%   $Revision: 1.9 $  $Date: 2000/08/02 16:06:43 $

% REVISIT
dummy = [];

% Find LTI Viewer
ViewFig = findview(sisodb,'ResponseGUI');

% If no Viewer is opened yet, open one
if isempty(ViewFig)
    ViewFig = LocalOpen(sisodb);
    % REVISIT: merge next 3
    Views = sisodb.DataViews;
    Views(end+1,:) = ViewFig;
    sisodb.DataViews = Views;
end

% Configure and populate Viewer
LocalConfigure(ViewFig,Contents,sisodb.LoopData)


%----------------- Callback functions -----------------

%%%%%%%%%%%%%%%%%
%%% LocalOpen %%%
%%%%%%%%%%%%%%%%%
function ViewFig = LocalOpen(sisodb)
% Open LTI Viewer

ViewFig = ltiview;
set(ViewFig,'Name','LTI Viewer for SISO Design Tool')

% Install CS help
cshelp(ViewFig,sisodb.Figure);

% Customize menus
ViewerObj = get(ViewFig,'UserData');
FigMenus = get(ViewerObj,'FigureMenu');
set([FigMenus.FileMenu.Import,FigMenus.FileMenu.Export,...
        FigMenus.EditMenu.PlotConfigurations,FigMenus.EditMenu.Systems],...
    'Visible','off')
set(FigMenus.EditMenu.LineStyles,'Separator','off')


% Install listeners
LoopData = sisodb.LoopData;
Listeners(1) = ...
    handle.listener(LoopData,'ObjectBeingDestroyed',{@LocalClose ViewFig});
Listeners(2) = ...
    handle.listener(LoopData,'LoopDataChanged',{@LocalUpdate ViewFig});
Listeners(3) = ...
    handle.listener(handle(ViewFig),'ObjectBeingDestroyed',{@LocalUncheck sisodb});
setappdata(ViewFig,'Listeners',Listeners)


%%%%%%%%%%%%%%
% LocalClose %
%%%%%%%%%%%%%%
function LocalClose(hSrc,event,ViewFig)
% Callback when closing SISO Tool
close(ViewFig);


%%%%%%%%%%%%%%%%
% LocalUncheck %
%%%%%%%%%%%%%%%%
function LocalUncheck(hSrc,event,sisodb)
% Uncheck Plot selection menus when closing Viewer
HG = sisodb.HG;
set(HG.Menus.Tools.PlotSelection,'Checked','off')


%%%%%%%%%%%%%%%%%%
% LocalConfigure %
%%%%%%%%%%%%%%%%%%
function LocalConfigure(ViewFig,Contents,LoopData)
% Configures the LTI Viewer
ViewerObj = get(ViewFig,'UserData');

% Plot list
PlotList = {Contents.PlotType}';

% Model list and model names (in Viewer)
ModelList = cat(1,Contents.OpenLoop,Contents.ClosedLoop);
NameTable = {...
        '$T_r2y', 'Closed Loop: r to y';...
        '$T_r2u', 'Closed Loop: r to u';...
        '$S_output' , 'Output Sensitivity';...
        '$S_input' , 'Input Sensitivity';...
        '$S_noise' , 'Noise Sensitivity';...
        '$L' , 'Open Loop L';...
        '$C' , 'Compensator C';...
        '$G' , 'Plant G';...
        '$H' , 'Sensor H';...
        '$F' , 'Prefilter F'};
iSel = find(ismember(NameTable(:,1),ModelList));
ModelList = NameTable(iSel,1);
ModelNames = NameTable(iSel,2);

% Model data
ModelData = getmodels(LoopData,ModelList);

% Configure Viewer object 
SystemData = struct('Names',{ModelNames},'Systems',{ModelData},'PlotStyles',[]);
initview(ViewerObj,SystemData,PlotList);

% Set model visibility 
ViewerObj = get(ViewFig,'UserData');  % watch for stale handles!
UiC = get(ViewerObj,'UIcontextMenu');
for ct=1:length(PlotList),
    % Get response object
    RespObj = get(UiC(ct),'UserData');
    % Filter out systems that can't be plotted (e.g., improper for step)
    isPlotted = ismember(ModelNames,get(RespObj,'SystemNames'));
    SystemVis = cell(sum(isPlotted),1);
    SystemVis(:) = {'off'};
    % Assess visibility
    isVisible = ismember(ModelList(isPlotted),[Contents(ct).OpenLoop;Contents(ct).ClosedLoop]);
    SystemVis(isVisible) = {'on'};
    % Pass on to response object
    RespObj = get(UiC(ct),'UserData');
    set(RespObj,'SystemVisibility',SystemVis)
end

% Store model identifiers (for plot updates)
setappdata(ViewFig,'Systems',ModelList)


%%%%%%%%%%%%%%%
% LocalUpdate %
%%%%%%%%%%%%%%%
function LocalUpdate(LoopData,event,ViewFig)
% Update LTI Viewer
ViewerObj = get(ViewFig,'UserData');

% Acquire model data
ModelList = getappdata(ViewFig,'Systems');
set(ViewerObj,'Systems',getmodels(LoopData,ModelList))





