The following example shows how you can detect when a property’s value changes on a Flex Gumbo LinearGradient object by listening for the propertyChange event (using PropertyChangeEvent.PROPERTY_CHANGE constant).
Full code after the jump.
To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see “Using the beta Gumbo SDK in Flex Builder 3″.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/12/03/detecting-when-properties-change-on-a-lineargradient-object-in-flex-gumbo/ -->
<Application name="LinearGradient_propertyChange_test"
xmlns="http://ns.adobe.com/mxml/2009"
layout="vertical"
verticalAlign="middle"
backgroundColor="white"
initialize="init();">
<Script>
<![CDATA[
import mx.events.ListEvent;
import mx.events.PropertyChangeEvent;
private function init():void {
linearGrad.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, linearGrad_propertyChange);
}
private function linearGrad_propertyChange(evt:PropertyChangeEvent):void {
arrColl.addItem(evt);
}
]]>
</Script>
<Declarations>
<LinearGradient id="linearGrad"
spreadMethod="{spreadMethodComboBox.selectedItem}"
interpolationMethod="{interpolationMethodComboBox.selectedItem}"
rotation="{rotationSlider.value}"
scaleX="{scaleXSlider.value}" />
<ArrayCollection id="arrColl" />
</Declarations>
<ApplicationControlBar dock="true">
<Form styleName="plain">
<FormItem label="spreadMethod:">
<ComboBox id="spreadMethodComboBox"
dataProvider="[pad,reflect,repeat]" />
</FormItem>
<FormItem label="interpolationMethod:">
<ComboBox id="interpolationMethodComboBox"
dataProvider="[linearRGB,rgb]"
selectedIndex="1" />
</FormItem>
<FormItem label="rotation:">
<HSlider id="rotationSlider"
minimum="0"
maximum="360"
value="0"
snapInterval="1"
tickInterval="20"
liveDragging="true" />
</FormItem>
<FormItem label="scaleX:">
<HSlider id="scaleXSlider"
minimum="0"
maximum="100"
value="0"
snapInterval="1"
tickInterval="20"
liveDragging="true" />
</FormItem>
</Form>
</ApplicationControlBar>
<DataGrid id="dataGrid"
dataProvider="{arrColl}"
verticalScrollPolicy="on"
width="100%"
height="100%">
<columns>
<DataGridColumn dataField="type" />
<DataGridColumn dataField="kind" />
<DataGridColumn dataField="property"
itemRenderer="mx.controls.Label" />
<DataGridColumn dataField="oldValue" />
<DataGridColumn dataField="newValue" />
<DataGridColumn dataField="source"
itemRenderer="mx.controls.Label" />
</columns>
</DataGrid>
</Application>
View source is enabled in the following example.
Or, you could use the BindingUtils class to create databindings, as seen in the following example:
<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<Application name="LinearGradient_propertyChange_test"
xmlns="http://ns.adobe.com/mxml/2009"
layout="vertical"
verticalAlign="middle"
backgroundColor="white"
creationComplete="init();">
<Script>
<![CDATA[
import mx.binding.utils.BindingUtils;
import mx.events.ListEvent;
import mx.events.PropertyChangeEvent;
private function init():void {
linearGrad.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, linearGrad_propertyChange);
BindingUtils.bindProperty(linearGrad, "spreadMethod", spreadMethodComboBox, "selectedItem");
BindingUtils.bindProperty(linearGrad, "interpolationMethod", interpolationMethodComboBox, "selectedItem");
BindingUtils.bindProperty(linearGrad, "rotation", rotationSlider, "value");
BindingUtils.bindProperty(linearGrad, "scaleX", scaleXSlider, "value");
}
private function linearGrad_propertyChange(evt:PropertyChangeEvent):void {
arrColl.addItem(evt);
}
]]>
</Script>
<Declarations>
<LinearGradient id="linearGrad" />
<ArrayCollection id="arrColl" />
</Declarations>
<ApplicationControlBar dock="true">
<Form styleName="plain">
<FormItem label="spreadMethod:">
<ComboBox id="spreadMethodComboBox"
dataProvider="[pad,reflect,repeat]" />
</FormItem>
<FormItem label="interpolationMethod:">
<ComboBox id="interpolationMethodComboBox"
dataProvider="[linearRGB,rgb]"
selectedIndex="1" />
</FormItem>
<FormItem label="rotation:">
<HSlider id="rotationSlider"
minimum="0"
maximum="360"
value="0"
snapInterval="1"
tickInterval="20"
liveDragging="true" />
</FormItem>
<FormItem label="scaleX:">
<HSlider id="scaleXSlider"
minimum="0"
maximum="100"
value="0"
snapInterval="1"
tickInterval="20"
liveDragging="true" />
</FormItem>
</Form>
</ApplicationControlBar>
<DataGrid id="dataGrid"
dataProvider="{arrColl}"
verticalScrollPolicy="on"
width="100%"
height="100%">
<columns>
<DataGridColumn dataField="type" />
<DataGridColumn dataField="kind" />
<DataGridColumn dataField="property"
itemRenderer="mx.controls.Label" />
<DataGridColumn dataField="oldValue" />
<DataGridColumn dataField="newValue" />
<DataGridColumn dataField="source"
itemRenderer="mx.controls.Label" />
</columns>
</DataGrid>
</Application>
This entry is based on a beta version of the Flex Gumbo SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex Gumbo SDK.
