package foo; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.Shell; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IMenuListener; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.IToolBarManager; import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.action.Separator; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.viewers.ILazyTreePathContentProvider; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.TreePath; import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.jface.viewers.Viewer; public class Snippet172640 { private TreeViewer fViewer; private Node fRoot= new Node(null, ""); private NodeComparator fComparator= new NodeComparator(); private static class Node { private String fName; private Node fParent; private ArrayList fChildren; public Node(Node parent, String name) { fName= name; fParent= parent; if (fParent != null) fParent.addChild(this); fChildren= new ArrayList(); } public String getName() { return fName; } public Node getParent() { return fParent; } public Node getChild(int index) { return (Node) fChildren.get(index); } public int getChildCount() { return fChildren.size(); } public String toString() { return getName(); } public void sort(Comparator comparator) { Collections.sort(fChildren, comparator); } private void addChild(Node node) { fChildren.add(node); } } private static class NodeComparator implements Comparator { private int fDirection= -1; public int compare(Object o1, Object o2) { Node n1 = (Node) o1; Node n2 = (Node) o2; return fDirection * (n1.getName().compareTo(n2.getName())); } public void reverse() { fDirection*= -1; } } /* * The content provider class is responsible for providing objects to the * view. It can wrap existing objects in adapters or simply return objects * as-is. These objects may be sensitive to the current input of the view, * or ignore it and always show the same content (like Task List, for * example). */ class ViewContentProvider implements ILazyTreePathContentProvider { /* * (non-Javadoc) * * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, * java.lang.Object, java.lang.Object) */ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { } public TreePath[] getParents(Object element) { LinkedList chain= new LinkedList(); Node node= (Node) element; while (node.getParent() != fRoot) { chain.add(node.getParent()); node= node.getParent(); } Collections.reverse(chain); return new TreePath[] { new TreePath(chain.toArray()) }; } public void updateChildCount(TreePath treePath, int currentChildCount) { Node node= getNode(treePath); fViewer.setChildCount(treePath, node.getChildCount()); } public void updateElement(TreePath parentPath, int index) { Node node= getNode(parentPath); System.out.println("updateElement " + node + ", " + index); if (index >= node.getChildCount()) { Thread.dumpStack(); return; } Node child= node.getChild(index); fViewer.replace(parentPath, index, child); fViewer.setChildCount(parentPath.createChildPath(child), child.getChildCount()); } public void updateHasChildren(TreePath path) { Node node= getNode(path); fViewer.setHasChildren(path, node.getChildCount() > 0); } /* * (non-Javadoc) * * @see org.eclipse.jface.viewers.IContentProvider#dispose() */ public void dispose() { } private Node getNode(TreePath path) { Node node= (Node) path.getLastSegment(); return node == null ? fRoot : node; } } class ViewLabelProvider extends LabelProvider implements ITableLabelProvider { public String getColumnText(Object obj, int index) { return getText(obj); } public Image getColumnImage(Object obj, int index) { return getImage(obj); } public Image getImage(Object obj) { return null; } } /** * The constructor. */ public Snippet172640() { } /** * This is a callback that will allow us to create the viewer and initialize * it. */ public void createPartControl(Composite parent) { Node r1= new Node(fRoot, "element 1"); Node r2= new Node(fRoot, "element 2"); Node r3= new Node(fRoot, "element 3"); Node r4= new Node(fRoot, "element 4"); new Node(r1, "child 1/1"); new Node(r1, "child 1/2"); new Node(r1, "child 1/3"); new Node(r2, "child 2/1"); new Node(r3, "child 3/1"); new Node(r3, "child 3/2"); new Node(r4, "child 4/1"); new Node(r4, "child 4/2"); new Node(r4, "child 4/3"); new Node(r4, "child 4/4"); new Node(r4, "child 4/5"); new Node(r4, "child 4/6"); new Node(r4, "child 4/7"); new Node(r4, "child 4/8"); new Node(r4, "child 4/9"); new Node(r4, "child 4/A"); new Node(r4, "child 4/B"); new Node(r4, "child 4/C"); new Node(r4, "child 4/D"); new Node(r4, "child 4/E"); new Node(r4, "child 4/F"); new Node(r4, "child 4/G"); new Node(r4, "child 4/H"); new Node(r4, "child 4/I"); new Node(r4, "child 4/J"); new Node(r4, "child 4/K"); new Node(r4, "child 4/L"); fViewer= new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.VIRTUAL); fViewer.setContentProvider(new ViewContentProvider()); fViewer.setLabelProvider(new ViewLabelProvider()); fViewer.setUseHashlookup(true); fViewer.setInput(fRoot); { Button button = new Button(parent, SWT.PUSH); button.setText("Sort"); button.addSelectionListener(new SelectionAdapter(){ public void widgetSelected(SelectionEvent e) { fComparator.reverse(); fRoot.sort(fComparator); fViewer.refresh(); } }); } { Button button = new Button(parent, SWT.PUSH); button.setText("Refresh"); button.addSelectionListener(new SelectionAdapter(){ public void widgetSelected(SelectionEvent e) { fViewer.refresh(); } }); } } public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); new Snippet172640().createPartControl(shell); GridLayoutFactory.swtDefaults().generateLayout(shell); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } }