[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.rcp] Re: Help with ITreePathContentProvider and ItreePathLabelProvider.

Hi Ali, 

This uses a threepathcontentprovider 



package org.eclipse.jface.snippets.viewers; 

import java.io.File; 
import java.util.ArrayList; 

import org.eclipse.jface.viewers.ITreePathContentProvider; 
import org.eclipse.jface.viewers.TreePath; 
import org.eclipse.jface.viewers.TreeViewer; 
import org.eclipse.jface.viewers.Viewer; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.layout.FillLayout; 
import org.eclipse.swt.layout.GridData; 
import org.eclipse.swt.layout.GridLayout; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 

/** 
* @author Wim Jongman, Industrial-TSI & Remain 
* 
*/ 
public class TreePathSnippet { 

	public TreePathSnippet(Shell shell) { 
		shell.setBounds(10, 10, 280, 200); 
		shell.setLayout(new GridLayout(1, true)); 
		final Composite sc = new Composite(shell, SWT.NONE); 
		sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 
		sc.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE)); 
		sc.setLayout(new GridLayout(1, true)); 

		final TreeViewer v = new TreeViewer(sc, SWT.BORDER); 
		v.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 
		 
		v.setContentProvider(new ITreePathContentProvider() { 

			public void inputChanged(Viewer viewer, Object oldInput, 
					Object newInput) { 
				 
			} 

			public void dispose() { 
			} 

			public Object[] getElements(Object inputElement) { 
				return (Object[]) inputElement; 
			} 

			public boolean hasChildren(TreePath path) { 
				if (((File) path.getLastSegment()).isDirectory()) 
					return true; 
				return false; 
			} 

			public TreePath[] getParents(Object element) { 

				ArrayList<File> files = new ArrayList<File>(); 
				File elm = (File) element; 
				while (elm.getParentFile() != null) { 
					files.add(elm.getParentFile()); 
					elm = elm.getParentFile(); 
				} 
				return new TreePath[] { new TreePath(files.toArray()) }; 
			} 

			public Object[] getChildren(TreePath parentPath) { 
				return ((File) parentPath.getLastSegment()).listFiles(); 
			} 
		}); 

	 
		v.setInput(File.listRoots()); 
	} 

	public static void main(String[] args) { 
		Display display = new Display(); 
		try { 
			Shell shell = new Shell(display); 
			shell.setLayout(new FillLayout()); 
			new TreePathSnippet(shell); 
			shell.open(); 

			while (!shell.isDisposed()) { 
				if (!display.readAndDispatch()) 
					display.sleep(); 
			} 
		} catch (Exception e) { 
			e.printStackTrace(); 
		} 

		display.dispose(); 

	} 

} 



> Hello all, 
> 
> I'm trying to evaluate the ITreePathContentProvider and 
> ItreePathLabelProvider, but haven?t been able to find any examples or get 
> them to work with a TreeView using guesswork. 
> 
> Does anyone know of any good example code that demonstrated a viewer using 
> TreePaths? 
> 
> Ali.