function GUI = grid_gui(h)
%GRID_GUI  GUI for editing grid properties of h

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

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

%---Top-level panel (MWGroupbox)
 s.Main = com.mathworks.mwt.MWGroupbox(sprintf('Grid'));
 s.Main.setLayout(java.awt.GridLayout(2,1,0,3));
 s.Main.setFont(Prefs.JavaFontB);
   
%---Checkbox to toggle grid visibility
 s.Grid = com.mathworks.mwt.MWCheckbox(sprintf('Show grid'));
  s.Grid.setFont(Prefs.JavaFontP);
  s.Main.add(s.Grid);
 s.GridLabelType = com.mathworks.mwt.MWCheckbox(sprintf('Display damping values as %% peak overshoot'));
  s.GridLabelType.setFont(Prefs.JavaFontP);
  s.Main.add(s.GridLabelType);

%---Install GUI callbacks
 GUICallback = {@localWriteProp,h};
 set(s.Grid,         'Name','Grid',         'ItemStateChangedCallback',GUICallback);
 set(s.GridLabelType,'Name','GridLabelType','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,GUI1,GUI2)
% Target -> GUI
 h = cstprefs.sisoprops;
 if h.ListenerEnable
    switch eventSrc.Name
    case 'Grid'
       GridOn = strcmpi(eventData.NewValue,'on');
       GUI1.setState(GridOn);
       GUI2.setEnabled(GridOn);
    case 'GridOptions'
       GUI2.setState(strcmpi(eventData.NewValue.GridLabelType,'overshoot'));
    end
 end


%%%%%%%%%%%%%%%%%%
% localWriteProp %
%%%%%%%%%%%%%%%%%%
function localWriteProp(eventSrc,eventData,h)
% GUI -> Target
 switch get(eventSrc,'Name')
 case 'Grid'
    h.Target.Grid = get(eventSrc,'State');
 case 'GridLabelType'
    GridOptions = h.Target.GridOptions;
    if strcmpi(get(eventSrc,'State'),'on')
       GridOptions.GridLabelType = 'overshoot';
    else
       GridOptions.GridLabelType = 'damping';
    end
    h.Target.GridOptions = GridOptions;
 end


%%%%%%%%%%%%%%%%%%%%%
% 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.grapheditor')
     s.GridListener    = handle.listener(t,findprop(t,'Grid'),'PropertyPostSet',{@localReadProp,s.Grid,s.GridLabelType});
     s.OptionsListener = handle.listener(t,findprop(t,'GridOptions'),'PropertyPostSet',{@localReadProp,s.Grid,s.GridLabelType});
     %---Sync
      GridOn = strcmpi(t.Grid,'on');
      s.Grid.setState(GridOn);
      s.GridLabelType.setEnabled(GridOn);
     %---Grid type for rleditor
      if isa(t,'sisogui.rleditor')
         if isempty(s.GridLabelType.getParent)
            GL = java.awt.GridLayout(2,1,0,3);
            s.Main.setLayout(GL); s.Main.add(s.GridLabelType);
         end
         s.GridLabelType.setState(strcmpi(t.GridOptions.GridLabelType,'overshoot'));
      else
         if ~isempty(s.GridLabelType.getParent)
            GL = java.awt.GridLayout(1,1,0,3);
            s.Main.remove(s.GridLabelType); s.Main.setLayout(GL);
         end
      end
  else
     %---Clear
      s.GridListener = [];
      s.OptionsListener = [];
  end
 %---Save structure
  set(eventSrc,'UserData',s);
