// Copyright 2005, Danger, Inc.  All Rights Reserved.
// This file is subject to the Danger, Inc. Sample Code License,
// which is provided in the file SAMPLE_CODE_LICENSE.
// Copies are also available from http://developer.danger.com/

package com.danger.text;

import danger.ui.*;
import danger.util.*;
import danger.text.span.*;

/**
 * This window has an EditText with a getContents() override that
 * causes the text to be displayed in all caps, while the text
 * in the underlying buffer retains the original capitalization
 * or lack thereof.
 * <p>
 * It also demonstrates how you can use onTextChanged() to mirror
 * text changes into some other buffer.
 */
public class
GetDisplayText
extends DemoWindow
{
	public
	GetDisplayText()
	{
		EditText et = new ToUpperEditText(getApplication().getString(Resources.ID_ALTERED_DISPLAY), true, true);

		et.setSize(getWidth(), getHeight() / 2);
		et.show();
		addChild(et);
		setFocusedChild(et);

		MutableTextBox mtb = new MutableTextBox(getApplication().getString(Resources.ID_MIRRORED), mBuffer);

		mtb.setSize(getWidth(), getHeight() / 2);
		mtb.setPosition(0, getHeight() / 2);
		mtb.show();
		addChild(mtb);

		et.setText(getApplication().getString(Resources.ID_QUICK_BROWN));

		setTitle(getApplication().getString(Resources.ID_DEMO_GET_DISPLAY_TEXT));
	}

	private class
	ToUpperEditText
	extends EditText
	{
		public
		ToUpperEditText(String title, boolean text, boolean cap)
		{
			super(title, text, cap);
		}

		/**
		 * This causes the EditText to display the capitalized version
	 	 * of the text instead of the underlying buffer.
		 * <p>
		 * In this case, the display text is a complete TextBuffer mirror
		 * of the underlying text with all characters capitalized.
		 * It could instead be another CharSequence implementation
		 * that dynamically generates its contents upon request,
		 * but it is currently much faster to display from a TextBuffer
		 * or String than from other CharSequences so this uses
		 * a TextBuffer for speed.
	 	 */
		protected CharSequence
		getDisplayText()
		{
			// have to do this here because it gets called too early
			// in construction
			if (mToUpper == null)
			{
				mToUpper = new TextBuffer();
				mToUpper.insert(getContents().toString().toUpperCase());
			}

			return mToUpper;
		}

		/**
		 * This causes any text changes to the buffer to be mirrored
		 * into the other text box so you can see what's really in
		 * the first one, and into the display buffer with all
		 * characters capitalized.
		 */
		protected void
		onTextChanged(int start, int before, int after)
		{
			mBuffer.setPoint(start + before);
			mBuffer.delete(before);
			mBuffer.insert(StringUtils.substring(getContents(), start, start + after));

			mToUpper.setPoint(start + before);
			mToUpper.delete(before);
			mToUpper.insert(StringUtils.substring(getContents(), start, start + after).toUpperCase());

			super.onTextChanged(start, before, after);
		}

		private TextBuffer mToUpper;
	}

	private TextBuffer mBuffer = new TextBuffer();
}
