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