WARNING: This feature is not supported in MATLAB and the API and functionality may change in a future release. UITREE creates a uitree component with hierarchical data in a figure window. UITREE creates an empty uitree object with default property values in a figure window. UITREE('PropertyName1', 'Value1', 'PropertyName2', 'Value2', ...) creates a uitree object with the specified properties. The properties that can be set are: Root, ExpandFcn, SelectionChangeFcn, Parent and Position. The 'Root' property must be specified to successfully to create a uitree. The other properties are optional. UITREE(figurehandle, ...) creates a uitree object in the figure window specified by the figurehandle. HANDLE = UITREE(...) creates a uitree object and returns its handle. Properties: Root - Root node for the uitree object. Could be handle to a HG object, a string, an open block diagram name, or handle to a UITREENODE object. ExpandFcn - Node expansion function. String or function handle. SelectionChangeFcn - Selection callback function. String or function handle. Parent - Parent figure handle. If not specified, it is the gcf. Position: 4 element vector specifying the position. DndEnabled: Boolean specifying if drag and drop is enabled (false). MultipleSelectionEnabled: Boolean specifying if multiple selection is allowed (false). SelectedNodes: vector of uitreenodes to be selected. Units: String - pixels/normalized/inches/points/centimeters. Visible: Boolean specifying if table is visible. NodeDroppedCallback: Callback for a drag and drop action. NodeExpandedCallback: Callback for a node expand action. NodeCollapsedCallback: Callback function for a node collapse action. NodeSelectedCallback: Callback for a node selection action. Examples: t = uitree('Root', 'D:\') %Creates a uitree widget in a figure window with which acts as a %directory browser with the D: drive as the root node. surf(peaks) f = figure t = uitree(f, 'Root', 0) %Creates a uitree object in the specified figure window which acts as %a MATLAB hierarchy browser with the MATLAB root (0) as the root node. root = uitreenode('S:\', 'S', [], false); t = uitree('Root', root, 'ExpandFcn', @myExpfcn, ... 'SelectionChangeFcn', 'disp(''Selection Changed'')'); %Creates a uitree object with the specified root node and a custom %function to return child nodes for any given node. The function %myExpfcn is an m-file in the MATLAB path with the following code: %This function should be added to your path % --------------------------------------------- function nodes = myExpfcn(tree, value) try count = 0; ch = dir(value); for i=1:length(ch) if (any(strcmp(ch(i).name, {'.', '..', ''})) == 0) count = count + 1; if ch(i).isdir iconpath = [matlabroot, '/toolbox/matlab/icons/foldericon.gif']; else iconpath = [matlabroot, '/toolbox/matlab/icons/pageicon.gif']; end nodes(count) = uitreenode([value, ch(i).name, filesep], ... ch(i).name, iconpath, ~ch(i).isdir); end end catch error(['The uitree node type is not recognized. You may need to ', ... 'define an ExpandFcn for the nodes.']); end if (count == 0) nodes = []; end % --------------------------------------------- See also UITREENODE, UITABLE, PATH Copyright 2003-2006 The MathWorks, Inc. $Revision: 1.2 $ $Date: 2008/01/22 20:45:22 $ Adapted for LTPDA $Id: ltpda_uitree.m,v 1.2 2008/01/22 20:45:22 hewitson Exp $
0001 function [tree, container] = ltpda_uitree(varargin) 0002 % WARNING: This feature is not supported in MATLAB 0003 % and the API and functionality may change in a future release. 0004 % 0005 % UITREE creates a uitree component with hierarchical data in a figure window. 0006 % UITREE creates an empty uitree object with default property values in 0007 % a figure window. 0008 % 0009 % UITREE('PropertyName1', 'Value1', 'PropertyName2', 'Value2', ...) 0010 % creates a uitree object with the specified properties. The properties 0011 % that can be set are: Root, ExpandFcn, SelectionChangeFcn, Parent and 0012 % Position. The 'Root' property must be specified to successfully to 0013 % create a uitree. The other properties are optional. 0014 % 0015 % UITREE(figurehandle, ...) creates a uitree object in the figure 0016 % window specified by the figurehandle. 0017 % 0018 % HANDLE = UITREE(...) creates a uitree object and returns its handle. 0019 % 0020 % Properties: 0021 % 0022 % Root - Root node for the uitree object. Could be handle to a HG 0023 % object, a string, an open block diagram name, or handle to a 0024 % UITREENODE object. 0025 % ExpandFcn - Node expansion function. String or function handle. 0026 % SelectionChangeFcn - Selection callback function. String or function 0027 % handle. 0028 % Parent - Parent figure handle. If not specified, it is the gcf. 0029 % Position: 4 element vector specifying the position. 0030 % 0031 % DndEnabled: Boolean specifying if drag and drop is enabled (false). 0032 % MultipleSelectionEnabled: Boolean specifying if multiple selection is 0033 % allowed (false). 0034 % SelectedNodes: vector of uitreenodes to be selected. 0035 % Units: String - pixels/normalized/inches/points/centimeters. 0036 % Visible: Boolean specifying if table is visible. 0037 % NodeDroppedCallback: Callback for a drag and drop action. 0038 % NodeExpandedCallback: Callback for a node expand action. 0039 % NodeCollapsedCallback: Callback function for a node collapse action. 0040 % NodeSelectedCallback: Callback for a node selection action. 0041 % 0042 % 0043 % Examples: 0044 % t = uitree('Root', 'D:\') 0045 % 0046 % %Creates a uitree widget in a figure window with which acts as a 0047 % %directory browser with the D: drive as the root node. 0048 % 0049 % surf(peaks) 0050 % f = figure 0051 % t = uitree(f, 'Root', 0) 0052 % 0053 % %Creates a uitree object in the specified figure window which acts as 0054 % %a MATLAB hierarchy browser with the MATLAB root (0) as the root node. 0055 % 0056 % root = uitreenode('S:\', 'S', [], false); 0057 % t = uitree('Root', root, 'ExpandFcn', @myExpfcn, ... 0058 % 'SelectionChangeFcn', 'disp(''Selection Changed'')'); 0059 % 0060 % %Creates a uitree object with the specified root node and a custom 0061 % %function to return child nodes for any given node. The function 0062 % %myExpfcn is an m-file in the MATLAB path with the following code: 0063 % 0064 % %This function should be added to your path 0065 % % --------------------------------------------- 0066 % function nodes = myExpfcn(tree, value) 0067 % 0068 % try 0069 % count = 0; 0070 % ch = dir(value); 0071 % 0072 % for i=1:length(ch) 0073 % if (any(strcmp(ch(i).name, {'.', '..', ''})) == 0) 0074 % count = count + 1; 0075 % if ch(i).isdir 0076 % iconpath = [matlabroot, '/toolbox/matlab/icons/foldericon.gif']; 0077 % else 0078 % iconpath = [matlabroot, '/toolbox/matlab/icons/pageicon.gif']; 0079 % end 0080 % nodes(count) = uitreenode([value, ch(i).name, filesep], ... 0081 % ch(i).name, iconpath, ~ch(i).isdir); 0082 % end 0083 % end 0084 % catch 0085 % error(['The uitree node type is not recognized. You may need to ', ... 0086 % 'define an ExpandFcn for the nodes.']); 0087 % end 0088 % 0089 % if (count == 0) 0090 % nodes = []; 0091 % end 0092 % % --------------------------------------------- 0093 % 0094 % See also UITREENODE, UITABLE, PATH 0095 % 0096 % Copyright 2003-2006 The MathWorks, Inc. 0097 % $Revision: 1.2 $ $Date: 2008/01/22 20:45:22 $ 0098 % 0099 % Adapted for LTPDA 0100 % 0101 % $Id: ltpda_uitree.m,v 1.2 2008/01/22 20:45:22 hewitson Exp $ 0102 % 0103 0104 % Release: R14. This feature will not work in previous versions of MATLAB. 0105 0106 %% Setup and P-V parsing. 0107 0108 error(javachk('awt')); 0109 error(nargoutchk(0, 2, nargout)); 0110 0111 fig = []; 0112 numargs = nargin; 0113 0114 if (nargin > 0 && isscalar(varargin{1}) && ishandle(varargin{1})) 0115 if ~isa(handle(varargin{1}), 'figure') 0116 error('MATLAB:uitree:InvalidFigureHandle', 'Unrecognized parameter.'); 0117 end 0118 fig = varargin{1}; 0119 varargin = varargin(2:end); 0120 numargs = numargs - 1; 0121 end 0122 0123 % RootFound = false; 0124 root = []; 0125 expfcn = []; 0126 selfcn = []; 0127 pos = []; 0128 % parent = []; 0129 0130 if (numargs == 1) 0131 error('MATLAB:uitree:InvalidNumInputs', 'Unrecognized parameter.'); 0132 end 0133 0134 for i = 1:2:numargs-1 0135 if ~ischar(varargin{i}) 0136 error('MALTAB:uitree:UnrecognizedParameter', 'Unrecognized parameter.'); 0137 0138 end 0139 switch lower(varargin{i}) 0140 case 'root' 0141 root = varargin{i+1}; 0142 case 'expandfcn' 0143 expfcn = varargin{i+1}; 0144 case 'selectionchangefcn' 0145 selfcn = varargin{i+1}; 0146 case 'parent' 0147 if ishandle(varargin{i+1}) 0148 f = varargin{i+1}; 0149 if isa(handle(f), 'figure') 0150 fig = f; 0151 end 0152 end 0153 case 'position' 0154 p = varargin{i+1}; 0155 if isnumeric(p) && (length(p) == 4) 0156 pos = p; 0157 end 0158 otherwise 0159 error('MALTAB:uitree:UnknownParameter', ['Unrecognized parameter: ', varargin{i}]); 0160 end 0161 end 0162 0163 if isempty(expfcn) 0164 [root, expfcn] = processNode(root); 0165 else 0166 root = processNode(root); 0167 end 0168 0169 tree_h = com.mathworks.hg.peer.UITreePeer; 0170 tree_h.setRoot(root); 0171 0172 if isempty(fig) 0173 fig = gcf; 0174 end 0175 0176 if isempty(pos) 0177 figpos = get(fig, 'Position'); 0178 pos = [0 0 min(figpos(3), 200) figpos(4)]; 0179 end 0180 0181 % pass the figure child in, let javacomponent introspect 0182 [obj, container] = javacomponent(tree_h, pos, fig); 0183 % javacomponent returns a UDD handle for the java component passed in. 0184 tree = obj; 0185 0186 if ~isempty(expfcn) 0187 set(tree, 'NodeExpandedCallback', {@nodeExpanded, tree, expfcn}); 0188 end 0189 0190 if ~isempty(selfcn) 0191 set(tree, 'NodeSelectedCallback', {@nodeSelected, tree, selfcn}); 0192 end 0193 0194 end 0195 0196 %% ----------------------------------------------------- 0197 function nodeExpanded(src, evd, tree, expfcn) %#ok 0198 0199 % tree = handle(src); 0200 % evdsrc = evd.getSource; 0201 0202 evdnode = evd.getCurrentNode; 0203 % indices = []; 0204 0205 if ~tree.isLoaded(evdnode) 0206 value = evdnode.getValue; 0207 0208 % <call a user function(value) which returns uitreenodes>; 0209 cbk = expfcn; 0210 if iscell(cbk) 0211 childnodes = feval(cbk{1}, tree, value, cbk{2:end}); 0212 else 0213 childnodes = feval(cbk, tree, value); 0214 end 0215 0216 if (length(childnodes) == 1) 0217 % Then we dont have an array of nodes. Create an array. 0218 chnodes = childnodes; 0219 childnodes = javaArray('com.mathworks.hg.peer.UITreeNode', 1); 0220 childnodes(1) = java(chnodes); 0221 end 0222 0223 tree.add(evdnode, childnodes); 0224 tree.setLoaded(evdnode, true); 0225 end 0226 0227 end 0228 0229 %% ----------------------------------------------------- 0230 function nodeSelected(src, evd, tree, selfcn) %#ok 0231 cbk = selfcn; 0232 hgfeval(cbk, tree, evd); 0233 0234 end 0235 0236 %% ----------------------------------------------------- 0237 function [node, expfcn] = processNode(root) 0238 expfcn = []; 0239 0240 if isempty(root) || isa(root, 'com.mathworks.hg.peer.UITreeNode') || ... 0241 isa(root, 'javahandle.com.mathworks.hg.peer.UITreeNode') 0242 node = root; 0243 elseif ishghandle(root) 0244 % Try to process as an HG object. 0245 try 0246 node = uitreenode(handle(root), get(root, 'Type'), ... 0247 [], isempty(get(0, 'children'))); 0248 catch 0249 node = []; 0250 end 0251 expfcn = @hgBrowser; 0252 elseif ismodel(root) 0253 % Try to process as an open Simulink system 0254 0255 % TODO if there is an open simulink system and a directory on the path with 0256 % the same name, the system will hide the directory. Perhaps we should 0257 % warn about this. 0258 try 0259 h = handle(get_param(root,'Handle')); 0260 % TODO we pass root to the tree as a string, 0261 % it would be better if we could just pass the 0262 % handle up 0263 node = uitreenode(root, get(h, 'Name'), ... 0264 [], isempty(h.getHierarchicalChildren)); 0265 catch 0266 node = []; 0267 end 0268 expfcn = @mdlBrowser; 0269 elseif ischar(root) 0270 % Try to process this as a directory structure. 0271 try 0272 iconpath = [matlabroot, '/toolbox/matlab/icons/foldericon.gif']; 0273 node = uitreenode(root, root, iconpath, ~isdir(root)); 0274 catch 0275 node = []; 0276 end 0277 expfcn = @dirBrowser; 0278 else 0279 node = []; 0280 end 0281 0282 end 0283 0284 %% ----------------------------------------------------- 0285 function nodes = hgBrowser(tree, value) %#ok 0286 0287 try 0288 count = 0; 0289 parent = handle(value); 0290 ch = parent.children; 0291 0292 for i=1:length(ch) 0293 count = count+1; 0294 nodes(count) = uitreenode(handle(ch(i)), get(ch(i), 'Type'), [], ... 0295 isempty(get(ch(i), 'children'))); 0296 end 0297 catch 0298 error('MATLAB:uitree:UnknownNodeType', ['The uitree node type is not recognized. You may need to ', ... 0299 'define an ExpandFcn for the nodes.']); 0300 end 0301 0302 if (count == 0) 0303 nodes = []; 0304 end 0305 0306 end 0307 0308 %% ----------------------------------------------------- 0309 function nodes = mdlBrowser(tree, value) %#ok 0310 0311 try 0312 count = 0; 0313 parent = handle(get_param(value,'Handle')); 0314 ch = parent.getHierarchicalChildren; 0315 0316 for i=1:length(ch) 0317 if isempty(findstr(class(ch(i)),'SubSystem')) 0318 % not a subsystem 0319 else 0320 % is a subsystem 0321 count = count+1; 0322 descr = get(ch(i),'Name'); 0323 isleaf = true; 0324 cch = ch(i).getHierarchicalChildren; 0325 if ~isempty(cch) 0326 for j = 1:length(cch) 0327 if ~isempty(findstr(class(cch(j)),'SubSystem')) 0328 isleaf = false; 0329 break; 0330 end 0331 end 0332 end 0333 nodes(count) = uitreenode([value '/' descr], descr, [], ... 0334 isleaf); 0335 end 0336 end 0337 catch 0338 error('MATLAB:uitree:UnknownNodeType', ['The uitree node type is not recognized. You may need to ', ... 0339 'define an ExpandFcn for the nodes.']); 0340 end 0341 0342 if (count == 0) 0343 nodes = []; 0344 end 0345 0346 end 0347 0348 0349 %% ----------------------------------------------------- 0350 function nodes = dirBrowser(tree, value) %#ok 0351 0352 try 0353 count = 0; 0354 ch = dir(value); 0355 0356 for i=1:length(ch) 0357 if (any(strcmp(ch(i).name, {'.', '..', ''})) == 0) 0358 count = count + 1; 0359 if ch(i).isdir 0360 iconpath = [matlabroot, '/toolbox/matlab/icons/foldericon.gif']; 0361 else 0362 iconpath = [matlabroot, '/toolbox/matlab/icons/pageicon.gif']; 0363 end 0364 nodes(count) = uitreenode([value, ch(i).name, filesep], ... 0365 ch(i).name, iconpath, ~ch(i).isdir); 0366 end 0367 end 0368 catch 0369 error('MATLAB:uitree:UnknownNodeType', ['The uitree node type is not recognized. You may need to ', ... 0370 'define an ExpandFcn for the nodes.']); 0371 end 0372 0373 if (count == 0) 0374 nodes = []; 0375 end 0376 0377 end 0378 0379 %% ----------------------------------------------------- 0380 function yesno = ismodel(input) 0381 yesno = false; 0382 0383 try 0384 get_param(input,'handle'); 0385 yesno = true; 0386 catch 0387 end 0388 0389 end