Home > m > gui > ltpdaRepoGUI > sqlResultsGUI.m

sqlResultsGUI

PURPOSE ^

SQLRESULTSGUI shows the results of a query in a new window.

SYNOPSIS ^

function sqlResultsGUI(results, fields, query)

DESCRIPTION ^

 SQLRESULTSGUI shows the results of a query in a new window.
 
 sqlResultsGUI(results, fields)
 
 M Hewitson 09-07-07
 
 $Id: sqlResultsGUI.m,v 1.7 2008/02/26 08:54:19 hewitson Exp $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function sqlResultsGUI(results, fields, query)
0002 
0003 % SQLRESULTSGUI shows the results of a query in a new window.
0004 %
0005 % sqlResultsGUI(results, fields)
0006 %
0007 % M Hewitson 09-07-07
0008 %
0009 % $Id: sqlResultsGUI.m,v 1.7 2008/02/26 08:54:19 hewitson Exp $
0010 %
0011 
0012 leftMargin   = 10;
0013 bottomMargin = 100;
0014 topMargin    = 20;
0015 
0016 btnHeight = 20;
0017 btnWidth  = 60;
0018 btnGap    = 20;
0019 
0020 Color     = [240 240 240]/255;
0021 Height    = 600;
0022 Width     = 800;
0023 
0024 Screen    = get(0,'screensize');
0025 Position  = [Screen(3)/2 - Width/2 Screen(4)/2-Height/2 Width Height];
0026 
0027 % create figure
0028 gui.main = figure('HandleVisibility','on',...
0029     'IntegerHandle','off',...
0030     'Menubar','none',...
0031     'NumberTitle','off',...
0032     'Name','Query Results',...
0033     'Tag','main',...
0034     'Color',Color,...
0035     'Resize', 'on',...
0036     'Position',Position,...
0037     'ResizeFcn', {@resizeMain, leftMargin, bottomMargin, topMargin});
0038 
0039 % add query string
0040 Tleft   = leftMargin;
0041 Twidth = Width - 2*leftMargin;
0042 Theight = 80;
0043 Tbottom = Position(4) - Theight - 20;
0044 sth = uicontrol(gui.main,'Style','edit',...
0045                 'String',query,...
0046                 'Fontsize', 14,...
0047                 'BackgroundColor','w',...
0048                 'Max', 100,...
0049                 'HorizontalAlignment', 'left',...
0050                 'Tag', 'QueryString', ...
0051                 'Enable', 'on', ...
0052                 'Position',[Tleft Tbottom Twidth Theight], ...
0053                 'Callback', {@qtxtCB, query});
0054 
0055 
0056 if size(fields,2) ~= size(results,2)              
0057   fields = cell(1,size(results,2));
0058 end
0059               
0060 %    [mtable, buttons] = createTable (pnContainer, headers, data, buttonsFlag, 'PropName',PropValue, ...)
0061 % [t,b] = createTable(gui.main, fields, results, false, ...
0062 %   'AutoResizeMode',javax.swing.JTable.AUTO_RESIZE_OFF,...
0063 %                   'Editable',false,'Position',[.01,.08,1-0.02,.73])
0064                   
0065 % add table
0066 Tleft   = leftMargin;
0067 Twidth  = Width - 2*leftMargin;
0068 Theight = 500;
0069 Tbottom = leftMargin + btnHeight + 20;
0070 
0071 % sometimes the user wrote the SQL query by hand
0072 % in which case getting the selected fields as the
0073 % column headings fails. Instead we fix this for now
0074 % by making empty (A-Z) column headings.
0075 
0076 t = muitable(gui.main, results, fields, 'Position',...
0077   [Tleft Tbottom Twidth Theight]);
0078 set(t, 'Editable', 0);
0079 % get(t.Table)
0080 t.Table.setAutoResizeMode(t.Table.AUTO_RESIZE_OFF);
0081 % get(t.Table)
0082 % cm = get(t.Table.getColumnModel)
0083 % get(cm.Columns(1))
0084 
0085 setappdata(gui.main, 'table', t);
0086 
0087 % add export button
0088 btn = uicontrol(gui.main,...
0089   'Style','pushbutton',...
0090   'String','Export',...
0091   'Tag', 'exportBtn',...
0092   'Callback', {@exportBtn_Callback, gui, results, fields}, ...
0093   'Position',[leftMargin leftMargin btnWidth btnHeight]);
0094 
0095 %--------------------------------------------------------------------------
0096 % Callbacks
0097 
0098 % callback to reset the query string in case the user deletes it
0099 function qtxtCB(hObject, eventdata, query)
0100 
0101 set(hObject, 'String', query);
0102 
0103 %-------------------------------------------------------
0104 % export button
0105 function exportBtn_Callback(hObject, eventdata, gui, results, fields)
0106 
0107 % get table
0108 t   = getappdata(gui.main, 'table');
0109 table = get(t.Table);
0110 
0111 rows = table.SelectedRows + 1;
0112 cols = table.SelectedColumns + 1;
0113 res = results(rows, cols);
0114 nResults = size(res, 1);
0115 
0116 % get file to export to
0117 [filename, pathname, filteridx] = uiputfile('*.txt', 'Select export file');
0118 
0119 fd = fopen(filename, 'w+');
0120 
0121 % header
0122 fprintf(fd, '# ');
0123 fprintf(fd, '%s\t|\t', fields{cols});
0124 fprintf(fd, '\n');
0125 
0126 
0127 for j=1:nResults
0128   for k=1:size(res,2)
0129     r = res{j,k};
0130     if ischar(r)
0131       fprintf(fd, '%s\t\t', char(r));
0132     elseif isnumeric(r)
0133       fprintf(fd, '%f\t\t', r);
0134     else
0135       error('### Unknown data format.');
0136     end
0137   end
0138   fprintf(fd, '\n');
0139 end
0140 fclose(fd);
0141 
0142 edit(filename);
0143 
0144 %-------------------------------------------------------
0145 % Resize Main figure
0146 function resizeMain(hObject, eventdata, leftMargin, bottomMargin, topMargin)
0147 
0148 % resize table
0149 s   = findobj('Tag', 'QueryString');
0150 ps  = get(s, 'Position');
0151 % t   = getappdata(hObject, 'table');
0152 fs  = get(hObject, 'Position');
0153 % pos = get(t, 'Position');
0154 
0155 % set(t, 'Position', [pos(1) pos(2) fs(3)-2*leftMargin fs(4)-bottomMargin-ps(4)]);
0156 set(s, 'Position', [ps(1) fs(4)-ps(4)-20 fs(3)-2*leftMargin ps(4)]);
0157 
0158 
0159 % END

Generated on Tue 26-Feb-2008 10:52:52 by m2html © 2003