package todomore.android;

import org.metawidget.android.widget.AndroidMetawidget;
import org.metawidget.android.widget.widgetprocessor.binding.simple.SimpleBindingProcessor;

import com.darwinsys.todo.model.Task;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import todomore.android.TodoMoreApplication;

/** 
 * An Activity that shows all the Details of one Task, using MetaWidget.
 * It starts in display mode, but an Edit button makes it editable.
 * @author Ian Darwin
 */
public class DetailsActivity extends Activity {

	private static final String TAG = DetailsActivity.class.getSimpleName();
	private Task mTask;
	private AndroidMetawidget mMetawidget;
	private Button enableEditButton;

	public void onCreate(Bundle saved) {
		super.onCreate(saved);
		setContentView(R.layout.details_activity);

		long id = getIntent().getLongExtra("taskId", -1);
		Log.d(TAG, "_id = " + id);
		if (id > 0) {
			mTask = ((TodoMoreApplication) getApplication()).getTaskDao().findById(id);
		} else {
			mTask = new AndroidTask();
		}

		mMetawidget = (AndroidMetawidget) findViewById(R.id.metawidget);
		mMetawidget.setConfig(R.raw.metawidget);
        mMetawidget.setToInspect(mTask);
        if (id == -1) {
            mMetawidget.setReadOnly(false);
        }

        if (!mMetawidget.isReadOnly()) {
            enableEditing();
        }
        
        enableEditButton = (Button) findViewById(R.id.enableEditButton);
        enableEditButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Log.d(TAG, "onClick");
				enableEditing();
			}
		});
        
        ((Button) findViewById(R.id.cancelDetailsButton)).setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	private final View.OnClickListener saver = new View.OnClickListener() {
		public void onClick(View v) {
			Toast.makeText(DetailsActivity.this, "Saving...", Toast.LENGTH_SHORT).show();
			
			// Get the fields from the metawidget subfields into mTask:
			mMetawidget.getWidgetProcessor(SimpleBindingProcessor.class).save(mMetawidget);

			// Do the actual save
			((TodoMoreApplication) getApplication()).getTaskDao().update(mTask);
			
			// If no exception thrown...
			finish();
		}
	};

	private void enableEditing() {
		Log.d(TAG, "enableEditing");
		mMetawidget.setReadOnly(false);
		mMetawidget.buildWidgets();
		View view = mMetawidget.findViewWithTag("name");
		view.setFocusable(true);
		view.setFocusableInTouchMode(true);
		view.requestFocus();
		// Change the "Done" button to "Cancel"
		((Button) findViewById(R.id.cancelDetailsButton)).setText(R.string.cancelEditing);
		// Lose the Edit button
		enableEditButton.setVisibility(View.GONE);
		// Let the Save button appear, and become active
		Button save = (Button) findViewById(R.id.saveButton);
		save.setVisibility(View.VISIBLE);
		save.setOnClickListener(saver);
	}
}

@@ -123,55 +123,424 @@
 dget
-processor.binding.simple.SimpleBindingProcessor
+builder.AndroidWidgetBuilder;%0Aimport org.metawidget.android.widget.widgetbuilder.OverriddenWidgetBuilder;%0Aimport org.metawidget.android.widget.widgetbuilder.ReadOnlyWidgetBuilder;%0Aimport org.metawidget.android.widget.widgetprocessor.binding.simple.SimpleBindingProcessor;%0Aimport org.metawidget.widgetbuilder.composite.CompositeWidgetBuilder;%0Aimport org.metawidget.widgetbuilder.composite.CompositeWidgetBuilderConfig
 ;%0A%0Ai
@@ -766,35 +766,55 @@
 android.
-TodoMoreApplication
+metawidget.TodoMoreAndroidWidgetBuilder
 ;%0A%0A/** %0A
@@ -2870,16 +2870,322 @@
 false);%0A
+%09%09/*%0A%09%09mMetawidget.setWidgetBuilder(%0A%09%09%09new CompositeWidgetBuilder(new CompositeWidgetBuilderConfig%3CView, AndroidMetawidget%3E()%0A%09%09%09%09.setWidgetBuilders(%0A%09%09%09%09%09new OverriddenWidgetBuilder(), %0A%09%09%09%09%09new ReadOnlyWidgetBuilder(),%0A%09%09%09%09%09new TodoAndroidWidgetBuilder(), %0A%09%09%09%09%09new AndroidWidgetBuilder()%0A%09%09%09%09)));%0A%09%09*/%0A
 %09%09mMetaw

Add a WidgetBuilder to handle custom date type.