Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » TreeViewer Custom Expand(Expand Treeviewer's specified nodes only)
TreeViewer Custom Expand [message #1061645] Mon, 03 June 2013 07:46 Go to next message
Sudesh Bulathsinhala is currently offline Sudesh Bulathsinhala
Messages: 180
Registered: October 2010
Senior Member
hello,

I need to expand the tree-viewer nodes conditionally.
i.e. expand nodes other than config type within tree-viewer.

tree-viewer.expandToLevel(N) does not help us here.

the requirement is to introduce method which expand only specified node types to N number of levels.

Ex.

    - dataset1
        + config
        - data
            - elements
                - element1
                - element2
            - attributes
                - attribute1
                - attribute2
    - dataset2
        + config
        - data
            - elements
                - element1
            - attributes
                - attribute1
                - attribute2
                - attribute3



Any suggestions ?
Re: TreeViewer Custom Expand [message #1061755 is a reply to message #1061645] Mon, 03 June 2013 14:57 Go to previous messageGo to next message
Greg Pugh is currently offline Greg Pugh
Messages: 15
Registered: July 2009
Junior Member
On 2013-06-03 11:46:58 +0000, Sudesh Bulathsinhala said:

> hello,
>
> I need to expand the tree-viewer nodes conditionally.
> i.e. expand nodes other than config type within tree-viewer.
>
> tree-viewer.expandToLevel(N) does not help us here.
>
> the requirement is to introduce method which expand only specified node
> types to N number of levels.
>
> Ex.
>
>
> - dataset1
> + config
> - data
> - elements
> - element1
> - element2
> - attributes
> - attribute1
> - attribute2
> - dataset2
> + config
> - data
> - elements
> - element1
> - attributes
> - attribute1
> - attribute2
> - attribute3
>
>
>
> Any suggestions ?

TreeViewer setExpandedElements takes an array of elements to expand
Re: TreeViewer Custom Expand [message #1061872 is a reply to message #1061755] Tue, 04 June 2013 08:59 Go to previous messageGo to next message
Sudesh Bulathsinhala is currently offline Sudesh Bulathsinhala
Messages: 180
Registered: October 2010
Senior Member
Hello Greg,

that didn't help..

tried the following and failed too..

// get the expanded elements
Object[] objects  = viewer.getExpandedElements();

// clear the viewer and reload
viewer.setInput(null);
final DesignerCommonRootNode rootNode = new DesignerCommonRootNode();
		rootNode.setHandler(DatasetDesignerEditor.this);
		rootNode.addAll(getNodes(rootNode));
		viewer.setInput(rootNode);

// set the expanded elements again
viewer.setExpandedElements(objects);


This does not work as expected..

Also tried with

// get the expanded tree paths
TreePath[] paths = viewer.getExpandedTreePaths();

// clear the viewer and reload
viewer.setInput(null);
final DesignerCommonRootNode rootNode = new DesignerCommonRootNode();
		rootNode.setHandler(DatasetDesignerEditor.this);
		rootNode.addAll(getNodes(rootNode));
		viewer.setInput(rootNode);

// set the expanded tree paths again
viewer.setExpandedTreePaths(treePaths);


once the above scenario works, i can remove the unwanted objects from the array.
any idea ?

Thanks & Regards,
Sudesh
Re: TreeViewer Custom Expand [message #1062251 is a reply to message #1061872] Thu, 06 June 2013 13:39 Go to previous messageGo to next message
Greg Pugh is currently offline Greg Pugh
Messages: 15
Registered: July 2009
Junior Member
On 2013-06-04 12:59:29 +0000, Sudesh Bulathsinhala said:

> Hello Greg,
>
> that didn't help..
>
> tried the following and failed too..
>
>
> // get the expanded elements
> Object[] objects = viewer.getExpandedElements();
>
> // clear the viewer and reload
> viewer.setInput(null);
> final DesignerCommonRootNode rootNode = new DesignerCommonRootNode();
> rootNode.setHandler(DatasetDesignerEditor.this);
> rootNode.addAll(getNodes(rootNode));
> viewer.setInput(rootNode);
>
> // set the expanded elements again
> viewer.setExpandedElements(objects);
>
>
> This does not work as expected..
>
> Also tried with
>
> // get the expanded tree paths
> TreePath[] paths = viewer.getExpandedTreePaths();
>
> // clear the viewer and reload
> viewer.setInput(null);
> final DesignerCommonRootNode rootNode = new DesignerCommonRootNode();
> rootNode.setHandler(DatasetDesignerEditor.this);
> rootNode.addAll(getNodes(rootNode));
> viewer.setInput(rootNode);
>
> // set the expanded tree paths again
> viewer.setExpandedTreePaths(treePaths);
>
>
> once the above scenario works, i can remove the unwanted objects from
> the array.
> any idea ?
>
> Thanks & Regards,
> Sudesh

For this to work the new nodes need to be equal to the old nodes -
equals() must match to old and new node and also hashCode() for the new
object must be the same as the old object.

You can also look at the viewer setComparer() method and
IElementComparer which lets you compare objects using custom hashCode
and equals methods.
Re: TreeViewer Custom Expand - SOLVED ! [message #1064235 is a reply to message #1062251] Tue, 18 June 2013 06:49 Go to previous message
Sudesh Bulathsinhala is currently offline Sudesh Bulathsinhala
Messages: 180
Registered: October 2010
Senior Member
hello all,

figured it out and wanted to share how i resolved.

basically you need to iterate over tree-items and compare their associated model for particular type object, hence call setExpanded(boolean expanded) method to expand/collapse nodes dynamically.

call the following method within public void createPartControl(final Composite parent) or custom event to dynamically expand/collapse different types of nodes based on underlying model.

        private void resetDefaultExpandedNodes() {
                viewer.getControl().setRedraw(false);
		viewer.expandAll();
		for (final TreeItem item : viewer.getTree().getItems()) {
			processExpandTreeItem(item);
		}
                viewer.getControl().setRedraw(true);
	}


	private void processExpandTreeItem(final TreeItem treeItem) {
		if (treeItem.getData() instanceof IDesignerNode) {
			if (((IDesignerNode) treeItem.getData()).getModel() != null) {
				if (((IDesignerNode) treeItem.getData()).getModel() instanceof CompositePanelModel) {
					treeItem.setExpanded(true);
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof TabFolderModel) {
					treeItem.setExpanded(true);
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof TabPageModel) {
					treeItem.setExpanded(true);
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof VerticalSplitterModel) {
					treeItem.setExpanded(true);
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof HorizontalSplitterModel) {
					treeItem.setExpanded(true);
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof WidgetContainerModel) {
					treeItem.setExpanded(true);
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof ChartContainerModel) {
					treeItem.setExpanded(true);
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof MapContainerModel) {
					treeItem.setExpanded(true);
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof GridContainerModel) {
					treeItem.setExpanded(true);
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof LayoutTemplateAModel) {
					treeItem.setExpanded(true);
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof LayoutTemplateBModel) {
					treeItem.setExpanded(true);
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof LayoutTemplateCModel) {
					treeItem.setExpanded(true);
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof LayoutTemplateDModel) {
					treeItem.setExpanded(true);
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof LayoutTemplateEModel) {
					treeItem.setExpanded(true);
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof LayoutTemplateFModel) {
					treeItem.setExpanded(true);
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof LayoutTemplateGModel) {
					treeItem.setExpanded(true);
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof CompositeRelationWidgetModel) {
					if (hasChildrenContainErrors(treeItem)) {
						treeItem.setExpanded(true);
					} else {
						treeItem.setExpanded(false);
					}
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof LookupWidgetModel) {
					if (((LookupWidgetModel) ((IDesignerNode) treeItem
							.getData()).getModel()).isWritable()) {
						treeItem.setExpanded(true);
					} else {
						treeItem.setExpanded(false);
					}
				} else if (((IDesignerNode) treeItem.getData()).getModel() instanceof GridModel) {
					if (hasChildrenContainErrors(treeItem)) {
						treeItem.setExpanded(true);
					} else {
						treeItem.setExpanded(false);
					}
				} else {
					treeItem.setExpanded(false);
				}
			} else {
				treeItem.setExpanded(true);
			}
		}

		if (treeItem.getItemCount() > 0) {
			for (final TreeItem item : treeItem.getItems()) {
				if (item.getData() instanceof IDesignerNode) {
					processExpandTreeItem(item);
				}
			}
		}
	}


	private boolean hasChildrenContainErrors(final TreeItem treeItem) {
		if (treeItem.getItemCount() > 0) {
			for (final TreeItem item : treeItem.getItems()) {
				if (item.getData() instanceof IDesignerNode) {
					if (((IDesignerNode) item.getData()).getModel() != null) {
						if (((IDesignerNode) item.getData()).getModel() instanceof WidgetModel
								&& ((IDesignerNode) item.getData()).getModel() instanceof
                                                        GroupHeadingWidgetModel == false) {
							if (!((WidgetModel) ((IDesignerNode) item.getData())
									.getModel()).isDataIntegrityValid()) {
								return true;
							}
						} else {
							if (hasChildrenContainErrors(item)) {
								return true;
							}
						}
					}
				}
			}
		}
		return false;
	}


Hope this helps..
Previous Topic:[databinding] Using TreeViewer with ViewerSupport.bind(...)
Next Topic:treeviewer resize issue
Goto Forum:
  


Current Time: Wed Jul 30 21:29:47 EDT 2014

Powered by FUDForum. Page generated in 0.03344 seconds