function GUI = mp_gui(h)
%MP_GUI  GUI for editing mag/phase visibility of h

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

%---Get Toolbox Preferences
 Prefs = cstprefs.tbxprefs;

%---Top-level panel (MWGroupbox)
 s.Main = com.mathworks.mwt.MWGroupbox(sprintf('Magnitude / Phase'));
 s.Main.setLayout(java.awt.GridLayout(3,1,0,3));
 s.Main.setFont(Prefs.JavaFontB);
 s.Main.setVisible(0);

%---Checkboxes
 GRP = com.mathworks.mwt.MWExclusiveGroup;
 s.MagPhase = com.mathworks.mwt.MWCheckbox(sprintf('Show magnitude & phase'),GRP,1);
  s.MagPhase.setFont(Prefs.JavaFontP);
  s.Main.add(s.MagPhase);
 s.MagOnly = com.mathworks.mwt.MWCheckbox(sprintf('Show magnitude only'),GRP,0);
  s.MagOnly.setFont(Prefs.JavaFontP);
  s.Main.add(s.MagOnly);
 s.PhaseOnly = com.mathworks.mwt.MWCheckbox(sprintf('Show phase only'),GRP,0);
  s.PhaseOnly.setFont(Prefs.JavaFontP);
  s.Main.add(s.PhaseOnly);

%---Install GUI callbacks
 GUICallback = {@localWriteProp,h};
 set(s.MagPhase, 'Name','MagPhase', 'ItemStateChangedCallback',GUICallback);
 set(s.MagOnly,  'Name','MagOnly',  'ItemStateChangedCallback',GUICallback);
 set(s.PhaseOnly,'Name','PhaseOnly','ItemStateChangedCallback',GUICallback);

%---Install listner for target change
 s.TargetListener = handle.listener(h,findprop(h,'Target'),'PropertyPreSet',@localTargetPreSet);
 s.TargetListener.CallbackTarget = s.Main;

%---Store java handles
 set(s.Main,'UserData',s);

%---Return handle of top-level GUI
 GUI = s.Main;


%%%%%%%%%%%%%%%%%
% localReadProp %
%%%%%%%%%%%%%%%%%
function localReadProp(eventSrc,eventData,GUI)
% Target -> GUI
 h = cstprefs.sisoprops;
 if h.ListenerEnable
    s = get(GUI,'UserData');
    t = eventData.AffectedObject;
    if strcmpi(t.MagVisible,'on') & strcmpi(t.PhaseVisible,'on')
       s.MagPhase.setState(1);
    elseif strcmpi(t.MagVisible,'on')
       s.MagOnly.setState(1);
    else
       s.PhaseOnly.setState(1);
    end
 end


%%%%%%%%%%%%%%%%%%
% localWriteProp %
%%%%%%%%%%%%%%%%%%
function localWriteProp(eventSrc,eventData,h)
% GUI -> Target
 h.ListenerEnable = 0;
 Name = get(eventSrc,'Name');
 switch Name
 case 'MagPhase'
    h.Target.MagVisible = 'on';
    h.Target.PhaseVisible = 'on';
    h.Target.up.Status = 'The magnitude & phase plots are now visible.';
 case 'MagOnly'
    h.Target.MagVisible = 'on';
    h.Target.PhaseVisible = 'off';
    h.Target.up.Status = 'The phase plot is now hidden.';
 case 'PhaseOnly'
    h.Target.MagVisible = 'off';
    h.Target.PhaseVisible = 'on';
    h.Target.up.Status = 'The magnitude plot is now hidden.';
 end
 h.ListenerEnable = 1;


%%%%%%%%%%%%%%%%%%%%%
% localTargetPreSet %
%%%%%%%%%%%%%%%%%%%%%
function localTargetPreSet(eventSrc,eventData)
% Update listeners when Target changes
 %---New target
  t = eventData.NewValue;
 %---Get structure
  s = get(eventSrc,'UserData');
 %---Update listener
  if isa(t,'sisogui.bodeditor')
     s.MagListener   = handle.listener(t,findprop(t,'MagVisible'),  'PropertyPostSet',{@localReadProp,s.Main});
     s.PhaseListener = handle.listener(t,findprop(t,'PhaseVisible'),'PropertyPostSet',{@localReadProp,s.Main});
     %---Sync
      if strcmpi(t.MagVisible,'on') & strcmpi(t.PhaseVisible,'on') 
         s.MagPhase.setState(1);
      elseif strcmpi(t.MagVisible,'on')
         s.MagOnly.setState(1);
      else
         s.PhaseOnly.setState(1);
      end
      s.Main.setVisible(1);
  else
     %---Clear
      s.MagListener = [];
      s.PhaseListener = [];
      s.Main.setVisible(0);
  end
 %---Save structure
  set(eventSrc,'UserData',s);
