<commit_before>/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */
package ti.modules.titanium.ui.widget;

import org.appcelerator.titanium.TiDict;
import org.appcelerator.titanium.TiProxy;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.Log;
import org.appcelerator.titanium.util.TiConfig;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.view.TiUIView;

import android.graphics.Rect;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.DialerKeyListener;
import android.text.method.DigitsKeyListener;
import android.text.method.NumberKeyListener;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TextKeyListener;
import android.text.method.TextKeyListener.Capitalize;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class TiUIText extends TiUIView
	implements TextWatcher, OnEditorActionListener, OnFocusChangeListener
{
	private static final String LCAT = "TiUIText";
	private static final boolean DBG = TiConfig.LOGD;

	public static final int RETURNKEY_GO = 0;
	public static final int RETURNKEY_GOOGLE = 1;
	public static final int RETURNKEY_JOIN = 2;
	public static final int RETURNKEY_NEXT = 3;
	public static final int RETURNKEY_ROUTE = 4;
	public static final int RETURNKEY_SEARCH = 5;
	public static final int RETURNKEY_YAHOO = 6;
	public static final int RETURNKEY_DONE = 7;
	public static final int RETURNKEY_EMERGENCY_CALL = 8;
	public static final int RETURNKEY_DEFAULT = 9;
	public static final int RETURNKEY_SEND = 10;

	private static final int KEYBOARD_ASCII = 0;
	private static final int KEYBOARD_NUMBERS_PUNCTUATION = 1;
	private static final int KEYBOARD_URL = 2;
	private static final int KEYBOARD_NUMBER_PAD = 3;
	private static final int KEYBOARD_PHONE_PAD = 4;
	private static final int KEYBOARD_EMAIL_ADDRESS = 5;
	private static final int KEYBOARD_NAMEPHONE_PAD = 6;
	private static final int KEYBOARD_DEFAULT = 7;
	
	// UIModule also has these as values - there's a chance they won't stay in sync if somebody changes one without changing these
	private static final int TEXT_AUTOCAPITALIZATION_NONE = 0;
	private static final int TEXT_AUTOCAPITALIZATION_SENTENCES = 1;
	private static final int TEXT_AUTOCAPITALIZATION_WORDS = 2;
	private static final int TEXT_AUTOCAPITALIZATION_ALL = 3;

	private boolean field;

	protected EditText tv;

	public TiUIText(TiViewProxy proxy, boolean field) {
		super(proxy);
		if (DBG) {
			Log.d(LCAT, "Creating a text field");
		}
		this.field = field;
		tv = new EditText(getProxy().getContext());
		if (field) {
			tv.setSingleLine();
			tv.setMaxLines(1);
		}
		tv.addTextChangedListener(this);
		tv.setOnEditorActionListener(this);
		tv.setOnFocusChangeListener(this); // TODO refactor to TiUIView?
		tv.setIncludeFontPadding(true); 
		//tv.setPadding(5, 0, 5, 0);
		if (field) {
			tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		} else {
			tv.setGravity(Gravity.TOP | Gravity.LEFT);
		}
		setNativeView(tv);

	}


	@Override
	public void processProperties(TiDict d)
	{
		super.processProperties(d);

		if (d.containsKey("enabled")) {
			tv.setEnabled(d.getBoolean("enabled"));
		}
		if (d.containsKey("value")) {
			tv.setText(d.getString("value"));
		}
		if (d.containsKey("color")) {
			tv.setTextColor(TiConvert.toColor(d, "color"));
		}
		if (d.containsKey("hintText")) {
			tv.setHint(d.getString("hintText"));
		}
		if (d.containsKey("font")) {
			TiUIHelper.styleText(tv, d.getTiDict("font"));
		}
		if (d.containsKey("textAlign") || d.containsKey("verticalAlign")) {
			String textAlign = null;
			String verticalAlign = null;
			if (d.containsKey("textAlign")) {
				textAlign = d.getString("textAlign");
			}
			if (d.containsKey("verticalAlign")) {
				verticalAlign = d.getString("verticalAlign");
			}
			handleTextAlign(textAlign, verticalAlign);
		}
		if (d.containsKey("returnKeyType")) {
			handleReturnKeyType(d.getInt("returnKeyType"));
		}
		if (d.containsKey("keyboardType") || d.containsKey("autocorrect") || d.containsKey("passwordMask") || d.containsKey("autocapitalization"))
		{
			handleKeyboard(d);
			
		}
	}


	@Override
	public void propertyChanged(String key, Object oldValue, Object newValue, TiProxy proxy)
	{
		if (DBG) {
			Log.d(LCAT, "Property: " + key + " old: " + oldValue + " new: " + newValue);
		}
		if (key.equals("enabled")) {
			tv.setEnabled(TiConvert.toBoolean(newValue));
		} else if (key.equals("value")) {
			tv.setText((String) newValue);
		} else if (key.equals("color")) {
			tv.setTextColor(TiConvert.toColor((String) newValue));
		} else if (key.equals("hintText")) {
			tv.setHint((String) newValue);
		} else if (key.equals("textAlign") || key.equals("verticalAlign")) {
			String textAlign = null;
			String verticalAlign = null;
			if (key.equals("textAlign")) {
				textAlign = TiConvert.toString(newValue);
			}
			if (key.equals("verticalAlign")) {
				verticalAlign = TiConvert.toString(newValue);
			}
			handleTextAlign(textAlign, verticalAlign);
		} else if (key.equals("keyboardType") || (key.equals("autocorrect") || key.equals("autocapitalization") || key.equals("passwordMask"))) {<commit_msg>[#1904 state:fixed-in-qa] property change now checks for existing values before reseting horizontal and vertical alignment.<commit_after>/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */
package ti.modules.titanium.ui.widget;

import org.appcelerator.titanium.TiDict;
import org.appcelerator.titanium.TiProxy;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.Log;
import org.appcelerator.titanium.util.TiConfig;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.view.TiUIView;

import android.graphics.Rect;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.DialerKeyListener;
import android.text.method.DigitsKeyListener;
import android.text.method.NumberKeyListener;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TextKeyListener;
import android.text.method.TextKeyListener.Capitalize;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class TiUIText extends TiUIView
	implements TextWatcher, OnEditorActionListener, OnFocusChangeListener
{
	private static final String LCAT = "TiUIText";
	private static final boolean DBG = TiConfig.LOGD;

	public static final int RETURNKEY_GO = 0;
	public static final int RETURNKEY_GOOGLE = 1;
	public static final int RETURNKEY_JOIN = 2;
	public static final int RETURNKEY_NEXT = 3;
	public static final int RETURNKEY_ROUTE = 4;
	public static final int RETURNKEY_SEARCH = 5;
	public static final int RETURNKEY_YAHOO = 6;
	public static final int RETURNKEY_DONE = 7;
	public static final int RETURNKEY_EMERGENCY_CALL = 8;
	public static final int RETURNKEY_DEFAULT = 9;
	public static final int RETURNKEY_SEND = 10;

	private static final int KEYBOARD_ASCII = 0;
	private static final int KEYBOARD_NUMBERS_PUNCTUATION = 1;
	private static final int KEYBOARD_URL = 2;
	private static final int KEYBOARD_NUMBER_PAD = 3;
	private static final int KEYBOARD_PHONE_PAD = 4;
	private static final int KEYBOARD_EMAIL_ADDRESS = 5;
	private static final int KEYBOARD_NAMEPHONE_PAD = 6;
	private static final int KEYBOARD_DEFAULT = 7;
	
	// UIModule also has these as values - there's a chance they won't stay in sync if somebody changes one without changing these
	private static final int TEXT_AUTOCAPITALIZATION_NONE = 0;
	private static final int TEXT_AUTOCAPITALIZATION_SENTENCES = 1;
	private static final int TEXT_AUTOCAPITALIZATION_WORDS = 2;
	private static final int TEXT_AUTOCAPITALIZATION_ALL = 3;

	private boolean field;

	protected EditText tv;

	public TiUIText(TiViewProxy proxy, boolean field) {
		super(proxy);
		if (DBG) {
			Log.d(LCAT, "Creating a text field");
		}
		this.field = field;
		tv = new EditText(getProxy().getContext());
		if (field) {
			tv.setSingleLine();
			tv.setMaxLines(1);
		}
		tv.addTextChangedListener(this);
		tv.setOnEditorActionListener(this);
		tv.setOnFocusChangeListener(this); // TODO refactor to TiUIView?
		tv.setIncludeFontPadding(true); 
		//tv.setPadding(5, 0, 5, 0);
		if (field) {
			tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		} else {
			tv.setGravity(Gravity.TOP | Gravity.LEFT);
		}
		setNativeView(tv);

	}


	@Override
	public void processProperties(TiDict d)
	{
		super.processProperties(d);

		if (d.containsKey("enabled")) {
			tv.setEnabled(d.getBoolean("enabled"));
		}
		if (d.containsKey("value")) {
			tv.setText(d.getString("value"));
		}
		if (d.containsKey("color")) {
			tv.setTextColor(TiConvert.toColor(d, "color"));
		}
		if (d.containsKey("hintText")) {
			tv.setHint(d.getString("hintText"));
		}
		if (d.containsKey("font")) {
			TiUIHelper.styleText(tv, d.getTiDict("font"));
		}
		if (d.containsKey("textAlign") || d.containsKey("verticalAlign")) {
			String textAlign = null;
			String verticalAlign = null;
			if (d.containsKey("textAlign")) {
				textAlign = d.getString("textAlign");
			}
			if (d.containsKey("verticalAlign")) {
				verticalAlign = d.getString("verticalAlign");
			}
			handleTextAlign(textAlign, verticalAlign);
		}
		if (d.containsKey("returnKeyType")) {
			handleReturnKeyType(d.getInt("returnKeyType"));
		}
		if (d.containsKey("keyboardType") || d.containsKey("autocorrect") || d.containsKey("passwordMask") || d.containsKey("autocapitalization"))
		{
			handleKeyboard(d);
			
		}
	}


	@Override
	public void propertyChanged(String key, Object oldValue, Object newValue, TiProxy proxy)
	{
		if (DBG) {
			Log.d(LCAT, "Property: " + key + " old: " + oldValue + " new: " + newValue);
		}
		if (key.equals("enabled")) {
			tv.setEnabled(TiConvert.toBoolean(newValue));
		} else if (key.equals("value")) {
			tv.setText((String) newValue);
		} else if (key.equals("color")) {
			tv.setTextColor(TiConvert.toColor((String) newValue));
		} else if (key.equals("hintText")) {
			tv.setHint((String) newValue);
		} else if (key.equals("textAlign") || key.equals("verticalAlign")) {
			String textAlign = null;
			String verticalAlign = null;
			if (key.equals("textAlign")) {
				textAlign = TiConvert.toString(newValue);
			} else if (proxy.hasDynamicValue("textAlign")){
				textAlign = TiConvert.toString(proxy.getDynamicValue("textAlign"));
			}
			if (key.equals("verticalAlign")) {
				verticalAlign = TiConvert.toString(newValue);
			} else if (proxy.hasDynamicValue("verticalAlign")){
				verticalAlign = TiConvert.toString(proxy.getDynamicValue("verticalAlign"));
			}
			handleTextAlign(textAlign, verticalAlign);
		} else if (key.equals("keyboardType") || (key.equals("autocorrect") || key.equals("autocapitalization") || key.equals("passwordMask"))) {
{'fraction_non_alphanumeric': 0.11684460260972716, 'fraction_numerical': 0.0066090493136756485, 'mean_word_length': 4.997459349593496, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2369977', 'n_tokens_mistral': 4192, 'n_tokens_neox': 3802, 'n_words': 882}