package de.markusfisch.android.shadereditor.widget;

import de.markusfisch.android.shadereditor.adapter.TextureSpinnerAdapter;
import de.markusfisch.android.shadereditor.app.ShaderEditorApp;
import de.markusfisch.android.shadereditor.database.Database;
import de.markusfisch.android.shadereditor.opengl.BackBufferParameters;
import de.markusfisch.android.shadereditor.R;

import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MergeCursor;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.Spinner;

public class BackBufferParametersView extends LinearLayout {
	private TextureSpinnerAdapter adapter;
	private Spinner presetView;

	public BackBufferParametersView(Context context) {
		super(context);
	}

	public BackBufferParametersView(Context context, AttributeSet attr) {
		super(context, attr);
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();

		MatrixCursor matrixCursor = null;
		try {
			matrixCursor = new MatrixCursor(new String[] {
				Database.TEXTURES_ID,
				Database.TEXTURES_NAME,
				Database.TEXTURES_WIDTH,
				Database.TEXTURES_HEIGHT,
				Database.TEXTURES_THUMB
			});
			matrixCursor.addRow(new Object[] {
				-1,
				getContext().getString(R.string.no_preset),
				0,
				0,
				null
			});

			MergeCursor mergeCursor = new MergeCursor(new Cursor[] {
				matrixCursor,
				ShaderEditorApp.db.getTextures()
			});

			adapter = new TextureSpinnerAdapter(getContext(), mergeCursor);

			presetView = findViewById(R.id.backbuffer_preset);
			presetView.setAdapter(adapter);
		} finally {
			if (matrixCursor != null) {
				matrixCursor.close();
			}
		}
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		adapter.changeCursor(null);
	}

	public void setParameters(BackBufferParameters tp) {
		Cursor cursor = (Cursor) presetView.getSelectedItem();
		long id = cursor.getLong(cursor.getColumnIndex(
				Database.TEXTURES_ID));
		if (id > 0) {
			String preset = cursor.getString(cursor.getColumnIndex(
					Database.TEXTURES_NAME));
			tp.setPreset(preset);
		}
	}
}

@@ -1951,16 +1951,55 @@
 Item();%0A
+%09%09if (cursor == null) %7B%0A%09%09%09return;%0A%09%09%7D%0A
 %09%09long i
@@ -2084,31 +2084,42 @@
  %3E 0
-) %7B%0A%09%09%09String p
+ && tp != null) %7B%0A%09%09%09tp.setP
 reset
- = 
+(
 curs
@@ -2187,32 +2187,8 @@
 ME))
-;%0A%09%09%09tp.setPreset(preset
 );%0A%09

Fix NPE when setting backbuffer parameters