function conditioncallbacks(callaction)
%CONDITIONCALLBACKS Switchyard of HAVING and WHERE clause dialog callbacks.

%   Author(s): C.F.Garvin, 05-08-98
%   Copyright (c) 1984-2000 The MathWorks, Inc.
%   $Revision: 1.14 $   $Date: 2000/01/21 16:17:22 $
    
set(findobj('Type','figure'),'Pointer','watch')

%Determine which dialog is making callback
tagname = lower(get(gcf,'Tag'));

%Get callback flag
if nargin == 1
  %Call action supplied
else
  callaction  = get(gcbo,'Tag');
end

switch callaction
  
  case 'rcond'
    
    rcobj = findobj(gcf,'Tag','rcond');
    set(rcobj,'Value',0)
    set(gcbo,'Value',1)
    set(findobj(gcf,'Style','edit'),'String',[])
    if any(strcmp(get(gcbo,'String'),{'Relation','In'}))
      set(findobj(gcf,'String','SubQuery...'),'Enable','on')
    else
      set(findobj(gcf,'String','SubQuery...'),'Enable','off')  
    end
    
  case 'andor'
    
    aoobj = findobj(gcf,'Tag','andor');
    set(aoobj,'Value',0)
    set(gcbo,'Value',1)
    
  case 'Apply'
    
    ud = cell(1,9);    %Initialize storage space for clause GUI parameters
    
    %Get current index into clause  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 the condition
    cobj = findobj(gcf,'Tag','rcond','Value',1);
    tmpstr = get(cobj,'String');
    if strcmp(tmpstr,'Relation')
      robj = findobj(gcf,'Tag','relation');
      rdat = get(robj,{'String','Value'});
      cndstr = rdat{1}{rdat{2}};
    else
      cndstr = tmpstr;  
    end
    ud{2} = get(findobj(gcf,'Tag','rcond'),'Value');
    
    %Get the constraints
    switch tmpstr
      case 'Relation'     %conditional operator
        lstr = get(findobj(gcf,'Tag','relstring'),'String');
        ud{3} = rdat{2};
        ud{4} = lstr;
        
      case 'Between'
        l1dat = get(findobj(gcf,'Tag','lwstring'),'String');
        l2dat = get(findobj(gcf,'Tag','upstring'),'String');
        lstr = [l1dat ' AND ' l2dat];
        ud{3} = l1dat;
        ud{4} = l2dat;
        
      case 'In'
        lstr = get(findobj(gcf,'Tag','instring'),'String');
        ud{3} = lstr;
                
      case 'Is Null'
        lstr = [];
        ud{3} = [];
        ud{4} = [];
        
      case 'Like'
        lstr = get(findobj(gcf,'Tag','matchstring'),'String');
        ud{3} = lstr;
        
    end
    
    %Get subquery data if exists
    ud{6} = getuprop(gcf,'SUBQUERYDATA');
    ud{7} = getuprop(gcf,'SUBQUERYSTRING');
    ud{8} = getuprop(gcf,'TABLEVALUES');
    ud{9} = getuprop(gcf,'FIELDVALUES');
    
    %Get the clause operator and reset operator radios
    aoobj = findobj(gcf,'Tag','andor');
    aostr = get(findobj(aoobj,'Value',1),'String');
    if strcmp(aostr,'None')
      aostr = [];  
    end
    ud{5} = get(aoobj,'Value');
    set(aoobj,'Value',0);
    set(findobj(aoobj,'String','None'),'Value',1)
    
    %Build the clause and save it
    wcobj = findobj(gcf,'Tag',[tagname 'clauses']);
    curclause = get(wcobj,'String');
    curuserd = get(wcobj,'Userdata');
    
    %Include existing grouping syntax
    if isempty(udind)
      openpar = [];
      closepar = [];
    else
      
      tmpclause = curclause{udind};
    
      %Restore open parenthesis
      pari = find(tmpclause == '(');
      spci = find(tmpclause == ' ');
      j = find(pari < spci(1));
      openpar = tmpclause(pari(j));
    
      %Restore closing parenthesis
      pari = find(tmpclause == ')');
      spci = find(tmpclause == ' ');
      j = find(pari > spci(end-1) & pari < spci(end));
      closepar = tmpclause(pari(j));
      
    end
    
    %Build new clause
    newclause = [openpar fldstr ' ' upper(cndstr) ' ' lstr ' ' closepar ' ' aostr];
    
    %Append/Update clause
    if isempty(curuserd)    %No user data
      newud = ud;
      hwclause = {newclause};
      lval = 1;
    elseif isempty(udind)   %Append user data
      newud = [curuserd;ud];
      hwclause = [curclause;{newclause}];
      lval = size(hwclause,1);
    else
      curuserd(udind,:) = ud;   %Modify user data
      newud = curuserd;
      curclause{udind} = newclause;
      hwclause = curclause;
      lval = udind;
    end 
    
    set(wcobj,'String',hwclause,'Value',[],'Userdata',newud)
    
    %Clear the gui (edit boxes for now)
    set(findobj(gcf,'Style','edit'),'String',[])
    set(findobj('Tag','rcond'),'Value',0)
    set(findobj('Tag','rcond','String','Relation'),'Value',1)
    set(findobj(gcf,'String','SubQuery...'),'Enable','on')
    
  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}) = [];
      hwclause = clsdat{1};
      
      %Update userdata
      curud = get(wcobj,'Userdata');
      curud(clsdat{2},:) = [];

      set(wcobj,'String',hwclause,'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 clause
    wcobj = findobj(gcf,'Tag',[tagname 'clauses']);
    wcval = get(wcobj,'Value');
    if length(wcval) > 1
      wcval = min(wcval);
      set(wcobj,'Value',wcval)
    end
    wcdat = get(wcobj,'Userdata');
    if isempty(wcval),set(findobj('Type','figure'),'Pointer','arrow'),return,end
    set(findobj(gcf,'Tag',[tagname 'fields']),'Value',wcdat{wcval,1})
    set(findobj(gcf,'Tag','rcond'),{'Value'},wcdat{wcval,2})
    
    %Fill in the uicontrols on the type of Condition
    rcstr = get(findobj(gcf,'Tag','rcond','Value',1),'String');
    switch rcstr
      case 'Relation'
        set(findobj(gcf,'Tag','relation'),'Value',wcdat{wcval,3})
        set(findobj(gcf,'Tag','relstring'),'String',wcdat{wcval,4})
        
      case 'Between'
        set(findobj(gcf,'Tag','lwstring'),'String',wcdat{wcval,3})
        set(findobj(gcf,'Tag','upstring'),'String',wcdat{wcval,4})
        
      case 'In'
        set(findobj(gcf,'Tag','instring'),'String',wcdat{wcval,3})
                
      case 'Is Null'
        
      case 'Like'
        set(findobj(gcf,'Tag','matchstring'),'String',wcdat{wcval,3})
        
    end
    
    %Fill the clause operator radio buttons
    set(findobj(gcf,'Tag','andor'),{'Value'},wcdat{wcval,5})  
    
    %Store index value into list of clauses
    set(findobj(gcf,'Tag','Apply'),'Userdata',wcval)
    
    %Enable subqueries if relation or in condition
    if any(strcmp(get(findobj('Tag','rcond','Value',1),'String'),{'Relation','In'}))
      set(findobj(gcf,'String','SubQuery...'),'Enable','on')
      setuprop(gcf,'SUBQUERYDATA',wcdat{wcval,6})
      setuprop(gcf,'SUBQUERYSTRING',wcdat{wcval,7})
      setuprop(gcf,'TABLEVALUES',wcdat{wcval,8})
      setuprop(gcf,'FIELDVALUES',wcdat{wcval,9})
    else
      set(findobj(gcf,'String','SubQuery...'),'Enable','off')      
    end
    
  case 'OK'
    
    %Add the clauses to the current query
    wcobj = findobj(gcf,'Tag',[tagname 'clauses']);
    wcstr = get(wcobj,'String');
    wcud = get(wcobj,'Userdata');
    wcdat = {wcstr wcud};
    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')
