31
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006 Tom Schindl and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * Tom Schindl - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * yidabu at gmail dot com ( D China http://www.d-programming-language-china.org/ )
|
|
12 *******************************************************************************/
|
|
13
|
32
|
14 module org.eclipse.jface.snippets.viewers.Snippet047VirtualLazyTreeViewer;
|
31
|
15
|
32
|
16 import org.eclipse.jface.viewers.ILazyTreeContentProvider;
|
|
17 import org.eclipse.jface.viewers.LabelProvider;
|
|
18 import org.eclipse.jface.viewers.TreeViewer;
|
|
19 import org.eclipse.jface.viewers.Viewer;
|
|
20 import org.eclipse.swt.SWT;
|
|
21 import org.eclipse.swt.layout.FillLayout;
|
|
22 import org.eclipse.swt.widgets.Display;
|
|
23 import org.eclipse.swt.widgets.Shell;
|
31
|
24
|
32
|
25 import java.lang.all;
|
31
|
26
|
|
27 import tango.util.Convert;
|
|
28 import tango.util.container.LinkedList;
|
|
29
|
|
30 version(JIVE) import jive.stacktrace;
|
|
31
|
|
32
|
|
33 /**
|
|
34 * @param args
|
|
35 */
|
|
36 void main(String[] args) {
|
|
37 Display display = new Display();
|
|
38 Shell shell = new Shell(display);
|
|
39 shell.setLayout(new FillLayout());
|
|
40 new Snippet047VirtualLazyTreeViewer(shell);
|
|
41 shell.open();
|
|
42
|
|
43 while (!shell.isDisposed()) {
|
|
44 if (!display.readAndDispatch())
|
|
45 display.sleep();
|
|
46 }
|
|
47
|
|
48 display.dispose();
|
|
49
|
|
50 }
|
|
51
|
|
52 /**
|
|
53 * A simple TreeViewer to demonstrate usage of an ILazyContentProvider.
|
|
54 *
|
|
55 */
|
|
56 public class Snippet047VirtualLazyTreeViewer {
|
|
57 alias ArrayWrapperT!(IntermediateNode) ArrayWrapperIntermediateNode;
|
|
58 alias ArrayWrapperT!(LeafNode) ArrayWrapperLeafNode;
|
|
59
|
|
60 private class MyContentProvider : ILazyTreeContentProvider {
|
|
61 private TreeViewer viewer;
|
|
62 private IntermediateNode[] elements;
|
|
63
|
|
64 public this(TreeViewer viewer) {
|
|
65 this.viewer = viewer;
|
|
66 }
|
|
67
|
|
68 public void dispose() {
|
|
69
|
|
70 }
|
|
71
|
|
72 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
|
|
73 if(cast(ArrayWrapperIntermediateNode) newInput)
|
|
74 this.elements = (cast(ArrayWrapperIntermediateNode) newInput).array;
|
|
75 }
|
|
76
|
|
77 /*
|
|
78 * (non-Javadoc)
|
|
79 *
|
32
|
80 * @see org.eclipse.jface.viewers.ILazyTreeContentProvider#getParent(java.lang.Object)
|
31
|
81 */
|
|
82 public Object getParent(Object element) {
|
|
83 if (cast(LeafNode)element)
|
|
84 return (cast(LeafNode) element).parent;
|
|
85 return new ArrayWrapperIntermediateNode(elements);
|
|
86 }
|
|
87
|
|
88 /*
|
|
89 * (non-Javadoc)
|
|
90 *
|
32
|
91 * @see org.eclipse.jface.viewers.ILazyTreeContentProvider#updateChildCount(java.lang.Object,
|
31
|
92 * int)
|
|
93 */
|
|
94 public void updateChildCount(Object element, int currentChildCount) {
|
|
95
|
|
96 int length = 0;
|
|
97 if (cast(IntermediateNode)element) {
|
|
98 IntermediateNode node = cast(IntermediateNode) element;
|
|
99 length = node.children.length;
|
|
100 }
|
|
101 /// TODO: fix me access violation here
|
|
102 if(element !is null && elements !is null && (cast(ArrayWrapperIntermediateNode)element) && (cast(ArrayWrapperIntermediateNode)element).array is elements)
|
|
103 length = elements.length;
|
|
104 viewer.setChildCount(element, length);
|
|
105 }
|
|
106
|
|
107 /*
|
|
108 * (non-Javadoc)
|
|
109 *
|
32
|
110 * @see org.eclipse.jface.viewers.ILazyTreeContentProvider#updateElement(java.lang.Object,
|
31
|
111 * int)
|
|
112 */
|
|
113 public void updateElement(Object parent, int index) {
|
|
114
|
|
115 Object element;
|
|
116 if (cast(IntermediateNode)parent)
|
|
117 element = (cast(IntermediateNode) parent).children[index];
|
|
118
|
|
119 else
|
|
120 element = elements[index];
|
|
121 viewer.replace(parent, index, element);
|
|
122 updateChildCount(element, -1);
|
|
123
|
|
124 }
|
|
125
|
|
126 }
|
|
127
|
|
128 public class LeafNode {
|
|
129 public int counter;
|
|
130 public IntermediateNode parent;
|
|
131
|
|
132 public this(int counter, IntermediateNode parent) {
|
|
133 this.counter = counter;
|
|
134 this.parent = parent;
|
|
135 }
|
|
136
|
|
137 public String toString() {
|
|
138 return "Leaf " ~ to!(String)(this.counter);
|
|
139 }
|
|
140 }
|
|
141
|
|
142 public class IntermediateNode {
|
|
143 public int counter;
|
|
144 public LeafNode[] children;
|
|
145
|
|
146 public this(int counter) {
|
|
147 this.counter = counter;
|
|
148 children = new LeafNode[0];
|
|
149 }
|
|
150
|
|
151 public String toString() {
|
|
152 return "Node " ~ to!(String)(this.counter);
|
|
153 }
|
|
154
|
|
155 public void generateChildren(int i) {
|
|
156 children = new LeafNode[i];
|
|
157 for (int j = 0; j < i; j++) {
|
|
158 children[j] = new LeafNode(j, this);
|
|
159 }
|
|
160
|
|
161 }
|
|
162 }
|
|
163
|
|
164 public this(Shell shell) {
|
32
|
165 final TreeViewer v = new TreeViewer(shell, SWT.VIRTUAL | SWT.BORDER);
|
31
|
166 v.setLabelProvider(new LabelProvider());
|
|
167 v.setContentProvider(new MyContentProvider(v));
|
|
168 v.setUseHashlookup(true);
|
|
169 IntermediateNode[] model = createModel();
|
|
170 v.setInput(new ArrayWrapperIntermediateNode(model));
|
|
171 v.getTree().setItemCount(model.length);
|
|
172
|
|
173 }
|
|
174
|
|
175 private IntermediateNode[] createModel() {
|
|
176 IntermediateNode[] elements = new IntermediateNode[10];
|
|
177
|
|
178 for (int i = 0; i < 10; i++) {
|
|
179 elements[i] = new IntermediateNode(i);
|
|
180 elements[i].generateChildren(1000);
|
|
181 }
|
|
182
|
|
183 return elements;
|
|
184 }
|
|
185
|
|
186 }
|