function Main = font_gui(PropObj)
%FONT_GUI  GUI for editing font properties of PropObj

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

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

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

%---Add a font panel for each text group
s.Row1 = localFontPanel('Title',   sprintf('Title:'),      PropObj);  Main.add(s.Row1);
s.Row2 = localFontPanel('XYLabels',sprintf('X/Y-Labels:'), PropObj);  Main.add(s.Row2);
s.Row3 = localFontPanel('Axes',    sprintf('Tick Labels:'),PropObj);  Main.add(s.Row3);
s.Row4 = localFontPanel('IOLabels',sprintf('I/O-Names:'),  PropObj);  Main.add(s.Row4);

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


%%%%%%%%%%%%%%%%%%
% localFontPanel %
%%%%%%%%%%%%%%%%%%
function Panel = localFontPanel(PropName,Label,PropObj)
% Create a java panel for editing font properties

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

%---Build GUI
Panel = com.mathworks.mwt.MWPanel(com.mathworks.mwt.MWBorderLayout(0,0));
s.Label = com.mathworks.mwt.MWLabel(sprintf('%s',Label),com.mathworks.mwt.MWLabel.LEFT);
   s.Label.setFont(Prefs.JavaFontP);
   Panel.add(s.Label,com.mathworks.mwt.MWBorderLayout.WEST);
s.East = com.mathworks.mwt.MWPanel(java.awt.FlowLayout(java.awt.FlowLayout.RIGHT,15,0));
   Panel.add(s.East,com.mathworks.mwt.MWBorderLayout.EAST);
   s.Size = com.mathworks.mwt.MWChoice; s.East.add(s.Size);
   s.Size.setFont(Prefs.JavaFontP);
   for n=8:2:16
      s.Size.add(sprintf('%d pt',n));
   end
   s.Weight = com.mathworks.mwt.MWCheckbox(sprintf('Bold')); s.East.add(s.Weight);
      s.Weight.setFont(Prefs.JavaFontB);
   s.Angle = com.mathworks.mwt.MWCheckbox(sprintf('Italic')); s.East.add(s.Angle);
      s.Angle.setFont(Prefs.JavaFontI);

%---Install PropObj listeners and GUI callbacks
cstP = findpackage('cstprefs');
resppropC = cstP.findclass('respprop');
EventType = 'PropertyPostSet';
PointOfAttachment = PropObj;
   %---FontSize listener
    Property = [PropName 'FontSize'];
    EventSrc = resppropC.findprop(Property);
    Callback = {@localReadProp,s.Size};
    s.SizeListener = handle.listener(PointOfAttachment,EventSrc,EventType,Callback);
   %---FontSize callback
    Callback = {@localWriteProp,PropObj,Property};
    set(s.Size,'Name','FontSize','ItemStateChangedCallback',Callback);
   %---FontWeight listener
    Property = [PropName 'FontWeight'];
    EventSrc = resppropC.findprop(Property);
    Callback = {@localReadProp,s.Weight};
    s.WeightListener = handle.listener(PointOfAttachment,EventSrc,EventType,Callback);
   %---FontWeight callback
    Callback = {@localWriteProp,PropObj,Property};
    set(s.Weight,'Name','FontWeight','ItemStateChangedCallback',Callback);
   %---FontAngle listener
    Property = [PropName 'FontAngle'];
    EventSrc = resppropC.findprop(Property);
    Callback = {@localReadProp,s.Angle};
    s.AngleListener = handle.listener(PointOfAttachment,EventSrc,EventType,Callback);
   %---FontAngle callback
    Callback = {@localWriteProp,PropObj,Property};
    set(s.Angle,'Name','FontAngle','ItemStateChangedCallback',Callback);

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


%%%%%%%%%%%%%%%%%
% localReadProp %
%%%%%%%%%%%%%%%%%
function localReadProp(eventSrc,eventData,GUI)
% Update GUI when property object changes
if eventData.AffectedObject.ListenerEnable
   switch char(GUI.getName)
   case 'FontSize'
      GUI.select((eventData.NewValue-8)/2);
   case 'FontWeight'
      GUI.setState(strcmpi(eventData.NewValue,'bold'));
   case 'FontAngle'
      GUI.setState(strcmpi(eventData.NewValue,'italic'));
   end
end


%%%%%%%%%%%%%%%%%%
% localWriteProp %
%%%%%%%%%%%%%%%%%%
function localWriteProp(eventSrc,eventData,PropObj,Property)
% Update property object when GUI changes
PropObj.ListenerEnable = 0;  %---Temporarily disable PropObj listeners
switch get(eventSrc,'Name')
case 'FontSize'
   Value = 8 + 2*get(eventSrc,'SelectedIndex');
case 'FontWeight'
   if strcmpi(get(eventSrc,'State'),'on')
      Value = 'bold';
   else
      Value = 'normal';
   end
case 'FontAngle'
   if strcmpi(get(eventSrc,'State'),'on')
      Value = 'italic';
   else
      Value = 'normal';
   end
end
set(PropObj,Property,Value);
PropObj.LastChange = Property;
PropObj.ListenerEnable = 1;  %---Re-enable PropObj listeners
