Mercurial > projects > dwt-samples
comparison 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 |
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.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 version(JIVE) import jive.stacktrace; | |
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 | |
59 * | |
60 * @author Tom Schindl <tom.schindl@bestsolution.at> | |
61 * | |
62 */ | |
63 public class Snippet014TreeViewerNoMandatoryLabelProvider { | |
64 alias LinkedList!(MyModel) ArrayList; | |
65 | |
66 private class MyContentProvider : ITreeContentProvider { | |
67 | |
68 /* | |
69 * (non-Javadoc) | |
70 * | |
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 } | |
76 | |
77 /* | |
78 * (non-Javadoc) | |
79 * | |
80 * @see dwtx.jface.viewers.IContentProvider#dispose() | |
81 */ | |
82 public void dispose() { | |
83 | |
84 } | |
85 | |
86 /* | |
87 * (non-Javadoc) | |
88 * | |
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) { | |
93 | |
94 } | |
95 | |
96 /* | |
97 * (non-Javadoc) | |
98 * | |
99 * @see dwtx.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) | |
100 */ | |
101 public Object[] getChildren(Object parentElement) { | |
102 return getElements(parentElement); | |
103 } | |
104 | |
105 /* | |
106 * (non-Javadoc) | |
107 * | |
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 } | |
114 | |
115 return (cast(MyModel) element).parent; | |
116 } | |
117 | |
118 /* | |
119 * (non-Javadoc) | |
120 * | |
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 } | |
126 | |
127 } | |
128 | |
129 public class MyModel { | |
130 public MyModel parent; | |
131 | |
132 public ArrayList child; | |
133 | |
134 public int counter; | |
135 | |
136 public this(int counter, MyModel parent) { | |
137 this.parent = parent; | |
138 this.counter = counter; | |
139 child = new ArrayList(); | |
140 } | |
141 | |
142 public String toString() { | |
143 String rv = "Item "; | |
144 if (parent !is null) { | |
145 rv = parent.toString() ~ "."; | |
146 } | |
147 | |
148 rv ~= to!(String)(counter); | |
149 | |
150 return rv; | |
151 } | |
152 } | |
153 | |
154 public class MyLabelProvider : LabelProvider, | |
155 ITableLabelProvider, ITableFontProvider, ITableColorProvider { | |
156 FontRegistry registry; | |
157 this() | |
158 { | |
159 registry = new FontRegistry(); | |
160 } | |
161 | |
162 public Image getColumnImage(Object element, int columnIndex) { | |
163 return null; | |
164 } | |
165 | |
166 public String getColumnText(Object element, int columnIndex) { | |
167 return "Column " ~ to!(String)(columnIndex) ~ " => " ~ element.toString(); | |
168 } | |
169 | |
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 } | |
177 | |
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 } | |
184 | |
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 } | |
191 | |
192 } | |
193 | |
194 public this(Shell shell) { | |
195 final TreeViewer v = new TreeViewer(shell); | |
196 | |
197 TreeColumn column = new TreeColumn(v.getTree(),DWT.NONE); | |
198 column.setWidth(200); | |
199 column.setText("Column 1"); | |
200 | |
201 column = new TreeColumn(v.getTree(),DWT.NONE); | |
202 column.setWidth(200); | |
203 column.setText("Column 2"); | |
204 | |
205 v.setLabelProvider(new MyLabelProvider()); | |
206 v.setContentProvider(new MyContentProvider()); | |
207 v.setInput(createModel()); | |
208 } | |
209 | |
210 private MyModel createModel() { | |
211 | |
212 MyModel root = new MyModel(0, null); | |
213 root.counter = 0; | |
214 | |
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 } | |
223 | |
224 return root; | |
225 } | |
226 | |
227 | |
228 } |