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 173971 Details for
Bug 310112
[Viewers] Treeviewer fails to call hasChildren() before getChildren()
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Changed Snippet v01 to help reproduce the problem
TreeViewerSnippet.java (text/x-java-source), 5.99 KB, created by
Mário Guimarães
on 2010-07-11 07:51:58 EDT
(
hide
)
Description:
Changed Snippet v01 to help reproduce the problem
Filename:
MIME Type:
Creator:
Mário Guimarães
Created:
2010-07-11 07:51:58 EDT
Size:
5.99 KB
patch
obsolete
>package org.eclipse.jface.snippets.viewers; > >import java.util.ArrayList; > >import org.eclipse.jface.viewers.ITreeContentProvider; >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.Shell; > >/** > * A simple TreeViewer to demonstrate usage > * > * > */ >public class TreeViewerSnippet { > private class MyContentProvider implements ITreeContentProvider { > > /* > * (non-Javadoc) > * > * @see > * org.eclipse.jface.viewers.IStructuredContentProvider#getElements( > * java.lang.Object) > */ > public Object[] getElements(Object inputElement) { > return ((MyModel) inputElement).child.toArray(); > } > > /* > * (non-Javadoc) > * > * @see org.eclipse.jface.viewers.IContentProvider#dispose() > */ > public void dispose() { > > } > > /* > * (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) { > > } > > /* > * (non-Javadoc) > * > * @see > * org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang > * .Object) > */ > public Object[] getChildren(Object parentElement) { > return getElements(parentElement); > } > > /* > * (non-Javadoc) > * > * @see > * org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang > * .Object) > */ > public Object getParent(Object element) { > if (element == null) { > return null; > } > > return ((MyModel) element).parent; > } > > /* > * (non-Javadoc) > * > * @see > * org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang > * .Object) > */ > public boolean hasChildren(Object element) { > //---------------------------------------------------------------------------- > // REQUIREMENT: MyModel2 objects appear in the view only if they have children > //---------------------------------------------------------------------------- > if (element instanceof MyModel1) { > MyModel1 elemObj = (MyModel1) element; > if (elemObj.child.size() == 0) return false; > MyModel2 childObj = (MyModel2)(elemObj.child.get(0)); > return childObj != null && childObj.child.size() > 0; > } else > return ((MyModel) element).child.size() > 0; > } > > } > > public class MyModel { > public MyModel parent; > public ArrayList child = new ArrayList(); > public int counter; > > public MyModel(int counter, MyModel parent) { > this.parent = parent; > this.counter = counter; > } > > public String toString() { > String rv = "Item "; > if (parent != null) { > rv = parent.toString() + "."; > } > > rv += counter; > > return rv; > } > } > > > public class MyModel1 extends MyModel { > public MyModel1(int counter, MyModel parent) { > super(counter, parent); > } > } > > public class MyModel2 extends MyModel { > public MyModel2(int counter, MyModel parent) { > super(counter, parent); > } > } > > public class MyModel3 extends MyModel { > public MyModel3(int counter, MyModel parent) { > super(counter, parent); > } > } > > public TreeViewerSnippet(Shell shell) { > final TreeViewer viewer = new TreeViewer(shell); > viewer.setLabelProvider(new LabelProvider()); > viewer.setContentProvider(new MyContentProvider()); > final MyModel input = createModel(); > viewer.setInput(input); > viewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH)); > Button button1 = new Button(shell, SWT.PUSH); > button1.setText("Refresh"); > button1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); > button1.addSelectionListener(new SelectionListener() { > public void widgetSelected(SelectionEvent e) { > viewer.refresh(true); > } > > public void widgetDefaultSelected(SelectionEvent e) { > } > }); > final Button button2 = new Button(shell, SWT.PUSH); > button2.setText("Remove Item 0.0.0.0"); > button2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); > button2.addSelectionListener(new SelectionListener() { > public void widgetSelected(SelectionEvent e) { > MyModel1 node = (MyModel1) input.child.get(0); > MyModel2 child2 = (MyModel2) node.child.get(0); > if (child2.child.size() > 0) { > child2.child.clear(); > button2.setText("Add Item 0.0.0.0"); > } else { > createChildren3(child2, 1); > button2.setText("Remove Item 0.0.0.0"); > } > viewer.refresh(true); > } > > public void widgetDefaultSelected(SelectionEvent e) { > } > }); > > } > > private MyModel createModel() { > MyModel root = new MyModel(0, null); > root.counter = 0; > createChildren1(root, 2); > MyModel1 child1 = (MyModel1) root.child.get(0); > createChildren2(child1, 1); > MyModel2 child2 = (MyModel2) child1.child.get(0); > createChildren3(child2, 1); > return root; > } > > protected void createChildren1(MyModel root, int count) { > MyModel tmp; > for (int i = 0; i < count; i++) { > tmp = new MyModel1(i, root); > root.child.add(tmp); > } > } > > protected void createChildren2(MyModel root, int count) { > MyModel tmp; > for (int i = 0; i < count; i++) { > tmp = new MyModel2(i, root); > root.child.add(tmp); > } > } > > protected void createChildren3(MyModel root, int count) { > MyModel tmp; > for (int i = 0; i < count; i++) { > tmp = new MyModel3(i, root); > root.child.add(tmp); > } > } > > public static void main(String[] args) { > Display display = new Display(); > Shell shell = new Shell(display); > shell.setLayout(new GridLayout()); > new TreeViewerSnippet(shell); > shell.open(); > > 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 310112
:
173778
| 173971 |
174065