function ordergroupcallbacks(callaction)
%ORDERGROUPCALLBACKS ORDER BY and GROUP BY clause dialog callbacks.

%   Author(s): C.F.Garvin, 05-08-98
%   Copyright (c) 1984-2000 The MathWorks, Inc.
%   $Revision: 1.11 $   $Date: 2000/01/20 14:06:48 $
    
set(findobj('Type','figure'),'Pointer','watch')

%Get type of dialog
tagname = lower(get(gcf,'Tag'));
i = find(tagname == ' ');
tagname(i) = [];

%Get callback flag
if nargin == 1
  %Callaction entered as input argument
else
  callaction  = get(gcbo,'Tag');
end

switch callaction
  
  case 'sortorder'
    
    rcobj = findobj(gcf,'Tag','sortorder');
    set(rcobj,'Value',0)
    set(gcbo,'Value',1)
        
  case 'Apply'
    
    ud = cell(1,4);    %Initialize storage space for clause GUI parameters
    
    %Get current index into orderbyclause data if editing an existing clause
    inobj = findobj(gcf,'Tag','Apply');
    udind = get(inobj,'Userdata');
    set(inobj,'Userdata',[]);
    
    %Get field
    wfobj = findobj(gcf,'Tag',[tagname 'fields']);
    wfdat = get(wfobj,{'String','Value'});
    fldstr = wfdat{1}{wfdat{2}};
    ud{1} = wfdat{2};
    
    %Get current clause data
    ocobj = findobj(gcf,'Tag',[tagname 'clauses']);
    curclause = get(ocobj,'String');
    curuserd = get(ocobj,'Userdata');
    
    %Define edit behavior to avoid duplicate entries
    if ~isempty(udind)
      curclause(udind) = [];
      curuserd(udind,:) = [];
    end

    %Get size of current clauses
    [m,n] = size(curclause);

    %Get the sort key number
    sobj = findobj(gcf,'Tag','sortkey');
    tmpstr = get(sobj,'String');
    tmpval = str2double(tmpstr);
    if isnan(tmpval)
      tmpval = m+1;     %Default sort key number is m+1  
    end
    ud{2} = tmpval;
        
    %Get the sort order
    if strcmp(tagname,'orderby');
      oobj = findobj(gcf,'Tag','sortorder');
      ud{3} = get(oobj,'Value');
      orderstr = [' ' get(findobj(oobj,'Value',1),'Userdata')];
      ud{4} = orderstr;
    else
      orderstr = [];
    end
    
    %Build the clause and save it
    newclause = [fldstr orderstr];
       
    %Display clause in desired order
    if isempty(curclause)
      so = tmpval;              %sortorder is single value if no current clause
    else
      so = [(2:m+1)';tmpval];   %Increment row number by 1 to get newclause into right slot
    end  
    [y,i] = sort(so);
    tmpclause = [curclause;{newclause}];
    tmpud = [curuserd;ud];
    ordergroupclause = tmpclause(i);
    newud = tmpud(i,:);
    
    set(ocobj,'String',ordergroupclause,'Value',[],'Userdata',newud)
    
    %Clear the gui (edit boxes for now)
    set(findobj(gcf,'Style','edit'),'String',[])
    
  case 'Cancel'
    
    close
    
  case 'Delete'
    
    %Delete selected clause from listbox
    wcobj = findobj(gcf,'Tag',[tagname 'clauses']);
    
    %Update string
    clsdat = get(wcobj,{'String','Value'});
    if ~isempty(clsdat{1})
      clsdat{1}(clsdat{2}) = [];
      ordergroupclause = clsdat{1};
      
      %Update userdata
      curud = get(wcobj,'Userdata');
      curud(clsdat{2},:) = [];

      set(wcobj,'String',ordergroupclause,'Value',[],'Userdata',curud)
    end
       
  case {'Edit',[tagname 'clauses']}
    
    %Enable listbox double click behavior
    if strcmp(get(gcbo,'Style'),'listbox')
      pbobj = findobj(gcf,'Tag','Edit','Style','pushbutton');
      if (now - get(pbobj,'Userdata')) > 5e-6
        set(pbobj,'Userdata',now)
        set(findobj('Type','figure'),'Pointer','arrow')
        return
      end
    end
                 
    %Clear edit boxes
    set(findobj(gcf,'Style','edit'),'String',[])
    
    %Edit the selected order/group clause
    wcobj = findobj(gcf,'Tag',[tagname 'clauses']);
    wcval = get(wcobj,'Value');
    if length(wcval) > 1
      wcval = min(wcval);
      set(wcobj,'Value',wcval)
    end
    if isempty(wcval),set(findobj('Type','figure'),'Pointer','arrow'),return,end
    wcdat = get(wcobj,'Userdata');
    set(findobj(gcf,'Tag',[tagname 'fields']),'Value',wcdat{wcval,1})
    set(findobj(gcf,'Tag','sortkey'),'String',num2str(wcval,2))
    if strcmp(tagname,'orderby')
      set(findobj(gcf,'Tag','sortorder'),{'Value'},wcdat{wcval,3})
    end
            
    %Store index value into list of clauses
    set(findobj(gcf,'Tag','Apply'),'Userdata',wcval)
    
  case 'OK'
    
    %Add the orderby clauses to the current query
    wcobj = findobj(gcf,'Tag',[tagname 'clauses']);
    wcstr = get(wcobj,'String');
    wcud = get(wcobj,'Userdata');
    if isempty(wcstr)
      wcdat = [];
    else
      wcdat = {wcstr wcud};
    end
    close
    set(findobj('Tag',[tagname 'clausedata']),'Userdata',wcdat)
    figure(findobj('Tag','SQLDLG'));
    qobj = findobj('Tag','query');     %Determine if SQL statement exists
    qstr = get(qobj,'String');
    if ~isempty(qstr)
      buildquery;
    end
    
    %Fill listbox for visual queue that clause data exists
    set(findobj('Tag',[tagname 'list']),'String',wcstr,'Value',[])

end

set(findobj('Type','figure'),'Pointer','arrow')
