function h = viewprefs(Target)
%VIEWPREFS  LTI Viewer preferences object contructor

%   Author(s): A. DiVergilio
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.9 $  $Date: 2000/08/02 16:06:18 $

a = findpackage('cstprefs');

if nargout==0
   %---Register class
    c = schema.class(a,'viewprefs');

   %---Define properties

    %---Units/Scales
     schema.prop(c,'FrequencyUnits',        'String');
     schema.prop(c,'FrequencyScale',        'String');
     schema.prop(c,'MagnitudeUnits',        'String');
     schema.prop(c,'MagnitudeScale',        'String');
     schema.prop(c,'PhaseUnits',            'String');

    %---Grids
     schema.prop(c,'Grid',                  'String');

    %---Fonts
     schema.prop(c,'TitleFontSize',         'mxArray');
     schema.prop(c,'TitleFontWeight',       'String');
     schema.prop(c,'TitleFontAngle',        'String');
     schema.prop(c,'XYLabelsFontSize',      'mxArray');
     schema.prop(c,'XYLabelsFontWeight',    'String');
     schema.prop(c,'XYLabelsFontAngle',     'String');
     schema.prop(c,'AxesFontSize',          'mxArray');
     schema.prop(c,'AxesFontWeight',        'String');
     schema.prop(c,'AxesFontAngle',         'String');
     schema.prop(c,'IOLabelsFontSize',      'mxArray');
     schema.prop(c,'IOLabelsFontWeight',    'String');
     schema.prop(c,'IOLabelsFontAngle',     'String');

    %---Colors
     schema.prop(c,'AxesForegroundColor',   'mxArray');

    %---Response Characteristics
     schema.prop(c,'SettlingTimeThreshold', 'mxArray');
     schema.prop(c,'RiseTimeLimits',        'mxArray');

    %---Phase Wrapping
     schema.prop(c,'UnwrapPhase',           'String');

    %---Parameters
     schema.prop(c,'TimeVectorMode',        'String');
     schema.prop(c,'TimeVector',            'mxArray');
     schema.prop(c,'FrequencyVectorMode',   'String');
     schema.prop(c,'FrequencyVector',       'mxArray');

    %---Handle to Figure containing target viewer
     schema.prop(c,'Target',                'mxArray');

    %---Handle to Toolbox Preferences
     schema.prop(c,'ToolboxPreferences',    'handle');

    %---Handle to Frame used to edit these preferences
     schema.prop(c,'EditorFrame',           'mxArray');

    %---Listeners
     schema.prop(c,'Listeners',             'mxArray');

    %---Preference file version control
     schema.prop(c,'Version',               'mxArray');

