function sbhndlpart(cmd,opt)
%SBHNDLPART
%
%  See also

% Initial coding by A. S. Bozin, 21-04-98.
% Copyright 1997-2000 The MathWorks, Inc.
% $Revision: 1.6 $ $Date: 2000/06/14 16:07:53 $

   %
   % Action depends on CMD
   %
   switch (cmd)
   case 'apply'
      %
      % Set a new working partition
      %
      hFig = get(0,'CurrentFigure');
      ud = get(hFig,'UserData');
      val = get(ud.partition,'Value');
      if (val <= ud.nopart)
	 shh = get(0,'ShowHiddenHandles');
	 set(0,'ShowHiddenHandles','on');
	 sFig = findobj(get(0,'Children'),'flat','Tag','sb2slTag');
	 set(0,'ShowHiddenHandles',shh);
	 ud = get(sFig,'UserData');
	 ud.pind = val;		  	   	   % Update working partition index
	 set(sFig,'UserData',ud);
	 set(ud.partition,'String',ud.wsList.names{val});
      else
	 sbhndlmsgs('write','Command ignored.');
      end
      if (strcmp(opt,'off'))
	 set(hFig,'Visible','off');
      end
   case 'close'
      %
      % Close the window
      %
      hFig = get(0,'CurrentFigure');
      set(hFig,'Visible','off');
   case 'update'
      %
      % Update a list of partitions if the window is opened
      %
      shh = get(0,'ShowHiddenHandles');
      set(0,'ShowHiddenHandles','on');
      hFig = findobj(get(0,'Children'),'flat','Tag','hndlpartTag');
      set(0,'ShowHiddenHandles',shh);
      if (~isempty(hFig))
	 if (strcmp(get(hFig','Visible'),'on'))
	    sbhndlpart('create');
	 end
      end
   case 'create'
      %
      % Create the list of partitions window
      %
      shh = get(0,'ShowHiddenHandles');
      set(0,'ShowHiddenHandles','on');
      kids = get(0,'Children');
      hFig = findobj(kids,'flat','Tag','hndlpartTag');
      set(0,'ShowHiddenHandles',shh);
      if (length(hFig) > 1)			   % There is another figure with the same tag
	 error('sb2sl: sbhndlpart detected more than one window.');
      elseif (length(hFig) == 1)		   % Partition window already created
	 set(0,'CurrentFigure',hFig);
	 set(hFig,'Visible','on');
      else
	 %
	 % Get preferences
	 %
	 sFig = findobj(kids,'flat','Tag','sb2slTag');
	 ud = get(sFig,'UserData');
	 winpref = ud.winpref;
	 %
	 % Create a new window
	 %
	 bgc = get(0,'DefaultUIControlBackGroundColor');
	 rect = [490 300 280 146];
	 hFig = figure('Units','pixels',...
		       'Position',rect,...
		       'Color',bgc,...
		       'Resize','off',...
		       'Tag','hndlpartTag',...
		       'IntegerHandle','off',...
		       'HandleVisibility','callback',...
		       'MenuBar','none',...
		       'Name','SB2SL: Working Partitions',...
		       'NumberTitle','off',...
		       'DeleteFcn','sbhndlpart(''delete'');',...
		       'ResizeFcn','sbhndlpart(''resize'');');
	 %
	 % List of working partitions
	 %
	 rect = [10 112 140 20];
	 hText = uicontrol('Parent',hFig,...
			   'Style','text',...
			   'Units','pixels',...
			   'Position',rect,...
			   'Interruptible','off',...
			   'FontName','Helvetica',...
			   'FontSize',winpref.varSize(2),...
			   'Tag','varNormal',...
			   'HorizontalAlignment','left',...
			   'String','Partitions');
	 set(hText,'Units','normalized');
	 rect = [10 40 260 72];
	 hPart = uicontrol('Parent',hFig,...
			   'Style','listbox',...
			   'Units','pixels',...
			   'Position',rect,...
			   'CallBack','',...
			   'Interruptible','on',...
			   'Enable','inactive',...
			   'UserData',[],...
			   'FontName','Courier',...
			   'FontSize',winpref.fixSize,...
			   'Tag','fixNormal',...
			   'HorizontalAlignment','left',...
			   'ListboxTop',1,...
			   'Min',0,...
			   'Max',2,...
			   'String','<<end of partition list>>');
	 set(hPart,'Units','normalized');
	 %
	 % Command buttons
	 %
	 labels = {'OK','Cancel','Apply'};
	 callBacks = {'sbhndlpart(''apply'',''off'');',...
		      'sbhndlpart(''close'');',...
		      'sbhndlpart(''apply'',''on'');'};
	 toolTips = {'Set a new working partition and close the window',...
		     'Close the window',...
		     'Set a new working partition'};
	 nbutts = size(labels,2);
	 hbutts = zeros(nbutts,1);
	 rect = [10 10 80 20];
	 for (k = 1:nbutts)
	    hbutts(k) = uicontrol('Parent',hFig,...
				  'Style','pushbutton',...
				  'Units','pixels',...
				  'Position',rect,...
				  'CallBack',callBacks{k},...
				  'Interruptible','on',...
				  'FontName','Helvetica',...
				  'FontSize',winpref.varSize(2),...
				  'Tag','varNormal',...
				  'HorizontalAlignment','center',...
				  'TooltipString',toolTips{k},...
				  'String',labels{k});
	    rect(1) = rect(1)+90;
	 end
	 set(hbutts,'Units','normalized');
	 %
	 % Save the handles
	 %
	 ud = struct('partition',hPart,'nopart',0);
	 set(hFig,'UserData',ud);
      end
      %
      % Update a list of partitions
      %
      sFig = findobj(kids,'flat','Tag','sb2slTag');
      ud = get(sFig,'UserData');
      if (ud.pind == 0)
	 str = {'<<end of partition list>>'};
	 val = 1;
	 nopart = 0;
      else
	 str = cat(1,ud.wsList.names,{'<<end of partition list>>'});
	 val = ud.pind;
	 nopart = size(ud.wsList.names,1);
      end
      ud = get(hFig,'UserData');
      set(ud.partition,'String',str,...
		       'Enable','on',...
		       'Value',val,...
		       'ListboxTop',1);
      ud.nopart = nopart;
      set(hFig,'UserData',ud);		   	   % Update user data
   case 'resize'
      %
      %
      %
   case 'delete'
      %
      %
      %
   case 'compile'
      % 
      % Compilation message
      % 
      sbhndlmsgs('write','.','append');	   	   % Show progress
   otherwise
      error('sb2sl: sbhndlpart called with incorrect argument.');
   end 
   return					   % Last card of SBHNDLPART
