function draw(this, Data, NormalRefresh)
%DRAW  Draws time response curves.
%
%  DRAW(VIEW,DATA) maps the response data in DATA to the curves in VIEW.

%  Author(s):  
%  Copyright 1986-2002 The MathWorks, Inc.
%  $Revision: 1.1.6.1 $ $Date: 2004/12/26 21:41:31 $

% Time:      Ns x 1
% Amplitude: Ns x Ny x Nu

% Input and output sizes
[Ny, Nu] = size(this.Curves);

%% Cache data
data = Data.Amplitude;
time = Data.Time;
watermarkdata = Data.Watermarkdata;
watermarktime = Data.Watermarktime;

% Redraw the curves
if strcmp(this.AxesGrid.YNormalization,'on')
   % RE: Defer to ADJUSTVIEW:postlim for normalized case (requires finalized X limits)
   set(double(this.Curves),'XData',[],'YData',[],'ZData',[])
   set(double(this.WatermarkCurves),'XData',[],'YData',[],'ZData',[])
else
  for ct = 1:Ny*Nu
     %% ZData = 10 - Make sure data lines are in the front, watermark
     %% lines behind
     set(double(this.Curves(ct)), 'XData', time, ...
        'YData', data(:,ct),'ZData', ...
        10*ones(size(data(:,ct))))
     if ~isempty(watermarkdata)
         set(double(this.WatermarkCurves(ct)), 'XData', watermarktime, ...
            'YData', watermarkdata(:,ct),'ZData', ...
            5*ones(size(watermarkdata(:,ct))))  
     else
         set(double(this.WatermarkCurves(ct)), 'XData', [], ...
            'YData', [],'ZData',[])  
     end
  end    
end

%% Draw selected points
if ~isempty(this.SelectedPoints) && isequal(size(this.SelectedPoints),size(data))
    for ct = 1:Ny*Nu
        if size(this.SelectedPoints,1)==length(time)
    
            % Get the data once to avoid repeted access to time series
            % data storage
            xdata = time;
            ydata = Data.Amplitude(:,ct);
            
            if length(xdata)>=2
                % Form the selected data array - one row per obs, half
                % line segments on each side of the point
                X = ...
                [NaN                                 NaN        xdata(1)           0.5*(xdata(1)+xdata(2))
                0.5*(xdata(1:end-2)+xdata(2:end-1)) xdata(2:end-1) xdata(2:end-1) 0.5*(xdata(2:end-1)+xdata(3:end)) 
                0.5*(xdata(end-1)+xdata(end))       xdata(end)     NaN            NaN];
                Y = ...
                [NaN                                 NaN        ydata(1)           0.5*(ydata(1)+ydata(2))
                0.5*(ydata(1:end-2)+ydata(2:end-1)) ydata(2:end-1) ydata(2:end-1) 0.5*(ydata(2:end-1)+ydata(3:end)) 
                0.5*(ydata(end-1)+ydata(end))       ydata(end)     NaN            NaN];

                % Set unselected points on SelectionCurves to NaN
                X(~this.SelectedPoints(:,ct),:) = ...
                    NaN*ones([sum(~this.SelectedPoints(:,ct)) 4]); % Null out excluded
                X = X';
                Y(~this.SelectedPoints(:,ct),:) = ...
                    NaN*ones([sum(~this.SelectedPoints(:,ct)) 4]); % Null out excluded
                Y = Y'; 
            else
                X = xdata(1);
                Y = ydata;
            end
            set(this.SelectionCurves(ct),'xdata',X(:),'ydata',...
                   Y(:),'zdata',20*ones(size(Y(:))),'Color',[1 0 0],'Linewidth',2);
        end
    end
else 
   this.SelectedPoints = []; % Reset selected points 
   s = size(time);
   for ct = 1:Ny*Nu 
       set(this.SelectionCurves(ct),'ydata',NaN*ones([max(s) 1]),'xdata', ...
           NaN*ones([max(s) 1]),'zdata',NaN*ones([max(s) 1]));
   end
end
      
%% Draw time selection rectangles. Each SelectionPatch need only be
%% drawn once in each axes since they all occur at the same times. Need to
%% detect this and avoid drawing overlapping rectangles. Also avoids xor
%% mode toggling the visibility of overlapping patches
ax = cell2mat(get(this.SelectionPatch,{'Parent'}));
[junk,I] = unique(ax);
for k=1:length(I)
    if length(this.SelectedTimes)>0
        ylim = get(get(this.SelectionPatch(I(k)),'Parent'),'yLim');
        xdata = [this.SelectedTimes this.SelectedTimes(:,end:-1:1)]';
        ydata = [ylim(1); ylim(1);ylim(2);ylim(2)]*ones(1,size(this.SelectedTimes,1));
        zdata = [-10;-10;-10;-10]*ones(1,size(this.SelectedTimes,1));
        set(this.SelectionPatch(I(k)),...
          'Zdata',zdata,...
          'Xdata',xdata,...
          'Ydata',ydata,...
          'LineStyle','none',...
          'HandleVisibility','on','HitTest','on');
        %drawnow expose
    else
         set(this.SelectionPatch(I(k)),...
          'Zdata',[NaN;NaN;NaN;NaN],...
          'Xdata',[NaN;NaN;NaN;NaN],...
          'Ydata',[NaN;NaN;NaN;NaN],...
          'LineStyle','none',...
          'HandleVisibility','on','HitTest','on');
    end
end
nullind = setdiff(1:length(ax),I);
for k=1:length(nullind)
          set(this.SelectionPatch(nullind(k)),...
          'Zdata',[NaN;NaN;NaN;NaN],...
          'Xdata',[NaN;NaN;NaN;NaN],...
          'Ydata',[NaN;NaN;NaN;NaN],...
          'LineStyle','none',...
          'HandleVisibility','on','HitTest','on');
end

