0
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
1 function [mtable, buttons] = createTable(pnContainer,headers,data,buttonsFlag,varargin)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
2 % createTable - create nice-looking table based on javax.swing.JTable
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
3 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
4 % Syntax:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
5 % [mtable, buttons] = createTable (pnContainer, headers, data, buttonsFlag, 'PropName',PropValue, ...)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
6 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
7 % Input Parameters:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
8 % pnContainer - optional handle to container uipanel or figure. If empty/unsupplied then current figure will be used
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
9 % headers - optional cell array of column header strings. If unsupplied then = {'A','B','C'}
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
10 % data - optional vector/matrix (either scalar or cell array) of data values
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
11 % buttonsFlag - optional flag indicating whether to create the table-manipulation buttons. Default = true
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
12 % 'PropName',PropValue -
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
13 % optional list of property pairs (e.g., 'AutoResizeMode',4,'Editable',false,'Position',[.1,.1,.5,.5])
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
14 % Note: PropName must be either an mtable property ('Visible','Editable','Position','Units',
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
15 % 'DataChangedCallback',...) or otherwise a Javax.swing.JTable property ('ShowGrid','Name',...).
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
16 % Abbreviated PropNames are unsupported for mtable properties (which are few) - only for JTable
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
17 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
18 % Output parameters:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
19 % mtable - handle to mtable object (a Matlab object)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
20 % buttons - handles to table manipulation buttons: [<appendRow> <insertRow> <deleteRow> <deleteAll> <printAll>]
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
21 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
22 % Examples:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
23 % [mtable, buttons] = createTable;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
24 % [mtable, buttons] = createTable(gcf,'column name');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
25 % mtable = createTable([],{'a','b','c','d'},{false,1.3,uint16(45),'ert'; true,pi,uint16(-4),'defrgt'})
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
26 % mtable = createTable([],{'a','b','c','d'},magic(4),false,'AutoResizeMode',javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
27 % mtable = createTable([],{'rads','sin','cos'},[pi,sin(pi),cos(pi)],'SelectionMode',javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
28 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
29 % Usage:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
30 % The table automatically resizes to fill the pnContainer (you may modify this via the 'Position' property).
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
31 % The table automatically sets the columns' cell editor and renderer based on the supplied data. Logical values are
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
32 % given a checkbox, strings are left-aligned (numbers are right-aligned). You can always override the defaults.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
33 % You can change column widths by dragging the column borders on the header row.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
34 % You can sort columns by clicking the column header (once to sort descending, once again to sort ascending and once
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
35 % more for the unsorted view). Sorting multiple columns is done by control-clicking all relevant columns (the
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
36 % sorting icon is decreased in size for each additional minor sort col).
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
37 % You can copy/paste any consecutive region of table cells, just as in Excel. You can select entire rows or columns
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
38 % by right-clicking their header. You can also paste Excel data directly, with Ctrl-Shift-V (or use the context
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
39 % menu by right-clicking) at the target table cell.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
40 % For additional tips about how to set multiple aspects of the table, refer to:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
41 % <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/table.html">http://java.sun.com/docs/books/tutorial/uiswing/components/table.html</a>
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
42 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
43 % Programming tips/cues/examples:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
44 % mtable = creatTable(...)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
45 % jtable = mtable.getTable;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
46 % mtable.setVisible(false);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
47 % mtable.setCheckBoxEditor(1); % Set first column to a checkbox (see Note 2 below)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
48 % cb = javax.swing.JComboBox({'First','Last'}); cb.setEditable(true); % prepare an editable drop-down CellEditor
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
49 % editor = javax.swing.DefaultCellEditor(cb);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
50 % jtable.getColumnModel.getColumn(1).setCellEditor(editor); % assign this editor to second column (see Note 2)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
51 % jtable.getColumnModel.getColumn(0).setMaxWidth(20); % Limit width of first (checkbox) column (see Note 2)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
52 % mtable.setEditable(0,false); % Disable editing first column (see note 2 below)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
53 % renderer = javax.swing.table.DefaultTableCellRenderer; % or: renderer = jtable.getColumnModel.getColumn(1).getCellRenderer
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
54 % renderer.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); % useful for numbers rendered as strings e.g.: jtable.setValueAt(sprintf('%.1f',pi,rowIdx,colIdx))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
55 % jtable.getColumnModel.getColumn(1).setCellRenderer(renderer); % right-align second column (see note 2)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
56 % data = cell(mtable.getData); % a cell matrix (mtable.getData is a java.lang.Object[][] object, using base-1 indexing)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
57 % data = mtable.getTableModel.getDataVector; % a java.util.Vector object ([[false, 1.3, 45, ert], [true, 3.14,...]])
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
58 % jtable.setValueAt(value,rowIdx,colIdx); % 0-based Idx - see Note 2 below
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
59 % jtable.getModel.addRow({true, pi, int16(45), 'test'}); % appends a row to the bottom of the table
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
60 % mtable.DataChangedCallback = []; % used to temporarily disable data-change callbacks
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
61 % mtable.DataChangedCallback = @myDataChange_Callback; % myDataChange_Callback is a Matlab function
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
62 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
63 % % Sample dataChange_Callback function
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
64 % function dataChange_Callback(mtable, eventdata)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
65 % if ~ishandle(mtable), return; end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
66 % % Prevent re-entry here if the callback is not thread-safe - see Note 3 below
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
67 % eventDetails = eventdata.getEvent;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
68 % modifiedColIdx = eventDetails.getColumn;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
69 % modifiedRowIdx = eventDetails.getFirstRow;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
70 % if modifiedColIdx>=0 && modifiedRowIdx>=0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
71 % data = mtable.getData;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
72 % newValue = data(modifiedRowIdx+1,modifiedColIdx+1); % see Note 2 below
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
73 % switch modifiedColIdx
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
74 % case ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
75 % end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
76 % end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
77 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
78 % Notes:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
79 % 1. Some (very few) JTable features are inconsistent or unavailable in different jave versions. Type
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
80 % '<a href="matlab:version -java">version -java</a>' at the command prompt to see your specific java version.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
81 % 2. Note that java uses 0-based indexing, while Matlab is 1-based. The returned mtable parameter is a Matlab object
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
82 % (so use 1-base), while mtable.getXXX returns java objects (0-based). jtable above is an example of a java object.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
83 % 3. Modifying mtable.DataChangedCallback within the callback doesn't work - you need to use some global flag/mutex
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
84 % 4. The <Print> button uses Excel to parse and print the table
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
85 % 5. Due to Matlab limitations (specifically, of uitable/UitablePeer) the table is created as a direct child of
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
86 % the container figure (although it is visually positioned within pnContainer)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
87 % 6. To enable sorting functionality, the attached TableSorter.jar file must be located in the java classpath.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
88 % See the Matlab documentation for <a href="matlab:doc javaclasspath">javaclasspath</a>. Note that using
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
89 % javaaddpath(...) to set the path has a nasty side-effect (at least since Matlab 7.2) of clearing all globals!
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
90 % An alternative is to place the pathname for TableSorter.jar in the <a href="matlab:which classpath.txt">classpath.txt</a> file
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
91 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
92 % Known issues/limitations:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
93 % - Column alignment not preserved during Print
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
94 % - Print fails if Excel unavailable (maybe directly print tab-separated text data)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
95 % - Unable to add/delete rows or to print via context menu (right-click)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
96 % - Table is created as a direct child of figure, not pnContainer (see Note 5 above)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
97 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
98 % Bugs and suggestions:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
99 % Please send to Yair Altman (altmany at gmail dot com)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
100 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
101 % See also:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
102 % uitable, java, javaclasspath
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
103 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
104 % Release history:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
105 % 1.0 2007-03-09: initial version
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
106 % 1.1 2007-03-22: fixed selected row# on deletion of bottom row, main comment, missing option; added header tooltip
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
107 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
108 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
109 % 25-02-08 Adapted and imported into LTPDA
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
110 % M Hewitson
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
111 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
112 % $Id: createTable.m,v 1.1 2009/02/03 08:15:30 hewitson Exp $
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
113 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
114 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
115
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
116 % License to use and modify this code is granted freely to all interested, as long as the original author is
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
117 % referenced and attributed as such. The original author maintains the right to be solely associated with this work.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
118
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
119 % Programmed and Copyright by Yair M. Altman: altmany(at)gmail.com
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
120 % $Revision: 1.1 $ $Date: 2009/02/03 08:15:30 $
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
121
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
122 %try
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
123 % Ensure that java swing is enabled...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
124 if ~usejava('swing')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
125 error('createTable:NeedSwing','Java tables require Java Swing.');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
126 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
127
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
128 % Create a panel spanning entire figure area, if panel handle was not supplied
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
129 if (nargin < 1) || isempty(pnContainer) || ~ishandle(pnContainer)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
130 pnContainer = uipanel('parent',gcf,'tag','TablePanel');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
131 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
132 pnContainerPos = getpixelposition(pnContainer,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
133 if isa(handle(pnContainer), 'figure')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
134 pnContainerPos(1:2) = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
135 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
136
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
137 % Get handle to parent figure
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
138 hFig = ancestor(pnContainer,'figure');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
139
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
140 % Determine whether table manipulation buttons are requested
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
141 if nargin < 4 || isempty(buttonsFlag) || ~(isnumeric(buttonsFlag) || islogical(buttonsFlag))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
142 if nargin >= 4, varargin = {buttonsFlag, varargin{:}}; end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
143 buttonsFlag = true;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
144 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
145 if buttonsFlag
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
146 margins = [1,30,0,-30]; % With buttons
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
147 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
148 margins = [1,1,0,0]; % No buttons
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
149 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
150
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
151 % Get the uitable's required position within pnContainer
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
152 tablePosition = pnContainerPos + margins; % Relative to the figure
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
153
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
154 % Set default header names, if not supplied
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
155 if nargin < 2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
156 headers = {'A','B','C'}; % 3 columns by default
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
157 elseif isempty(headers)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
158 headers = cell(1,size(data,2));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
159 elseif ischar(headers)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
160 headers = {headers};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
161 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
162
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
163 % Start with dummy data, just so that uitable can be initialized (or use supplied data, if available)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
164 if nargin < 3 || isempty(data)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
165 numRows = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
166 numCols = length(headers);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
167 data = zeros(1,numCols);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
168 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
169 numRows = size(data,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
170 numCols = size(data,2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
171 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
172 % Convert to cell-format (if not so already)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
173 if ~iscell(data)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
174 data = mat2cell(data,ones(1,size(data,1)),ones(1,numCols));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
175 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
176
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
177 % Create a sortable uitable within pnHandle
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
178 mtable = uitable(hFig, 'position',tablePosition, 'Data',data, 'ColumnNames',headers);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
179 mtable.setNumRows(numRows);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
180 set(mtable,'units','normalized'); % this will resize the table whenever its container is resized
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
181
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
182 % jtable is the underlying java JTable - access to lots more functionality...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
183 % Note: actually, jtable is a com.mathworks.hg.peer.UitablePeer$PeerSpreadsheetTable object, but this extends
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
184 % ^^^^ javax.swing.JTable, so for all practical purposes you may use it as a JTable
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
185 jtable = mtable.getTable;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
186
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
187 % Fix for JTable focus bug : see http://bugs.sun.com/bugdatabase/view_bug.do;:WuuT?bug_id=4709394
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
188 % Taken from: http://xtargets.com/snippets/posts/show/37
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
189 jtable.putClientProperty('terminateEditOnFocusLost', java.lang.Boolean.TRUE);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
190
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
191 % We want to use sorter, not data model...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
192 % unfortunately, UitablePeer expects DefaultTableModel (not TableSorter) so we need a modified UitablePeer class
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
193 % however, UitablePeer is a Matlab class, so instead let's use a modified TableSorter and attach it to the Model
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
194 %sorter = com.mathworks.toolbox.dasstudio.util.TableSorter; % Failed attempt...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
195 %sorter = com.mathworks.mwswing.DefaultSortableTable; % ...another failed attempt...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
196 if ~isempty(which('TableSorter'))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
197 % Add TableSorter as TableModel listener
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
198 sorter = TableSorter(jtable.getModel()); %(table.getTableModel);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
199 %tablePeer = UitablePeer(sorter); % This is not accepted by UitablePeer... - see comment above
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
200 jtable.setModel(sorter);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
201 sorter.setTableHeader(jtable.getTableHeader());
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
202
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
203 % Set the header tooltip (with sorting instructions)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
204 jtable.getTableHeader.setToolTipText('<html> <b>Click</b> to sort up; <b>Shift-click</b> to sort down<br> <b>Ctrl-click</b> (or <b>Ctrl-Shift-click</b>) to sort secondary <br> <b>Click again</b> to change sort direction<br> <b>Click a third time</b> to return to unsorted view<br> <b>Right-click</b> to select entire column</html>');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
205 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
206 % Set the header tooltip (no sorting instructions...)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
207 jtable.getTableHeader.setToolTipText('<html> <b>Click</b> to select entire column<br> <b>Ctrl-click</b> (or <b>Shift-click</b>) to select multiple columns </html>');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
208 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
209
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
210 % Store the uitable's handle within the pnContainer's userdata, for later use
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
211 set(pnContainer,'userdata',[get(pnContainer,'userdata'), mtable]); % add to parent userdata, so we have a handle for deletion
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
212
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
213 % Enable multiple row selection, auto-column resize, and auto-scrollbars
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
214 scroll = mtable.TableScrollPane;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
215 scroll.setVerticalScrollBarPolicy(scroll.VERTICAL_SCROLLBAR_AS_NEEDED);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
216 scroll.setHorizontalScrollBarPolicy(scroll.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
217 jtable.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
218 jtable.setAutoResizeMode(jtable.AUTO_RESIZE_SUBSEQUENT_COLUMNS)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
219
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
220 % Set the jtable name based on the containing panel's tag
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
221 basicTagName = get(pnContainer,'tag');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
222 jtable.setName([basicTagName 'Table']);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
223
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
224 % Move the selection to first table cell (if any data available)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
225 if (jtable.getRowCount > 0)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
226 jtable.changeSelection(0,0,false,false);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
227 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
228
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
229 % Process optional args
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
230 for argIdx = 1 : 2 : length(varargin)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
231 if argIdx<2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
232 % We need this pause to let java complete all table rendering
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
233 % TODO: We should really use calls to awtinvoke() instead, though...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
234 pause(0.05);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
235 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
236 if (length(varargin) > argIdx) % ensure the arg value is there...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
237 varargin{argIdx}(1) = upper(varargin{argIdx}(1)); % property names always start with capital letters...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
238 propMethodName = ['set' varargin{argIdx}];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
239 if ismethod(mtable,propMethodName)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
240 set(mtable,varargin{argIdx},varargin{argIdx+1});
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
241 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
242 %javaMethod(propMethodName, jtable, varargin{argIdx+1});
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
243 set(jtable,varargin{argIdx},varargin{argIdx+1});
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
244 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
245 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
246 end % for argIdx
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
247
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
248 % Create table manipulation buttons
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
249 if buttonsFlag
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
250 buttons = createManipulationButtons(pnContainer,mtable);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
251 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
252 buttons = [];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
253 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
254 %catch
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
255 % Insert your code here
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
256 %handleError;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
257 %end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
258
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
259
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
260 %% --- Executes on button press in btInsert.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
261 function buttons = createManipulationButtons(pnContainer, mtable)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
262 % pnContainer handle to container uipanel
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
263 % mtable handle to mtable (Matlab) object
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
264 %try
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
265 btAppendRow = uicontrol('tag','btTableAppendRow', 'callback',@btTableAppendRow_Callback, 'position', [10,5,60,20], 'string','Append', 'parent',pnContainer, 'userdata',mtable);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
266 btInsertRow = uicontrol('tag','btTableInsertRow', 'callback',@btTableInsertRow_Callback, 'position', [80,5,60,20], 'string','Insert', 'parent',pnContainer, 'userdata',mtable);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
267 btDeleteRow = uicontrol('tag','btTableDeleteRow', 'callback',@btTableDeleteRow_Callback, 'position', [150,5,60,20], 'string','Delete', 'parent',pnContainer, 'userdata',mtable);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
268 btDeleteAll = uicontrol('tag','btTableDeleteAll', 'callback',@btTableDeleteAll_Callback, 'position', [220,5,60,20], 'string','Delete All', 'parent',pnContainer, 'userdata',mtable);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
269 btPrintAll = uicontrol('tag','btTablePrintAll', 'callback',@btTablePrintAll_Callback, 'position', [290,5,60,20], 'string','Print', 'parent',pnContainer, 'userdata',mtable);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
270 buttons = [btInsertRow btAppendRow btDeleteRow btDeleteAll btPrintAll];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
271 if mtable.getNumRows < 1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
272 setVisibility(pnContainer, 'off');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
273 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
274 %catch
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
275 % Insert your code here
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
276 %handleError;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
277 %end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
278
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
279
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
280 %% --- Executes on button press in btInsert.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
281 % Insert a new row immediately before the current row
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
282 function btTableInsertRow_Callback(hObject, eventdata, handles) %#ok
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
283 % hObject handle to btTableInsertRow (see GCBO)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
284 % eventdata reserved - to be defined in a future version of MATLAB
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
285 % handles structure with handles and user data (see GUIDATA)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
286 %try
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
287 mtable = get(hObject,'userdata');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
288 jtable = mtable.getTable;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
289
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
290 % Stop any current editing
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
291 stopEditing(jtable);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
292
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
293 % Insert the new row immediately before the current row
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
294 newRowData = cell(1,mtable.getNumColumns); % empty data
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
295 mtable.getTableModel.insertRow(max(0,jtable.getSelectedRow), newRowData);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
296 %catch
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
297 % Insert your code here
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
298 %handleError;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
299 %end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
300
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
301
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
302 %% --- Executes on button press in btAppend.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
303 % Insert a new row as the last row in the table
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
304 function btTableAppendRow_Callback(hObject, eventdata, handles) %#ok
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
305 % hObject handle to btTableAppendRow (see GCBO)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
306 % eventdata reserved - to be defined in a future version of MATLAB
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
307 % handles structure with handles and user data (see GUIDATA)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
308 %try
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
309 mtable = get(hObject,'userdata');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
310 jtable = mtable.getTable;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
311
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
312 % Stop any current editing
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
313 stopEditing(jtable);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
314
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
315 % Add a new row at the bottom of the data table
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
316 newRowData = cell(1,mtable.getNumColumns); % empty data
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
317 mtable.getTableModel.addRow(newRowData);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
318
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
319 % Move the selection to Column A of this new row
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
320 jtable.changeSelection(jtable.getRowCount-1,0,false,false);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
321
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
322 % There must be at least one table row now, so display the table in any case
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
323 mtable.setVisible(true);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
324 setVisibility(hObject, 'on');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
325 %catch
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
326 % Insert your code here
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
327 %handleError;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
328 %end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
329
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
330
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
331 %% --- Executes on button press in btDelete.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
332 % If there are any rows displayed, then delete the currently-selected row
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
333 function btTableDeleteRow_Callback(hObject, eventdata, handles) %#ok
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
334 % hObject handle to btTableDeleteRow (see GCBO)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
335 % eventdata reserved - to be defined in a future version of MATLAB
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
336 % handles structure with handles and user data (see GUIDATA)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
337 %try
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
338 mtable = get(hObject,'userdata');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
339 jtable = mtable.getTable;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
340
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
341 % Stop any current editing
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
342 stopEditing(jtable);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
343
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
344 % If there are any rows displayed, then delete the currently-selected row
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
345 rowCount = jtable.getRowCount;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
346 if (rowCount > 0) % might be==0 during slow processing & user double-click
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
347 currentRow = max(0,jtable.getSelectedRow);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
348 currentCol = max(0,jtable.getSelectedColumn);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
349 mtable.getTableModel.removeRow(currentRow);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
350 if currentRow >= rowCount-1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
351 jtable.changeSelection(currentRow-1, currentCol, false, false);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
352 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
353 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
354 if (jtable.getRowCount <= 0)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
355 %table.setVisible(false);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
356 setVisibility(hObject, 'off');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
357 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
358 %catch
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
359 % Insert your code here
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
360 %handleError;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
361 %end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
362
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
363
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
364 %% --- Executes on button press in btDeleteAll.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
365 % Deletes all table rows
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
366 function btTableDeleteAll_Callback(hObject, eventdata, handles) %#ok
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
367 % hObject handle to btTableDeleteAll (see GCBO)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
368 % eventdata reserved - to be defined in a future version of MATLAB
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
369 % handles structure with handles and user data (see GUIDATA)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
370 %try
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
371 mtable = get(hObject,'userdata');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
372 jtable = mtable.getTable;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
373
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
374 % Stop any current editing
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
375 stopEditing(jtable);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
376
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
377 % Delete all table rows
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
378 mtable.setNumRows(0);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
379
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
380 % Hide irrelevant controls
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
381 %mtable.setVisible(false);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
382 setVisibility(hObject, 'off');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
383 %catch
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
384 % Insert your code here
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
385 %handleError;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
386 %end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
387
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
388
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
389 %% --- Executes on button press in btPrint.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
390 % Prints the table via Excel
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
391 function btTablePrintAll_Callback(hObject, eventdata, handles) %#ok
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
392 % hObject handle to btTablePrintAll (see GCBO)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
393 % eventdata reserved - to be defined in a future version of MATLAB
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
394 % handles structure with handles and user data (see GUIDATA)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
395 persistent hExcel
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
396 try
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
397 mtable = get(hObject,'userdata');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
398
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
399 % Try to open an Excel COM server
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
400 % Note: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_2003_ta/html/odc_landoffice03_vba.asp
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
401 try
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
402 % try to reuse an existing (pre-opened) COM server
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
403 % If we can't access the ActiveX parent, it means it's closed
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
404 parent = hExcel.parent; %#ok
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
405 catch
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
406 hExcel = actxserver('excel.application');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
407 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
408
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
409 % Try to open the requested document
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
410 hExcel.Workbooks.Add;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
411
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
412 % Format field headers
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
413 headers = cell(mtable.getColumnNames)';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
414 if ~isempty(headers)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
415 hExcel.Range(['A1:' n2a(length(headers)) '1']).Select;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
416 hExcel.Selection.Value = headers;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
417 hExcel.Selection.Font.Bold = true; % bold
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
418 hExcel.Selection.Font.Color = hex2dec('0000FF'); % Red
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
419 hExcel.Selection.Border.Item(4).Weight = 3; % underline
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
420 hExcel.Selection.Cells.HorizontalAlignment = -4108; % =xlCenter
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
421 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
422
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
423 % Set the data from the table
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
424 data = cell(mtable.data);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
425 if ~isempty(headers)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
426 hExcel.Range(['A2:' n2a(size(data,2)) num2str(1+size(data,1))]).Select;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
427 hExcel.Selection.Value = data;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
428 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
429
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
430 % TODO: Change checkbox fields to boolean (TRUE/empty)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
431
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
432 % Other formats
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
433 %hExcel.Cells.HorizontalAlignment = -4108; % =xlCenter % TODO: preserve original jtable column alignment
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
434 hExcel.Cells.EntireColumn.AutoFit;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
435 hExcel.ActiveSheet.DisplayRightToLeft = false;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
436 set(hExcel.ActiveSheet.PageSetup, 'LeftMargin', hExcel.InchesToPoints(0.1), ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
437 'RightMargin', hExcel.InchesToPoints(0.1), ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
438 'HeaderMargin',hExcel.InchesToPoints(0), ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
439 'FooterMargin',hExcel.InchesToPoints(0.1), ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
440 'TopMargin',120, ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
441 'FitToPagesWide',1, ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
442 'FitToPagesTall',1, ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
443 'Orientation','xlPortrait', ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
444 'LeftHeader','&D &T', ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
445 'CenterHeader',char(mtable.getTable.getName), ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
446 'RightHeader','&G');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
447
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
448 % Send to printer
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
449 hExcel.ActiveWindow.SelectedSheets.PrintOut;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
450
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
451 % Close the workbook
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
452 invoke(hExcel.ActiveWindow,'close',false);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
453 catch
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
454 % Insert your code here
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
455 %handleError;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
456 err = lasterror;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
457 try invoke(hExcel.ActiveWindow,'close',false); catch end; % just in case of a printing error
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
458 rethrow(err);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
459 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
460
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
461
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
462 %% --- Convert col # format to 'A','B','C','AA',... format
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
463 % Thanks Brett Shoelson, via CSSM
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
464 function colStr = n2a(c)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
465 t = [floor((c-1)/26)+64, rem(c-1,26)+65];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
466 if (t(1)<65), t(1) = []; end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
467 colStr = char(t);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
468
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
469
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
470 %% --- Executes on button press in btInsert.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
471 function stopEditing(jtable)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
472 %try
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
473 component = jtable.getEditorComponent;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
474 if ~isempty(component)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
475 event = javax.swing.event.ChangeEvent(component);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
476 jtable.editingStopped(event);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
477 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
478 %catch
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
479 % Insert your code here
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
480 %handleError;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
481 %end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
482
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
483
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
484 %% --- Utility function to set visibility of row manipulation buttons
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
485 function setVisibility(hObject, enableStr)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
486 % hObject handle to some element within the figure
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
487 % enableStr 'on' or 'off'
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
488 %try
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
489 hParent = ancestor(hObject,'figure');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
490 set(findall(hParent,'tag','btTableInsertRow'),'enable',enableStr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
491 set(findall(hParent,'tag','btTableDeleteRow'),'enable',enableStr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
492 set(findall(hParent,'tag','btTableDeleteAll'),'enable',enableStr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
493 set(findall(hParent,'tag','btTablePrintAll'), 'enable',enableStr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
494 %catch
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
495 % Insert your code here
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
496 %handleError;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
497 %end
|