
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2008 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any.  The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
*
* $DateTime: 2008/04/10 13:35:00 $
*
* Adobe Output Module 2.0
*
* MediaGallery Originally Written by Quality Process Incorporated, Enhanced by
* Adobe System China. Contact Sheet written by Adobe System China.
* 
*
**************************************************************************/

AOM.AutoIncrementEditText = function(control, layoutGroup) {
	this.control = control;
	this.layoutGroup = layoutGroup;
	
	this.initialize();
	this.initEventHandler();
}

AOM.AutoIncrementEditText.PRESET_LINE = 5;
AOM.AutoIncrementEditText.SUFFIX_PIXELS = 3;
AOM.AutoIncrementEditText.MAXSIZE = 1000;

AOM.AutoIncrementEditText.prototype.initialize = function() {
	var control = this.control;
	control.maximumSize.height = 1000;

	var minSizes = this.computeMinSizes();
	this.horizontalMargin = minSizes.horizontalMargin;
	this.verticalMargin = minSizes.verticalMargin;
	this.minHeight = minSizes.minHeight;
	this.minWidth = control.size.width;
	this.textMaxWidth = this.minWidth - this.horizontalMargin;
	if (control.maxLine == undefined) {
		this.presetLine = AOM.AutoIncrementEditText.PRESET_LINE;
	} else {
		this.presetLine = control.maxLine;
	}
	this.presetHeight = this.presetLine * this.minHeight + AOM.AutoIncrementEditText.SUFFIX_PIXELS;
	
	//reset control size.
	var gfx = control.graphics;
	var textSize = gfx.measureString(control.text, undefined, this.textMaxWidth);
	var textHeight = textSize.height + AOM.AutoIncrementEditText.SUFFIX_PIXELS;
	if (textHeight > this.presetHeight) {
		control.size.height = textHeight ;
	} else {
		control.size.height = this.presetHeight;
	}
}


AOM.AutoIncrementEditText.prototype.computeMinSizes = function() {
	var etHorizontalMargins, etVerticalMargins;

	var dummyWinResource =
			"""window {
				dummyEt: EditText {
					properties:{
						borderless:true, multiline:true, scrolling:false
					},
					text:'X'
				}
			}""";
			
	var win = new Window(dummyWinResource);
	var dummyTextSize = win.dummyEt.graphics.measureString ("X");
	var etHorizontalMargins = win.dummyEt.preferredSize.width - dummyTextSize.width;
	var etVerticalMargins = win.dummyEt.preferredSize.height - dummyTextSize.height;
	var etMinHeight = win.dummyEt.preferredSize.height;
	
	return {horizontalMargin: etHorizontalMargins, verticalMargin: etVerticalMargins, minHeight:etMinHeight};
}



AOM.AutoIncrementEditText.prototype.keyDownEventHandler = function(event) {
	if (this.text.length >= AOM.AutoIncrementEditText.MAXSIZE) {
		//only allow copyOperator and deleteOperator
		var keyIsOk = ( (event.keyName == 'Backspace' ) && !(event.ctrlKey) )  
							|| ( (event.keyName == 'C') && (event.ctrlKey || event.metaKey) ) 
							|| ( event.keyName == 'Delete' );
								
		if (keyIsOk) {
			//ok
		} else {
			event.preventDefault();
		}
	}
}

AOM.AutoIncrementEditText.prototype.initEventHandler = function() {
	var controlObj = this;
	var control = this.control;
	var gfx = control.graphics;
	
	control.onChanging = function() {	
		if (this.text.length > AOM.AutoIncrementEditText.MAXSIZE) {
			var tempText = this.text;
			this.text = tempText.substr(0, AOM.AutoIncrementEditText.MAXSIZE);	
		}
		
		var textSize = gfx.measureString(this.text, undefined, controlObj.textMaxWidth);
		var textHeight = textSize.height + AOM.AutoIncrementEditText.SUFFIX_PIXELS;
		var currentHeight = this.size.height;
			
		if (textHeight > currentHeight) {
			this.size.height = textHeight ;
			controlObj.layoutGroup.layout.layout(true);
		} else if (textHeight < currentHeight) {
			if (textHeight > controlObj.presetHeight) {
				this.size.height = textHeight ;
				controlObj.layoutGroup.layout.layout(true);
			} else {
				if (currentHeight != controlObj.presetHeight) {
					this.size.height = controlObj.presetHeight;
					controlObj.layoutGroup.layout.layout(true);
				} 
			}
		} 
	}

	//add keyboard event listener
	control.addEventListener('keydown', this.keyDownEventHandler);	
}


