function Main = unit_gui(PropObj)
%UNIT_GUI  GUI for editing unit & scale properties of PropObj

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

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

%---Definitions
FL_L  = java.awt.FlowLayout(java.awt.FlowLayout.LEFT,8,0);
GL_12 = java.awt.GridLayout(1,2,8,0);
GL_31 = java.awt.GridLayout(3,1,0,3);

%---Top-level panel (MWGroupbox)
Main = com.mathworks.mwt.MWGroupbox(sprintf('Units'));
Main.setLayout(com.mathworks.mwt.MWBorderLayout(0,0));
Main.setFont(Prefs.JavaFontB);

%---For case of no units to set, display the following string
s.NullString = com.mathworks.mwt.MWLabel(sprintf('(no units available for selected response plot)'));
s.NullString.setFont(Prefs.JavaFontP);
Main.add(s.NullString,com.mathworks.mwt.MWBorderLayout.NORTH);

%---Units panel (west)
s.Units = com.mathworks.mwt.MWPanel(GL_31); Main.add(s.Units,com.mathworks.mwt.MWBorderLayout.WEST);
   %---Frequency units
    s.FrequencyUnitsPanel = com.mathworks.mwt.MWPanel(GL_12); s.Units.add(s.FrequencyUnitsPanel);
    s.FrequencyUnitsLabel = com.mathworks.mwt.MWLabel(sprintf('Frequency in')); s.FrequencyUnitsPanel.add(s.FrequencyUnitsLabel);
    s.FrequencyUnits = com.mathworks.mwt.MWChoice; s.FrequencyUnitsPanel.add(s.FrequencyUnits);
    s.FrequencyUnits.add(sprintf('Hz')); s.FrequencyUnits.add(sprintf('rad/sec'));
    s.FrequencyUnitsLabel.setFont(Prefs.JavaFontP);
    s.FrequencyUnits.setFont(Prefs.JavaFontP);
   %---Magnitude units
    s.MagnitudeUnitsPanel = com.mathworks.mwt.MWPanel(GL_12); s.Units.add(s.MagnitudeUnitsPanel);
    s.MagnitudeUnitsLabel = com.mathworks.mwt.MWLabel(sprintf('Magnitude in')); s.MagnitudeUnitsPanel.add(s.MagnitudeUnitsLabel);
    s.MagnitudeUnits = com.mathworks.mwt.MWChoice; s.MagnitudeUnitsPanel.add(s.MagnitudeUnits);
    s.MagnitudeUnits.add(sprintf('dB')); s.MagnitudeUnits.add(sprintf('absolute'));
    s.MagnitudeUnitsLabel.setFont(Prefs.JavaFontP);
    s.MagnitudeUnits.setFont(Prefs.JavaFontP);
   %---Phase units
    s.PhaseUnitsPanel = com.mathworks.mwt.MWPanel(GL_12); s.Units.add(s.PhaseUnitsPanel);
    s.PhaseUnitsLabel = com.mathworks.mwt.MWLabel(sprintf('Phase in')); s.PhaseUnitsPanel.add(s.PhaseUnitsLabel);
    s.PhaseUnits = com.mathworks.mwt.MWChoice; s.PhaseUnitsPanel.add(s.PhaseUnits);
    s.PhaseUnits.add(sprintf('degrees')); s.PhaseUnits.add(sprintf('radians'));
    s.PhaseUnitsLabel.setFont(Prefs.JavaFontP);
    s.PhaseUnits.setFont(Prefs.JavaFontP);

