function View = createView(this,PlotType,varargin)
%CREATEVIEW  Creates one of the built-in LTI plots.

%   Authors: Kamesh Subbarao, Pascal Gahinet
%   Copyright 1986-2011 The MathWorks, Inc.

% Create seed axes
View = createView@viewgui.ltiviewer(this,PlotType,varargin{:});

%Info for multi-model display
View.Options.MultiModelDisplayType = 'Systems';

PosInc = 2;
if any(strcmp(PlotType,{'step','impulse','bode'}))
   MMMenu = LocalAddMultiModelMenu(this,View);
   set(MMMenu,'Position',length(Menus.Group1)+PosInc)
   PosInc = PosInc+1;
elseif any(strcmp(PlotType,{'pzmap'}))
   MMMenu = LocalAddSingleMultiModelMenu(this,View);
   set(MMMenu,'Position',length(Menus.Group1)+PosInc)
   PosInc = PosInc+1;
end

% Requirements menu
hMenu = uimenu('Parent',View.AxesGrid.UIContextMenu, ...
   'Label', getString(message('Control:viewer:menuDesignRequirements')),...
   'Tag', 'DesignRequirement');
set(hMenu,'Position',length(Menus.Group1)+PosInc)
% Constraint submenus
uimenu(hMenu, ...
   'Label', getString(message('Control:viewer:menuNewEllipsis')), ...
   'Tag','NewRequirement', ...
   'Callback', {@LocalDesignConstr this View 'new'});
uimenu(hMenu, ...
   'Label', getString(message('Control:viewer:menuEditEllipsis')), ...
   'Tag','EditRequirement', ...
   'Callback', {@LocalDesignConstr this View 'edit'});
%Hide menu if view does not support requirements
if isempty(View.newconstr)
   set(hMenu,'Visible','off')
else
   set(hMenu,'Visible','on')
end

% Set requirement color consistency
View.Options.RequirementColor = this.Parent.Preferences.RequirementColor;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                     UTILITIES                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalDesignConstr %%%
%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalDesignConstr(~, ~, Viewer, View, ActionType)
% Opens dialogs to add/edit design constraints
switch ActionType
   case 'new'
      % Add new constraint
      editconstr.newdlg.getInstance(View, View.Axes.Parent);
   case 'edit'
      % Edit constraints in editor if there are constraints to edit.
      Constr = View.findconstr;
      if isempty(Constr)
         % No constraints to show in this View
         warnstr = getString(message('Control:viewer:msgNoRequirementToEdit'));
         warndlg(warnstr,getString(message('Control:viewer:strEditRequirementWarning')));
      else
         % Point constraint editor to this view
         sisoDB = Viewer.Parent;
         if isempty(sisoDB.PlotEditors)
            %No Design editor created so no global Constraint Editor
            Constr(1).EditDlg.show(Constr(1).TextEditor)
         else
            %Have global constraint editor to use
            Viewer.Parent.PlotEditors(1).ConstraintEditor.show(View);
         end
      end
end


%%%%%%%%%%%%%%%%%%%%%%
% LocalAddMultiModelMenu
%%%%%%%%%%%%%%%%%%%%%%
function  h = LocalAddMultiModelMenu(this,View)
% LocalAddMultiModelMenu adds the multi model menu
% to the right click menus of all the plots.

h = uimenu('Parent', View.AxesGrid.UIContextMenu,...
   'Label',ctrlMsgUtils.message('Control:compDesignTask:strMultiModelDisplay'), ...
   'Tag','MultiModel');


% Adds menu items to Multimodel menu
hb = uimenu(h,'Label',ctrlMsgUtils.message('Control:compDesignTask:strMultiModelBounds'));
hs = uimenu(h,'Label',ctrlMsgUtils.message('Control:compDesignTask:strMultiModelIndividualResponses'));


m = struct(...
    'BoundsMenu',hb,...
    'SystemsMenu',hs);

% Set Callbacks
set(hb,'Callback',{@LocalToggleBoundsMenu View m})
set(hs,'Callback',{@LocalToggleSystemsMenu View m})
    
