summaryrefslogtreecommitdiffstats
path: root/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/OpenGLExample.java
blob: a13da258e0dcac18459f163eb9e1154ce187476f (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
/*******************************************************************************
 * 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.opengl.examples;


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

import java.util.*;

public class OpenGLExample {
	private static ResourceBundle resourceBundle =
		ResourceBundle.getBundle("examples_opengl");
	private TabFolder tabFolder;
	private OpenGLTab[] tabs;
	private int sleep;
	
	public OpenGLExample() {}

	/**
	 * Creates an instance of an OpenGLExample embedded inside
	 * the supplied parent Composite.
	 * 
	 * @param parent the container of the example
	 */
	public OpenGLExample(Composite parent) {
		tabFolder = new TabFolder(parent, SWT.NONE);
		tabs =
			new OpenGLTab[] {
				new AntialiasingTab(),
				new AreaTab(),
				new FogTab(),
				new GradientTab(),
				new LightTab(),
				new NurbTab(),
				new ObjectsTab(),
				new ReflectionTab(),
				new StencilTab(),
				new TextureTab(),
				new TransparencyTab()
			};
				
		for (int i = 0; i < tabs.length; i++) {
			TabItem item = new TabItem(tabFolder, SWT.NONE);
			item.setText(tabs[i].getTabText());
			item.setControl(tabs[i].createTabFolderPage(tabFolder));
		}
		
		tabFolder.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				OpenGLTab tab = tabs[tabFolder.getSelectionIndex()];
				tab.setCurrent();
				sleep = tab.getSleepLength();
			}
		});
		if (tabs.length > 0) {
			tabs[0].setCurrent();
			sleep = tabs[0].getSleepLength();
		}
		
		Runnable timer = new Runnable() {
			public void run() {
				if (tabFolder.isDisposed()) return;
				display();
				tabFolder.getDisplay().timerExec(sleep, this);
			}
		};
		timer.run();
		tabFolder.addListener(SWT.Dispose, new Listener() {
			public void handleEvent(Event e) {
				dispose();
			}
		});
	}

	/**
	 * Renders the scene of the current tab.
	 */
	void display() {
		int index = tabFolder.getSelectionIndex();
		tabs[index].render();
		tabs[index].swap();
	}
	
	/**
	 * Disposes of all contained tabs.
	 */
	void dispose() {
		tabFolder = null;
		for (int i = 0; i < tabs.length; i++) {
			tabs[i].dispose();
		}
	}

	/**
	 * 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 resourceBundle.getString(key);
		} catch (MissingResourceException e) {
			return key;
		} catch (NullPointerException e) {
			return "!" + key + "!";
		}			
	}

	/**
	 * Invokes as a standalone program.
	 */
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		OpenGLExample instance = new OpenGLExample(shell);
		shell.setText(getResourceString("window.title"));
		shell.open();
		while (! shell.isDisposed()) {
			if (! display.readAndDispatch()) display.sleep();
		}
		instance.dispose();
	}

	public Shell open (Display display) {
		Shell shell = new Shell (display);
		shell.setLayout(new FillLayout());
		new OpenGLExample(shell);
		shell.setText(getResourceString("window.title"));
		shell.open();
		return shell;
	}
}