package go.graphics.swing;

import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;

import java.awt.Component;
import java.awt.LayoutManager;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import go.graphics.event.GOEventHandlerProvider;
import go.graphics.swing.contextcreator.BackendSelector;
import go.graphics.swing.contextcreator.ContextCreator;
import go.graphics.swing.contextcreator.EBackendType;
import go.graphics.swing.opengl.LWJGLDrawContext;

public abstract class GLContainer extends JPanel implements GOEventHandlerProvider {


	protected ContextCreator cc;
	protected LWJGLDrawContext context;

	public GLContainer(EBackendType backend, LayoutManager layout) {
		setLayout(layout);

		try {
			cc = BackendSelector.createBackend(this, backend);
			cc.init();
		} catch (Exception ex) {
			ex.printStackTrace();
			JOptionPane.showMessageDialog(null, "Could not create opengl context through " + backend.cc_name + "\nPress ok to exit");
			System.exit(1);
		}
	}

	public void resize_gl(int width, int height) {
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		// coordinate system origin at lower left with width and height same as
		// the window
		GL11.glOrtho(0, width, 0, height, -1, 1);

		GL11.glMatrixMode(GL11.GL_MODELVIEW);
		GL11.glLoadIdentity();
		GL11.glViewport(0, 0, width, height);
	}


	public void init() {
		context = new LWJGLDrawContext(GL.createCapabilities());
	}

	/**
	 * Disposes all textures / buffers that were allocated by this context.
	 */
	public void disposeAll() {
		cc.stop();
		if (context != null) {
			context.disposeAll();
		}
		context = null;
	}

	public void draw() {
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		GL11.glLoadIdentity();
	}

	public void requestRedraw() {
		cc.repaint();
	}

	/**
	 * Forward the focus call to the Input canvas
	 */
	@Override
	public void requestFocus() {
		cc.requestFocus();
	}

	public void addCanvas(Component canvas) {
		add(canvas);
	}
}

@@ -197,16 +197,51 @@
 .JPanel;
+%0Aimport javax.swing.SwingUtilities;
 %0A%0Aimport
@@ -1382,16 +1382,61 @@
 t);%0A%09%7D%0A%0A
+%09private boolean errormessage_shown = false;%0A
 %0A%09public
@@ -1509,16 +1509,422 @@
 ies());%0A
+%09%09String version = GL11.glGetString(GL11.GL_VERSION);%0A%09%09if(version != null && version.length() %3E 5) %7B%0A%09%09%09if(version.charAt(0) %3E 1 %7C%7C version.charAt(3) %3E= 5) %7B%0A%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09if(!errormessage_shown) %7B%0A%09%09%09SwingUtilities.invokeLater(() -%3E %7B%0A%09%09%09%09JOptionPane.showMessageDialog(this, %22JSettlers needs at least OpenGL 1.5%5CnPress ok to exit%22);%0A%09%09%09%09System.exit(1);%0A%09%09%09%7D);%0A%09%09%09errormessage_shown = true;%0A%09%09%7D%0A
 %09%7D%0A%0A%09/**

print an error message if vbos arent supported