Mercurial > projects > dwt-samples
annotate jface/snippets/viewers/Snippet047VirtualLazyTreeViewer.d @ 145:161f7698cfb8
Moved jface snippets to viewers subpackage, there are more with conflicting numbers for other packages
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 08 Aug 2008 14:40:21 +0200 |
parents | jface/snippets/Snippet047VirtualLazyTreeViewer.d@42c3056512ba |
children |
rev | line source |
---|---|
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: | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
11 * yidabu at gmail dot com ( D China http://www.d-programming-language-china.org/ ) |
137 | 12 *******************************************************************************/ |
13 | |
145
161f7698cfb8
Moved jface snippets to viewers subpackage, there are more with conflicting numbers for other packages
Frank Benoit <benoit@tionex.de>
parents:
143
diff
changeset
|
14 module snippets.viewers.Snippet047VirtualLazyTreeViewer; |
137 | 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 | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
30 version(JIVE) import jive.stacktrace; |
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
31 |
137 | 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. | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
54 * |
137 | 55 */ |
56 public class Snippet047VirtualLazyTreeViewer { | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
57 alias ArrayWrapperT!(IntermediateNode) ArrayWrapperIntermediateNode; |
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
58 alias ArrayWrapperT!(LeafNode) ArrayWrapperLeafNode; |
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
59 |
140 | 60 private class MyContentProvider : ILazyTreeContentProvider { |
61 private TreeViewer viewer; | |
62 private IntermediateNode[] elements; | |
137 | 63 |
140 | 64 public this(TreeViewer viewer) { |
65 this.viewer = viewer; | |
66 } | |
137 | 67 |
140 | 68 public void dispose() { |
137 | 69 |
140 | 70 } |
137 | 71 |
140 | 72 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
137 | 73 if(cast(ArrayWrapperIntermediateNode) newInput) |
74 this.elements = (cast(ArrayWrapperIntermediateNode) newInput).array; | |
140 | 75 } |
137 | 76 |
140 | 77 /* |
78 * (non-Javadoc) | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
79 * |
140 | 80 * @see dwtx.jface.viewers.ILazyTreeContentProvider#getParent(java.lang.Object) |
81 */ | |
82 public Object getParent(Object element) { | |
83 if (cast(LeafNode)element) | |
84 return (cast(LeafNode) element).parent; | |
85 return new ArrayWrapperIntermediateNode(elements); | |
86 } | |
137 | 87 |
140 | 88 /* |
89 * (non-Javadoc) | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
90 * |
140 | 91 * @see dwtx.jface.viewers.ILazyTreeContentProvider#updateChildCount(java.lang.Object, |
92 * int) | |
93 */ | |
94 public void updateChildCount(Object element, int currentChildCount) { | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
95 |
140 | 96 int length = 0; |
97 if (cast(IntermediateNode)element) { | |
98 IntermediateNode node = cast(IntermediateNode) element; | |
99 length = node.children.length; | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
100 } |
137 | 101 /// TODO: fix me access violation here |
140 | 102 if(element !is null && elements !is null && (cast(ArrayWrapperIntermediateNode)element) && (cast(ArrayWrapperIntermediateNode)element).array is elements) |
103 length = elements.length; | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
104 viewer.setChildCount(element, length); |
140 | 105 } |
137 | 106 |
140 | 107 /* |
108 * (non-Javadoc) | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
109 * |
140 | 110 * @see dwtx.jface.viewers.ILazyTreeContentProvider#updateElement(java.lang.Object, |
111 * int) | |
112 */ | |
113 public void updateElement(Object parent, int index) { | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
114 |
140 | 115 Object element; |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
116 if (cast(IntermediateNode)parent) |
140 | 117 element = (cast(IntermediateNode) parent).children[index]; |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
118 |
140 | 119 else |
120 element = elements[index]; | |
121 viewer.replace(parent, index, element); | |
122 updateChildCount(element, -1); | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
123 |
140 | 124 } |
137 | 125 |
140 | 126 } |
137 | 127 |
140 | 128 public class LeafNode { |
129 public int counter; | |
130 public IntermediateNode parent; | |
137 | 131 |
140 | 132 public this(int counter, IntermediateNode parent) { |
133 this.counter = counter; | |
134 this.parent = parent; | |
135 } | |
137 | 136 |
140 | 137 public String toString() { |
138 return "Leaf " ~ to!(String)(this.counter); | |
139 } | |
140 } | |
137 | 141 |
140 | 142 public class IntermediateNode { |
143 public int counter; | |
144 public LeafNode[] children; | |
137 | 145 |
140 | 146 public this(int counter) { |
147 this.counter = counter; | |
137 | 148 children = new LeafNode[0]; |
140 | 149 } |
137 | 150 |
140 | 151 public String toString() { |
152 return "Node " ~ to!(String)(this.counter); | |
153 } | |
137 | 154 |
140 | 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 } | |
137 | 160 |
140 | 161 } |
162 } | |
137 | 163 |
140 | 164 public this(Shell shell) { |
165 final TreeViewer v = new TreeViewer(shell, DWT.VIRTUAL | DWT.BORDER); | |
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); | |
137 | 172 |
140 | 173 } |
137 | 174 |
140 | 175 private IntermediateNode[] createModel() { |
176 IntermediateNode[] elements = new IntermediateNode[10]; | |
137 | 177 |
140 | 178 for (int i = 0; i < 10; i++) { |
179 elements[i] = new IntermediateNode(i); | |
180 elements[i].generateChildren(1000); | |
181 } | |
137 | 182 |
140 | 183 return elements; |
184 } | |
137 | 185 |
140 | 186 } |