function h = sisoprefs(Target)
%SISOPREFS  SISO Tool preferences object contructor

%   Author(s): A. DiVergilio
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.11 $  $Date: 2000/08/29 14:39:43 $

a = findpackage('cstprefs');

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

   %---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');

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

    %---Siso Tool Options
     schema.prop(c,'CompensatorFormat',     'String');
     schema.prop(c,'ShowSystemPZ',          'String');
     schema.prop(c,'LineStyle',             'mxArray');

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

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

    %---UI Preferences
     schema.prop(c,'UIFontSize',            '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');

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

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

   %---Copy relevent toolbox preferences
    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.AxesForegroundColor   = h.ToolboxPreferences.AxesForegroundColor;
    h.CompensatorFormat     = h.ToolboxPreferences.CompensatorFormat;
    h.ShowSystemPZ          = h.ToolboxPreferences.ShowSystemPZ;
    h.LineStyle             = h.ToolboxPreferences.SISOToolStyle;
    h.UnwrapPhase           = h.ToolboxPreferences.UnwrapPhase;
    h.Target                = Target;
    h.UIFontSize            = h.ToolboxPreferences.UIFontSize;
    h.EditorFrame           = [];

   %---Install listeners
    p(1) = findprop(h,'FrequencyUnits');
    p(2) = findprop(h,'FrequencyScale');
    p(3) = findprop(h,'MagnitudeUnits');
    p(4) = findprop(h,'MagnitudeScale');
    p(5) = findprop(h,'PhaseUnits');
    Listeners(1)  = handle.listener(h,p,'PropertyPostSet',@localSetUnits);
    q(1) = findprop(h,'Grid');
    q(2) = findprop(h,'AxesForegroundColor');
    Listeners(2)  = handle.listener(h,q,'PropertyPostSet',@localSetEditor);
    Listeners(3)  = handle.listener(h,findprop(h,'TitleFontSize'),...
        'PropertyPostSet',{@localSetLabel 'FontSize'});
    Listeners(4)  = handle.listener(h,findprop(h,'TitleFontWeight'),...
        'PropertyPostSet',{@localSetLabel 'FontWeight'});
    Listeners(5)  = handle.listener(h,findprop(h,'TitleFontAngle'),...
        'PropertyPostSet',{@localSetLabel 'FontAngle'});
    Listeners(6) = handle.listener(h,findprop(h,'XYLabelsFontSize'),...
        'PropertyPostSet',{@localSetLabel 'FontSize'});
    Listeners(7) = handle.listener(h,findprop(h,'XYLabelsFontWeight'),...
        'PropertyPostSet',{@localSetLabel 'FontWeight'});
    Listeners(8) = handle.listener(h,findprop(h,'XYLabelsFontAngle'),...
        'PropertyPostSet',{@localSetLabel 'FontAngle'});
    Listeners(9) = handle.listener(h,findprop(h,'AxesFontSize'),...
        'PropertyPostSet',{@localSetTickLabel 'FontSize'});
    Listeners(10) = handle.listener(h,findprop(h,'AxesFontWeight'),...
        'PropertyPostSet',{@localSetTickLabel 'FontWeight'});
    Listeners(11) = handle.listener(h,findprop(h,'AxesFontAngle'),...
        'PropertyPostSet',{@localSetTickLabel 'FontAngle'});
    Listeners(12) = handle.listener(h,findprop(h,'CompensatorFormat'),...
        'PropertyPostSet',@localChangeFormat);
    Listeners(13) = handle.listener(h,findprop(h,'ShowSystemPZ'),...
        'PropertyPostSet',@localShowSystemPZ);
    Listeners(14) = handle.listener(h,findprop(h,'LineStyle'),...
        'PropertyPostSet',@localSetEditor);
    hTarget = handle(Target);
    Listeners(15) = handle.listener(hTarget,findprop(hTarget,'CSHelpMode'),...
        'PropertyPostSet',@LocalSwitchMode);
    h.Listeners = Listeners;

end


%-------------------- Listeners callbacks --------------------------------

% Set units (for all editors)
function localSetUnits(eventSrc,eventData)
sisodb = localGetSISOTool(eventData);
Property = eventSrc.Name;
if strcmp(Property,'FrequencyUnits')
    set(sisodb.PlotEditors,Property,eventData.NewValue);
else
    % Bode specific
    set(sisodb.PlotEditors(2),Property,eventData.NewValue);
end


% Set editor property (for all editors)
function localSetEditor(eventSrc,eventData)
sisodb = localGetSISOTool(eventData);
set(sisodb.PlotEditors,eventSrc.Name,eventData.NewValue);


% Set label characteristics
function localSetLabel(eventSrc,eventData,Attribute)
sisodb = localGetSISOTool(eventData);
if strncmpi('title',eventSrc.Name,5)
    % Title related
    for ct=1:length(sisodb.PlotEditors)
        setfont(sisodb.PlotEditors(ct),'title',Attribute,eventData.NewValue);
    end
else
    % XY label related
    for ct=1:length(sisodb.PlotEditors)
        setfont(sisodb.PlotEditors(ct),'xylabel',Attribute,eventData.NewValue);
    end
end    


% Set tick label characteristics
function localSetTickLabel(eventSrc,eventData,Property)
sisodb = localGetSISOTool(eventData);
for ct=1:length(sisodb.PlotEditors)
    setfont(sisodb.PlotEditors(ct),'axes',Property,eventData.NewValue);
end
    

% Set system pole/zero visibility 
function localShowSystemPZ(eventSrc,eventData)
sisodb = localGetSISOTool(eventData);
set(sisodb.PlotEditors(2),'ShowSystemPZ',eventData.NewValue);


% Change compensator format
function localChangeFormat(eventSrc,eventData)
sisodb = localGetSISOTool(eventData);
set(sisodb.LoopData.Compensator,'Format',eventData.NewValue);


% Enable/disable java GUI based on CSHelpMode
function LocalSwitchMode(eventSrc,eventData)
sisodb = eventData.AffectedObject.UserData;
if ~isempty(sisodb.Preferences.EditorFrame)
   if strcmpi(eventData.NewValue,'on')
      set(sisodb.Preferences.EditorFrame,'Enabled','off')
   else
      set(sisodb.Preferences.EditorFrame,'Enabled','on')
   end
end


%-------------------- Utility functions ----------------------------------

function sisodb = localGetSISOTool(eventData)
sisodb = get(eventData.AffectedObject.Target,'UserData');
