[virtualcommons-svn] SF.net SVN: virtualcommons:[160] mentalmodels/trunk/flex/src
Status: Beta
Brought to you by:
alllee
From: <kj...@us...> - 2009-06-12 23:08:48
|
Revision: 160 http://virtualcommons.svn.sourceforge.net/virtualcommons/?rev=160&view=rev Author: kjonas Date: 2009-06-12 23:08:38 +0000 (Fri, 12 Jun 2009) Log Message: ----------- ForecastComponent.mxml now contains methods save() and load(). These methods are common to all four "styles". ForecastComponent.mxml also contains value-checking, which only occurs on styles 0 and 2, the two styles that allow data entry. Style 0 checks for number validity, range, and sum (in columns). Style 2 checks for number validity and range only. Error messages are generated and accessible through the public variable errorMessage, but they are not displayed. Modified Paths: -------------- mentalmodels/trunk/flex/src/TestForecast.mxml mentalmodels/trunk/flex/src/customComponents/ForecastComponent.mxml Modified: mentalmodels/trunk/flex/src/TestForecast.mxml =================================================================== --- mentalmodels/trunk/flex/src/TestForecast.mxml 2009-06-12 02:39:11 UTC (rev 159) +++ mentalmodels/trunk/flex/src/TestForecast.mxml 2009-06-12 23:08:38 UTC (rev 160) @@ -1,30 +1,65 @@ <?xml version="1.0" encoding="utf-8"?> -<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" xmlns:comp="customComponents.*"> +<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:comp="customComponents.*" initialize="init()"> <mx:VBox id="content" minWidth="160"> <comp:ForecastComponent id="forecastComponent0" numColumns="15" numBays="3" style="0" minimumWidth="{content.minWidth}"/> <comp:ForecastComponent id="forecastComponent1" numColumns="15" numBays="3" style="1" minimumWidth="{content.minWidth}"/> <comp:ForecastComponent id="forecastComponent2" numColumns="6" numBays="3" style="2" minimumWidth="{content.minWidth}"/> <comp:ForecastComponent id="forecastComponent3" numColumns="6" numBays="3" style="3" minimumWidth="{content.minWidth}"/> + <mx:HBox> + <mx:Button label="Save" click="save()"/> + <!--<mx:Button label="Load" click="load()"/>--> + </mx:HBox> </mx:VBox> <mx:Script> <![CDATA[ -// import mx.controls.Alert; -// -// public function init():void -// { + import mx.collections.ArrayCollection; + import mx.controls.Alert; + + public var saved:ArrayCollection = null; + // try // { -// forecastComponent.initialize(); -// forecastComponent.init(); +// // } // catch(error:Error) // { // Alert.show(error.message + "\n" + error.getStackTrace()); // } -// } + public function init():void + { + //forecastComponent0.changed(); + } + + public function save():void + { + try + { + saved = forecastComponent0.save(); + forecastComponent1.load(saved); + forecastComponent2.load(saved); + forecastComponent3.load(saved); + } + catch(error:Error) + { + Alert.show(error.message + "\n" + error.getStackTrace()); + } + } + + public function load():void + { + try + { + forecastComponent1.load(saved); + } + catch(error:Error) + { + Alert.show(error.message + "\n" + error.getStackTrace()); + } + } + ]]> </mx:Script> Modified: mentalmodels/trunk/flex/src/customComponents/ForecastComponent.mxml =================================================================== --- mentalmodels/trunk/flex/src/customComponents/ForecastComponent.mxml 2009-06-12 02:39:11 UTC (rev 159) +++ mentalmodels/trunk/flex/src/customComponents/ForecastComponent.mxml 2009-06-12 23:08:38 UTC (rev 160) @@ -5,18 +5,36 @@ <mx:Script> <![CDATA[ + import mx.controls.Alert; + import mx.events.FlexEvent; import mx.controls.DataGrid; import mx.collections.ArrayCollection; import mx.controls.Label; import mx.controls.dataGridClasses.DataGridColumn; + import mx.utils.StringUtil; - public var dataGrid:DataGrid; - [Bindable] public var dataProvider:ArrayCollection = new ArrayCollection(); + // vital components + [Bindable] public var dataGrid:DataGrid; + [Bindable] public var dataProvider:ArrayCollection; + [Bindable] public var errorMessage:String = ""; + + // Game Data + [Bindable] public var style:int = 0; // 0:PeopleEntry, 1:PeopleViewing, 2:FishEntry, 3:CalculatedViewing [Bindable] public var numBays:int = 3; - [Bindable] public var numColumns:int = 15; + [Bindable] public var numColumns:int = -1; + [Bindable] public var numFields:int = 15; + [Bindable] public var minValue:int = 0; + [Bindable] public var maxValue:int = 5; + [Bindable] public var groupSize:int = 4; + [Bindable] public var finished:Boolean = false; + + // for lining up grids [Bindable] public var minimumWidth:int = 30; - [Bindable] public var style:int = 0; // 0:PeopleEntry, 1:PeopleViewing, 2:FishEntry, 3:Calculated + // + // public accessible functions + // + public function init():void { createGrid(); @@ -26,6 +44,60 @@ addChild(dataGrid); } + public function save():ArrayCollection + { + var saveArray:ArrayCollection = new ArrayCollection(); + for(var field:int = 0; field < dataProvider.length; field++) + { + saveArray.addItem(new ArrayCollection()); + for(var column:int = 0; column < dataGrid.columnCount; column++) + { + (saveArray.getItemAt(field) as ArrayCollection).addItem(getItem(field, column)); + } + } + return saveArray; + } + + public function load(loadArray:ArrayCollection):void + { + for(var field:int = 0; field < loadArray.length && field < dataProvider.length; field++) + { + var loadSubArray:ArrayCollection = loadArray.getItemAt(field) as ArrayCollection; + for(var column:int = 0; column < loadSubArray.length && column < dataGrid.columnCount; column++) + { + setItem(field, column, loadSubArray.getItemAt(column)); + } + } + redraw(); + } + + public function changed(event:Object=null):void + { + if((style == 0) || (style == 2)) // 0:PeopleEntry, 2:FishEntry + { + errorCheck(); + } + redraw(); + } + + public function getItem(field:Number, col:Number):Object + { + return dataProvider.getItemAt(field)["day"+(col+1)]; + } + public function setItem(field:Number, col:Number, value:Object):void + { + dataProvider.getItemAt(field)["day"+(col+1)] = value; + } + + public function redraw():void + { + dataGrid.invalidateList(); + } + + // + // private Utility functions + // + private function addLabel(text:String="", html:Boolean=false):void { var newLabel:Label = new Label(); @@ -47,37 +119,39 @@ private function createGrid():void { - dataGrid = new DataGrid; + dataGrid = new DataGrid(); dataGrid.id = "dataGrid"; dataGrid.editable = true; dataGrid.enabled = true; + dataGrid.addEventListener("change", changed); } private function fillGridColumns():void { - var newColumnArray:Array = new Array(numColumns); + var columnArray:Array = dataGrid.columns; for(var columnNumber:int = 0; columnNumber < numColumns; columnNumber++) { - var newDataGridColumn:DataGridColumn = new DataGridColumn(); + var newDataGridColumn:DataGridColumn = new DataGridColumn("day"+(columnNumber+1)); + newDataGridColumn.headerText = ""+(columnNumber+1); // 1, 2, 3, ... + newDataGridColumn.dataField = "day"+(columnNumber+1); // day1, day2, day3, ... + + newDataGridColumn.editable = ((style == 0) || (style == 2)); // 0:PeopleEntry, 2:FishEntry newDataGridColumn.draggable = false; newDataGridColumn.sortable = false; newDataGridColumn.resizable = false; newDataGridColumn.width = 30; - newDataGridColumn.headerText = ""+(columnNumber+1); // 1, 2, 3, ... - newDataGridColumn.dataField = "day"+(columnNumber+1); // day1, day2, day3, ... - - newDataGridColumn.editable = ((style == 0) || (style == 2)); // 0:PeopleEntry, 2:FishEntry - - newColumnArray[columnNumber] = newDataGridColumn; + columnArray[columnNumber] = newDataGridColumn; } - dataGrid.columns = newColumnArray; + dataGrid.columns = columnArray; dataGrid.editable = ((style == 0) || (style == 2)); // 0:PeopleEntry, 2:FishEntry } private function fillGridFields():void { + dataProvider = new ArrayCollection(); + var bayNumber:int = 0; if(style==0 || style==1) // 0:PeopleEntry, 1:PeopleViewing { @@ -126,6 +200,7 @@ } } dataGrid.dataProvider = dataProvider; + numFields = dataProvider.length; } private function setGridHeight():void @@ -139,12 +214,78 @@ case(2): // 2:FishEntry dataGrid.height = (23)*(numBays+1); break; - case(3): // 3:Calculated + case(3): // 3:CalculatedViewing dataGrid.height = (23)*(numBays+4)-3; break; } } + private function errorCheck():void + { + markNoError(); + var error:Boolean = false; + + for(var column:Number=0; column < numColumns && !error; column++) + { + var colSum:Number = 0; + for(var field:Number=0; field < numFields && !error; field++) + { + var value:Object = getItem(field, column); + if(!validNum(value)) + { + errorMessage = "Enter a value between "+minValue+" and "+maxValue+"."; + error = true; + markError(field,column); + } + else if(!inBounds(value)) + { + errorMessage = "Value must be between "+minValue+" and "+maxValue+"."; + error = true; + markError(field,column); + } + else if(style == 0) // 0:PeopleEntry + { + colSum += Number(value); + } + } + + if(!error && style == 0 && colSum != groupSize-1) // 0:PeopleEntry + { + errorMessage = "Sum of all columns must be exactly "+(groupSize-1)+"."; + error = true; + markError(-1,column); + } + } + + finished = !error; + } + + private function validNum(n:Object):Boolean + { + if(n == null) return false; + var pattern:RegExp = /^\d+$/; //the entire string must be consecutive digits + var s:String = StringUtil.trim(String(n)); + return (n is Number) || pattern.test(s); + } + private function inBounds(n:Object):Boolean + { + return Number(n) >= minValue && Number(n) <= maxValue; + } + public function markError(field:Number, column:Number):void + { + if(style != 0 && style != 2) return; // 0:PeopleEntry, 2:FishEntry + DataGridColumn(dataGrid.columns[column]).setStyle("backgroundColor", "#ee82ee"); + dataGrid.selectedIndex = field; + } + private function markNoError():void + { + for(var column:Number=0; column < numColumns; column++) + { + DataGridColumn(dataGrid.columns[column]).setStyle("backgroundColor", "#FFFFFF"); + } + dataGrid.selectedIndex = -1; + } + ]]> </mx:Script> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |