% SQLRESULTSGUI shows the results of a query in a new window.
%
% sqlResultsGUI(results, fields)
%
% M Hewitson 09-07-07
%
% $Id: sqlResultsGUI.m,v 1.3 2011/04/08 08:56:36 hewitson Exp $
%
function sqlResultsGUI(results, fields, query)
  
  
  leftMargin   = 10;
  bottomMargin = 100;
  topMargin    = 20;
  
  btnHeight = 20;
  btnWidth  = 60;
  btnGap    = 20;
  
  Color     = [240 240 240]/255;
  Height    = 600;
  Width     = 800;
  
  Screen    = get(0,'screensize');
  Position  = [Screen(3)/2 - Width/2 Screen(4)/2-Height/2 Width Height];
  
  % create figure
  gui.main = figure('HandleVisibility','on',...
    'IntegerHandle','off',...
    'Menubar','none',...
    'NumberTitle','off',...
    'Name','Query Results',...
    'Tag','main',...
    'Color',Color,...
    'Resize', 'on',...
    'Position',Position,...
    'ResizeFcn', {@resizeMain, leftMargin, bottomMargin, topMargin});
  
  % add query string
  Tleft   = leftMargin;
  Twidth = Width - 2*leftMargin;
  Theight = 80;
  Tbottom = Position(4) - Theight - 20;
  sth = uicontrol(gui.main,'Style','edit',...
    'String',query,...
    'Fontsize', 14,...
    'BackgroundColor','w',...
    'Max', 100,...
    'HorizontalAlignment', 'left',...
    'Tag', 'QueryString', ...
    'Enable', 'on', ...
    'Position',[Tleft Tbottom Twidth Theight], ...
    'Callback', {@qtxtCB, query});
  
  setappdata(gui.main, 'qstring', sth);
  
  if size(fields,2) ~= size(results,2)
    fields = cell(1,size(results,2));
  end
  
  % In MATLAB 2008a we can use a proper uitable:
  v = ver('MATLAB');
  if strcmp(v.Version, '7.6')
    t = uitable(gui.main, 'Data', results, ...
      'ColumnName', fields,...
      'units', 'normalized',...
      'Position', [.01,.08,1-0.02,.73]);
  else
    
    % Otherwise, we use the createTable function which uses a JTable
    [t,b] = createTable(gui.main, fields, results, false, ...
      'AutoResizeMode',javax.swing.JTable.AUTO_RESIZE_OFF,...
      'Editable',false,'Position',[.01,.08,1-0.02,.73]);
    
  end
  
  setappdata(gui.main, 'table', t);
  
  % add export button
  btn = uicontrol(gui.main,...
    'Style','pushbutton',...
    'String','Export',...
    'Tag', 'exportBtn',...
    'Callback', {@exportBtn_Callback, gui, results, fields}, ...
    'Position',[leftMargin leftMargin btnWidth btnHeight], 'Visible', 'off');
  
  get(btn)
  
end
%--------------------------------------------------------------------------
% Callbacks

% callback to reset the query string in case the user deletes it
function qtxtCB(hObject, eventdata, query)
  
  set(hObject, 'String', query);
end
%-------------------------------------------------------
% export button
function exportBtn_Callback(hObject, eventdata, gui, results, fields)
  
  % get table
  t   = getappdata(gui.main, 'table');
  table = get(t.Table);
  
  rows = table.SelectedRows + 1;
  cols = table.SelectedColumns + 1;
  res = results(rows, cols);
  nResults = size(res, 1);
  
  % get file to export to
  [filename, pathname, filteridx] = uiputfile('*.txt', 'Select export file');
  
  fd = fopen(filename, 'w+');
  
  % header
  fprintf(fd, '# ');
  fprintf(fd, '%s\t|\t', fields{cols});
  fprintf(fd, '\n');
  
  
  for j=1:nResults
    for k=1:size(res,2)
      r = res{j,k};
      if ischar(r)
        fprintf(fd, '%s\t\t', char(r));
      elseif isnumeric(r)
        fprintf(fd, '%f\t\t', r);
      else
        error('### Unknown data format.');
      end
    end
    fprintf(fd, '\n');
  end
  fclose(fd);
  
  edit(filename);
end
%-------------------------------------------------------
% Resize Main figure
function resizeMain(hObject, eventdata, leftMargin, bottomMargin, topMargin)
  
  % resize table
  s   = getappdata(hObject, 'qstring');
  ps  = get(s, 'Position');
  t   = getappdata(hObject, 'table');
  fs  = get(hObject, 'Position');
  pos = get(t, 'Position');
  
  % set(t, 'Position', [pos(1) pos(2) fs(3)-2*leftMargin fs(4)-bottomMargin-ps(4)]);
  set(s, 'Position', [ps(1) fs(4)-ps(4)-20 fs(3)-2*leftMargin ps(4)]);
  
end
% END
