<commit_before>package org.openforis.collect.presenter {
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.binding.utils.ChangeWatcher;
	import mx.collections.ArrayCollection;
	import mx.collections.IList;
	import mx.events.PropertyChangeEvent;
	
	import org.openforis.collect.Application;
	import org.openforis.collect.event.UIEvent;
	import org.openforis.collect.i18n.Message;
	import org.openforis.collect.model.CollectRecord$Step;
	import org.openforis.collect.model.FieldSymbol;
	import org.openforis.collect.model.proxy.AttributeProxy;
	import org.openforis.collect.model.proxy.FieldProxy;
	import org.openforis.collect.ui.component.input.DropDownInputField;
	import org.openforis.collect.util.CollectionUtil;
	import org.openforis.collect.util.ObjectUtil;
	import org.openforis.collect.util.StringUtil;
	
	/**
	 * 
	 * @author <NAME>
	 * */
	public class DropDownInputFieldPresenter extends InputFieldPresenter {
		
		public static const EMPTY_ITEM:Object = {label: Message.get('global.dropDownEmpty'), separator: true};
		public static const BLANK_ON_FORM_ITEM:Object = {label: Message.get('edit.dropDownList.blankOnForm'), shortCut: "*", separator: true};
		public static const DASH_ON_FORM_ITEM:Object = {label: Message.get('edit.dropDownList.dashOnForm'), shortCut: "-"};
		public static const ILLEGIBLE_ITEM:Object = {label: Message.get('edit.dropDownList.illegible'), shortCut: "?"};

		private var _view:DropDownInputField;
		
		public function DropDownInputFieldPresenter(inputField:DropDownInputField) {
			_view = inputField;
			
			super(inputField);
			
			initInternalDataProvider();
		}
		
		override internal function initEventListeners():void {
			super.initEventListeners();
			
			_view.dropDownList.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
			_view.dropDownList.addEventListener(FocusEvent.FOCUS_OUT, focusOutHandler);
			_view.dropDownList.addEventListener(Event.CHANGE, changeHandler);
			_view.dropDownList.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			eventDispatcher.addEventListener(UIEvent.ACTIVE_RECORD_CHANGED, activeRecordChangeHandler);
			
			BindingUtils.bindSetter(setDataProvider, _view, "dataProvider");
			BindingUtils.bindSetter(setDefaultValue, _view, "defaultValue");
		}
		
		override protected function changeHandler(event:Event):void {
			_view.changed = true;
			if(_view.applyChangesOnFocusOut) {
				updateValue();
			}
		}
		
		protected function activeRecordChangeHandler(event:Event):void {
			initInternalDataProvider();
			updateView();
		}
		
		protected function setDataProvider(value:IList):void {
			initInternalDataProvider();
			updateView();
		}
		
		protected function setDefaultValue(value:String):void {
			updateView();
		}
		
		protected function initInternalDataProvider():void {
			var temp:ArrayCollection = new ArrayCollection();
			if(_view.dataProvider != null) {
				temp.addAll(_view.dataProvider);
			}
			temp.addItem(BLANK_ON_FORM_ITEM);
			temp.addItem(DASH_ON_FORM_ITEM);
			temp.addItem(ILLEGIBLE_ITEM);
			_view.internalDataProvider = temp;
		}
		
		override protected function textToRequestValue():String {
			var result:String = null;
			var selectedItem:* = _view.dropDownList.selectedItem;
			if(selectedItem != null) {
				switch(selectedItem) {
					case BLANK_ON_FORM_ITEM:
					case DASH_ON_FORM_ITEM:
					case ILLEGIBLE_ITEM:
						result = selectedItem.shortCut;
						break;
					default:
						var value:* = ObjectUtil.getValue(selectedItem, _view.dataField);
						if(value != null) {
							result = String(value);
						}
				}
			}
			return result;
		}
		
		override protected function updateView():void {
			var item:Object = null;
			var hasRemarks:Boolean = false;
			var attribute:AttributeProxy = _view.attribute;
			if(attribute != null) {
				var field:FieldProxy = attribute.getField(_view.fieldIndex);
				hasRemarks = StringUtil.isNotBlank(getRemarks());
				var value:Object = field.value;
				if(field.symbol != null && isReasonBlankSymbol(field.symbol)) {
					switch(field.symbol) {
						case FieldSymbol.BLANK_ON_FORM:
							item = BLANK_ON_FORM_ITEM;
							break;
						case FieldSymbol.DASH_ON_FORM:
							item = DASH_ON_FORM_ITEM;
							break;
						case FieldSymbol.ILLEGIBLE:
							item = ILLEGIBLE_ITEM;
							break;
					}
				} else if(value != null) {
					item = getItem(value);
				}
			}
			if(item == null && _view.defaultValue != null) {
				item = getItem(_view.defaultValue);
			}
			contextMenu.updateItems();
			_view.editable = Application.activeRecord.step != CollectRecord$Step.ANALYSIS;
			_view.dropDownList.selectedItem = item;
			_view.hasRemarks = hasRemarks;
		}
		
		override protected function keyDownHandler(event:KeyboardEvent):void {
			var item:Object = null;
			var char:String = String.fromCharCode(event.charCode);
			switch(char) {
				case SHORTCUT_BLANK_ON_FORM:
					item = BLANK_ON_FORM_ITEM;
					break;
				case SHORTCUT_DASH_ON_FORM:
					item = DASH_ON_FORM_ITEM;
					break;
				case SHORTCUT_ILLEGIBLE:
					item = ILLEGIBLE_ITEM;
					break;
			}
			if(item != null) {
				_view.dropDownList.selectedItem = item;
				updateValue();
			}
		}

		protected function getItem(value:*):Object {
			var dataProvider:IList = _view.dropDownList.dataProvider;
			var dataField:String = _view.dataField;
			var item:Object = CollectionUtil.getItem(dataProvider, dataField, value);
			return item;
		}
	}
}
<commit_msg>OFC-528 User with role DATA_ENTRY can edit records in phase 2 and 3. He can even reject the record\!\!\!<commit_after>package org.openforis.collect.presenter {
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.binding.utils.ChangeWatcher;
	import mx.collections.ArrayCollection;
	import mx.collections.IList;
	import mx.events.PropertyChangeEvent;
	
	import org.openforis.collect.Application;
	import org.openforis.collect.event.UIEvent;
	import org.openforis.collect.i18n.Message;
	import org.openforis.collect.model.CollectRecord$Step;
	import org.openforis.collect.model.FieldSymbol;
	import org.openforis.collect.model.proxy.AttributeProxy;
	import org.openforis.collect.model.proxy.FieldProxy;
	import org.openforis.collect.ui.component.input.DropDownInputField;
	import org.openforis.collect.util.CollectionUtil;
	import org.openforis.collect.util.ObjectUtil;
	import org.openforis.collect.util.StringUtil;
	
	/**
	 * 
	 * @author <NAME>
	 * */
	public class DropDownInputFieldPresenter extends InputFieldPresenter {
		
		public static const EMPTY_ITEM:Object = {label: Message.get('global.dropDownEmpty'), separator: true};
		public static const BLANK_ON_FORM_ITEM:Object = {label: Message.get('edit.dropDownList.blankOnForm'), shortCut: "*", separator: true};
		public static const DASH_ON_FORM_ITEM:Object = {label: Message.get('edit.dropDownList.dashOnForm'), shortCut: "-"};
		public static const ILLEGIBLE_ITEM:Object = {label: Message.get('edit.dropDownList.illegible'), shortCut: "?"};

		private var _view:DropDownInputField;
		
		public function DropDownInputFieldPresenter(inputField:DropDownInputField) {
			_view = inputField;
			
			super(inputField);
			
			initInternalDataProvider();
		}
		
		override internal function initEventListeners():void {
			super.initEventListeners();
			
			_view.dropDownList.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
			_view.dropDownList.addEventListener(FocusEvent.FOCUS_OUT, focusOutHandler);
			_view.dropDownList.addEventListener(Event.CHANGE, changeHandler);
			_view.dropDownList.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			eventDispatcher.addEventListener(UIEvent.ACTIVE_RECORD_CHANGED, activeRecordChangeHandler);
			
			BindingUtils.bindSetter(setDataProvider, _view, "dataProvider");
			BindingUtils.bindSetter(setDefaultValue, _view, "defaultValue");
		}
		
		override protected function changeHandler(event:Event):void {
			_view.changed = true;
			if(_view.applyChangesOnFocusOut) {
				updateValue();
			}
		}
		
		protected function activeRecordChangeHandler(event:Event):void {
			initInternalDataProvider();
			updateView();
		}
		
		protected function setDataProvider(value:IList):void {
			initInternalDataProvider();
			updateView();
		}
		
		protected function setDefaultValue(value:String):void {
			updateView();
		}
		
		protected function initInternalDataProvider():void {
			var temp:ArrayCollection = new ArrayCollection();
			if(_view.dataProvider != null) {
				temp.addAll(_view.dataProvider);
			}
			temp.addItem(BLANK_ON_FORM_ITEM);
			temp.addItem(DASH_ON_FORM_ITEM);
			temp.addItem(ILLEGIBLE_ITEM);
			_view.internalDataProvider = temp;
		}
		
		override protected function textToRequestValue():String {
			var result:String = null;
			var selectedItem:* = _view.dropDownList.selectedItem;
			if(selectedItem != null) {
				switch(selectedItem) {
					case BLANK_ON_FORM_ITEM:
					case DASH_ON_FORM_ITEM:
					case ILLEGIBLE_ITEM:
						result = selectedItem.shortCut;
						break;
					default:
						var value:* = ObjectUtil.getValue(selectedItem, _view.dataField);
						if(value != null) {
							result = String(value);
						}
				}
			}
			return result;
		}
		
		override protected function updateView():void {
			var item:Object = null;
			var hasRemarks:Boolean = false;
			var attribute:AttributeProxy = _view.attribute;
			if(attribute != null) {
				var field:FieldProxy = attribute.getField(_view.fieldIndex);
				hasRemarks = StringUtil.isNotBlank(getRemarks());
				var value:Object = field.value;
				if(field.symbol != null && isReasonBlankSymbol(field.symbol)) {
					switch(field.symbol) {
						case FieldSymbol.BLANK_ON_FORM:
							item = BLANK_ON_FORM_ITEM;
							break;
						case FieldSymbol.DASH_ON_FORM:
							item = DASH_ON_FORM_ITEM;
							break;
						case FieldSymbol.ILLEGIBLE:
							item = ILLEGIBLE_ITEM;
							break;
					}
				} else if(value != null) {
					item = getItem(value);
				}
			}
			if(item == null && _view.defaultValue != null) {
				item = getItem(_view.defaultValue);
			}
			contextMenu.updateItems();
			_view.editable = Application.activeRecordEditable;
			_view.dropDownList.selectedItem = item;
			_view.hasRemarks = hasRemarks;
		}
		
		override protected function keyDownHandler(event:KeyboardEvent):void {
			var item:Object = null;
			var char:String = String.fromCharCode(event.charCode);
			switch(char) {
				case SHORTCUT_BLANK_ON_FORM:
					item = BLANK_ON_FORM_ITEM;
					break;
				case SHORTCUT_DASH_ON_FORM:
					item = DASH_ON_FORM_ITEM;
					break;
				case SHORTCUT_ILLEGIBLE:
					item = ILLEGIBLE_ITEM;
					break;
			}
			if(item != null) {
				_view.dropDownList.selectedItem = item;
				updateValue();
			}
		}

		protected function getItem(value:*):Object {
			var dataProvider:IList = _view.dropDownList.dataProvider;
			var dataField:String = _view.dataField;
			var item:Object = CollectionUtil.getItem(dataProvider, dataField, value);
			return item;
		}
	}
}

{'fraction_non_alphanumeric': 0.10116492949110975, 'fraction_numerical': 0.0007882981518787772, 'mean_word_length': 3.6151980598221503, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2899048', 'n_tokens_mistral': 4243, 'n_tokens_neox': 3492, 'n_words': 591}