Home > m > gui > ao_browser > ltpda_uitree.m

ltpda_uitree

PURPOSE ^

WARNING: This feature is not supported in MATLAB

SYNOPSIS ^

function [tree, container] = ltpda_uitree(varargin)

DESCRIPTION ^

 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.1 $  $Date: 2007/06/14 14:53:32 $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

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.1 $  $Date: 2007/06/14 14:53:32 $
0098 
0099 %   Release: R14. This feature will not work in previous versions of MATLAB.
0100 
0101 %% Setup and P-V parsing.
0102 
0103 error(javachk('awt'));
0104 error(nargoutchk(0, 2, nargout));
0105 
0106 fig = [];
0107 numargs = nargin;
0108 
0109 if (nargin > 0 && isscalar(varargin{1}) && ishandle(varargin{1}))
0110     if ~isa(handle(varargin{1}), 'figure')
0111         error('MATLAB:uitree:InvalidFigureHandle', 'Unrecognized parameter.');
0112     end
0113     fig = varargin{1};
0114     varargin = varargin(2:end);
0115     numargs = numargs - 1;
0116 end
0117 
0118 % RootFound = false;
0119 root   = [];
0120 expfcn = [];
0121 selfcn = [];
0122 pos    = [];
0123 % parent = [];
0124 
0125 if (numargs == 1)
0126     error('MATLAB:uitree:InvalidNumInputs', 'Unrecognized parameter.');
0127 end
0128 
0129 for i = 1:2:numargs-1
0130     if ~ischar(varargin{i})
0131         error('MALTAB:uitree:UnrecognizedParameter', 'Unrecognized parameter.');
0132 
0133     end
0134     switch lower(varargin{i})
0135         case 'root'
0136             root = varargin{i+1};
0137         case 'expandfcn'
0138             expfcn = varargin{i+1};
0139         case 'selectionchangefcn'
0140             selfcn = varargin{i+1};
0141         case 'parent'
0142             if ishandle(varargin{i+1})
0143                 f = varargin{i+1};
0144                 if isa(handle(f), 'figure')
0145                     fig = f;
0146                 end
0147             end
0148         case 'position'
0149             p = varargin{i+1};
0150             if isnumeric(p) && (length(p) == 4)
0151                 pos = p;
0152             end
0153         otherwise
0154             error('MALTAB:uitree:UnknownParameter', ['Unrecognized parameter: ', varargin{i}]);
0155     end
0156 end
0157 
0158 if isempty(expfcn)
0159     [root, expfcn] = processNode(root);
0160 else
0161     root = processNode(root);
0162 end
0163 
0164 tree_h = com.mathworks.hg.peer.UITreePeer;
0165 tree_h.setRoot(root);
0166 
0167 if isempty(fig)
0168     fig = gcf;
0169 end
0170 
0171 if isempty(pos)
0172     figpos = get(fig, 'Position');
0173     pos =  [0 0 min(figpos(3), 200) figpos(4)];
0174 end
0175 
0176 % pass the figure child in, let javacomponent introspect
0177 [obj, container] = javacomponent(tree_h, pos, fig);
0178 % javacomponent returns a UDD handle for the java component passed in.
0179 tree = obj;
0180 
0181 if ~isempty(expfcn)
0182     set(tree, 'NodeExpandedCallback', {@nodeExpanded, tree, expfcn});
0183 end
0184 
0185 if ~isempty(selfcn)
0186     set(tree, 'NodeSelectedCallback', {@nodeSelected, tree, selfcn});
0187 end
0188 
0189 end
0190 
0191 %% -----------------------------------------------------
0192 function nodeExpanded(src, evd, tree, expfcn)                           %#ok
0193 
0194 % tree = handle(src);
0195 % evdsrc = evd.getSource;
0196 
0197 evdnode  = evd.getCurrentNode;
0198 % indices = [];
0199 
0200 if ~tree.isLoaded(evdnode)
0201     value = evdnode.getValue;
0202 
0203     % <call a user function(value) which returns uitreenodes>;
0204     cbk = expfcn;
0205     if iscell(cbk)
0206         childnodes = feval(cbk{1}, tree, value, cbk{2:end});
0207     else
0208         childnodes = feval(cbk, tree, value);
0209     end
0210 
0211     if (length(childnodes) == 1)
0212         % Then we dont have an array of nodes. Create an array.
0213         chnodes = childnodes;
0214         childnodes = javaArray('com.mathworks.hg.peer.UITreeNode', 1);
0215         childnodes(1) = java(chnodes);
0216     end
0217 
0218     tree.add(evdnode, childnodes);
0219     tree.setLoaded(evdnode, true);
0220 end
0221 
0222 end
0223 
0224 %% -----------------------------------------------------
0225 function nodeSelected(src, evd, tree, selfcn)                           %#ok
0226 cbk = selfcn;
0227 hgfeval(cbk, tree, evd);
0228 
0229 end
0230 
0231 %% -----------------------------------------------------
0232 function [node, expfcn] = processNode(root)
0233 expfcn = [];
0234 
0235 if isempty(root) || isa(root, 'com.mathworks.hg.peer.UITreeNode') || ...
0236         isa(root, 'javahandle.com.mathworks.hg.peer.UITreeNode')
0237     node = root;
0238 elseif ishghandle(root)
0239     % Try to process as an HG object.
0240     try
0241         node = uitreenode(handle(root), get(root, 'Type'), ...
0242             [], isempty(get(0, 'children')));
0243     catch
0244         node = [];
0245     end
0246     expfcn = @hgBrowser;
0247 elseif ismodel(root)
0248     % Try to process as an open Simulink system
0249 
0250     % TODO if there is an open simulink system and a directory on the path with
0251     % the same name, the system will hide the directory. Perhaps we should
0252     % warn about this.
0253     try
0254         h = handle(get_param(root,'Handle'));
0255         % TODO we pass root to the tree as a string,
0256         % it would be better if we could just pass the
0257         % handle up
0258         node = uitreenode(root, get(h, 'Name'), ...
0259             [], isempty(h.getHierarchicalChildren));
0260     catch
0261         node = [];
0262     end
0263     expfcn = @mdlBrowser;
0264 elseif ischar(root)
0265     % Try to process this as a directory structure.
0266     try
0267         iconpath = [matlabroot, '/toolbox/matlab/icons/foldericon.gif'];
0268         node = uitreenode(root, root, iconpath, ~isdir(root));
0269     catch
0270         node = [];
0271     end
0272     expfcn = @dirBrowser;
0273 else
0274     node = [];
0275 end
0276 
0277 end
0278 
0279 %% -----------------------------------------------------
0280 function nodes = hgBrowser(tree, value)                                 %#ok
0281 
0282 try
0283     count = 0;
0284     parent = handle(value);
0285     ch = parent.children;
0286 
0287     for i=1:length(ch)
0288         count = count+1;
0289         nodes(count) = uitreenode(handle(ch(i)), get(ch(i), 'Type'), [], ...
0290             isempty(get(ch(i), 'children')));
0291     end
0292 catch
0293     error('MATLAB:uitree:UnknownNodeType', ['The uitree node type is not recognized. You may need to ', ...
0294         'define an ExpandFcn for the nodes.']);
0295 end
0296 
0297 if (count == 0)
0298     nodes = [];
0299 end
0300 
0301 end
0302 
0303 %% -----------------------------------------------------
0304 function nodes = mdlBrowser(tree, value)                                %#ok
0305 
0306 try
0307     count = 0;
0308     parent = handle(get_param(value,'Handle'));
0309     ch = parent.getHierarchicalChildren;
0310 
0311     for i=1:length(ch)
0312         if isempty(findstr(class(ch(i)),'SubSystem'))
0313             % not a subsystem
0314         else
0315             % is a subsystem
0316             count = count+1;
0317             descr = get(ch(i),'Name');
0318             isleaf = true;
0319             cch =  ch(i).getHierarchicalChildren;
0320             if ~isempty(cch)
0321                 for j = 1:length(cch)
0322                     if ~isempty(findstr(class(cch(j)),'SubSystem'))
0323                         isleaf = false;
0324                         break;
0325                     end
0326                 end
0327             end
0328             nodes(count) = uitreenode([value '/' descr], descr, [], ...
0329                 isleaf);
0330         end
0331     end
0332 catch
0333     error('MATLAB:uitree:UnknownNodeType', ['The uitree node type is not recognized. You may need to ', ...
0334         'define an ExpandFcn for the nodes.']);
0335 end
0336 
0337 if (count == 0)
0338     nodes = [];
0339 end
0340 
0341 end
0342 
0343 
0344 %% -----------------------------------------------------
0345 function nodes = dirBrowser(tree, value)                                %#ok
0346 
0347 try
0348     count = 0;
0349     ch = dir(value);
0350 
0351     for i=1:length(ch)
0352         if (any(strcmp(ch(i).name, {'.', '..', ''})) == 0)
0353             count = count + 1;
0354             if ch(i).isdir
0355                 iconpath = [matlabroot, '/toolbox/matlab/icons/foldericon.gif'];
0356             else
0357                 iconpath = [matlabroot, '/toolbox/matlab/icons/pageicon.gif'];
0358             end
0359             nodes(count) = uitreenode([value, ch(i).name, filesep], ...
0360                 ch(i).name, iconpath, ~ch(i).isdir);
0361         end
0362     end
0363 catch
0364     error('MATLAB:uitree:UnknownNodeType', ['The uitree node type is not recognized. You may need to ', ...
0365         'define an ExpandFcn for the nodes.']);
0366 end
0367 
0368 if (count == 0)
0369     nodes = [];
0370 end
0371 
0372 end
0373 
0374 %% -----------------------------------------------------
0375 function yesno = ismodel(input)
0376 yesno = false;
0377 
0378 try
0379     get_param(input,'handle');
0380     yesno = true;
0381 catch
0382 end
0383 
0384 end

Generated on Mon 03-Sep-2007 12:12:34 by m2html © 2003