%---Scale panel (center)
s.Scale = com.mathworks.mwt.MWPanel(GL_31); Main.add(s.Scale,com.mathworks.mwt.MWBorderLayout.CENTER);
   %---Frequency scale
    s.FrequencyScalePanel = com.mathworks.mwt.MWPanel(FL_L); s.Scale.add(s.FrequencyScalePanel);
    s.FrequencyScaleLabel = com.mathworks.mwt.MWLabel(sprintf('using')); s.FrequencyScalePanel.add(s.FrequencyScaleLabel);
    s.FrequencyScale = com.mathworks.mwt.MWChoice; s.FrequencyScalePanel.add(s.FrequencyScale);
    s.FrequencyScale.add(sprintf('linear scale')); s.FrequencyScale.add(sprintf('log scale'));
    s.FrequencyScaleLabel.setFont(Prefs.JavaFontP);
    s.FrequencyScale.setFont(Prefs.JavaFontP);
   %---Magnitude scale
    s.MagnitudeScalePanel = com.mathworks.mwt.MWPanel(FL_L); s.Scale.add(s.MagnitudeScalePanel);
    s.MagnitudeScaleLabel = com.mathworks.mwt.MWLabel(sprintf('using')); s.MagnitudeScalePanel.add(s.MagnitudeScaleLabel);
    s.MagnitudeScale = com.mathworks.mwt.MWChoice; s.MagnitudeScalePanel.add(s.MagnitudeScale);
    s.MagnitudeScale.add(sprintf('linear scale')); s.MagnitudeScale.add(sprintf('log scale'));
    s.MagnitudeScaleLabel.setFont(Prefs.JavaFontP);
    s.MagnitudeScale.setFont(Prefs.JavaFontP);

%---Install PropObj listeners and GUI callbacks
cstP = findpackage('cstprefs');
resppropC = cstP.findclass('respprop');
EventType = 'PropertyPostSet';
PointOfAttachment = PropObj;
Callback = {@localReadProp,s};
GUICallback = {@localWriteProp,PropObj};
   %---Frequency Units
    EventSrc = resppropC.findprop('FrequencyUnits');
    s.FrequencyUnitsListener = handle.listener(PointOfAttachment,EventSrc,EventType,Callback);
    set(s.FrequencyUnits,'Name','FrequencyUnits','ItemStateChangedCallback',GUICallback);
   %---Magnitude Units
    EventSrc = resppropC.findprop('MagnitudeUnits');
    s.MagnitudeUnitsListener = handle.listener(PointOfAttachment,EventSrc,EventType,Callback);
    set(s.MagnitudeUnits,'Name','MagnitudeUnits','ItemStateChangedCallback',GUICallback);
   %---Phase Units
    EventSrc = resppropC.findprop('PhaseUnits');
    s.PhaseUnitsListener = handle.listener(PointOfAttachment,EventSrc,EventType,Callback);
    set(s.PhaseUnits,'Name','PhaseUnits','ItemStateChangedCallback',GUICallback);
   %---Frequency Scale
    EventSrc = resppropC.findprop('FrequencyScale');
    s.FrequencyScaleListener = handle.listener(PointOfAttachment,EventSrc,EventType,Callback);
    set(s.FrequencyScale,'Name','FrequencyScale','ItemStateChangedCallback',GUICallback);
   %---Magnitude Scale
    EventSrc = resppropC.findprop('MagnitudeScale');
    s.MagnitudeScaleListener = handle.listener(PointOfAttachment,EventSrc,EventType,Callback);
    set(s.MagnitudeScale,'Name','MagnitudeScale','ItemStateChangedCallback',GUICallback);

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


