comparison m-toolbox/classes/@repogui2/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.3 2011/04/08 08:56:36 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 we can use a proper uitable:
62 v = ver('MATLAB');
63 if strcmp(v.Version, '7.6')
64 t = uitable(gui.main, 'Data', results, ...
65 'ColumnName', fields,...
66 'units', 'normalized',...
67 'Position', [.01,.08,1-0.02,.73]);
68 else
69
70 % Otherwise, we use the createTable function which uses a JTable
71 [t,b] = createTable(gui.main, fields, results, false, ...
72 'AutoResizeMode',javax.swing.JTable.AUTO_RESIZE_OFF,...
73 'Editable',false,'Position',[.01,.08,1-0.02,.73]);
74
75 end
76
77 setappdata(gui.main, 'table', t);
78
79 % add export button
80 btn = uicontrol(gui.main,...
81 'Style','pushbutton',...
82 'String','Export',...
83 'Tag', 'exportBtn',...
84 'Callback', {@exportBtn_Callback, gui, results, fields}, ...
85 'Position',[leftMargin leftMargin btnWidth btnHeight], 'Visible', 'off');
86
87 get(btn)
88
89 end
90 %--------------------------------------------------------------------------
91 % Callbacks
92
93 % callback to reset the query string in case the user deletes it
94 function qtxtCB(hObject, eventdata, query)
95
96 set(hObject, 'String', query);
97 end
98 %-------------------------------------------------------
99 % export button
100 function exportBtn_Callback(hObject, eventdata, gui, results, fields)
101
102 % get table
103 t = getappdata(gui.main, 'table');
104 table = get(t.Table);
105
106 rows = table.SelectedRows + 1;
107 cols = table.SelectedColumns + 1;
108 res = results(rows, cols);
109 nResults = size(res, 1);
110
111 % get file to export to
112 [filename, pathname, filteridx] = uiputfile('*.txt', 'Select export file');
113
114 fd = fopen(filename, 'w+');
115
116 % header
117 fprintf(fd, '# ');
118 fprintf(fd, '%s\t|\t', fields{cols});
119 fprintf(fd, '\n');
120
121
122 for j=1:nResults
123 for k=1:size(res,2)
124 r = res{j,k};
125 if ischar(r)
126 fprintf(fd, '%s\t\t', char(r));
127 elseif isnumeric(r)
128 fprintf(fd, '%f\t\t', r);
129 else
130 error('### Unknown data format.');
131 end
132 end
133 fprintf(fd, '\n');
134 end
135 fclose(fd);
136
137 edit(filename);
138 end
139 %-------------------------------------------------------
140 % Resize Main figure
141 function resizeMain(hObject, eventdata, leftMargin, bottomMargin, topMargin)
142
143 % resize table
144 s = getappdata(hObject, 'qstring');
145 ps = get(s, 'Position');
146 t = getappdata(hObject, 'table');
147 fs = get(hObject, 'Position');
148 pos = get(t, 'Position');
149
150 % set(t, 'Position', [pos(1) pos(2) fs(3)-2*leftMargin fs(4)-bottomMargin-ps(4)]);
151 set(s, 'Position', [ps(1) fs(4)-ps(4)-20 fs(3)-2*leftMargin ps(4)]);
152
153 end
154 % END