Mercurial > projects > dwt-samples
comparison 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 |
comparison
equal
deleted
inserted
replaced
144:7248e4c09c4f | 145:161f7698cfb8 |
---|---|
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 snippets.viewers.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 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 * | |
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 } | |
87 | |
88 /* | |
89 * (non-Javadoc) | |
90 * | |
91 * @see dwtx.jface.viewers.ILazyTreeContentProvider#updateChildCount(java.lang.Object, | |
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 * | |
110 * @see dwtx.jface.viewers.ILazyTreeContentProvider#updateElement(java.lang.Object, | |
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) { | |
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); | |
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 } |