function Main = 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:05:51 $

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

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

%---Checkbox to toggle grid visibility
 s.Grid = com.mathworks.mwt.MWCheckbox(sprintf('Show grid'));
  s.Grid.setFont(Prefs.JavaFontP);
  Main.add(s.Grid);
 s.GridLabelType = com.mathworks.mwt.MWCheckbox(sprintf('Display damping values as %% peak overshoot'));
  s.GridLabelType.setFont(Prefs.JavaFontP);
 Main.add(s.GridLabelType);

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

%---Install listners
 Callback = {@localReadProp,s.Grid};
 s.GridListener = handle.listener(h,findprop(h,'Grid'),'PropertyPostSet',{@localReadProp,s.Grid,s.GridLabelType});
 s.DampListener = handle.listener(h,findprop(h,'GridLabelType'),'PropertyPostSet',{@localReadProp,s.Grid,s.GridLabelType});

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


%%%%%%%%%%%%%%%%%
% localReadProp %
%%%%%%%%%%%%%%%%%
function localReadProp(eventSrc,eventData,GUI1,GUI2)
% Target -> GUI
if eventData.AffectedObject.ListenerEnable
   switch eventSrc.Name
   case 'Grid'
      GUI1.setState(strcmpi(eventData.NewValue,'on'));
   case 'GridLabelType'
      if isempty(eventData.NewValue)
         GL = java.awt.GridLayout(1,1,0,3);
         Main = GUI1.getParent;
         Main.remove(GUI2);
         Main.setLayout(GL);
      else
         if isempty(GUI2.getParent)
            GL = java.awt.GridLayout(2,1,0,3);
            Main = GUI1.getParent;
            Main.setLayout(GL);
            Main.add(GUI2);
         end
         GUI2.setState(strcmpi(eventData.NewValue,'overshoot'));
      end
   end
   if strcmpi(get(GUI1,'State'),'on')
      GUI2.setEnabled(1);
   else
      GUI2.setEnabled(0);
   end
end


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