1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
/*******************************************************************************
* Copyright (c) 2006, 2016 Tom Schindl and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Tom Schindl - initial API and implementation
* Lars Vogel <Lars.Vogel@vogella.com> - Bug 486603
* Wim Jongman <wim.jongman@remainsoftware.com> - Overhaul without preloading model
*******************************************************************************/
package org.eclipse.jface.snippets.viewers;
import java.util.Random;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.viewers.ILazyTreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
* A simple TreeViewer example to demonstrate the usage of an
* ILazyContentProvider.
*
*/
public class Snippet047VirtualLazyTreeViewer {
private long fParentsLoaded = 0;
private long fGlobalChildrenLoaded = 0;
private TreeViewer fViewer;
private Text fText;
private class MyContentProvider implements ILazyTreeContentProvider {
@Override
public Object getParent(Object element) {
return ((Node) element).getParent();
}
@Override
public void updateChildCount(Object element, int currentChildCount) {
System.out.println(element + " " + ((Node) element).getChildCount());
fViewer.setChildCount(element, ((Node) element).getChildCount());
}
@Override
public void updateElement(Object parent, int index) {
Node element = ((Node) parent).getChild(index);
fViewer.replace(parent, index, element);
updateChildCount(element, -1);
fText.setText("1 root, " + fParentsLoaded + " nodes and " + fGlobalChildrenLoaded + " leafs in memory...");
}
}
public class Node {
private int fChildCount;
public int fChildrenLoaded;
private Node[] fChildren;
private int fCounter;
private Node fParent;
public Node(int counter, int childCount, Node parent) {
fCounter = counter;
fChildCount = childCount;
fParent = parent;
}
public Node getParent() {
return fParent;
}
public int getChildCount() {
return fChildCount;
}
public Node[] getChildren() {
if (fChildren == null) {
fChildren = new Node[fChildCount];
}
return fChildren;
}
public Node getChild(int index) {
if (getChildren()[index] != null) {
return getChildren()[index];
}
Node leafNode = new Node(index, getRandomChildCount(), this);
getChildren()[index] = leafNode;
fViewer.update(leafNode.getParent(), null);
if (leafNode.getParent().getParent() == null) {
fParentsLoaded++;
} else {
fGlobalChildrenLoaded++;
}
fChildrenLoaded++;
return leafNode;
}
@Override
public String toString() {
String type = getParent() == null ? "Root" : getParent().getParent() == null ? "Node" : "Leaf";
return type + " " + this.fCounter + " of " + (getParent() == null ? "0" : getParent().getChildCount())
+ " (" + fChildCount + "/" + fChildrenLoaded + ")";
}
}
public Snippet047VirtualLazyTreeViewer(Shell shell) {
fText = new Text(shell, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
fText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
fText.setEnabled(false);
fViewer = new TreeViewer(shell, SWT.VIRTUAL | SWT.BORDER);
fViewer.setLabelProvider(new LabelProvider());
fViewer.setContentProvider(new MyContentProvider());
fViewer.setUseHashlookup(true);
Node root = new Node(0, getRandomChildCount(), null);
fViewer.setInput(root);
fViewer.getTree().setLayoutData(GridDataFactory.fillDefaults().create());
fViewer.setChildCount(root, root.getChildCount());
fViewer.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
// at this point the model only contains what is visible on the screen.
}
/**
* @param args
*/
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
new Snippet047VirtualLazyTreeViewer(shell);
shell.setSize(800, 600);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
public static int getRandomChildCount() {
return new Random().nextInt(1000) + 100;
}
}
|