function ViewerObj = plotapply(ViewerObj,varargin)
%PLOTAPPLY Apply Linestyle Preference settings to the associated LTI Viewer

%   Karen Gondoly, 3-23-98
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.15 $  $Date: 2000/06/14 20:03:14 $

if nargin>1,
   NumConfigs = varargin{1};   
else
   NumConfigs = 1:length(ViewerObj.ActivePlotTypes);
end

PlotPrefFig = get(ViewerObj.FigureMenu.EditMenu.LineStyles,'UserData');

if ishandle(PlotPrefFig) & PlotPrefFig~=0,
   udPlot=get(PlotPrefFig ,'UserData');
   
   [garb,is1] = sort(ViewerObj.ColorOrder(:,2));
   [garb,is2] = sort(udPlot.Revert.ColorList);
   perm = [];
   perm(is2) = is1;
   ViewerObj.ColorOrder = ViewerObj.ColorOrder(perm,:);
   NumC = size(ViewerObj.ColorOrder,1);
   
   ViewerObj.LineStyleOrder = udPlot.Revert.LineList;
   NumL=length(ViewerObj.LineStyleOrder);
   
   ViewerObj.MarkerOrder = udPlot.Revert.MarkerList;   
   NumM=length(ViewerObj.MarkerOrder);
   
   %---Need to update Response Object Orders, as well...but, only if we intend
   %----to keep the Orders as Response Object Properties. I vote for removing them
   
   strs = {'color';'marker';'linestyle';'none'};
   %---Find SysInd
   AllSysInd=[udPlot.Revert.ColorSystem;
      udPlot.Revert.MarkSystem;
      udPlot.Revert.LineSystem;
      udPlot.Revert.NoSystem];
   ViewerObj.SystemPlotVariable = strs{find(AllSysInd)};
   
   %---Find InInd	
   AllInInd=[udPlot.Revert.ColorInput;
      udPlot.Revert.MarkInput;
      udPlot.Revert.LineInput;
      udPlot.Revert.NoInput];
   ViewerObj.InputPlotVariable = strs{find(AllInInd)};
   
   %---Find OutInd
   AllOutInd=[udPlot.Revert.ColorOutput;
      udPlot.Revert.MarkOutput;
      udPlot.Revert.LineOutput;
      udPlot.Revert.NoOutput];
   ViewerObj.OutputPlotVariable = strs{find(AllOutInd)};
   
   %---Find ChInd
   AllChInd=[udPlot.Revert.ColorChannel;
      udPlot.Revert.MarkChannel;
      udPlot.Revert.LineChannel;
      udPlot.Revert.NoChannel];
   ViewerObj.ChannelPlotVariable = strs{find(AllChInd)};
   
   Inds=[find(AllInInd),find(AllOutInd),find(AllChInd),find(AllSysInd)];
   Cind=find(Inds==1);  Cind = [Cind zeros(isempty(Cind))];
   Mind=find(Inds==2);  Mind = [Mind zeros(isempty(Mind))];
   Lind=find(Inds==3);  Lind = [Lind zeros(isempty(Lind))];
   
   if ~isempty(ViewerObj.Systems),
      %---Determine the new plot styles and legends for each system
      % RE: This blows away any user-defined style
      Nsys = length(ViewerObj.Systems);
      SysNames = ViewerObj.SystemNames;
      ViewerObj.PlotStyles = ...
         struct('Color',cell(Nsys,1),'LineStyle',[],'Marker',[],'Legend',[]);
      ViewerObj = setstyle(ViewerObj);
      Legends = cell(Nsys,1);
      for i=1:Nsys,
         SizeSys = size(ViewerObj.Systems{i});
         SizeArray = SizeSys(3:end);
         if prod(SizeArray)>1,
            ArrayDimsStr = sprintf('%dx',SizeArray);
            Legends{i} = sprintf('%s (%s array: %s)',...
               SysNames{i},ArrayDimsStr(1:end-1),ViewerObj.PlotStyles(i).Legend);
         else
            Legends{i} = sprintf('%s (%s)',SysNames{i},ViewerObj.PlotStyles(i).Legend);
         end
      end
      
      % Cycle over each individual response plot area
      RespObjs = get(ViewerObj.UIContextMenu,{'UserData'});
      for ctConfig = NumConfigs,
         %---Get plot type and list of displayed systems 
         AllProps = get(RespObjs{ctConfig},'undocumented');
         plottype = AllProps.ResponseType; 
         ResponseHandles = AllProps.ResponseHandles;
         
         %---Watch for missing systems (FRDs, improper,...)
         if length(AllProps.SystemNames)<length(SysNames),
            SysInd = find(ismember(SysNames,AllProps.SystemNames));
         else
            SysInd = 1:length(SysNames);
         end
         Nsys = length(SysInd);
            
         %---Update the legend in the Systems menu
         ContextMenu = AllProps.UIContextMenu;
         if strcmp(plottype,'pzmap') & ~strcmp(ViewerObj.SystemPlotVariable,'color')
            % Linestyle and marker differentiators don't make sense for PZMAP
             set(ContextMenu.Systems.Names,{'label'},SysNames(SysInd))
         else
            set(ContextMenu.Systems.Names,{'label'},Legends(SysInd));
         end
         
         % All handles grouped by systems
         SysH = cell(Nsys,1);
         for ctSys=1:Nsys
            tmpH = cat(1,ResponseHandles{ctSys}{:});
            tmpH = cat(1,tmpH{:});
            SysH{ctSys} = tmpH(:);
         end
         
         % All handles grouped by channels
         tmpH = cat(3,ResponseHandles{:});
         for ctEntry=1:prod(size(tmpH))
            % Collapse LTI array handles
            tmpH{ctEntry} = cat(1,tmpH{ctEntry}{:});
         end
         if strcmpi(plottype,'bode')
            tmpH = cat(4,tmpH(1:2:end,:,:),tmpH(2:2:end,:,:));
         end
         [numout,numin] = size(tmpH(:,:,1));  % actual dimensions of plot grid
         ChannelH = cell(numout,numin);
         for ctIn=1:numin,
            for ctOut=1:numout,
               ChannelH{ctOut,ctIn} = cat(1,tmpH{ctOut,ctIn,:});
            end
         end
         
         
         %---Color-based differentiation 
         %
         switch Cind, 
         case 1, % Use colors to differentiate input channels
            Colors = cell(numin,1);
            HandleGroups = cell(numin,1);
            for ctIn=1:numin,
               Colors(ctIn) = ViewerObj.ColorOrder(ctIn-(NumC*floor((ctIn-1)/NumC)),1);
               HandleGroups{ctIn} = cat(1,ChannelH{:,ctIn}); % group handles by input channels
            end
         case 2 % Use colors to differentiate output channels
            Colors = cell(numout,1);
            HandleGroups = cell(numout,1);
            for ctOut=1:numout,
               Colors(ctOut) = ViewerObj.ColorOrder(ctOut-(NumC*floor((ctOut-1)/NumC)),1);
               HandleGroups{ctOut} = cat(1,ChannelH{ctOut,:}); % group handles by output channels
            end
         case 3  % Use colors to differentiate channels
            Colors = cell(numout,numin);
            HandleGroups = ChannelH;
            for ctCh=1:numout*numin,
               Colors(ctCh) = ViewerObj.ColorOrder(ctCh-(NumC*floor((ctCh-1)/NumC)),1);
            end
         case 4  % Use colors to differentiate systems 
            % RE: Use colors generated by SETSTYLE to be consistent with legends in Systems menu
            Colors = {ViewerObj.PlotStyles(SysInd).Color};
            HandleGroups = SysH;
         otherwise
            Colors = ViewerObj.ColorOrder(1,1);
            HandleGroups = {cat(1,SysH{:})};
         end
         
         % Set colors
         for ctColor=1:prod(size(Colors))
            Hgrp = HandleGroups{ctColor};
            CC = Colors{ctColor};
            set(findobj(Hgrp,'Tag','LTIresponseLines'),'Color',CC)
            set(findobj(Hgrp,'Tag','NyquistArrow'),'FaceColor',CC,'EdgeColor',CC)
            Markers = [findobj(Hgrp,'Tag','SteadyStateMarker','Marker','o');
               findobj(Hgrp,'Tag','StabilityMarginMarker','Marker','o');
               findobj(Hgrp,'Tag','SettlingTimeMarker','Marker','o');
               findobj(Hgrp,'Tag','RiseTimeMarker','Marker','o');
               findobj(Hgrp,'Tag','PeakResponseMarker','Marker','o')];
            set(Markers,'MarkerFaceColor',CC,'MarkerEdgeColor',CC);
         end
         
         
         %---Linestyle-based differentiation 
         %
         switch Lind, 
         case 1, % Use linestyles to differentiate input channels
            LineStyles = cell(numin,1);
            HandleGroups = cell(numin,1);
            for ctIn=1:numin,
               LineStyles(ctIn) = ViewerObj.LineStyleOrder(ctIn-(NumL*floor((ctIn-1)/NumL)));
               HandleGroups{ctIn} = cat(1,ChannelH{:,ctIn}); % group handles by input channels
            end
         case 2 % Use linestyles to differentiate output channels
            LineStyles = cell(numout,1);
            HandleGroups = cell(numout,1);
            for ctOut=1:numout,
               LineStyles(ctOut) = ViewerObj.LineStyleOrder(ctOut-(NumL*floor((ctOut-1)/NumL)));
               HandleGroups{ctOut} = cat(1,ChannelH{ctOut,:}); % group handles by output channels
            end
         case 3  % Use linestyles to differentiate channels
            LineStyles = cell(numout,numin);
            HandleGroups = ChannelH;
            for ctCh=1:numout*numin,
               LineStyles(ctCh) = ViewerObj.LineStyleOrder(ctCh-(NumL*floor((ctCh-1)/NumL)));
            end
         case 4  % Use linestyles to differentiate systems 
            % RE: Use linestyles generated by SETSTYLE to be consistent with legends in Systems menu
            LineStyles = {ViewerObj.PlotStyles(SysInd).LineStyle};
            HandleGroups = SysH;
         otherwise
            LineStyles = ViewerObj.LineStyleOrder(1,1);
            HandleGroups = {cat(1,SysH{:})};
         end
         
         % Set line styles. RE: don't mess with marker and linestyle for pole/zero plots
         if ~strcmp(plottype,'pzmap')
            for ctLs=1:prod(size(LineStyles))
               set(findobj(HandleGroups{ctLs},'Tag','LTIresponseLines'),'Linestyle',LineStyles{ctLs})
            end
         end
         
         %---Marker-based differentiation 
         %
         switch Mind, 
         case 1, % Use linestyles to differentiate input channels
            Markers = cell(numin,1);
            HandleGroups = cell(numin,1);
            for ctIn=1:numin,
               Markers(ctIn) = ViewerObj.MarkerOrder(ctIn-(NumM*floor((ctIn-1)/NumM)));
               HandleGroups{ctIn} = cat(1,ChannelH{:,ctIn}); % group handles by input channels
            end
         case 2 % Use Markers to differentiate output channels
            Markers = cell(numout,1);
            HandleGroups = cell(numout,1);
            for ctOut=1:numout,
               Markers(ctOut) = ViewerObj.MarkerOrder(ctOut-(NumM*floor((ctOut-1)/NumM)));
               HandleGroups{ctOut} = cat(1,ChannelH{ctOut,:}); % group handles by output channels
            end
         case 3  % Use Markers to differentiate channels
            Markers = cell(numout,numin);
            HandleGroups = ChannelH;
            for ctCh=1:numout*numin,
               Markers(ctCh) = ViewerObj.MarkerOrder(ctCh-(NumM*floor((ctCh-1)/NumM)));
            end
         case 4  % Use Markers to differentiate systems 
            % RE: Use Markers generated by SETSTYLE to be consistent with legends in Systems menu
            Markers = {ViewerObj.PlotStyles(SysInd).Marker};
            HandleGroups = SysH;
         otherwise
            Markers = ViewerObj.MarkerOrder(1);
            HandleGroups = {cat(1,SysH{:})};
         end
         
         % Set markers
         if ~strcmp(plottype,'pzmap')
            for ctM=1:prod(size(Markers))
               set(findobj(HandleGroups{ctM},'Tag','LTIresponseLines'),'Marker',Markers{ctM})
            end
         end
         
      end % for ctConfig
   end % if ~isempty(ViewerObj.Systems)   
end % ishandle(PlotPref)
