Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Remove/Delete a TreeItem from Tree
Remove/Delete a TreeItem from Tree [message #466607] Wed, 11 January 2006 23:47 Go to next message
JavaJ Missing name is currently offline JavaJ Missing name
Messages: 45
Registered: July 2009
Member
How can i delete a TreeItem from a tree and just remove that treeItem and its children? I have a situation where i have two trees. My first tree has all the data and then i build my second tree by selecting treeItems from the first tree, click a button and a new tree item get's created on the second tree. If an item is selected on the second tree, then the item is added underneath that node. Any idea's? I've tried dispose() and that didn't work.
Re: Remove/Delete a TreeItem from Tree [message #466639 is a reply to message #466607] Wed, 11 January 2006 23:57 Go to previous messageGo to next message
JavaJ Missing name is currently offline JavaJ Missing name
Messages: 45
Registered: July 2009
Member
SWT/JFace in Action: A parent is set by passing it to the constructor of TreeItem and can’t be modified
from either end of the relationship. Because of the way these relationships
are maintained, removing a single item from a Tree is awkward: You must call
removeAll() to empty the Tree and rebuild its contents, minus the items you wish
to remove.

Does anyone know of a work around?
Re: Remove/Delete a TreeItem from Tree [message #466650 is a reply to message #466639] Thu, 12 January 2006 07:58 Go to previous message
venkataramana m is currently offline venkataramana m
Messages: 86
Registered: July 2009
Member
If you use JFace based TreeViewer, that should be an easy task. Something like this should work ..

TreeViewer fSourceTree;
TreeViewer fTargetTree;

public void onButtonMoveToTarget()
{
	Object sourceElem = getSelectedSourceElement();
	Object targetElem = getSelectedTargetElement();

	/* remove from source */
	fSourceTree.remove(sourceElem);
	
	/* add to target */
	fTargetTree.add(targetElem, sourceElem); /* here you may like to make a copy of sourceElem by
						a deepCopy instead to avoid unforeseen effects if any */
}

private Object getSelectedSourceElement()
{
	IStructuredSelection selection = (IStructuredSelection)(fSourceTree.getSelection());
	if( selection == null || selection.isEmpty() ) return null;
				
	
	return selection.getFirstElement();
}


private Object getSelectedTargetElement()
{
	IStructuredSelection selection = (IStructuredSelection)(fTargetTree.getSelection());
	if( selection == null || selection.isEmpty() ) 
		return fTargetTree.getInput(); /* return root as the selected element 
						if nothing is added yet*/				
	
	return selection.getFirstElement();
}


Thanks
~Venkat
Previous Topic:swt 3.2
Next Topic:Executing a simple java application!!!!
Goto Forum:
  


Current Time: Sun Sep 21 14:28:18 GMT 2014

Powered by FUDForum. Page generated in 0.01535 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software