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:
|
|
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
|
140
|
65 private class MyContentProvider : ITreeContentProvider {
|
137
|
66
|
140
|
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 }
|
137
|
75
|
140
|
76 /*
|
|
77 * (non-Javadoc)
|
|
78 *
|
|
79 * @see dwtx.jface.viewers.IContentProvider#dispose()
|
|
80 */
|
|
81 public void dispose() {
|
137
|
82
|
140
|
83 }
|
137
|
84
|
140
|
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) {
|
137
|
92
|
140
|
93 }
|
137
|
94
|
140
|
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 }
|
137
|
103
|
140
|
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 }
|
137
|
113
|
140
|
114 return (cast(MyModel) element).parent;
|
|
115 }
|
137
|
116
|
140
|
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 }
|
137
|
125
|
140
|
126 }
|
137
|
127
|
140
|
128 public class MyModel {
|
|
129 public MyModel parent;
|
137
|
130
|
140
|
131 public ArrayList child;
|
137
|
132
|
140
|
133 public int counter;
|
137
|
134
|
140
|
135 public this(int counter, MyModel parent) {
|
|
136 this.parent = parent;
|
|
137 this.counter = counter;
|
137
|
138 child = new ArrayList();
|
140
|
139 }
|
137
|
140
|
140
|
141 public String toString() {
|
|
142 String rv = "Item ";
|
|
143 if (parent !is null) {
|
|
144 rv = parent.toString() ~ ".";
|
|
145 }
|
137
|
146
|
140
|
147 rv ~= to!(String)(counter);
|
137
|
148
|
140
|
149 return rv;
|
|
150 }
|
|
151 }
|
137
|
152
|
140
|
153 public class MyLabelProvider : LabelProvider,
|
|
154 ITableLabelProvider, ITableFontProvider, ITableColorProvider {
|
|
155 FontRegistry registry;
|
137
|
156 this()
|
|
157 {
|
|
158 registry = new FontRegistry();
|
|
159 }
|
|
160
|
140
|
161 public Image getColumnImage(Object element, int columnIndex) {
|
|
162 return null;
|
|
163 }
|
137
|
164
|
140
|
165 public String getColumnText(Object element, int columnIndex) {
|
|
166 return "Column " ~ to!(String)(columnIndex) ~ " => " ~ element.toString();
|
|
167 }
|
137
|
168
|
140
|
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 }
|
137
|
176
|
140
|
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 }
|
137
|
183
|
140
|
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 }
|
137
|
190
|
140
|
191 }
|
137
|
192
|
140
|
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 }
|
137
|
208
|
140
|
209 private MyModel createModel() {
|
137
|
210
|
140
|
211 MyModel root = new MyModel(0, null);
|
|
212 root.counter = 0;
|
137
|
213
|
140
|
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 }
|
137
|
222
|
140
|
223 return root;
|
|
224 }
|
137
|
225
|
|
226
|
|
227 }
|