LocalUncertainSetCheck(this, m, View)

L = handle.listener(View, ...
    View.findprop('CharacteristicManager'),'PropertyPostSet', ...
    @(x,y) LocalUncertainSetCheck(this,m,View));

set(hb,'UserData',L)  % Anchor listeners for persistency


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalToggleBoundsMenu %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalToggleBoundsMenu(hSrc,~,View,m)
% Callbacks for Stability Margins submenu (hSrc = menu handle)
if strcmp(get(hSrc,'Checked'),'on')
    View.hideCharacteristic('MultipleModelView');
else
    View.Options.MultiModelDisplayType = 'Bounds';
    if LocalIsMultiModelVisible(View)
        LocalUncertainSetCheck([], m, View)
    else
        if hasCharacteristic(View,'MultipleModelView')
            View.showCharacteristic('MultipleModelView');
        end
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalToggleSystemsMenu %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalToggleSystemsMenu(hSrc,~,View,m)
% Callbacks for Stability Margins submenu (hSrc = menu handle)
if strcmp(get(hSrc,'Checked'),'on')
    View.hideCharacteristic('MultipleModelView');
else
    View.Options.MultiModelDisplayType = 'Systems';
    if LocalIsMultiModelVisible(View)
        LocalUncertainSetCheck([], m, View)
    else
        if hasCharacteristic(View,'MultipleModelView')
            View.showCharacteristic('MultipleModelView');
        end
    end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalUncertainSetCheck %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalUncertainSetCheck(~, m, View)

isVisible = LocalIsMultiModelVisible(View);

if isVisible && strcmpi(View.Options.MultiModelDisplayType,'Bounds')
    set(m.BoundsMenu,'Checked','on')
else
    set(m.BoundsMenu,'Checked','off')
end

if isVisible  && strcmpi(View.Options.MultiModelDisplayType,'Systems')
    set(m.SystemsMenu,'Checked','on')
else
    set(m.SystemsMenu,'Checked','off')
end

function isVisible = LocalIsMultiModelVisible(View)

[b,idx] = hasCharacteristic(View,'MultipleModelView');

if b
    isVisible = View.CharacteristicManager(idx).Visible;
else
    isVisible = false;
end


%%%%%%%%%%%%%%%%%%%%%%
% LocalAddSingleMultiModelMenu
%%%%%%%%%%%%%%%%%%%%%%
function  h = LocalAddSingleMultiModelMenu(this,View)
% LocalAddMultiModelMenu adds the multi model menu
% to the right click menus of all the plots.

h = uimenu('Parent', View.AxesGrid.UIContextMenu,...
   'Label',ctrlMsgUtils.message('Control:compDesignTask:strMultiModelDisplay'), ...
   'Tag','MultiModel');


% Adds menu items to Multimodel menu
hb = uimenu(h,'Label',ctrlMsgUtils.message('Control:compDesignTask:strShow'));
m = struct('ShowMenu',hb);
    
set(hb,'Callback',{@LocalToggleShowMenu, View, m});

LocalShowMenuSetCheck(this, m, View)

L = handle.listener(View, ...
    View.findprop('CharacteristicManager'),'PropertyPostSet', ...
    @(x,y) LocalShowMenuSetCheck(this,m,View));
set(hb,'UserData',L)  % Anchor listeners for persistency


function LocalShowMenuSetCheck(~,m,View)
isVisible = LocalIsMultiModelVisible(View);
if isVisible
    set(m.ShowMenu,'Checked','on')
else
    set(m.ShowMenu,'Checked','off')
end

function LocalToggleShowMenu(hSrc,~,View,m)
if strcmp(get(hSrc,'Checked'),'on')
    View.hideCharacteristic('MultipleModelView');
else
    if LocalIsMultiModelVisible(View)
        LocalShowMenuSetCheck([], m, View);
    else
        if hasCharacteristic(View,'MultipleModelView')
            View.showCharacteristic('MultipleModelView');
        end
    end
end


