Mercurial > projects > dwt-samples
comparison jface/snippets/Snippet014TreeViewerNoMandatoryLabelProvider.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.Snippet014TreeViewerNoMandatoryLabelProvider; | |
15 | |
16 | |
17 import dwtx.jface.resource.FontRegistry; | |
18 import dwtx.jface.viewers.ITableColorProvider; | |
19 import dwtx.jface.viewers.ITableFontProvider; | |
20 import dwtx.jface.viewers.ITableLabelProvider; | |
21 import dwtx.jface.viewers.ITreeContentProvider; | |
22 import dwtx.jface.viewers.LabelProvider; | |
23 import dwtx.jface.viewers.TreeViewer; | |
24 import dwtx.jface.viewers.Viewer; | |
25 import dwt.DWT; | |
26 import dwt.graphics.Color; | |
27 import dwt.graphics.Font; | |
28 import dwt.graphics.Image; | |
29 import dwt.layout.FillLayout; | |
30 import dwt.widgets.Display; | |
31 import dwt.widgets.Shell; | |
32 import dwt.widgets.TreeColumn; | |
33 | |
34 import dwt.dwthelper.utils; | |
35 | |
36 import tango.util.Convert; | |
37 import tango.util.container.LinkedList; | |
38 | |
39 | |
40 | |
41 void main(String[] args) { | |
42 Display display = new Display(); | |
43 Shell shell = new Shell(display); | |
44 shell.setLayout(new FillLayout()); | |
45 new Snippet014TreeViewerNoMandatoryLabelProvider(shell); | |
46 shell.open(); | |
47 | |
48 while (!shell.isDisposed()) { | |
49 if (!display.readAndDispatch()) | |
50 display.sleep(); | |
51 } | |
52 | |
53 display.dispose(); | |
54 } | |
55 | |
56 /** | |
57 * A simple TreeViewer to demonstrate usage | |
58 * | |
59 * @author Tom Schindl <tom.schindl@bestsolution.at> | |
60 * | |
61 */ | |
62 public class Snippet014TreeViewerNoMandatoryLabelProvider { | |
63 alias LinkedList!(MyModel) ArrayList; | |
64 | |
65 private class MyContentProvider : ITreeContentProvider { | |
66 | |
67 /* | |
68 * (non-Javadoc) | |
69 * | |
70 * @see dwtx.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) | |
71 */ | |
72 public Object[] getElements(Object inputElement) { | |
73 return (cast(MyModel) inputElement).child.toArray(); | |
74 } | |
75 | |
76 /* | |
77 * (non-Javadoc) | |
78 * | |
79 * @see dwtx.jface.viewers.IContentProvider#dispose() | |
80 */ | |
81 public void dispose() { | |
82 | |
83 } | |
84 | |
85 /* | |
86 * (non-Javadoc) | |
87 * | |
88 * @see dwtx.jface.viewers.IContentProvider#inputChanged(dwtx.jface.viewers.Viewer, | |
89 * java.lang.Object, java.lang.Object) | |
90 */ | |
91 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { | |
92 | |
93 } | |
94 | |
95 /* | |
96 * (non-Javadoc) | |
97 * | |
98 * @see dwtx.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) | |
99 */ | |
100 public Object[] getChildren(Object parentElement) { | |
101 return getElements(parentElement); | |
102 } | |
103 | |
104 /* | |
105 * (non-Javadoc) | |
106 * | |
107 * @see dwtx.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) | |
108 */ | |
109 public Object getParent(Object element) { | |
110 if (element is null) { | |
111 return null; | |
112 } | |
113 | |
114 return (cast(MyModel) element).parent; | |
115 } | |
116 | |
117 /* | |
118 * (non-Javadoc) | |
119 * | |
120 * @see dwtx.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) | |
121 */ | |
122 public bool hasChildren(Object element) { | |
123 return (cast(MyModel) element).child.size() > 0; | |
124 } | |
125 | |
126 } | |
127 | |
128 public class MyModel { | |
129 public MyModel parent; | |
130 | |
131 public ArrayList child; | |
132 | |
133 public int counter; | |
134 | |
135 public this(int counter, MyModel parent) { | |
136 this.parent = parent; | |
137 this.counter = counter; | |
138 child = new ArrayList(); | |
139 } | |
140 | |
141 public String toString() { | |
142 String rv = "Item "; | |
143 if (parent !is null) { | |
144 rv = parent.toString() ~ "."; | |
145 } | |
146 | |
147 rv ~= to!(String)(counter); | |
148 | |
149 return rv; | |
150 } | |
151 } | |
152 | |
153 public class MyLabelProvider : LabelProvider, | |
154 ITableLabelProvider, ITableFontProvider, ITableColorProvider { | |
155 FontRegistry registry; | |
156 this() | |
157 { | |
158 registry = new FontRegistry(); | |
159 } | |
160 | |
161 public Image getColumnImage(Object element, int columnIndex) { | |
162 return null; | |
163 } | |
164 | |
165 public String getColumnText(Object element, int columnIndex) { | |
166 return "Column " ~ to!(String)(columnIndex) ~ " => " ~ element.toString(); | |
167 } | |
168 | |
169 public Font getFont(Object element, int columnIndex) { | |
170 if ((cast(MyModel) element).counter % 2 == 0) { | |
171 return registry.getBold(Display.getCurrent().getSystemFont() | |
172 .getFontData()[0].getName()); | |
173 } | |
174 return null; | |
175 } | |
176 | |
177 public Color getBackground(Object element, int columnIndex) { | |
178 if ((cast(MyModel) element).counter % 2 == 0) { | |
179 return Display.getCurrent().getSystemColor(DWT.COLOR_RED); | |
180 } | |
181 return null; | |
182 } | |
183 | |
184 public Color getForeground(Object element, int columnIndex) { | |
185 if ((cast(MyModel) element).counter % 2 == 1) { | |
186 return Display.getCurrent().getSystemColor(DWT.COLOR_RED); | |
187 } | |
188 return null; | |
189 } | |
190 | |
191 } | |
192 | |
193 public this(Shell shell) { | |
194 final TreeViewer v = new TreeViewer(shell); | |
195 | |
196 TreeColumn column = new TreeColumn(v.getTree(),DWT.NONE); | |
197 column.setWidth(200); | |
198 column.setText("Column 1"); | |
199 | |
200 column = new TreeColumn(v.getTree(),DWT.NONE); | |
201 column.setWidth(200); | |
202 column.setText("Column 2"); | |
203 | |
204 v.setLabelProvider(new MyLabelProvider()); | |
205 v.setContentProvider(new MyContentProvider()); | |
206 v.setInput(createModel()); | |
207 } | |
208 | |
209 private MyModel createModel() { | |
210 | |
211 MyModel root = new MyModel(0, null); | |
212 root.counter = 0; | |
213 | |
214 MyModel tmp; | |
215 for (int i = 1; i < 10; i++) { | |
216 tmp = new MyModel(i, root); | |
217 root.child.add(tmp); | |
218 for (int j = 1; j < i; j++) { | |
219 tmp.child.add(new MyModel(j, tmp)); | |
220 } | |
221 } | |
222 | |
223 return root; | |
224 } | |
225 | |
226 | |
227 } |