comparison jface/snippets/viewers/Snippet010OwnerDraw.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/Snippet010OwnerDraw.d@781fd8aadeae
children
comparison
equal deleted inserted replaced
144:7248e4c09c4f 145:161f7698cfb8
1 /*******************************************************************************
2 * Copyright (c) 2006, 2008 IBM Corporation 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 * IBM Corporation - initial API and implementation
10 * oliver.schaefer@mbtech-services.com - Fix for Bug 225051 [Snippets] Snippet010OwnerDraw - Wrong german flag
11 * Port to the D programming language:
12 * yidabu at gmail dot com ( D China http://www.d-programming-language-china.org/ )
13 *******************************************************************************/
14 module snippets.viewers.Snippet010OwnerDraw.d;
15
16 import dwtx.jface.resource.JFaceResources;
17 import dwtx.jface.viewers.Viewer;
18 import dwtx.jface.viewers.ColumnPixelData;
19 import dwtx.jface.viewers.IStructuredContentProvider;
20 import dwtx.jface.viewers.OwnerDrawLabelProvider;
21 import dwtx.jface.viewers.StructuredSelection;
22 import dwtx.jface.viewers.TableLayout;
23 import dwtx.jface.viewers.TableViewer;
24 import dwt.DWT;
25 import dwt.graphics.Rectangle;
26 import dwt.graphics.TextLayout;
27 import dwt.graphics.TextStyle;
28 import dwt.layout.GridData;
29 import dwt.layout.GridLayout;
30 import dwt.widgets.Composite;
31 import dwt.widgets.Display;
32 import dwt.widgets.Event;
33 import dwt.widgets.Shell;
34 import dwt.widgets.TableColumn;
35
36 import dwt.dwthelper.utils;
37
38 void main(String[] args) {
39 Snippet010OwnerDraw.main(args);
40 }
41
42 public class Snippet010OwnerDraw {
43
44 public static void main(String[] args) {
45
46 Display display = new Display();
47 Shell shell = new Shell(display, DWT.CLOSE);
48 shell.setSize(400, 400);
49 shell.setLayout(new GridLayout());
50
51 Snippet010OwnerDraw example = new Snippet010OwnerDraw();
52 example.createPartControl(shell);
53
54 shell.open();
55
56 while (!shell.isDisposed ()) {
57 if (!display.readAndDispatch ()) display.sleep ();
58 }
59 display.dispose();
60 }
61
62 private static int COLUMN_COUNT = 3;
63
64 class CountryEntry {
65
66 String name;
67
68 String cupYear;
69
70 private String baseName;
71
72 /**
73 * Create a new instance of the receiver.
74 *
75 * @param countryName
76 * @param worldCupYear
77 */
78 this(String countryName, String englishName, String worldCupYear) {
79 name = countryName;
80 cupYear = worldCupYear;
81 baseName = englishName;
82 }
83
84 /**
85 * @param index
86 * @return
87 */
88 public int getHeight(Event event) {
89 switch (event.index) {
90 case 0:
91 return event.gc.textExtent(name).y;
92 case 1:
93 return 50;
94 case 2:
95 return event.gc.textExtent(cupYear).y;
96 default:
97 return 10;
98 }
99 }
100
101 /**
102 * @param index
103 * @return
104 */
105 public int getWidth(Event event) {
106
107 switch (event.index) {
108 case 0:
109 return event.gc.textExtent(getDisplayString().toString()).x + 4;
110
111 case 1:
112 return 200;
113
114 case 2:
115 return event.gc.textExtent(cupYear).x + 5;
116
117 default:
118 return 10;
119 }
120 }
121
122 /**
123 * Draw the flag in bounds.
124 *
125 * @param event
126 */
127 protected void drawFlag(Event event) {
128 event.gc.setBackground(viewer.getControl().getDisplay()
129 .getSystemColor(DWT.COLOR_BLUE));
130
131 Rectangle bounds = event.getBounds();
132 bounds.width += 100;
133 event.gc.fillRectangle(bounds);
134 }
135
136 /**
137 * Draw the cup year
138 *
139 * @param event
140 */
141 private void drawCupYear(Event event) {
142 event.gc.drawText(cupYear, event.x, event.y);
143
144 }
145
146 /**
147 * Draw the name of the receiver.
148 *
149 * @param event
150 */
151 protected void drawName(Event event) {
152
153 StringBuffer buffer = getDisplayString();
154
155 Display display = viewer.getControl().getDisplay();
156 TextLayout layout = new TextLayout(display);
157 layout.setText(buffer.toString());
158
159 TextStyle plain = new TextStyle(JFaceResources
160 .getFont(JFaceResources.DEFAULT_FONT), display
161 .getSystemColor(DWT.COLOR_LIST_FOREGROUND), display
162 .getSystemColor(DWT.COLOR_LIST_BACKGROUND));
163
164 TextStyle italic = new TextStyle(JFaceResources.getFontRegistry()
165 .getItalic(JFaceResources.DEFAULT_FONT), display
166 .getSystemColor(DWT.COLOR_BLUE), display
167 .getSystemColor(DWT.COLOR_LIST_BACKGROUND));
168
169 layout.setStyle(plain, 0, name.length - 1);
170 layout.setStyle(italic, name.length, buffer.length - 1);
171
172 layout.draw(event.gc, event.x, event.y);
173
174 }
175
176 /**
177 * @return
178 */
179 private StringBuffer getDisplayString() {
180 StringBuffer buffer = new StringBuffer();
181 buffer.append(name);
182 buffer.append(" (");
183 buffer.append(baseName);
184 buffer.append(")");
185 return buffer;
186 }
187
188 /**
189 * @param event
190 */
191 public void draw(Event event) {
192
193 switch (event.index) {
194 case 0:
195 drawName(event);
196 break;
197 case 1:
198 drawFlag(event);
199 break;
200 case 2:
201 drawCupYear(event);
202 break;
203
204 default:
205 break;
206 }
207
208 }
209 }
210
211 private class GermanyEntry : CountryEntry {
212
213 this() {
214 super("Deutschland", "Germany", "1954 1974 1990");
215 }
216
217 /*
218 * (non-Javadoc)
219 *
220 * @see dwtx.jface.tests.viewers.OwnerDrawExample.CountryEntry#drawFlag(dwt.widgets.Event)
221 */
222 protected void drawFlag(Event event) {
223
224 Rectangle bounds = event.getBounds();
225 bounds.width += 100;
226 int stripeHeight = bounds.height / 3;
227 Rectangle stripe = new Rectangle(bounds.x, bounds.y, bounds.width,
228 stripeHeight);
229
230 event.gc.setBackground(viewer.getControl().getDisplay()
231 .getSystemColor(DWT.COLOR_BLACK));
232 event.gc.fillRectangle(stripe);
233
234 stripe.y += stripeHeight;
235
236 event.gc.setBackground(viewer.getControl().getDisplay()
237 .getSystemColor(DWT.COLOR_RED));
238 event.gc.fillRectangle(stripe);
239
240 stripe.y += stripeHeight;
241
242 event.gc.setBackground(viewer.getControl().getDisplay()
243 .getSystemColor(DWT.COLOR_YELLOW));
244 event.gc.fillRectangle(stripe);
245
246 }
247
248 }
249
250 private class AustriaEntry : CountryEntry {
251
252 this() {
253 super("\u00D6sterreich", "Austria", "TBD");
254 }
255
256 /*
257 * (non-Javadoc)
258 *
259 * @see dwtx.jface.tests.viewers.OwnerDrawExample.CountryEntry#drawFlag(dwt.widgets.Event)
260 */
261 protected void drawFlag(Event event) {
262
263 Rectangle bounds = event.getBounds();
264 bounds.width += 100;
265 int stripeHeight = bounds.height / 3;
266 Rectangle stripe = new Rectangle(bounds.x, bounds.y, bounds.width,
267 stripeHeight);
268
269 event.gc.setBackground(viewer.getControl().getDisplay()
270 .getSystemColor(DWT.COLOR_RED));
271 event.gc.fillRectangle(stripe);
272
273 stripe.y += stripeHeight;
274
275 event.gc.setBackground(viewer.getControl().getDisplay()
276 .getSystemColor(DWT.COLOR_WHITE));
277 event.gc.fillRectangle(stripe);
278
279 stripe.y += stripeHeight;
280
281 event.gc.setBackground(viewer.getControl().getDisplay()
282 .getSystemColor(DWT.COLOR_RED));
283 event.gc.fillRectangle(stripe);
284
285 }
286 }
287
288 private class EnglandEntry : CountryEntry {
289 this() {
290 super("Blighty", "England", "1966");
291 }
292
293 /*
294 * (non-Javadoc)
295 *
296 * @see dwtx.jface.tests.viewers.OwnerDrawExample.CountryEntry#drawFlag(dwt.widgets.Event)
297 */
298 protected void drawFlag(Event event) {
299
300 Rectangle bounds = event.getBounds();
301 bounds.width += 100;
302
303 event.gc.setBackground(viewer.getControl().getDisplay()
304 .getSystemColor(DWT.COLOR_RED));
305 event.gc.fillRectangle(new Rectangle(bounds.width / 2 + bounds.x
306 - 5, bounds.y, 10, bounds.height));
307 event.gc.fillRectangle(new Rectangle(bounds.x, bounds.height / 2
308 + bounds.y - 5, bounds.width, 10));
309
310 }
311 }
312
313 private TableViewer viewer;
314
315 private CountryEntry[] entries;
316
317 public this() {
318 entries = new CountryEntry[3];
319 entries[0] = new AustriaEntry();
320 entries[1] = new GermanyEntry();
321 entries[2] = new EnglandEntry();
322 }
323
324 /*
325 * (non-Javadoc)
326 *
327 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(dwt.widgets.Composite)
328 */
329 public void createPartControl(Composite parent) {
330 viewer = new TableViewer(parent, DWT.FULL_SELECTION);
331
332 viewer.setContentProvider(new class() IStructuredContentProvider {
333 /*
334 * (non-Javadoc)
335 *
336 * @see dwtx.jface.viewers.IContentProvider#dispose()
337 */
338 public void dispose() {
339 };
340
341 /*
342 * (non-Javadoc)
343 *
344 * @see dwtx.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
345 */
346 public Object[] getElements(Object inputElement) {
347 return (cast(Snippet010OwnerDraw) inputElement).entries;
348 };
349
350 /*
351 * (non-Javadoc)
352 *
353 * @see dwtx.jface.viewers.IContentProvider#inputChanged(dwtx.jface.viewers.Viewer,
354 * java.lang.Object, java.lang.Object)
355 */
356 public void inputChanged(Viewer viewer,
357 Object oldInput, Object newInput) {
358 }
359
360 });
361 createColumns();
362
363 viewer.setLabelProvider(new class() OwnerDrawLabelProvider {
364
365
366 protected void measure(Event event, Object element) {
367 CountryEntry country = cast(CountryEntry) element;
368
369 event.setBounds(new Rectangle(event.x, event.y, country.getWidth(event),
370 country.getHeight(event)));
371
372 }
373
374 /*
375 * (non-Javadoc)
376 *
377 * @see dwtx.jface.viewers.OwnerDrawLabelProvider#paint(dwt.widgets.Event,
378 * java.lang.Object)
379 */
380 protected void paint(Event event, Object element) {
381 CountryEntry entry = cast(CountryEntry) element;
382 entry.draw(event);
383
384 }
385 });
386
387 OwnerDrawLabelProvider.setUpOwnerDraw(viewer);
388 viewer.setInput(this);
389 GridData data = new GridData(GridData.GRAB_HORIZONTAL
390 | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
391
392 viewer.getControl().setLayoutData(data);
393
394 viewer.setSelection(new StructuredSelection(entries[1]));
395 }
396
397 /**
398 * Create the columns to be used in the tree.
399 */
400 private void createColumns() {
401 TableLayout layout = new TableLayout();
402 viewer.getTable().setLayout(layout);
403 viewer.getTable().setHeaderVisible(true);
404 viewer.getTable().setLinesVisible(true);
405
406 for (int i = 0; i < COLUMN_COUNT; i++) {
407 TableColumn tc = new TableColumn(viewer.getTable(), DWT.NONE, i);
408 layout.addColumnData(new ColumnPixelData(100));
409 tc.setText(getTitleFor(i));
410 }
411 ;
412 }
413
414 /**
415 * @param i
416 * @return
417 */
418 private String getTitleFor(int i) {
419 switch (i) {
420 case 0:
421 return "Name";
422 case 1:
423 return "Flag";
424 case 2:
425 return "World Cup Year";
426 }
427 return "Unknown";
428 }
429
430 public void setFocus() {
431
432 }
433
434 }