%%%%%%%%%%%%%%%%%
% localReadProp %
%%%%%%%%%%%%%%%%%
function localReadProp(eventSrc,eventData,s)
% Update GUI when property object changes
if eventData.AffectedObject.ListenerEnable
   Name = eventSrc.Name;
   NewValue = eventData.NewValue;
   %---Set GUI state
   switch Name
   case 'FrequencyUnits'
      if isempty(NewValue)
         s.Units.remove(s.FrequencyUnitsPanel);
         s.Scale.remove(s.FrequencyScalePanel);
         GL = java.awt.GridLayout(s.Units.getComponentCount,1,0,3);
         s.Units.setLayout(GL);
         s.Scale.setLayout(GL);
      else
         if isempty(s.FrequencyUnitsPanel.getParent)
            GL = java.awt.GridLayout(s.Units.getComponentCount+1,1,0,3);
            s.Units.setLayout(GL);
            s.Scale.setLayout(GL);
            s.Units.add(s.FrequencyUnitsPanel);
            s.Scale.add(s.FrequencyScalePanel);
         end
         if strcmpi(eventData.NewValue(1),'h')
            s.FrequencyUnits.select(0);
         else
            s.FrequencyUnits.select(1);
         end
      end
   case 'MagnitudeUnits'
      if isempty(NewValue)
         s.Units.remove(s.MagnitudeUnitsPanel);
         s.Scale.remove(s.MagnitudeScalePanel);
         GL = java.awt.GridLayout(s.Units.getComponentCount,1,0,3);
         s.Units.setLayout(GL);
         s.Scale.setLayout(GL);
      else
         if isempty(s.MagnitudeUnitsPanel.getParent)
            GL = java.awt.GridLayout(s.Units.getComponentCount+1,1,0,3);
            s.Units.setLayout(GL);
            s.Scale.setLayout(GL);
            s.Units.add(s.MagnitudeUnitsPanel);
            s.Scale.add(s.MagnitudeScalePanel);
         end
         PropObj = eventData.AffectedObject;
         if strcmpi(eventData.NewValue(1),'d')
            s.MagnitudeUnits.select(0);
            if ~isempty(PropObj.MagnitudeScale)
               s.MagnitudeScalePanel.setVisible(0);
            end
         else
            s.MagnitudeUnits.select(1);
            if ~isempty(PropObj.MagnitudeScale)
               s.MagnitudeScalePanel.setVisible(1);
            end
         end
      end
   case 'PhaseUnits'
      if isempty(NewValue)
         s.Units.remove(s.PhaseUnitsPanel);
         GL = java.awt.GridLayout(s.Units.getComponentCount,1,0,3);
         s.Units.setLayout(GL);
         s.Scale.setLayout(GL);
      else
         if isempty(s.PhaseUnitsPanel.getParent)
            GL = java.awt.GridLayout(s.Units.getComponentCount+1,1,0,3);
            s.Units.setLayout(GL);
            s.Scale.setLayout(GL);
            s.Units.add(s.PhaseUnitsPanel);
         end
         if strcmpi(eventData.NewValue(1),'d')
            s.PhaseUnits.select(0);
         else
            s.PhaseUnits.select(1);
         end
      end
   case 'FrequencyScale'
      if isempty(NewValue)
         s.FrequencyScalePanel.setVisible(0);
      else
         s.FrequencyScalePanel.setVisible(1);
         if strcmpi(eventData.NewValue,'linear')
            s.FrequencyScale.select(0);
         else
            s.FrequencyScale.select(1);
         end
      end
   case 'MagnitudeScale'
      if isempty(NewValue)
         s.MagnitudeScalePanel.setVisible(0);
      else
         PropObj = eventData.AffectedObject;
         if strcmpi(PropObj.MagnitudeUnits(1),'d')
            s.MagnitudeScalePanel.setVisible(0);
         else
            s.MagnitudeScalePanel.setVisible(1);
         end
         if strcmpi(eventData.NewValue,'linear')
            s.MagnitudeScale.select(0);
         else
            s.MagnitudeScale.select(1);
         end
      end
   end
   %---Display string if no units are available
   if s.Units.getComponentCount==0
      s.NullString.setVisible(1);
   else
      s.NullString.setVisible(0);
   end
end

%%%%%%%%%%%%%%%%%%
% localWriteProp %
%%%%%%%%%%%%%%%%%%
function localWriteProp(eventSrc,eventData,PropObj)
% Update property object when GUI changes
PropObj.ListenerEnable = 1;  %---Keep PropObj listener enabled here !!!
Name = get(eventSrc,'Name');
switch Name
case {'FrequencyUnits'}
   if get(eventSrc,'SelectedIndex')==0
      Value = 'Hz';
   else
      Value = 'rad/sec';
   end
case {'MagnitudeUnits'}
   if get(eventSrc,'SelectedIndex')==0
      Value = 'dB';
   else
      Value = 'abs';
   end
case {'PhaseUnits'}
   if get(eventSrc,'SelectedIndex')==0
      Value = 'deg';
   else
      Value = 'rad';
   end
case {'FrequencyScale','MagnitudeScale'}
   if get(eventSrc,'SelectedIndex')==0
      Value = 'linear';
   else
      Value = 'log';
   end
end
set(PropObj,Name,Value);
PropObj.LastChange = Name;
