summaryrefslogtreecommitdiffstats
path: root/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/graphics/GraphicsExample.java
blob: 31d1dbc0386ca62d3a5531b77de3fc61fd0110d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.examples.graphics;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Vector;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class GraphicsExample {
	
	Composite parent;
	GraphicsTab[] tabs;
	GraphicsTab tab;
	Object[] tabBackground;
	
	boolean animate;
	
	Listener redrawListener;
	
	ToolBar toolBar;
	Tree tabList;
	Canvas canvas;
	Composite controlPanel, tabPanel;
	ToolItem playItem, pauseItem, backItem, dbItem;
	Spinner timerSpinner;
	
	Menu backMenu;
	MenuItem customMI;
	Image customImage;
	Color customColor;
	
	Vector images;
	
	static boolean advanceGraphics, advanceGraphicsInit;
	
	static final int TIMER = 30;
	static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle("examples_graphics"); //$NON-NLS-1$

public GraphicsExample(final Composite parent) {
	this.parent = parent;
	redrawListener = new Listener() {
		public void handleEvent(Event e) {
			redraw();
		}
	};
	GridData data;
	GridLayout layout = new GridLayout(3, false);
	layout.horizontalSpacing = 1;
	parent.setLayout(layout);
	tabs = createTabs();	
	images = new Vector();	
	createToolBar(parent);
	createTabList(parent);
	final Sash sash = new Sash(parent, SWT.VERTICAL);
	createTabPanel(parent);
	data = new GridData(SWT.FILL, SWT.CENTER, true, false);
	data.horizontalSpan = 3;
	toolBar.setLayoutData(data);	
	data = new GridData(SWT.CENTER, SWT.FILL, false, true);
	data.widthHint = tabList.computeSize(SWT.DEFAULT, SWT.DEFAULT).x + 50;
	tabList.setLayoutData(data);
	data = new GridData(SWT.CENTER, SWT.FILL, false, true);
	sash.setLayoutData(data);
	data = new GridData(SWT.FILL, SWT.FILL, true, true);
	tabPanel.setLayoutData(data);
	sash.addListener(SWT.Selection, new Listener() {
		public void handleEvent(Event event) {
			if (event.detail != SWT.DRAG) {
				GridData data = (GridData)tabList.getLayoutData();				
				data.widthHint = event.x - tabList.computeTrim(0, 0, 0, 0).width;
				parent.layout(true);
				animate = pauseItem.getEnabled();
			} else {
				animate = false;
			}
		}
	});	
	setTab(tab);
	startAnimationTimer();
}

boolean checkAdvancedGraphics() {
	if (advanceGraphicsInit) return advanceGraphics;
	advanceGraphicsInit = true;
	Display display = Display.getCurrent();
	try {
		Path path = new Path(display);
		path.dispose();
	} catch (SWTException e) {
		Shell shell = display.getActiveShell(), newShell = null;
		if (shell == null) shell = newShell = new Shell(display);
		MessageBox dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
		dialog.setText(RESOURCE_BUNDLE.getString("Warning")); //$NON-NLS-1$
		dialog.setMessage(RESOURCE_BUNDLE.getString("LibNotFound")); //$NON-NLS-1$
		dialog.open();
		if (newShell != null) newShell.dispose();
		return false;
	}
	return advanceGraphics = true;
}

void createCanvas(Composite parent) {
	canvas = new Canvas(parent, SWT.NO_BACKGROUND);
	canvas.addListener(SWT.Paint, new Listener() {
		public void handleEvent(Event event) {
			GC gc;
			Rectangle rect = canvas.getClientArea();
			Image buffer = null;
			if (dbItem.getSelection()) {
				buffer = new Image(canvas.getDisplay(), rect);
				gc = new GC(buffer);
			} else {
				gc = event.gc;
			}
			paintBackground(gc, rect);
			GraphicsTab tab = getTab();
			if (tab != null) tab.paint(gc, rect.width, rect.height);
			if (gc != event.gc) gc.dispose();
			if (buffer != null) {
				event.gc.drawImage(buffer, 0, 0);
				buffer.dispose();
			}
		}
	});
}

void createControlPanel(Composite parent) {
	Group group;
	controlPanel = group = new Group(parent, SWT.NONE);
	group.setText(getResourceString("Settings")); //$NON-NLS-1$
	controlPanel.setLayout(new RowLayout());
}

void createTabPanel(Composite parent) {
	tabPanel = new Composite(parent, SWT.NONE);
	GridData data;
	GridLayout layout = new GridLayout(1, false);
	layout.marginHeight = layout.marginWidth = 0;
	tabPanel.setLayout(layout);
	createCanvas(tabPanel);
	createControlPanel(tabPanel);
	data = new GridData(SWT.FILL, SWT.FILL, true, true);
	canvas.setLayoutData(data);	
	data = new GridData(SWT.FILL, SWT.CENTER, true, false);
	controlPanel.setLayoutData(data);
}

void createToolBar(final Composite parent) {
	final Display display = parent.getDisplay();
	
	toolBar = new ToolBar(parent, SWT.FLAT);
	Listener toolBarListener = new Listener() {
		public void handleEvent(Event event) {
			switch (event.type) {
				case SWT.Selection: {
					if (event.widget == playItem) {
						animate = true;
						playItem.setEnabled(!animate);
						pauseItem.setEnabled(animate);
					} else if (event.widget == pauseItem) {
						animate = false;
						playItem.setEnabled(!animate);
						pauseItem.setEnabled(animate);
					} else if (event.widget == backItem) {
						final ToolItem toolItem = (ToolItem) event.widget;
						final ToolBar  toolBar = toolItem.getParent();			
						Rectangle toolItemBounds = toolItem.getBounds();
						Point point = toolBar.toDisplay(new Point(toolItemBounds.x, toolItemBounds.y));
						backMenu.setLocation(point.x, point.y + toolItemBounds.height);
						backMenu.setVisible(true);
					}
				}
				break;
			}
		}
	};
	
	playItem = new ToolItem(toolBar, SWT.PUSH);
	playItem.setText(getResourceString("Play")); //$NON-NLS-1$
	playItem.setImage(loadImage(display, "play.gif")); //$NON-NLS-1$
	playItem.addListener(SWT.Selection, toolBarListener);
	
	pauseItem = new ToolItem(toolBar, SWT.PUSH);
	pauseItem.setText(getResourceString("Pause")); //$NON-NLS-1$
	pauseItem.setImage(loadImage(display, "pause.gif")); //$NON-NLS-1$
	pauseItem.addListener(SWT.Selection, toolBarListener);
	
	backItem = new ToolItem(toolBar, SWT.PUSH);
	backItem.setText(getResourceString("Background")); //$NON-NLS-1$
	backItem.addListener(SWT.Selection, toolBarListener);
	String[] names = new String[]{
		getResourceString("White"), //$NON-NLS-1$
		getResourceString("Black"), //$NON-NLS-1$
		getResourceString("Red"), //$NON-NLS-1$
		getResourceString("Green"), //$NON-NLS-1$
		getResourceString("Blue"), //$NON-NLS-1$
		getResourceString("CustomColor"), //$NON-NLS-1$
	};
	Color[] colors = new Color[]{
		display.getSystemColor(SWT.COLOR_WHITE),	
		display.getSystemColor(SWT.COLOR_BLACK),	
		display.getSystemColor(SWT.COLOR_RED),	
		display.getSystemColor(SWT.COLOR_GREEN),	
		display.getSystemColor(SWT.COLOR_BLUE),
		null,
	};	
	backMenu = new Menu(parent);
	Listener listener = new Listener() {
		public void handleEvent(Event event) {
			MenuItem item = (MenuItem)event.widget;
			if (customMI == item) {
				ColorDialog dialog = new ColorDialog(parent.getShell());
				RGB rgb = dialog.open();
				if (rgb == null) return;
				if (customColor != null) customColor.dispose();
				customColor = new Color(display, rgb);
				if (customImage != null) customImage.dispose();
				customImage = createImage(display, customColor);
				item.setData(new Object[]{customColor, customImage});
				item.setImage(customImage);
			}
			tabBackground = (Object[])item.getData();
			backItem.setImage((Image)tabBackground[1]);
			canvas.redraw();
		}
	};
	for (int i = 0; i < names.length; i++) {
		MenuItem item = new MenuItem(backMenu, SWT.NONE);
		item.setText(names[i]);
		item.addListener(SWT.Selection, listener);
		Image image = null;
		if (colors[i] != null) {
			image = createImage(display, colors[i]);
			images.addElement(image);
			item.setImage(image);
		} else {
			// custom menu item
			customMI = item;
		}
		item.setData(new Object[]{colors[i], image});
		if (tabBackground == null) {
			tabBackground = (Object[])item.getData();
			backItem.setImage((Image)tabBackground[1]);
		}
	}
	
	dbItem = new ToolItem(toolBar, SWT.CHECK);
	dbItem.setText(getResourceString("DoubleBuffer")); //$NON-NLS-1$
	dbItem.setImage(loadImage(display, "db.gif")); //$NON-NLS-1$

	ToolItem separator = new ToolItem(toolBar, SWT.SEPARATOR);
	Composite comp = new Composite(toolBar, SWT.NONE);
	GridData data;
	GridLayout layout = new GridLayout(1, false);
	layout.verticalSpacing = 0;
	layout.marginWidth = layout.marginHeight = 3;
	comp.setLayout(layout);
	timerSpinner = new Spinner(comp, SWT.BORDER | SWT.WRAP);
	data = new GridData(SWT.CENTER, SWT.CENTER, false, false);
	timerSpinner.setLayoutData(data);
	Label label = new Label(comp, SWT.NONE);
	label.setText(getResourceString("Animation")); //$NON-NLS-1$
	data = new GridData(SWT.CENTER, SWT.CENTER, false, false);
	label.setLayoutData(data);
	timerSpinner.setMaximum(1000);
	timerSpinner.setSelection(TIMER);
	timerSpinner.setSelection(TIMER);
	separator.setControl(comp);
	separator.setWidth(comp.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
}

Image createImage(Display display, Color color) {
	Image image = new Image(display, 16, 16);
	GC gc = new GC(image);
	gc.setBackground(color);
	Rectangle rect = image.getBounds();
	gc.fillRectangle(rect);
	if (color.equals(display.getSystemColor(SWT.COLOR_BLACK))) {
		gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
	}
	gc.drawRectangle(rect.x, rect.y, rect.width - 1, rect.height - 1);
	gc.dispose();
	return image;
}

void createTabList(Composite parent) {
	tabList = new Tree(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
	HashSet set = new HashSet();
	for (int i = 0; i < tabs.length; i++) {
		GraphicsTab tab = tabs[i];
		set.add(tab.getCategory());
	}
	for (Iterator iter = set.iterator(); iter.hasNext();) {
		String text = (String) iter.next();
		TreeItem item = new TreeItem(tabList, SWT.NONE);
		item.setText(text);
	}
	TreeItem[] items = tabList.getItems();
	for (int i = 0; i < items.length; i++) {
		TreeItem item = items[i];
		for (int j = 0; j < tabs.length; j++) {
			GraphicsTab tab = tabs[j];
			if (item.getText().equals(tab.getCategory())) {
				TreeItem item1 = new TreeItem(item, SWT.NONE);
				item1.setText(tab.getText());
				item1.setData(tab);
			}
		}
	}
	tabList.addListener(SWT.Selection, new Listener() {
		public void handleEvent(Event event) {
			TreeItem item = (TreeItem)event.item;
			if (item != null) {
				setTab((GraphicsTab)item.getData());
			}
		}
	});
}

GraphicsTab[] createTabs() {
	return new GraphicsTab[] {
		new LineTab(this),
		new StarPolyTab(this),
		tab = new IntroTab(this),
		new BlackHoleTab(this),
	};
}

/**
 * Disposes all resources created by the receiver.
 */
public void dispose() {
	if (tabs != null) {
		for (int i = 0; i < tabs.length; i++) {
			GraphicsTab tab = tabs[i];
			tab.dispose();
		}
	}
	tabs = null;
	if (images != null) {
		for (int i = 0; i < images.size(); i++) {
			((Image)images.elementAt(i)).dispose();
		}
	}
	images = null;
	if (customColor != null) customColor.dispose();
	customColor = null;
	if (customImage != null) customImage.dispose();
	customImage = null;
}

TreeItem findItemByData(TreeItem[] items, Object data) {
	for (int i = 0; i < items.length; i++) {
		TreeItem item = items[i];
		if (item.getData() == data) return item;
		item = findItemByData(item.getItems(), data);
		if (item != null) return item;
	}
	return null;
}

/**
 * Gets the current tab.
 */
public GraphicsTab getTab() {
	return tab;
}

Listener getRedrawListener() {
	return redrawListener;
}

/**
 * Gets a string from the resource bundle.
 * We don't want to crash because of a missing String.
 * Returns the key if not found.
 */
static String getResourceString(String key) {
	try {
		return RESOURCE_BUNDLE.getString(key);
	} catch (MissingResourceException e) {
		return key;
	} catch (NullPointerException e) {
		return "!" + key + "!"; //$NON-NLS-1$ //$NON-NLS-2$
	}			
}

static Image loadImage (Display display, Class clazz, String string) {
	InputStream stream = clazz.getResourceAsStream (string);
	if (stream == null) return null;
	Image image = null;
	try {
		image = new Image (display, stream);
	} catch (SWTException ex) {
	} finally {
		try {
			stream.close ();
		} catch (IOException ex) {}
	}
	return image;
}

Image loadImage(Display display, String name) {
	Image image = loadImage(display, GraphicsExample.class, name);
	if (image != null) images.addElement(image);
	return image;
}

void paintBackground(GC gc, Rectangle rect) {
	gc.setBackground((Color)tabBackground[0]);
	gc.fillRectangle(rect);
}

/**
 * Redraws the current tab.
 */
public void redraw() {
	canvas.redraw();
}

/**
 * Grabs input focus.
 */
public void setFocus() {
	tabList.setFocus();
}

/**
 * Sets the current tab.
 */
public void setTab(GraphicsTab tab) {
	this.tab = tab;
	Control[] children = controlPanel.getChildren();
	for (int i = 0; i < children.length; i++) {
		Control control = children[i];
		control.dispose();
	}
	if (tab != null) {
		tab.createControlPanel(controlPanel);
		animate = tab.isAnimated();
	}
	playItem.setEnabled(!animate);
	pauseItem.setEnabled(animate);
	GridData data = (GridData)controlPanel.getLayoutData();
	children = controlPanel.getChildren();
	data.exclude = children.length == 0;
	controlPanel.setVisible(!data.exclude);
	if (data.exclude) {
		tabPanel.layout();
	} else {
		tabPanel.layout(children);
	}
	if (tab != null) {
		TreeItem[] selection = tabList.getSelection();
		if (selection.length == 0 || selection[0].getData() != tab) {
			TreeItem item = findItemByData(tabList.getItems(), tab);
			if (item != null) tabList.setSelection(new TreeItem[]{item});
		}
	}
	canvas.redraw();
}

void startAnimationTimer() {
	final Display display = Display.getCurrent();
	display.timerExec(timerSpinner.getSelection(), new Runnable() {
		public void run() {
			if (canvas.isDisposed()) return;
			if (animate) {
				GraphicsTab tab = getTab();
				if (tab != null && tab.isAnimated()) {
					Rectangle rect = canvas.getClientArea();
					tab.next(rect.width, rect.height);
					canvas.redraw();
					canvas.update();
				}
			}
			display.timerExec(timerSpinner.getSelection(), this);
		}
	});
}

public static void main(String[] args) {
	Display display = new Display();
	Shell shell = new Shell(display);
	shell.setText(getResourceString("SWTGraphics")); //$NON-NLS-1$
	final GraphicsExample example = new GraphicsExample(shell);
	shell.addListener(SWT.Close, new Listener() {
		public void handleEvent(Event event) {
			example.dispose();
		}
	});	
	shell.open();
	while (!shell.isDisposed()) {
		if (!display.readAndDispatch()) display.sleep();
	}
}
}