Mercurial > projects > dwt-samples
comparison jface/snippets/Snippet047VirtualLazyTreeViewer.d @ 137:96a2d0b35360
jface snippets 014, 026, 043, 047
author | yidabu <yidabu@gmail.com> |
---|---|
date | Wed, 06 Aug 2008 10:03:06 +0800 |
parents | |
children | 2b4e94cafb85 |
comparison
equal
deleted
inserted
replaced
136:7931ee9b41e6 | 137:96a2d0b35360 |
---|---|
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 | |
58 private class MyContentProvider : ILazyTreeContentProvider { | |
59 private TreeViewer viewer; | |
60 private IntermediateNode[] elements; | |
61 | |
62 public this(TreeViewer viewer) { | |
63 this.viewer = viewer; | |
64 } | |
65 | |
66 public void dispose() { | |
67 | |
68 } | |
69 | |
70 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { | |
71 if(cast(ArrayWrapperIntermediateNode) newInput) | |
72 this.elements = (cast(ArrayWrapperIntermediateNode) newInput).array; | |
73 } | |
74 | |
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 } | |
85 | |
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 } | |
99 /// TODO: fix me access violation here | |
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 } | |
104 | |
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 } | |
123 | |
124 } | |
125 | |
126 public class LeafNode { | |
127 public int counter; | |
128 public IntermediateNode parent; | |
129 | |
130 public this(int counter, IntermediateNode parent) { | |
131 this.counter = counter; | |
132 this.parent = parent; | |
133 } | |
134 | |
135 public String toString() { | |
136 return "Leaf " ~ to!(String)(this.counter); | |
137 } | |
138 } | |
139 | |
140 public class IntermediateNode { | |
141 public int counter; | |
142 public LeafNode[] children; | |
143 | |
144 public this(int counter) { | |
145 this.counter = counter; | |
146 children = new LeafNode[0]; | |
147 } | |
148 | |
149 public String toString() { | |
150 return "Node " ~ to!(String)(this.counter); | |
151 } | |
152 | |
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 } | |
158 | |
159 } | |
160 } | |
161 | |
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); | |
170 | |
171 } | |
172 | |
173 private IntermediateNode[] createModel() { | |
174 IntermediateNode[] elements = new IntermediateNode[10]; | |
175 | |
176 for (int i = 0; i < 10; i++) { | |
177 elements[i] = new IntermediateNode(i); | |
178 elements[i].generateChildren(1000); | |
179 } | |
180 | |
181 return elements; | |
182 } | |
183 | |
184 } |