function addeventchar(this,cd_class,cv_class)
%ADDCHAR  Adds event characteristics to wave form.
 
%  Author(s):  
%  Copyright 1986-2004 The MathWorks, Inc.
%  $Revision: 1.1.6.1 $  $Date: 2004/12/26 21:40:56 $
 
% If the number of events have changed rebuild the event char 
eventChar = this.characteristics.find('Identifier','events');
if ~isempty(eventChar) || ...
       length(eventChar.Data.EventTimes)==length(this.DataSrc.Timeseries.Events)
   % Char already exists - do nothing
   return
elseif ~isempty(eventChar) && ...
       length(eventChar.Data.EventTimes)~=length(this.DataSrc.Timeseries.Events)
   % Incompatable eventchar exists - delete it
   this.Characteristics(find(wf.Characteristics==eventChar)) = [];
   delete(eventChar);
end
       
% Create @wavechar instance (container for characteristic data + view)
c = wavepack.wavechar;

% Initialize characteristic
set(c,'Parent',this,'Identifier','events')

% Store Characteristic Data
AxGrid = this.Parent.AxesGrid;
c.Data = tsguis.eventCharData;
c.Data.Parent = this.Data;

%% Initialize char view
c.View = tsguis.eventCharView;
set(c.View,'AxesGrid',AxGrid,'Parent',this.View)
VLines = [];
for ct=1:prod(size(AxGrid))
   % RE: Set Z=-10 to make sure dc lines don't obscure response lines
   for k=1:length(this.DataSrc.Timeseries.Events)
       VLines = [VLines(:); line(NaN,NaN,-10,...
          'Parent',Axes(ct),...
          'Visible','off',...
          'LineStyle',':',...
          'Selected','off',...
          'XlimInclude','off', 'YlimInclude','off',...
          'HandleVisibility','off',...
          'Color','k')];
   end
end
c.View.VLines = handle(VLines);
c.View.LineTips = cell(size(VLines));

% Initialize visibility of characteristics' g-objects
refresh(c)
 
% Set default DataFcn and TipFcn
c.DataFcn = {@LocalUpdateData c};
addtip(c)

% Install generic @dataview listeners (don't make users do it)
generic_listeners(c);

% Listeners
L = [handle.listener(c, c.findprop('Visible'),'PropertyPostSet', @LocalVisible); ...
      handle.listener(cva,cva(1).findprop('Visible'),'PropertyPostSet',@LocalViewVisible)];
set(L,'CallbackTarget',c)
c.addlisteners(L);

% Append to characteristics list
this.Characteristics = [this.Characteristics;c];


%------------- Local Functions ---------------------------

function LocalUpdateData(c)
wf = c.Parent; % parent waveform
for ct=1:length(c.Data)
   % Propagate exceptions
   c.Data(ct).Exception = wf.Data(ct).Exception;
   if ~c.Data(ct).Exception
      update(c.Data(ct),wf)
   end
end

% ----------------------------------------------------------------------------%
% Purpose: Updates HG object visibility when visibility of 
%          particular View changes
% ----------------------------------------------------------------------------%
function LocalViewVisible(c, eventdata)
View = eventdata.AffectedObject;  % affected char. view
if isvisible(c) && isvisible(View)
   % Characteristic view is visible: refresh it
   View.refresh(refreshmask(c.Parent))
else
   View.refresh(false)
end
% Redraw
draw(c)


% ----------------------------------------------------------------------------%
% Purpose: Sets visibility of the overall @wavechar component.
% ----------------------------------------------------------------------------%
function LocalVisible(c, eventdata)
% Update visibility of characteristic's g-objects
refresh(c)
% Redraw
draw(c)
