Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Traverse to get all children
Traverse to get all children [message #485608] Mon, 14 September 2009 06:52 Go to next message
Mikael Petterson is currently offline Mikael PettersonFriend
Messages: 158
Registered: July 2009
Senior Member
Hi,
In our plug-in we will do a refresh of all elements ( and it's children)
that have been selected.

IResource [] resources = getSelectedResources();

Then for each selected resource I need to traverse the children ( files
and folders) add them to an array.

This array I will send to a method that will do refresh State on each
element.

What is the most efficient method to traverse children and add it to an
array?

The above will be done in an Action.

Are there any code examples for this?

br,

//mike
Re: Traverse to get all children [message #485654 is a reply to message #485608] Mon, 14 September 2009 10:41 Go to previous message
Wim Jongman is currently offline Wim JongmanFriend
Messages: 493
Registered: July 2009
Senior Member

Hi Mikael,

Yep, this is the Visitor pattern. You have to create an IVisitor object with
the required code (e.g. building an array) in the visit(IResource) method.
You pass this object to the accept(IVisitor) method of IResource. If the
visit(IResource) method of the visitor returns true then the children are
traversed as well.

There is also an optimized method which involves IResourceProxy objects.
Please see the eclipse local help for details.

Here is a code example using an Action allthough you should switch to
Commands and Handlers ;-)

Best Regards,

Wim Jongman
http://www.remainsoftware.com
http://www.industrial-tsi.com
http://twitter.com/wimjongman

public void run(IAction action) {
if (selection instanceof IStructuredSelection) {
for (Iterator it = ((IStructuredSelection) selection).iterator();
it.hasNext();) {
Object element = it.next();
IProject project = null;
if (element instanceof IProject) {
project = (IProject) element;
} else if (element instanceof IAdaptable) {
project = (IProject) ((IAdaptable) element).getAdapter(IProject.class);
}
if (project != null) {
doit(project.getWorkspace());
}
}
}
}

private void doit(IWorkspace workspace) {
try {
workspace.getRoot().accept(new IResourceVisitor() {
public boolean visit(IResource resource) throws CoreException {
System.out.println(getType(resource) + ": " + resource.getName() +
printNatures(resource));
return true;
}

private String getType(IResource resource) {
switch (resource.getType()) {
case 1:
return "File: ";
case 2:
return "Folder: ";
case 4:
return "Project: ";
case 8:
return "Root: ";
}
return null;
}
});
} catch (CoreException e) {
}
}


"Mikael Petterson" <mikaelpetterson@hotmail.com> wrote in message
news:3ec829d5a4a5824180fa084aad0b4353$1@www.eclipse.org...
> Hi,
> In our plug-in we will do a refresh of all elements ( and it's children)
> that have been selected.
> IResource [] resources = getSelectedResources();
>
> Then for each selected resource I need to traverse the children ( files
> and folders) add them to an array.
>
> This array I will send to a method that will do refresh State on each
> element.
>
> What is the most efficient method to traverse children and add it to an
> array?
>
> The above will be done in an Action.
>
> Are there any code examples for this?
>
> br,
>
> //mike
>
>
>
Previous Topic:org.eclipse.ui.edit.copy NotHandledException
Next Topic:[jface.viewers] How do discriminate active and non-active links in ColumnViewer?
Goto Forum:
  


Current Time: Thu Mar 28 09:40:16 GMT 2024

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

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

Back to the top