function Main = text_gui(PropObj)
%TEXT_GUI  GUI for editing text properties of PropObj

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

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

%---Definitions
LEFT  = com.mathworks.mwt.MWLabel.LEFT;
GL_31 = java.awt.GridLayout(3,1,0,3);

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

%---West panel (labels)
s.W = com.mathworks.mwt.MWPanel(GL_31);
Main.add(s.W,com.mathworks.mwt.MWBorderLayout.WEST);
   %---Labels
   s.LTP = com.mathworks.mwt.MWPanel(com.mathworks.mwt.MWBorderLayout(0,0)); s.W.add(s.LTP);
   s.LXP = com.mathworks.mwt.MWPanel(com.mathworks.mwt.MWBorderLayout(0,0)); s.W.add(s.LXP);
   s.LYP = com.mathworks.mwt.MWPanel(com.mathworks.mwt.MWBorderLayout(0,0)); s.W.add(s.LYP);
   s.LT = com.mathworks.mwt.MWLabel(sprintf('Title:'),  LEFT); s.LTP.add(s.LT,com.mathworks.mwt.MWBorderLayout.NORTH);
   s.LX = com.mathworks.mwt.MWLabel(sprintf('X-Label:'),LEFT); s.LXP.add(s.LX,com.mathworks.mwt.MWBorderLayout.NORTH);
   s.LY = com.mathworks.mwt.MWLabel(sprintf('Y-Label:'),LEFT); s.LYP.add(s.LY,com.mathworks.mwt.MWBorderLayout.NORTH);
   s.LT.setFont(Prefs.JavaFontP);
   s.LX.setFont(Prefs.JavaFontP);
   s.LY.setFont(Prefs.JavaFontP);

%---Center panel (textfields)
s.C = com.mathworks.mwt.MWPanel(GL_31);
Main.add(s.C,com.mathworks.mwt.MWBorderLayout.CENTER);
   %---TextFields
   s.Title  = com.mathworks.mwt.MWTextArea; s.C.add(s.Title);  s.Title.setRows(3);
      s.Title.setHScrollStyle(com.mathworks.mwt.MWTextArea.SCROLLBAR_NEVER);
      s.Title.setVScrollStyle(com.mathworks.mwt.MWTextArea.SCROLLBAR_NEVER);
      s.Title.setFont(Prefs.JavaFontP);
   s.Xlabel = com.mathworks.mwt.MWTextArea; s.C.add(s.Xlabel); s.Xlabel.setRows(3);
      s.Xlabel.setHScrollStyle(com.mathworks.mwt.MWTextArea.SCROLLBAR_NEVER);
      s.Xlabel.setVScrollStyle(com.mathworks.mwt.MWTextArea.SCROLLBAR_NEVER);
      s.Xlabel.setFont(Prefs.JavaFontP);
   s.Ylabel = com.mathworks.mwt.MWTextArea; s.C.add(s.Ylabel); s.Ylabel.setRows(3);
      s.Ylabel.setHScrollStyle(com.mathworks.mwt.MWTextArea.SCROLLBAR_NEVER);
      s.Ylabel.setVScrollStyle(com.mathworks.mwt.MWTextArea.SCROLLBAR_NEVER);
      s.Ylabel.setFont(Prefs.JavaFontP);

%---Install PropObj listeners
cstP = findpackage('cstprefs');
resppropC = cstP.findclass('respprop');
EventType = 'PropertyPostSet';
PointOfAttachment = PropObj;
GUICallback = {@localWriteProp,PropObj};
   %---Title
    EventSrc = resppropC.findprop('Title');
    Callback = {@localReadProp,s.Title};
    s.TitleListener = handle.listener(PointOfAttachment,EventSrc,EventType,Callback);
    set(s.Title,'Name','Title','TextValueChangedCallback',GUICallback);
   %---Xlabel
    EventSrc = resppropC.findprop('Xlabel');
    Callback = {@localReadProp,s.Xlabel};
    s.XlabelListener = handle.listener(PointOfAttachment,EventSrc,EventType,Callback);
    set(s.Xlabel,'Name','Xlabel','TextValueChangedCallback',GUICallback);
   %---Ylabel
    EventSrc = resppropC.findprop('Ylabel');
    Callback = {@localReadProp,s.Ylabel};
    s.YlabelListener = handle.listener(PointOfAttachment,EventSrc,EventType,Callback);
    set(s.Ylabel,'Name','Ylabel','TextValueChangedCallback',GUICallback);

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


%%%%%%%%%%%%%%%%%
% localReadProp %
%%%%%%%%%%%%%%%%%
function localReadProp(eventSrc,eventData,GUI)
% Update GUI when property object changes
if eventData.AffectedObject.ListenerEnable
   %---Fix carriage return for pc
    if ispc, txt = strrep(eventData.NewValue,sprintf('\n'),sprintf('\r\n'));
    else,    txt = eventData.NewValue;
    end
   %---Update GUI
    GUI.setText(txt);
end


%%%%%%%%%%%%%%%%%%
% localWriteProp %
%%%%%%%%%%%%%%%%%%
function localWriteProp(eventSrc,eventData,PropObj)
% Update property object when GUI changes
PropObj.ListenerEnable = 0;  %---Temporarily disable PropObj listeners
Name = get(eventSrc,'Name');
%---Fix carriage return for pc
 if ispc, txt = strrep(get(eventSrc,'Text'),sprintf('\r\n'),sprintf('\n'));
 else,    txt = get(eventSrc,'Text');
 end
%---Update PropObj
 set(PropObj,Name,txt);
 PropObj.LastChange = Name;
PropObj.ListenerEnable = 1;  %---Re-enable PropObj listeners
