[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.rcp] Re: Traverse to get all children

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@xxxxxxxxxxx> wrote in message news:3ec829d5a4a5824180fa084aad0b4353$1@xxxxxxxxxxxxxxxxxx
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