Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 164200 Details for
Bug 305739
[viewers] #expandToLevel(Object, level) retrieves all children when virtual
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
Snippetv01
Snippet.java (text/plain), 6.68 KB, created by
Hitesh
on 2010-04-08 07:48:53 EDT
(
hide
)
Description:
Snippetv01
Filename:
MIME Type:
Creator:
Hitesh
Created:
2010-04-08 07:48:53 EDT
Size:
6.68 KB
patch
obsolete
>/******************************************************************************* > * Copyright (c) 2000, 2010 IBM Corporation and others. > * All rights reserved. This program and the accompanying materials > * are made available under the terms of the Eclipse Public License v1.0 > * which accompanies this distribution, and is available at > * http://www.eclipse.org/legal/epl-v10.html > * > * Contributors: > * IBM Corporation > *******************************************************************************/ > >package org.eclipse.jface.snippets.viewers; > >import java.util.Random; > >import org.eclipse.jface.viewers.ILazyTreeContentProvider; >import org.eclipse.jface.viewers.LabelProvider; >import org.eclipse.jface.viewers.TreeViewer; >import org.eclipse.jface.viewers.Viewer; >import org.eclipse.swt.SWT; >import org.eclipse.swt.events.SelectionEvent; >import org.eclipse.swt.events.SelectionListener; >import org.eclipse.swt.layout.GridData; >import org.eclipse.swt.layout.GridLayout; >import org.eclipse.swt.widgets.Button; >import org.eclipse.swt.widgets.Display; >import org.eclipse.swt.widgets.ScrollBar; >import org.eclipse.swt.widgets.Shell; > >public class Snippet { > > private static final int TOP_PARENTS = 5; > private static final int LEVEL = 6; > private static final int COUNT = 3; > static Random random = new Random(); > private Node[] elements; > private int genCount = 0; > private TreeViewer fViewer; > > private class ContentProvider implements ILazyTreeContentProvider { > private TreeViewer viewer; > > public ContentProvider(TreeViewer viewer) { > this.viewer = viewer; > } > > public void dispose() { > > } > > public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { > elements = (Node[]) newInput; > } > > /* > * (non-Javadoc) > * > * @see > * org.eclipse.jface.viewers.ILazyTreeContentProvider#getParent(java > * .lang.Object) > */ > public Object getParent(Object element) { > Node node = (Node) element; > if (node.isLeaf()) > return node.parent; > return elements; > } > > /* > * (non-Javadoc) > * > * @see > * org.eclipse.jface.viewers.ILazyTreeContentProvider#updateChildCount > * (java.lang.Object, int) > */ > public void updateChildCount(Object element, int currentChildCount) { > int length = 0; > if (element instanceof Node) { > Node node = (Node) element; > length = node.children.length; > } > if (element == elements) > length = elements.length; > viewer.setChildCount(element, length); > } > > /* > * (non-Javadoc) > * > * @see > * org.eclipse.jface.viewers.ILazyTreeContentProvider#updateElement( > * java.lang.Object, int) > */ > public void updateElement(Object parent, int index) { > Object element; > if (parent instanceof Node) > element = ((Node) parent).children[index]; > else > element = elements[index]; > viewer.replace(parent, index, element); > updateChildCount(element, -1); > > } > > } > > private int nodeCounter = 0; > > class Node { > String name; > int count; > Node parent; > Node[] children = new Node[0]; > > public Node(String name) { > this(name, null); > } > > public Node(String name, Node parent) { > this.name = name; > this.parent = parent; > count = nodeCounter++; > } > > public boolean isLeaf() { > return children == null || children.length == 0; > } > > public String toString() { > if (isLeaf()) { > return parent.toString() + name + "-Leaf-" + count; > } > return name + "-Node-" + count; > } > > public void generateChildren(String prefix, int count, int level, > boolean recursive) { > children = new Node[count]; > for (int j = 0; j < count; j++) { > children[j] = new Node(prefix, this); > if (recursive && level > 1) { > children[j].generateChildren(prefix, count, level - 1, > recursive); > } > } > } > } > > public Snippet(Shell shell) { > fViewer = new TreeViewer(shell, SWT.VIRTUAL | SWT.MULTI | SWT.BORDER); > fViewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH)); > Button button = new Button(shell, SWT.PUSH); > button.setText("Refresh and Reveal"); > button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); > button.addSelectionListener(new SelectionListener() { > public void widgetSelected(SelectionEvent e) { > refreshModel(); > refresh(); > for (int i = 0; i < elements.length; i++) { > expandToLevel(elements[i], random.nextBoolean() ? LEVEL > : LEVEL / 2); > } > Object node = revealRandomLeaf(); > Object[] objects = fViewer.testFindItems(node); > System.out.println("Reveal:Found widgets " + objects.length); > scroll2Random(); > } > > public void widgetDefaultSelected(SelectionEvent e) { > } > }); > fViewer.setLabelProvider(new LabelProvider()); > fViewer.setContentProvider(new ContentProvider(fViewer)); > fViewer.setUseHashlookup(true); > Node[] model = createModel(); > fViewer.setInput(model); > fViewer.setAutoExpandLevel(LEVEL); > } > > Node[] createModel() { > nodeCounter = 0; > String prefix = "Gen" + genCount; > Node[] elements = new Node[TOP_PARENTS]; > for (int i = 0; i < TOP_PARENTS; i++) { > elements[i] = new Node(prefix); > elements[i].generateChildren(prefix, COUNT, LEVEL, true); > } > genCount++; > return elements; > } > > void refreshModel() { > nodeCounter = 0; > String prefix = "Gen" + genCount; > for (int i = 0; i < TOP_PARENTS; i++) { > elements[i] = new Node(prefix); > elements[i].generateChildren(prefix, COUNT, LEVEL, true); > } > genCount++; > } > > void refresh() { > fViewer.refresh(true); > } > > void autoExpand() { > fViewer.setAutoExpandLevel(LEVEL); > } > > Node revealRandomLeaf() { > Node elementOrTreePath = elements[elements.length - 1]; > while (!elementOrTreePath.isLeaf()) { > elementOrTreePath = elementOrTreePath.children[0]; > } > fViewer.reveal(elementOrTreePath); > return elementOrTreePath; > } > > void expandToLevel(Node element, int level) { > fViewer.expandToLevel(element, level); > } > > void scroll2Random() { > ScrollBar bar = fViewer.getTree().getVerticalBar(); > int max = bar.getMaximum(); > int min = bar.getMaximum(); > int curr = bar.getSelection(); > bar.setSelection((curr - min > max - curr) ? min : max); > } > > /** > * @param args > */ > public static void main(String[] args) { > final Display display = new Display(); > Shell shell = new Shell(display); > shell.setLayout(new GridLayout()); > final Snippet snippet = new Snippet(shell); > shell.open(); > final Node[] elements = snippet.elements; > for (int i = 0; i < elements.length; i++) { > snippet.expandToLevel(elements[i], LEVEL); > } > snippet.autoExpand(); > while (!shell.isDisposed()) { > if (!display.readAndDispatch()) > display.sleep(); > } > display.dispose(); > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 305739
:
161940
|
163972
| 164200 |
164264