else
   %---Create class instance
    h = cstprefs.viewprefs;

   %---Get a copy of the toolbox preferences
    h.ToolboxPreferences    = cstprefs.tbxprefs;

   %---Copy relevent toolbox preferences to viewer
    h.FrequencyUnits        = h.ToolboxPreferences.FrequencyUnits;
    h.FrequencyScale        = h.ToolboxPreferences.FrequencyScale;
    h.MagnitudeUnits        = h.ToolboxPreferences.MagnitudeUnits;
    h.MagnitudeScale        = h.ToolboxPreferences.MagnitudeScale;
    h.PhaseUnits            = h.ToolboxPreferences.PhaseUnits;
    h.Grid                  = h.ToolboxPreferences.Grid;
    h.TitleFontSize         = h.ToolboxPreferences.TitleFontSize;
    h.TitleFontWeight       = h.ToolboxPreferences.TitleFontWeight;
    h.TitleFontAngle        = h.ToolboxPreferences.TitleFontAngle;
    h.XYLabelsFontSize      = h.ToolboxPreferences.XYLabelsFontSize;
    h.XYLabelsFontWeight    = h.ToolboxPreferences.XYLabelsFontWeight;
    h.XYLabelsFontAngle     = h.ToolboxPreferences.XYLabelsFontAngle;
    h.AxesFontSize          = h.ToolboxPreferences.AxesFontSize;
    h.AxesFontWeight        = h.ToolboxPreferences.AxesFontWeight;
    h.AxesFontAngle         = h.ToolboxPreferences.AxesFontAngle;
    h.IOLabelsFontSize      = h.ToolboxPreferences.IOLabelsFontSize;
    h.IOLabelsFontWeight    = h.ToolboxPreferences.IOLabelsFontWeight;
    h.IOLabelsFontAngle     = h.ToolboxPreferences.IOLabelsFontAngle;
    h.AxesForegroundColor   = h.ToolboxPreferences.AxesForegroundColor;
    h.SettlingTimeThreshold = h.ToolboxPreferences.SettlingTimeThreshold;
    h.RiseTimeLimits        = h.ToolboxPreferences.RiseTimeLimits;
    h.UnwrapPhase           = h.ToolboxPreferences.UnwrapPhase;
    h.TimeVectorMode        = 'auto';
    h.TimeVector            = [];
    h.FrequencyVectorMode   = 'auto';
    h.FrequencyVector       = [];
    h.Target                = Target;
    h.EditorFrame           = [];
    h.Version               = h.ToolboxPreferences.Version;

   %---Install listeners
    CLS = findclass(findpackage('cstprefs'),'viewprefs');
    EventType = 'PropertyPostSet';
    Callback = @localApply;
    Listeners(1)  = handle.listener(h,CLS.findprop('FrequencyUnits'),       EventType,Callback);
    Listeners(2)  = handle.listener(h,CLS.findprop('FrequencyScale'),       EventType,Callback);
    Listeners(3)  = handle.listener(h,CLS.findprop('MagnitudeUnits'),       EventType,Callback);
    Listeners(4)  = handle.listener(h,CLS.findprop('MagnitudeScale'),       EventType,Callback);
    Listeners(5)  = handle.listener(h,CLS.findprop('PhaseUnits'),           EventType,Callback);
    Listeners(6)  = handle.listener(h,CLS.findprop('Grid'),                 EventType,Callback);
    Listeners(7)  = handle.listener(h,CLS.findprop('TitleFontSize'),        EventType,Callback);
    Listeners(8)  = handle.listener(h,CLS.findprop('TitleFontWeight'),      EventType,Callback);
    Listeners(9)  = handle.listener(h,CLS.findprop('TitleFontAngle'),       EventType,Callback);
    Listeners(10) = handle.listener(h,CLS.findprop('XYLabelsFontSize'),     EventType,Callback);
    Listeners(11) = handle.listener(h,CLS.findprop('XYLabelsFontWeight'),   EventType,Callback);
    Listeners(12) = handle.listener(h,CLS.findprop('XYLabelsFontAngle'),    EventType,Callback);
    Listeners(13) = handle.listener(h,CLS.findprop('AxesFontSize'),         EventType,Callback);
    Listeners(14) = handle.listener(h,CLS.findprop('AxesFontWeight'),       EventType,Callback);
    Listeners(15) = handle.listener(h,CLS.findprop('AxesFontAngle'),        EventType,Callback);
    Listeners(16) = handle.listener(h,CLS.findprop('IOLabelsFontSize'),     EventType,Callback);
    Listeners(17) = handle.listener(h,CLS.findprop('IOLabelsFontWeight'),   EventType,Callback);
    Listeners(18) = handle.listener(h,CLS.findprop('IOLabelsFontAngle'),    EventType,Callback);
    Listeners(19) = handle.listener(h,CLS.findprop('AxesForegroundColor'),  EventType,Callback);
    Listeners(20) = handle.listener(h,CLS.findprop('SettlingTimeThreshold'),EventType,Callback);
    Listeners(21) = handle.listener(h,CLS.findprop('RiseTimeLimits'),       EventType,Callback);
    Listeners(22) = handle.listener(h,CLS.findprop('UnwrapPhase'),          EventType,Callback);
    Listeners(23) = handle.listener(h,CLS.findprop('TimeVectorMode'),       EventType,Callback);
    Listeners(24) = handle.listener(h,CLS.findprop('TimeVector'),           EventType,Callback);
    Listeners(25) = handle.listener(h,CLS.findprop('FrequencyVectorMode'),  EventType,Callback);
    Listeners(26) = handle.listener(h,CLS.findprop('FrequencyVector'),      EventType,Callback);
    h.Listeners = Listeners;
end


%%%%%%%%%%%%%%
% localApply %
%%%%%%%%%%%%%%
function localApply(eventSrc,eventData)
% Apply viewer preference changes to response plots

%---Get viewer and response handles
 ViewerObj = get(eventData.AffectedObject.Target,'UserData');
 RespObjs = get(get(ViewerObj,'UIContextMenu'),{'UserData'});

%---Update response objects
 Property = eventSrc.Name;
 NewValue = eventData.NewValue;
 if findstr('Font',Property)
    idx = findstr('Font',Property);
    head = Property(1:idx-1);
    foot = Property(idx:end);
    if strcmpi(head,'xylabels')
       head1 = 'xlabel';
       head2 = 'ylabel';
       for n=1:length(RespObjs)
          setfont(RespObjs{n},head1,foot,NewValue);
          setfont(RespObjs{n},head2,foot,NewValue);
       end
    else
       for n=1:length(RespObjs)
          setfont(RespObjs{n},head,foot,NewValue);
       end
    end
 elseif findstr('Scale',Property)
    for n=1:length(RespObjs)
       setscale(RespObjs{n},Property,NewValue);
    end
 elseif strcmp('AxesForegroundColor',Property)
    for n=1:length(RespObjs)
       setcolor(RespObjs{n},Property,NewValue);
    end
 elseif findstr('TimeVector',Property)
    %---Apply callback will call plotswitch when all property values are set
    status(ViewerObj,'Time vector modified.',1);
 elseif findstr('FrequencyVector',Property)
    %---Apply callback will call plotswitch when all property values are set
    status(ViewerObj,'Frequency vector modified.',1);
 else
    for n=1:length(RespObjs)
       try
          set(RespObjs{n},Property,NewValue);
       end
    end
 end
