|
On Feb 26, 1:14=A0pm, "Gwendolyn Fischer"
<gwendolyn.fisc...@nowhere.com> wrote:
> Mikhail <ra...@gmx.com> wrote in message
>
> <24449352.1203761787051.JavaMail.jaka...@nitrogen.mathforum.
> org>...> >Hi,
>
> > >Thanks for your help. You did really save my time!
> > >Do you know how can I add checkboxes beside any node?
>
> and >how can I change the icon of each node?
>
>
>
> > >Thanks,
> > >Sogol
>
> Hi Sogol,
>
> here is a scribble which may work as a starting point:
>
> <--SNIP-->
>
> function treeExperiment6
> % function based on treeExperiment6 by John Anderson
> % see
> %http://www.mathworks.com/matlabcentral/newsreader/view_threa
> d/104957#269485
> %
> % The mousePressedCallback part is inspired by Yair Altman's
> %https://foxitsoftware.com/foxit_manage/ticketsupport/develop
> er/download.php?file_name=3D20
> % 08-02-06-2119.09.pdf
> %
> % derived from Brad Phelan's tree demo
> % create a tree model based on UITreeNodes and insert into
> uitree.
> % add and remove nodes from the treeModel and update the
> display
> import javax.swing.*
> import javax.swing.tree.*;
>
> % figure window
> f =3D figure('WindowStyle', 'docked', 'Units', 'normalized' );
> f =3D gcf;
>
> b1 =3D uicontrol( 'string','add Node', ...
> =A0 =A0'units' , 'normalized', ...
> =A0 =A0'position', [0 0.5 0.5 0.5], ...
> =A0 =A0'callback', @b1_cb ...
> =A0 =A0);
>
> b2 =3D uicontrol( 'string','remove Node', ...
> =A0 =A0'units' , 'normalized', ...
> =A0 =A0'position', [0.5 0.5 0.5 0.5], ...
> =A0 =A0'callback', @b2_cb ...
> =A0 =A0);
>
> %[I,map] =3D imread
> ([matlab_work_path, '/Projectmanager/checkedIcon.gif']);
> [I,map] =3D checkedIcon;
> javaImage_checked =3D im2java(I,map);
>
> %[I,map] =3D imread
> ([matlab_work_path, '/Projectmanager/unCheckedIcon.gif']);
> [I,map] =3D uncheckedIcon;
> javaImage_unchecked =3D im2java(I,map);
>
> % javaImage_checked and javaImage_unchecked are assumed to
> be of the same
> % width
> iconWidth =3D javaImage_unchecked.getWidth;
>
> % create top node
> rootNode =3D UITreeNode('root', 'File List', ...
> =A0 =A0 [], 0);
> % =A0[matlab_work_path, '/Projectmanager/fileListIcon.gif'],
> 0);
>
> % create two children with checkboxes
> cNode =3D UITreeNode('unselected', 'File A', [], 0); =A0
> % as icon is embedded here we set the icon via java,
> otherwise one could
> % use the uitreenode syntax uitreenode(value, string, icon,
> isLeaf) with
> % icon being a qualified pathname to an image to be used.
> cNode.setIcon(javaImage_unchecked);
> rootNode.add(cNode);
>
> cNode =3D UITreeNode('unselected', 'File B', [], 0);
> cNode.setIcon(javaImage_unchecked);
> rootNode.add(cNode);
>
> % set treeModel
> treeModel =3D DefaultTreeModel( rootNode );
>
> % create the tree
> tree =3D uitree;
> tree.setModel( treeModel );
> % we often rely on the underlying java tree
> jtree =3D tree.getTree;
> % some layout
> set(tree, 'Units', 'normalized',...
> =A0 =A0'position', [0 0 1 0.5]);
> set( tree, 'NodeSelectedCallback', @selected_cb );
>
> % make root the initially selected node
> tree.setSelectedNode( rootNode );
>
> % MousePressedCallback is not supported by the uitree, but
> by jtree
> set(jtree, 'MousePressedCallback', @mousePressedCallback);
>
> =A0 % Set the mouse-press callback
> =A0 function mousePressedCallback(hTree, eventData) %,
> additionalVar)
> =A0 % if eventData.isMetaDown % right-click is like a Meta-
> button
> =A0 % if eventData.getClickCount=3D=3D2 % how to detect double
> clicks
>
> =A0 % Get the clicked node
> =A0 =A0 clickX =3D eventData.getX;
> =A0 =A0 clickY =3D eventData.getY;
> =A0 =A0 treePath =3D jtree.getPathForLocation(clickX, clickY);
> =A0 =A0 % check if a node was clicked
> =A0 =A0 if ~isempty(treePath)
> =A0 =A0 =A0 % check if the checkbox was clicked
> =A0 =A0 =A0 if clickX <=3D (jtree.getPathBounds
> (treePath).x+iconWidth)
> =A0 =A0 =A0 =A0 node =3D treePath.getLastPathComponent;
> =A0 =A0 =A0 =A0 nodeValue =3D node.getValue;
> =A0 =A0 =A0 =A0 % as the value field is the selected/unselected
> flag,
> =A0 =A0 =A0 =A0 % we can also use it to only act on nodes with
> these values
> =A0 =A0 =A0 =A0 switch nodeValue
> =A0 =A0 =A0 =A0 =A0 case 'selected'
> =A0 =A0 =A0 =A0 =A0 =A0 node.setValue('unselected');
> =A0 =A0 =A0 =A0 =A0 =A0 node.setIcon(javaImage_unchecked);
> =A0 =A0 =A0 =A0 =A0 =A0 jtree.treeDidChange();
> =A0 =A0 =A0 =A0 =A0 case 'unselected'
> =A0 =A0 =A0 =A0 =A0 =A0 node.setValue('selected');
> =A0 =A0 =A0 =A0 =A0 =A0 node.setIcon(javaImage_checked);
> =A0 =A0 =A0 =A0 =A0 =A0 jtree.treeDidChange();
> =A0 =A0 =A0 =A0 end
> =A0 =A0 =A0 end
> =A0 =A0 end
> =A0 end % function mousePressedCallback
>
> =A0 =A0function selected_cb( tree, ev )
> =A0 =A0 =A0 =A0nodes =3D tree.getSelectedNodes;
> =A0 =A0 =A0 =A0node =3D nodes(1);
> =A0 =A0 =A0 =A0path =3D node2path(node);
> =A0 =A0 =A0 =A0%path =A0 =A0 =A0
> =A0 =A0end
>
> =A0 =A0function path =3D node2path(node)
> =A0 =A0 =A0 =A0path =3D node.getPath;
> =A0 =A0 =A0 =A0for i=3D1:length(path);
> =A0 =A0 =A0 =A0 =A0 =A0p{i} =3D char(path(i).getName);
> =A0 =A0 =A0 =A0end
> =A0 =A0 =A0 =A0if length(p) > 1
> =A0 =A0 =A0 =A0 =A0 =A0path =3D fullfile(p{:});
> =A0 =A0 =A0 =A0else
> =A0 =A0 =A0 =A0 =A0 =A0path =3D p{1};
> =A0 =A0 =A0 =A0end
> =A0 =A0end
>
> =A0 =A0% add node
> =A0 =A0function b1_cb( h, env )
> =A0 =A0 =A0 =A0nodes =3D tree.getSelectedNodes;
> =A0 =A0 =A0 =A0node =3D nodes(1);
> =A0 =A0 =A0 =A0parent =3D node;
> =A0 =A0 =A0 =A0childNode =3D UITreeNode('dummy', 'Child Node', [], 0);
> =A0 =A0 =A0 =A0treeModel.insertNodeInto
> (childNode,parent,parent.getChildCount());
> =A0 =A0 =A0 =A0% expand to show added child
> =A0 =A0 =A0 =A0tree.setSelectedNode( childNode );
> =A0 =A0 =A0 =A0% insure additional nodes are added to parent
> =A0 =A0 =A0 =A0tree.setSelectedNode( parent );
> =A0 =A0end
>
> =A0 =A0% remove node
> =A0 =A0function b2_cb( h, env )
> =A0 =A0 =A0 =A0nodes =3D tree.getSelectedNodes;
> =A0 =A0 =A0 =A0node =3D nodes(1);
> =A0 =A0 =A0 =A0if ~node.isRoot
> =A0 =A0 =A0 =A0 =A0 =A0nP =3D node.getPreviousSibling;
> =A0 =A0 =A0 =A0 =A0 =A0nN =3D node.getNextSibling;
> =A0 =A0 =A0 =A0 =A0 =A0if ~isempty( nN )
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0tree.setSelectedNode( nN );
> =A0 =A0 =A0 =A0 =A0 =A0elseif ~isempty( nP )
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0tree.setSelectedNode( nP );
> =A0 =A0 =A0 =A0 =A0 =A0else
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0tree.setSelectedNode( node.getParent );
> =A0 =A0 =A0 =A0 =A0 =A0end
> =A0 =A0 =A0 =A0 =A0 =A0%node
> =A0 =A0 =A0 =A0 =A0 =A0treeModel.removeNodeFromParent( node );
> =A0 =A0 =A0 =A0end
> =A0 =A0 end
> end % of main function treeExperiment6
>
> =A0 function [I,map] =3D checkedIcon()
> =A0 =A0 I =3D uint8(...
> =A0 =A0 =A0 =A0 [1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0;
> =A0 =A0 =A0 =A0 =A02,2,2,2,2,2,2,2,2,2,2,2,2,0,0,1;
> =A0 =A0 =A0 =A0 =A02,2,2,2,2,2,2,2,2,2,2,2,0,2,3,1;
> =A0 =A0 =A0 =A0 =A02,2,1,1,1,1,1,1,1,1,1,0,2,2,3,1;
> =A0 =A0 =A0 =A0 =A02,2,1,1,1,1,1,1,1,1,0,1,2,2,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0 2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,1;
> =A0 =A0 =A0 =A0 2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
> =A0 =A0 =A0 =A0 2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
> =A0 =A0 =A0 =A0 2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
> =A0 =A0 =A0 =A0 2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
> =A0 =A0 =A0 =A0 2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
> =A0 =A0 =A0 =A0 2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
> =A0 =A0 =A0 =A0 2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
> =A0 =A0 =A0 =A0 2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
> =A0 =A0 =A0 =A0 2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
> =A0 =A0 =A0 =A0 2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
> =A0 =A0 =A0 =A0 2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,1;
> =A0 =A0 =A0 =A0 2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,1;
> =A0 =A0 =A0 =A0 1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1]);
> =A0 =A0 =A0map =3D ...
> =A0 =A0 =A0 [0.023529,0.4902,0;
> =A0 =A0 =A0 =A01,1,1;
> =A0 =A0 =A0 =A00,0,0;
> =A0 =A0 =A0 =A00.50196,0.50196,0.50196;
> =A0 =A0 =A0 =A00.50196,0.50196,0.50196;
> =A0 =A0 =A0 =A00,0,0;
> =A0 =A0 =A0 =A00,0,0;
> =A0 =A0 =A0 =A00,0,0];
> =A0 end
>
> <--SNAP-->
>
> Beware of the line wraps.
>
> Hope this helps.
>
> Gwendolyn
Thanks Gwendolyn,
It was what I searched for!
|