comparison m-toolbox/classes/@repogui/sqlResultsGUI.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % SQLRESULTSGUI shows the results of a query in a new window.
2 %
3 % sqlResultsGUI(results, fields)
4 %
5 % M Hewitson 09-07-07
6 %
7 % $Id: sqlResultsGUI.m,v 1.2 2011/04/08 08:56:26 hewitson Exp $
8 %
9 function sqlResultsGUI(results, fields, query)
10
11
12 leftMargin = 10;
13 bottomMargin = 100;
14 topMargin = 20;
15
16 btnHeight = 20;
17 btnWidth = 60;
18 btnGap = 20;
19
20 Color = [240 240 240]/255;
21 Height = 600;
22 Width = 800;
23
24 Screen = get(0,'screensize');
25 Position = [Screen(3)/2 - Width/2 Screen(4)/2-Height/2 Width Height];
26
27 % create figure
28 gui.main = figure('HandleVisibility','on',...
29 'IntegerHandle','off',...
30 'Menubar','none',...
31 'NumberTitle','off',...
32 'Name','Query Results',...
33 'Tag','main',...
34 'Color',Color,...
35 'Resize', 'on',...
36 'Position',Position,...
37 'ResizeFcn', {@resizeMain, leftMargin, bottomMargin, topMargin});
38
39 % add query string
40 Tleft = leftMargin;
41 Twidth = Width - 2*leftMargin;
42 Theight = 80;
43 Tbottom = Position(4) - Theight - 20;
44 sth = uicontrol(gui.main,'Style','edit',...
45 'String',query,...
46 'Fontsize', 14,...
47 'BackgroundColor','w',...
48 'Max', 100,...
49 'HorizontalAlignment', 'left',...
50 'Tag', 'QueryString', ...
51 'Enable', 'on', ...
52 'Position',[Tleft Tbottom Twidth Theight], ...
53 'Callback', {@qtxtCB, query});
54
55 setappdata(gui.main, 'qstring', sth);
56
57 if size(fields,2) ~= size(results,2)
58 fields = cell(1,size(results,2));
59 end
60
61 % In MATLAB 2008a and above we can use a proper uitable:
62 if ~verLessThan('MATLAB', '7.6')
63 t = uitable(gui.main, 'Data', results, ...
64 'ColumnName', fields,...
65 'units', 'normalized',...
66 'Position', [.01,.08,1-0.02,.73]);
67 else
68
69 % Otherwise, we use the createTable function which uses a JTable
70 [t,b] = repogui.createTable(gui.main, fields, results, false, ...
71 'AutoResizeMode',javax.swing.JTable.AUTO_RESIZE_OFF,...
72 'Editable',false,'Position',[.01,.08,1-0.02,.73]);
73
74 end
75
76 setappdata(gui.main, 'table', t);
77
78 % add export button
79 btn = uicontrol(gui.main,...
80 'Style','pushbutton',...
81 'String','Export',...
82 'Tag', 'exportBtn',...
83 'Callback', {@exportBtn_Callback, gui, results, fields}, ...
84 'Position',[leftMargin leftMargin btnWidth btnHeight], 'Visible', 'off');
85
86 end
87 %--------------------------------------------------------------------------
88 % Callbacks
89
90 % callback to reset the query string in case the user deletes it
91 function qtxtCB(hObject, eventdata, query)
92
93 set(hObject, 'String', query);
94 end
95 %-------------------------------------------------------
96 % export button
97 function exportBtn_Callback(hObject, eventdata, gui, results, fields)
98
99 % get table
100 t = getappdata(gui.main, 'table');
101 table = get(t.Table);
102
103 rows = table.SelectedRows + 1;
104 cols = table.SelectedColumns + 1;
105 res = results(rows, cols);
106 nResults = size(res, 1);
107
108 % get file to export to
109 [filename, pathname, filteridx] = uiputfile('*.txt', 'Select export file');
110
111 fd = fopen(filename, 'w+');
112
113 % header
114 fprintf(fd, '# ');
115 fprintf(fd, '%s\t|\t', fields{cols});
116 fprintf(fd, '\n');
117
118
119 for j=1:nResults
120 for k=1:size(res,2)
121 r = res{j,k};
122 if ischar(r)
123 fprintf(fd, '%s\t\t', char(r));
124 elseif isnumeric(r)
125 fprintf(fd, '%f\t\t', r);
126 else
127 error('### Unknown data format.');
128 end
129 end
130 fprintf(fd, '\n');
131 end
132 fclose(fd);
133
134 edit(filename);
135 end
136 %-------------------------------------------------------
137 % Resize Main figure
138 function resizeMain(hObject, eventdata, leftMargin, bottomMargin, topMargin)
139
140 % resize table
141 s = getappdata(hObject, 'qstring');
142 ps = get(s, 'Position');
143 t = getappdata(hObject, 'table');
144 fs = get(hObject, 'Position');
145 pos = get(t, 'Position');
146
147 % set(t, 'Position', [pos(1) pos(2) fs(3)-2*leftMargin fs(4)-bottomMargin-ps(4)]);
148 set(s, 'Position', [ps(1) fs(4)-ps(4)-20 fs(3)-2*leftMargin ps(4)]);
149
150 end
151 % END