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

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

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

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

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

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

%---Center panel (textfields)
 s.C = com.mathworks.mwt.MWPanel(GL_41); s.Main.add(s.C,com.mathworks.mwt.MWBorderLayout.CENTER);
  s.Title   = com.mathworks.mwt.MWTextField; s.C.add(s.Title);   s.Title.setFont(Prefs.JavaFontP);
  s.Xlabel  = com.mathworks.mwt.MWTextField; s.C.add(s.Xlabel);  s.Xlabel.setFont(Prefs.JavaFontP);
  s.Ylabel  = com.mathworks.mwt.MWTextField; s.C.add(s.Ylabel);  s.Ylabel.setFont(Prefs.JavaFontP);
  s.Ylabel2 = com.mathworks.mwt.MWTextField; s.C.add(s.Ylabel2); s.Ylabel2.setFont(Prefs.JavaFontP);

%---Install GUI callbacks
 GUICallback = {@localWriteProp,h};
 set(s.Title,  'Name','Title',  'TextValueChangedCallback',GUICallback);
 set(s.Xlabel, 'Name','Xlabel', 'TextValueChangedCallback',GUICallback);
 set(s.Ylabel, 'Name','Ylabel', 'TextValueChangedCallback',GUICallback);
 set(s.Ylabel2,'Name','Ylabel2','TextValueChangedCallback',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
    String = eventData.NewValue.String;
    if iscell(String)
       GUI1.setText(sprintf(String{1}));
       GUI2.setText(sprintf(String{2}));
    else
       GUI1.setText(sprintf(String));
    end
 end


%%%%%%%%%%%%%%%%%%
% localWriteProp %
%%%%%%%%%%%%%%%%%%
function localWriteProp(eventSrc,eventData,h)
% GUI -> Target
 WarnState = warning('off');
 h.ListenerEnable = 0;
 Name = get(eventSrc,'Name');
 switch Name
 case {'Title','Xlabel','Ylabel'}
    Str = get(h.Target,Name);
    if iscell(Str.String)
       Str.String{1} = get(eventSrc,'Text');
    else
       Str.String = get(eventSrc,'Text');
    end
    set(h.Target,Name,Str);
 case {'Ylabel2'}
    Str = h.Target.Ylabel;
    Str.String{2} = get(eventSrc,'Text');
    h.Target.Ylabel = Str;
 end
 h.ListenerEnable = 1;
 drawnow; %---Flush graphics to prevent race condition with HG listener
 warning(WarnState);


%%%%%%%%%%%%%%%%%%%%%
% 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.TitleListener  = handle.listener(t,findprop(t,'Title'), 'PropertyPostSet',{@localReadProp,s.Title});
     s.XlabelListener = handle.listener(t,findprop(t,'Xlabel'),'PropertyPostSet',{@localReadProp,s.Xlabel});
     s.YlabelListener = handle.listener(t,findprop(t,'Ylabel'),'PropertyPostSet',{@localReadProp,s.Ylabel,s.Ylabel2});
     %---Sync
      s.Title.setText(sprintf(t.Title.String));
      s.Xlabel.setText(sprintf(t.Xlabel.String));
      if iscell(t.Ylabel.String)
         s.Ylabel.setText(sprintf(t.Ylabel.String{1}));
         s.Ylabel2.setText(sprintf(t.Ylabel.String{2}));
         if isempty(s.Ylabel2.getParent)
            GL = java.awt.GridLayout(4,1,0,3);
            s.W.setLayout(GL); s.W.add(s.LY2);
            s.C.setLayout(GL); s.C.add(s.Ylabel2);
         end
      else
         s.Ylabel.setText(sprintf(t.Ylabel.String));
         if ~isempty(s.Ylabel2.getParent)
            GL = java.awt.GridLayout(3,1,0,3);
            s.W.remove(s.LY2);     s.W.setLayout(GL);
            s.C.remove(s.Ylabel2); s.C.setLayout(GL);
         end
      end
  else
     %---Clear
      s.TitleListener  = [];
      s.XlabelListener = [];
      s.YlabelListener = [];
  end
 %---Save structure
  set(eventSrc,'UserData',s);
