137
|
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
|
|
14 module jface.snippets.Snippet047VirtualLazyTreeViewer;
|
|
15
|
|
16 import dwtx.jface.viewers.ILazyTreeContentProvider;
|
|
17 import dwtx.jface.viewers.LabelProvider;
|
|
18 import dwtx.jface.viewers.TreeViewer;
|
|
19 import dwtx.jface.viewers.Viewer;
|
|
20 import dwt.DWT;
|
|
21 import dwt.layout.FillLayout;
|
|
22 import dwt.widgets.Display;
|
|
23 import dwt.widgets.Shell;
|
|
24
|
|
25 import dwt.dwthelper.utils;
|
|
26
|
|
27 import tango.util.Convert;
|
|
28 import tango.util.container.LinkedList;
|
|
29
|
|
30
|
|
31 /**
|
|
32 * @param args
|
|
33 */
|
|
34 void main(String[] args) {
|
|
35 Display display = new Display();
|
|
36 Shell shell = new Shell(display);
|
|
37 shell.setLayout(new FillLayout());
|
|
38 new Snippet047VirtualLazyTreeViewer(shell);
|
|
39 shell.open();
|
|
40
|
|
41 while (!shell.isDisposed()) {
|
|
42 if (!display.readAndDispatch())
|
|
43 display.sleep();
|
|
44 }
|
|
45
|
|
46 display.dispose();
|
|
47
|
|
48 }
|
|
49
|
|
50 /**
|
|
51 * A simple TreeViewer to demonstrate usage of an ILazyContentProvider.
|
|
52 *
|
|
53 */
|
|
54 public class Snippet047VirtualLazyTreeViewer {
|
|
55 alias ArrayWrapperT!(IntermediateNode) ArrayWrapperIntermediateNode;
|
|
56 alias ArrayWrapperT!(LeafNode) ArrayWrapperLeafNode;
|
|
57
|
140
|
58 private class MyContentProvider : ILazyTreeContentProvider {
|
|
59 private TreeViewer viewer;
|
|
60 private IntermediateNode[] elements;
|
137
|
61
|
140
|
62 public this(TreeViewer viewer) {
|
|
63 this.viewer = viewer;
|
|
64 }
|
137
|
65
|
140
|
66 public void dispose() {
|
137
|
67
|
140
|
68 }
|
137
|
69
|
140
|
70 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
|
137
|
71 if(cast(ArrayWrapperIntermediateNode) newInput)
|
|
72 this.elements = (cast(ArrayWrapperIntermediateNode) newInput).array;
|
140
|
73 }
|
137
|
74
|
140
|
75 /*
|
|
76 * (non-Javadoc)
|
|
77 *
|
|
78 * @see dwtx.jface.viewers.ILazyTreeContentProvider#getParent(java.lang.Object)
|
|
79 */
|
|
80 public Object getParent(Object element) {
|
|
81 if (cast(LeafNode)element)
|
|
82 return (cast(LeafNode) element).parent;
|
|
83 return new ArrayWrapperIntermediateNode(elements);
|
|
84 }
|
137
|
85
|
140
|
86 /*
|
|
87 * (non-Javadoc)
|
|
88 *
|
|
89 * @see dwtx.jface.viewers.ILazyTreeContentProvider#updateChildCount(java.lang.Object,
|
|
90 * int)
|
|
91 */
|
|
92 public void updateChildCount(Object element, int currentChildCount) {
|
|
93
|
|
94 int length = 0;
|
|
95 if (cast(IntermediateNode)element) {
|
|
96 IntermediateNode node = cast(IntermediateNode) element;
|
|
97 length = node.children.length;
|
|
98 }
|
137
|
99 /// TODO: fix me access violation here
|
140
|
100 if(element !is null && elements !is null && (cast(ArrayWrapperIntermediateNode)element) && (cast(ArrayWrapperIntermediateNode)element).array is elements)
|
|
101 length = elements.length;
|
|
102 viewer.setChildCount(element, length);
|
|
103 }
|
137
|
104
|
140
|
105 /*
|
|
106 * (non-Javadoc)
|
|
107 *
|
|
108 * @see dwtx.jface.viewers.ILazyTreeContentProvider#updateElement(java.lang.Object,
|
|
109 * int)
|
|
110 */
|
|
111 public void updateElement(Object parent, int index) {
|
|
112
|
|
113 Object element;
|
|
114 if (cast(IntermediateNode)parent)
|
|
115 element = (cast(IntermediateNode) parent).children[index];
|
|
116
|
|
117 else
|
|
118 element = elements[index];
|
|
119 viewer.replace(parent, index, element);
|
|
120 updateChildCount(element, -1);
|
|
121
|
|
122 }
|
137
|
123
|
140
|
124 }
|
137
|
125
|
140
|
126 public class LeafNode {
|
|
127 public int counter;
|
|
128 public IntermediateNode parent;
|
137
|
129
|
140
|
130 public this(int counter, IntermediateNode parent) {
|
|
131 this.counter = counter;
|
|
132 this.parent = parent;
|
|
133 }
|
137
|
134
|
140
|
135 public String toString() {
|
|
136 return "Leaf " ~ to!(String)(this.counter);
|
|
137 }
|
|
138 }
|
137
|
139
|
140
|
140 public class IntermediateNode {
|
|
141 public int counter;
|
|
142 public LeafNode[] children;
|
137
|
143
|
140
|
144 public this(int counter) {
|
|
145 this.counter = counter;
|
137
|
146 children = new LeafNode[0];
|
140
|
147 }
|
137
|
148
|
140
|
149 public String toString() {
|
|
150 return "Node " ~ to!(String)(this.counter);
|
|
151 }
|
137
|
152
|
140
|
153 public void generateChildren(int i) {
|
|
154 children = new LeafNode[i];
|
|
155 for (int j = 0; j < i; j++) {
|
|
156 children[j] = new LeafNode(j, this);
|
|
157 }
|
137
|
158
|
140
|
159 }
|
|
160 }
|
137
|
161
|
140
|
162 public this(Shell shell) {
|
|
163 final TreeViewer v = new TreeViewer(shell, DWT.VIRTUAL | DWT.BORDER);
|
|
164 v.setLabelProvider(new LabelProvider());
|
|
165 v.setContentProvider(new MyContentProvider(v));
|
|
166 v.setUseHashlookup(true);
|
|
167 IntermediateNode[] model = createModel();
|
|
168 v.setInput(new ArrayWrapperIntermediateNode(model));
|
|
169 v.getTree().setItemCount(model.length);
|
137
|
170
|
140
|
171 }
|
137
|
172
|
140
|
173 private IntermediateNode[] createModel() {
|
|
174 IntermediateNode[] elements = new IntermediateNode[10];
|
137
|
175
|
140
|
176 for (int i = 0; i < 10; i++) {
|
|
177 elements[i] = new IntermediateNode(i);
|
|
178 elements[i].generateChildren(1000);
|
|
179 }
|
137
|
180
|
140
|
181 return elements;
|
|
182 }
|
137
|
183
|
140
|
184 }
|