You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flex.apache.org by pe...@apache.org on 2014/03/06 16:13:49 UTC

git commit: [flex-asjs] [refs/heads/develop] - Adding example of using format beads.

Repository: flex-asjs
Updated Branches:
  refs/heads/develop 455273f4c -> 47a43defa


Adding example of using format beads.


Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/47a43def
Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/47a43def
Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/47a43def

Branch: refs/heads/develop
Commit: 47a43defa31922e1a33fd451ed3232afe955fd1f
Parents: 455273f
Author: Peter Ent <pe...@apache.org>
Authored: Thu Mar 6 10:13:36 2014 -0500
Committer: Peter Ent <pe...@apache.org>
Committed: Thu Mar 6 10:13:36 2014 -0500

----------------------------------------------------------------------
 examples/FormatExample/build.xml                |  51 +++++
 examples/FormatExample/src/FormatExample.mxml   |  33 +++
 examples/FormatExample/src/MyFormView.mxml      |  99 +++++++++
 .../accessories/NumberFormatter.as              | 210 +++++++++++++++++++
 .../accessories/PhoneNumberFormatter.as         | 195 +++++++++++++++++
 examples/build.xml                              |   2 +
 6 files changed, 590 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/47a43def/examples/FormatExample/build.xml
----------------------------------------------------------------------
diff --git a/examples/FormatExample/build.xml b/examples/FormatExample/build.xml
new file mode 100644
index 0000000..40ceadc
--- /dev/null
+++ b/examples/FormatExample/build.xml
@@ -0,0 +1,51 @@
+<?xml version="1.0"?>
+<!--
+
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+-->
+
+
+<project name="formatexample" default="main" basedir=".">
+    <property name="FLEXJS_HOME" location="../.."/>
+    <property name="example" value="FormatExample" />
+    
+    <property file="${FLEXJS_HOME}/env.properties"/>
+    <property environment="env"/>
+    <property file="${FLEXJS_HOME}/build.properties"/>
+    <property name="FLEX_HOME" value="${FLEXJS_HOME}"/>
+    <property name="FALCON_HOME" value="${env.FALCON_HOME}"/>
+    <property name="FALCONJX_HOME" value="${env.FALCONJX_HOME}"/>
+    <property name="GOOG_HOME" value="${env.GOOG_HOME}"/>
+
+    <include file="${basedir}/../build_example.xml" />
+    
+    <target name="main" depends="clean,build_example.compile" description="Clean build of ${example}">
+    </target>
+    <!-- Uncomment to reproduce cross-compilation error in compilejs step
+    <target name="main" depends="clean,build_example.compile,build_example.compilejs" description="Clean build of ${example}">
+    </target>
+    -->
+    
+    <target name="clean">
+        <delete dir="${basedir}/bin" failonerror="false" />
+        <delete dir="${basedir}/bin-debug" failonerror="false" />
+        <delete dir="${basedir}/bin-release" failonerror="false" />
+    </target>
+
+    
+    
+</project>

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/47a43def/examples/FormatExample/src/FormatExample.mxml
----------------------------------------------------------------------
diff --git a/examples/FormatExample/src/FormatExample.mxml b/examples/FormatExample/src/FormatExample.mxml
new file mode 100644
index 0000000..0664147
--- /dev/null
+++ b/examples/FormatExample/src/FormatExample.mxml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!---
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+-->
+<basic:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
+				   xmlns:local="*"
+				   xmlns:models="models.*"
+				   xmlns:basic="library://ns.apache.org/flexjs/basic" 
+				   >
+	
+	<basic:valuesImpl>
+		<basic:SimpleCSSValuesImpl />
+	</basic:valuesImpl>
+	<basic:initialView>
+		<local:MyFormView />
+	</basic:initialView>
+</basic:Application>

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/47a43def/examples/FormatExample/src/MyFormView.mxml
----------------------------------------------------------------------
diff --git a/examples/FormatExample/src/MyFormView.mxml b/examples/FormatExample/src/MyFormView.mxml
new file mode 100644
index 0000000..2231da3
--- /dev/null
+++ b/examples/FormatExample/src/MyFormView.mxml
@@ -0,0 +1,99 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+-->
+<basic:ViewBase xmlns:fx="http://ns.adobe.com/mxml/2009"
+				xmlns:basic="library://ns.apache.org/flexjs/basic"
+				xmlns:local="*" 
+				xmlns:models="models.*" 
+				xmlns:acc="org.apache.flex.html.staticControls.accessories.*">
+	
+	<fx:Style>
+		.title {
+			font-size: 14pt;
+			font-weight: bold;
+		}
+
+	</fx:Style>
+	
+	<basic:Container width="600" height="700" x="50" y="50">
+		<basic:beads>
+			<basic:NonVirtualVerticalLayout />
+		</basic:beads>
+		
+		<basic:Label text="Loan Form" className="title" />
+		
+		<basic:Container width="500">
+			<basic:beads>
+				<basic:NonVirtualHorizontalLayout />
+			</basic:beads>
+			<basic:Label text="Your Name:" />
+			<basic:TextInput id="nameField" />
+		</basic:Container>
+		
+		<basic:Container width="500">
+			<basic:beads>
+				<basic:NonVirtualHorizontalLayout />
+			</basic:beads>
+			<basic:Label text="Your Age:" />
+			<basic:TextInput id="ageField">
+				<basic:beads>
+					<basic:NumericOnlyTextInputBead />
+				</basic:beads>
+			</basic:TextInput>
+		</basic:Container>
+		
+		<basic:Container width="500">
+			<basic:beads>
+				<basic:NonVirtualHorizontalLayout />
+			</basic:beads>
+			<basic:Label text="Phone Number:" />
+			<basic:TextInput id="phoneField" >
+				<basic:beads>
+					<basic:TextPromptBead prompt="9995551235" />
+					<basic:FormatableTextInputView />
+					<acc:PhoneNumberFormatter propertyName="text" />
+				</basic:beads>
+			</basic:TextInput>
+		</basic:Container>
+		
+		<basic:Container width="500">
+			<basic:beads>
+				<basic:NonVirtualHorizontalLayout />
+			</basic:beads>
+			<basic:Label text="Date of Loan:" />
+			<basic:DateField id="dateOfLoan"/>
+		</basic:Container>
+		
+		<basic:Container width="500">
+			<basic:beads>
+				<basic:NonVirtualHorizontalLayout />
+			</basic:beads>
+			<basic:Label text="Loan Amount:" />
+			<basic:TextInput id="loanAmount">
+				<basic:beads>
+					<basic:TextPromptBead prompt="US dollars" />
+					<basic:FormatableTextInputView />
+					<acc:NumberFormatter />
+				</basic:beads>
+			</basic:TextInput>
+		</basic:Container>
+		
+	</basic:Container>
+	
+</basic:ViewBase>

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/47a43def/examples/FormatExample/src/org/apache/flex/html/staticControls/accessories/NumberFormatter.as
----------------------------------------------------------------------
diff --git a/examples/FormatExample/src/org/apache/flex/html/staticControls/accessories/NumberFormatter.as b/examples/FormatExample/src/org/apache/flex/html/staticControls/accessories/NumberFormatter.as
new file mode 100644
index 0000000..ab9e6b7
--- /dev/null
+++ b/examples/FormatExample/src/org/apache/flex/html/staticControls/accessories/NumberFormatter.as
@@ -0,0 +1,210 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.flex.html.staticControls.accessories
+{
+	import org.apache.flex.core.IBeadModel;
+	import org.apache.flex.core.IFormatBead;
+	import org.apache.flex.core.IStrand;
+	import org.apache.flex.events.Event;
+	import org.apache.flex.events.EventDispatcher;
+	import org.apache.flex.events.IEventDispatcher;
+	
+	/**
+	 * The NumberFormatter class formats a value in separated groups. The formatter listens
+	 * to a property on a model and when the property changes, formats it and dispatches a
+	 * formatChanged event.
+	 *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @playerversion AIR 2.6
+	 *  @productversion FlexJS 0.0
+	 */
+	public class NumberFormatter extends EventDispatcher implements IFormatBead
+	{
+		/**
+		 *  constructor
+		 *  
+		 *  @langversion 3.0
+		 *  @playerversion Flash 10.2
+		 *  @playerversion AIR 2.6
+		 *  @productversion FlexJS 0.0
+		 */
+		public function NumberFormatter()
+		{
+			super();
+		}
+		
+		private var _strand:IStrand;
+		
+		/**
+		 *  @copy org.apache.flex.core.IBead#strand
+		 *  
+		 *  @langversion 3.0
+		 *  @playerversion Flash 10.2
+		 *  @playerversion AIR 2.6
+		 *  @productversion FlexJS 0.0
+		 */
+		public function set strand(value:IStrand):void
+		{
+			_strand = value;
+			
+			// Listen for the beadsAdded event which signals when all of a strand's
+			// beads have been added.
+			IEventDispatcher(value).addEventListener("beadsAdded",handleBeadsAdded);
+		}
+		
+		/**
+		 * @private
+		 */
+		private function handleBeadsAdded(event:Event):void
+		{
+			// Listen for the change in the model
+			var model:IBeadModel = _strand.getBeadByType(IBeadModel) as IBeadModel;
+			model.addEventListener(eventName,propertyChangeHandler);
+			model.addEventListener(propertyName+"Change",propertyChangeHandler);
+			
+			// format the current value of that property
+			propertyChangeHandler(null);
+		}
+		
+		private var _propertyName:String;
+		private var _eventName:String;
+		private var _formattedResult:String;
+		private var _groupSize:Number = 3;
+		private var _thousandsSeparator:String = ",";
+		
+		/**
+		 *  The name of the property on the model to format.
+		 *  
+		 *  @langversion 3.0
+		 *  @playerversion Flash 10.2
+		 *  @playerversion AIR 2.6
+		 *  @productversion FlexJS 0.0
+		 */
+		public function get propertyName():String
+		{
+			if (_propertyName == null) {
+				return "text";
+			}
+			return _propertyName;
+		}
+		public function set propertyName(value:String):void
+		{
+			_propertyName = value;
+		}
+		
+		/**
+		 *  The event dispatched by the model when the property changes. The
+		 *  default is propertyName+"Changed".
+		 *  
+		 *  @langversion 3.0
+		 *  @playerversion Flash 10.2
+		 *  @playerversion AIR 2.6
+		 *  @productversion FlexJS 0.0
+		 */
+		public function get eventName():String
+		{
+			if (_eventName == null) {
+				return _propertyName+"Changed";
+			}
+			return _eventName;
+		}
+		public function set eventName(value:String):void
+		{
+			_eventName = value;
+		}
+		
+		/**
+		 *  Character to use to separate thousands groups. The default is
+		 *  the comma (,).
+		 *  
+		 *  @langversion 3.0
+		 *  @playerversion Flash 10.2
+		 *  @playerversion AIR 2.6
+		 *  @productversion FlexJS 0.0
+		 */
+		public function get thousandsSeparator():String
+		{
+			return _thousandsSeparator;
+		}
+		public function set thousandsSeparator(value:String):void
+		{
+			_thousandsSeparator = value;
+		}
+		
+		/**
+		 *  The formatted string.
+		 *  
+		 *  @langversion 3.0
+		 *  @playerversion Flash 10.2
+		 *  @playerversion AIR 2.6
+		 *  @productversion FlexJS 0.0
+		 */
+		public function get formattedString():String
+		{
+			return _formattedResult;
+		}
+		
+		/**
+		 * @private
+		 */
+		private function propertyChangeHandler(event:Event):void
+		{
+			// When the property changes, fetch it from the model and
+			// format it, storing the result in _formattedResult.
+			var model:IBeadModel = _strand.getBeadByType(IBeadModel) as IBeadModel;
+			var value:Object = model[propertyName];
+			_formattedResult = format(value);
+			
+			// Dispatch the formatChanged event so any bead that's interested in
+			// the formatted string knows to use it.
+			var newEvent:Event = new Event("formatChanged");
+			this.dispatchEvent(newEvent);
+		}
+		
+		/**
+		 * @private
+		 */
+		private function format(value:Object):String
+		{
+			if (value == null) return "";
+			
+			var num:Number = Number(value);
+			var source:String = String(value);
+			var parts:Array = source.split(thousandsSeparator);
+			source = parts.join("");
+			
+			var l:int = source.length;
+			var result:String = "";
+			var group:int = 0;
+			
+			for(var i:int=l-1; i >= 0; i--)
+			{
+				if (group == _groupSize && result.length > 0) {
+					result = thousandsSeparator + result;
+					group = 0;
+				}
+				result = source.charAt(i) + result;
+				group++;
+			}
+			
+			return result;
+		}
+	}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/47a43def/examples/FormatExample/src/org/apache/flex/html/staticControls/accessories/PhoneNumberFormatter.as
----------------------------------------------------------------------
diff --git a/examples/FormatExample/src/org/apache/flex/html/staticControls/accessories/PhoneNumberFormatter.as b/examples/FormatExample/src/org/apache/flex/html/staticControls/accessories/PhoneNumberFormatter.as
new file mode 100644
index 0000000..5655732
--- /dev/null
+++ b/examples/FormatExample/src/org/apache/flex/html/staticControls/accessories/PhoneNumberFormatter.as
@@ -0,0 +1,195 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.flex.html.staticControls.accessories
+{
+	import org.apache.flex.core.IBead;
+	import org.apache.flex.core.IBeadModel;
+	import org.apache.flex.core.IFormatBead;
+	import org.apache.flex.core.IStrand;
+	import org.apache.flex.events.Event;
+	import org.apache.flex.events.EventDispatcher;
+	import org.apache.flex.events.IEventDispatcher;
+
+	/**
+	 *  The PhoneNumberFormatter class bead formats a numeric string into a 
+	 *  US style phone number. The format bead listens for changes to a property
+	 *  in a model, formats the value, and dispatches a formatChanged event.
+	 *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @playerversion AIR 2.6
+	 *  @productversion FlexJS 0.0
+	 */
+	public class PhoneNumberFormatter extends EventDispatcher implements IBead, IFormatBead
+	{
+		/**
+		 *  constructor
+		 *  
+		 *  @langversion 3.0
+		 *  @playerversion Flash 10.2
+		 *  @playerversion AIR 2.6
+		 *  @productversion FlexJS 0.0
+		 */
+		public function PhoneNumberFormatter()
+		{
+		}
+		
+		private var _strand:IStrand;
+		
+		/**
+		 *  @copy org.apache.flex.core.IBead#strand
+		 *  
+		 *  @langversion 3.0
+		 *  @playerversion Flash 10.2
+		 *  @playerversion AIR 2.6
+		 *  @productversion FlexJS 0.0
+		 */
+		public function set strand(value:IStrand):void
+		{
+			_strand = value;
+			
+			// Listen for the beadsAdded event which signals when all of a strand's
+			// beads have been added.
+			IEventDispatcher(value).addEventListener("beadsAdded",handleBeadsAdded);
+		}
+		
+		/**
+		 * @private
+		 */
+		private function handleBeadsAdded(event:Event):void
+		{
+			// Listen for the change in the model.
+			var model:IBeadModel = _strand.getBeadByType(IBeadModel) as IBeadModel;
+			model.addEventListener(eventName,propertyChangeHandler);
+			model.addEventListener(propertyName+"Change",propertyChangeHandler);
+			
+			// format the current value of that property
+			propertyChangeHandler(null);
+		}
+		
+		private var _propertyName:String;
+		private var _eventName:String;
+		private var _formattedResult:String;
+		
+		/**
+		 *  The name of the property in the model holding the value to be
+		 *  formatted. The default is text.
+		 *  
+		 *  @langversion 3.0
+		 *  @playerversion Flash 10.2
+		 *  @playerversion AIR 2.6
+		 *  @productversion FlexJS 0.0
+		 */
+		public function get propertyName():String
+		{
+			if (_propertyName == null) {
+				return "text";
+			}
+			return _propertyName;
+		}
+		public function set propertyName(value:String):void
+		{
+			_propertyName = value;
+		}
+		
+		/**
+		 *  The name of the event dispatched when the property changes. The
+		 *  default is propertyName + "Changed".
+		 *  
+		 *  @langversion 3.0
+		 *  @playerversion Flash 10.2
+		 *  @playerversion AIR 2.6
+		 *  @productversion FlexJS 0.0
+		 */
+		public function get eventName():String
+		{
+			if (_eventName == null) {
+				return _propertyName+"Changed";
+			}
+			return _eventName;
+		}
+		public function set eventName(value:String):void
+		{
+			_eventName = value;
+		}
+		
+		/**
+		 *  The formatted string.
+		 *  
+		 *  @langversion 3.0
+		 *  @playerversion Flash 10.2
+		 *  @playerversion AIR 2.6
+		 *  @productversion FlexJS 0.0
+		 */
+		public function get formattedString():String
+		{
+			return _formattedResult;
+		}
+		
+		/**
+		 * @private
+		 */
+		private function propertyChangeHandler(event:Event):void
+		{
+			// When the model's value changes, format it and store it in
+			// _formattedResult.
+			var model:IBeadModel = _strand.getBeadByType(IBeadModel) as IBeadModel;
+			var value:Object = model[propertyName];
+			_formattedResult = format(value);
+			
+			// Dispatch the formatChanged event so other beads can use the newly
+			// formatted value.
+			var newEvent:Event = new Event("formatChanged");
+			this.dispatchEvent(newEvent);
+		}
+		
+		/**
+		 * @private
+		 */
+		private function format(value:Object):String
+		{
+			if (value == null) return "";
+			
+			var source:String = String(value);
+			if (source.length != 10) return source;
+			
+			var areaCode:String = "";
+			var exchange:String;
+			var num:String;
+			
+			var pos:int = 0;
+			if (source.length == 10) {
+				areaCode = source.substr(pos,3);
+				pos += 3;
+			}
+			
+			exchange = source.substr(pos,3);
+			pos += 3;
+			
+			num = source.substr(pos,4);
+			
+			var result:String = "";
+			if (source.length == 10) {
+				result = result + "("+areaCode+") ";
+			}
+			result = result + exchange + "-" + num;
+			return result;
+		}
+	}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/47a43def/examples/build.xml
----------------------------------------------------------------------
diff --git a/examples/build.xml b/examples/build.xml
index 3a67004..081ee30 100644
--- a/examples/build.xml
+++ b/examples/build.xml
@@ -82,6 +82,7 @@
         <ant dir="${basedir}/FlexJSTest_jquery"/>
         <ant dir="${basedir}/FlexJSTest_Panel"/>
         <ant dir="${basedir}/FlexJSTest_SVG"/>
+        <ant dir="${basedir}/FormatExample"/>
         <ant dir="${basedir}/LanguageTests"/>
         <ant dir="${basedir}/ListsTest"/>
         <ant dir="${basedir}/StatesTest"/>
@@ -108,6 +109,7 @@
         <ant dir="${basedir}/FlexJSTest_jquery" target="clean"/>
         <ant dir="${basedir}/FlexJSTest_Panel" target="clean"/>
         <ant dir="${basedir}/FlexJSTest_SVG" target="clean"/>
+        <ant dir="${basedir}/FormatExample" target="clean"/>
         <ant dir="${basedir}/LanguageTests" target="clean"/>
         <ant dir="${basedir}/ListsTest" target="clean"/>
         <ant dir="${basedir}/StatesTest" target="clean"/>