Flex component binding click handlers in application - actionscript-3

I have created a component with 2 buttons. Each button's click function is bound to a variable. I need to swap out the default button handlers that are defined in the component with the handlers that are defined in the application.
ZoomButtons.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml"
preinitialize="onPreinitialize(event)">
<mx:script>
import mx.events.FlexEvent;
[Bindable] public var zoomOutHandler:Function;
[Bindable] public var zoomInHandler:Function;
protected function onPreinitialize(event:FlexEvent):void {
zoomOutHandler = localZoomOutHandler;
zoomInHandler = localZoomInHandler;
}
private function localZoomOutHandler(event:MouseEvent):void {
// Do nothing.
}
private function localZoomInHandler(event:MouseEvent):void {
// Do nothing.
}
</mx:script>
<mx:Button id="zoomOut" label="-" width="20"
toolTip="Zoom Out" click="{zoomOutHandler}" />
<mx:Button id="zoomIn" label="+" width="20"
toolTip="Zoom In" click="{zoomInHandler}" />
</mx:HBox>
App.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
mx:local="*"
creationComplete="onCompelete(event)">
<mx:script>
[Binable] public var scale:Number = 1.0;
protected function onComplete(event:FlexEvent):void {
zoomButtons.zoomOutHandler = handleChangeZoomOut;
zoomButtons.zoomInHandler = handleChangeZoomIn;
}
private function handleChangeZoomOut(event:MouseEvent):void {
scale /= 2;
}
private function handleChangeZoomIn(event:MouseEvent):void {
scale *= 2;
}
</mx:script>
<local:ZoomButtons id="zoomButtons" />
</mx:Application>

I believe I have figured it out. I removed the binding from the event handlers and assigned/unassigned them through a function call. This seems to work.
ZoomButtons.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:script>
import mx.events.FlexEvent;
private var zoomOutHandler:Function = localZoomOutHandler;
private var zoomInHandler:Function = localZoomInHandler;
public function addHandlers(zoomOutHandler:Function, zoomInHandler:Function):void {
removeHandlers();
this.zoomOutHandler = zoomOutHandler;
this.zoomInHandler = zoomInHandler;
zoomOut.addEventListener(MouseEvent.CLICK, zoomOutHandler);
zoomIn.addEventListener(MouseEvent.CLICK, zoomInHandler);
}
pubblic function removeHandlers():void {
zoomOut.removeEventListener(MouseEvent.CLICK, zoomOutHandler);
zoomIn.removeEventListener(MouseEvent.CLICK, zoomInHandler);
}
private function localZoomOutHandler(event:MouseEvent):void {
// Do nothing.
}
private function localZoomInHandler(event:MouseEvent):void {
// Do nothing.
}
</mx:script>
<mx:Button id="zoomOut" label="-" width="20" toolTip="Zoom Out" />
<mx:Button id="zoomIn" label="+" width="20" toolTip="Zoom In" />
</mx:HBox>
App.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
mx:local="*"
creationComplete="onCompelete(event)">
<mx:script>
[Binable] public var scale:Number = 1.0;
protected function onComplete(event:FlexEvent):void {
zoomButtons.addHandlers(handleChangeZoomOut, handleChangeZoomIn);
}
private function handleChangeZoomOut(event:MouseEvent):void {
scale /= 2;
}
private function handleChangeZoomIn(event:MouseEvent):void {
scale *= 2;
}
</mx:script>
<local:ZoomButtons id="zoomButtons" />
</mx:Application>

Good thing you found a solution. However IMO the cleanest approach to do communication from a component to its container is to use events. For example dispatch some custom zoom events from ZoomButtons:
<fx:MetaData>
[Event(name="zoomIn", type="flash.events.Event")]
[Event(name="zoomOut", type="flash.events.Event")]
</fx:MetaData>
<mx:Button id="zoomOut" label="-" width="20"
toolTip="Zoom Out" click="dispatchEvent(new Event('zoomOut'))" />
<mx:Button id="zoomIn" label="+" width="20"
toolTip="Zoom In" click="dispatchEvent(new Event('zoomIn'))" />
And then listen for those events:
<local:ZoomButtons id="zoomButtons"
zoomIn="handleZoomIn()"
zoomOut="handleZoomOut()" />
The metadata in the custom component makes sure that the compiler understands the 'zoomIn' and 'zoomOut' event handlers written in MXML.

Related

Simple flex popup with dispatch event in child and listener in parent not working

Parent.mxml
This file is calling the popup class below when the button is clicked.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.PopUpManager;
var popup:pop = null ;
protected function clickHandler(event:MouseEvent):void
{
popup = PopUpManager.createPopUp(this,pop,true) as pop;
popup.ok.addEventListener(MouseEvent.CLICK, captureComments);
popup.close();
}
var str:String="";
private function captureComments():void{
Alert.show("Invoked Event Listener", "Alert");
str=popup.comments;
}
]]>
</fx:Script>
<mx:Button id="id1" label="Click" click="clickHandler(event)"/>
<s:Label id="lbl" height="18" width="282" text={str} />
</s:Application>
Child.mxml
This file is the popup reference, creating popup.
I have configured the close action and cancel buttons to work as needed, but unable to propagate the text entered in comment are back to the parent.
<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
layout="vertical" width="524" height="322"
showCloseButton="true"
close="close()" chromeColor="#CCCCCC"
horizontalAlign="center" verticalAlign="middle" color="#000000">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.PopUpManager;
import mx.rpc.events.ResultEvent;
[Bindable]public var comments:String;
public function close(): void{
PopUpManager.removePopUp(this);
}
private function save(): void{
if(comments == null){
comments = "";
}
dispatchEvent(new ResultEvent(comments));
}
]]>
</fx:Script>
<mx:HBox width="100%" height="70%">
<mx:VBox width="100%" height="100%">
<mx:TextArea id="comment" text="{comments}" width="100%" height="80%" change="comments = event.target.text">
</mx:TextArea>
<mx:HBox horizontalAlign="center" width="100%" height="20%">
<s:Button id="ok" label="OK" chromeColor="#CCCCCC" click="save()" fontWeight="bold"/>
<s:Button id="cancel" label="Cancel" chromeColor="#CCCCCC" click="close()" fontWeight="bold"/>
</mx:HBox>
</mx:VBox>
</mx:HBox>
</mx:TitleWindow>
You are dispatching a ResultEvent, like this:
dispatchEvent(new ResultEvent(comments));
However, the event type is the user entered input. Check out the ResultEvent constructor. The first argument is the event type. I have no idea how you'll be able to consistently listen for an event type that is unknown at compile time.
I think you are trying to pass your custom data back to your parent class. My preference would be to use a custom event for this; but if you wanted to use a ResultEvent you could pass your comments in the result property:
dispatchEvent(new ResultEvent(ResultEvent.RESULT,false,true,comments));
In your parent file, add an event listener like this:
popup.addEventListener(ResultEvent.RESULT, processResult);
You should be able to get at your comments value as a property on the event:
protected function processResult(event:ResultEvent):void{
str=event.result
}
Another thing I've done as well is in the Child Container Create a public variable to pass the function from the parent ie:
in parent:
protected function captureComments(comments:String):void{
str=comments;
}
in child :
declare a public var and add the function to the save() function
public var parentFunction:Function;
private function save(): void{
if(comments == null){
comments = "";
}
parentFunction(comments);
Then when Creating your popup container in the parent:
protected function clickHandler(event:MouseEvent):void
{
popup = PopUpManager.createPopUp(this,pop,true) as pop;
popup.parentFunction = captureComments;
popup.close();
}

Create Custom Dialog Box using dispatch events,states and titlewindow in flex/AS3.0

Since a month Iam working on creating a custom dialog box, having parameters like message,state and modal(true/false)
Eg:
showAlert("Hi, how are you doing","Goodmorning", true);
I have learned how to dispatch event. but unable to dispatch the alertevent/popupManager using States.
Below is the code, I am struggling with.
Main.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
public function onclick(event:MouseEvent):void
{
this.dispatchEvent("Hi, How are you?", "Morning", true);
}
public function dispatchEvent(arg1:String, arg2:String, arg3:Boolean):void
{
var str:*=null;
str.message = arg1;
str.stateName = arg2;
str.modal = arg3;
this.dispatchEvent(new ShowAlert(ShowAlert.SHOW, true));
}
]]>
</mx:Script>
<mx:Button id="click" click="onclick(event)"/>
</mx:Application>
ShowAlert.as
package
{
import flash.events.*;
public class ShowAlert extends Event
{
public var _stateName:String;
public var _closable:Boolean;
public var _message:String;
public var _isModal:Boolean;
public static const SHOW:String="show";
public function flash.events.(arg1:String, arg2:Boolean=false, arg3:Boolean=false)
{
super(arg1, arg2, arg3);
trace("arg1: "+arg1+"\t arg2: "+arg2+"\t arg3: "+arg3);
}
public function set message(arg1:String):void
{
this._message = arg1;
}
public function get message():String
{
return _message;
}
public function get modal():Boolean
{
return _isModal;
}
public function get stateName():String
{
return _stateName;
}
public function set stateName(arg1:String):void
{
this._stateName = arg1;
}
public function set modal(arg1:Boolean):void
{
this._isModal = arg1;
}
public override function clone():flash.events.Event
{
return new ShowAlert(type);
}
}
}
I was unable to write the custom titlewindow using states, so I dint post that.
Please let me know, how to make this happen.
Below is the Sample code, specifying my problem
main.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
public var info:IModuleInfo;
public var loadalert:DisplayObject;
import mx.modules.ModuleManager;
import mx.modules.IModuleInfo;
import mx.events.ModuleEvent;
public function init():void
{
Alert.show("This is forst alert.");
}
public function Loadalerrt():void
{
trace("loadalertmodule");
info = ModuleManager.getModule("../bin-debug/alerrtmod.swf");
info.addEventListener(ModuleEvent.READY, modEventHandler);
info.load();
}
public function modEventHandler(event:ModuleEvent):void
{
trace("modeventHandler");
loadalert=info.factory.create() as DisplayObject;
can1.addChild(loadalert);
}
]]>
</mx:Script>
<mx:Button label="Alert in Application" id="b1" click="init()" x="29" y="21"/>
<mx:Button label="Load Module" id="b2" click="Loadalerrt();" x="10" y="92"/>
<mx:Canvas id="can1" x="409" y="57" backgroundColor="cyan"/>
</mx:Application>
alerttmod.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
public function alertshow():void
{
Alert.show("This is second alert, You Can see it covers whole application, What I want is its scope should be limited to this specific module, not to whole application ");
}
]]>
</mx:Script>
<mx:Button label="Alert in Module" id="b1" click="alertshow()" x="163" y="100"/>
</mx:Module>
Now I see your problem. I think it is not possible to restrict the modal area of the Alert. I can suggest you to fake the modal behaviour by desabling the modules elements while your dialog is being shown.
May be it can help you...
Here are two components to demonstrate it:
//your module with another Alert
<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.PopUpManager;
public function alertshow():void
{
Alert.show("This is second alert...");
}
public function alertshow2():void
{
var customAlert:CustomAlert = new CustomAlert();
customAlert.addEventListener("CLOSED", onCustomAlertClosed);
this.enabled = false;
PopUpManager.addPopUp(customAlert, this, false);
PopUpManager.centerPopUp(customAlert);
}
private function onCustomAlertClosed(evt:Event):void
{
this.enabled = true;
}
]]>
</mx:Script>
<mx:Button label="Alert in Module" id="b1" click="alertshow()" x="163" y="100"/>
<mx:Button label="CustomAlert in Module" id="b2" click="alertshow2()" x="163" y="150"/>
<mx:Canvas width="100%" height="100%" backgroundColor="0xbbbbbb" alpha="0.8" visible="{!this.enabled}"/>
</mx:Module>
//simple CustomAlert as a Panel
<?xml version="1.0" encoding="utf-8"?>
<mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="300" height="200">
<mx:Script>
<![CDATA[
import mx.managers.PopUpManager;
protected function button1_clickHandler(event:MouseEvent):void
{
PopUpManager.removePopUp(this);
this.dispatchEvent(new Event("CLOSED"));
}
]]>
</mx:Script>
<mx:Button x="112" y="128" label="Close" click="button1_clickHandler(event)"/>
</mx:Panel>

changing the state of the variable from the child component using custom event

this is the main application
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
verticalAlign="middle"
backgroundColor="white" layout="absolute" initialize="init();" xmlns:MyComp="screens.*" >
<mx:Script>
<![CDATA[
import screens.MyEvent;
private function init():void
{
systemManager.addEventListener("data_transfer",handleDataTransfer);
}
private function handleDataTransfer(evt:MyEvent):void{
this.myViewStack.selectedIndex=1;
}
]]>
</mx:Script>
<mx:ViewStack id="myViewStack" selectedIndex="0" width="1110" height="636">
<MyComp:Welcome />
<MyComp:Screen id="fillPage" />
</mx:ViewStack>
</mx:Application>
this is my Welcome component
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="930" height="300" >
<mx:Script>
<![CDATA[
private function changeHandler():void{
var myEVT:MyEvent = new MyEvent("data_transfer",false, true);
this.dispatchEvent(myEVT);
}
]]>
</mx:Script>
<mx:Button label="Fill The Form" id="fillForm" click="changeHandler()"/>
</mx:Canvas>
this is my screen.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">
<mx:Label text="hai">
</mx:Label>
</mx:Canvas>
this is MYEvent.as
package screens
{
import flash.events.Event;
public class MyEvent extends Event
{
public static const DATA_TRANSFER:String = "data_transfer";
public function MyEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
}
}
I want to get the Screen mxml by clicking the button at welcome mxml...i am initial learner..
but it is not working..Please help me..
As your custom event has bubbling=false, it wont bubble upto the systemManager. try:
var myEVT:MyEvent = new MyEvent("data_transfer", true, true);
Alternatively, add the listener to the object that is dispatching the event:
welcomePage.addEventListener("data_transfer",handleDataTransfer);
....
<MyComp:Welcome id="welcomePage" />
Second solution is generally better/preferred - less event bubbling is a good thing

finding last character shown on a flex spark label

Among all the options of the spark label, there is none that tells me the last character shown when the text is truncated, is there any way to accomplish this?
For spark Label you can capture the textLines property after updateDisplayList drew the control.
package frm.ria.signoff.views.components
{
import flash.text.engine.TextLine;
import spark.components.Label;
import mx.core.mx_internal;
public class LastShownCharLabel extends Label
{
[Bindable]
public var lastChar:String;
protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth,unscaledHeight);
if(mx_internal::textLines.length>0)
{
var charsInfirstLine:int = TextLine(mx_internal::textLines[0]).rawTextLength;
if(text) lastChar = text.charAt(charsInfirstLine-1);
}
}
}
}
here is the mxml file for that. please execute the following code.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
public function onClick():void
{
lbl.text = StringUtil.trim(lbl.text);
output.text = lbl.text.charAt(lbl.text.length -1);
}
]]>
</mx:Script>
<mx:VBox>
<mx:Label id="lbl" text="Sagar "/>
<mx:Button click="{onClick();}" label="Click" />
<mx:TextInput id="output" />
</mx:VBox>
</mx:Application>

ItemRollOver with a custom ToolTip

I have this custom toolTip:
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
implements="mx.core.IToolTip"
creationPolicy="all"
cornerRadius="4" borderStyle="solid" backgroundColor="#FFFFFF"
creationComplete="init()" width="100" height="100">
<fx:Script>
<![CDATA[
import mx.core.IToolTip;
public var arrItemsKits:Array=[];
public var _text:String;
public function get text():String {
return _text;
}
public function set text(value:String):void {
}
protected function init():void
{
grid.dataProvider=arrItemsKits;
}
]]>
</fx:Script>
<mx:DataGrid id="grid" width="100%" height="100%">
<mx:columns>
<mx:DataGridColumn headerText="Code" dataField="itemPartNumber"/>
<mx:DataGridColumn headerText="Description" dataField="kitItemsNotes"/>
</mx:columns>
</mx:DataGrid>
</mx:VBox>
and i want it to fire it when i roll the mouse over a row from a datagrid, so i need to add an event listener(toolTipCreate) to the row of that grid.
Any ideas how can i solve this?
Thanks
Check out this
<!-- myDataGridTest.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.events.ListEvent;
import mx.core.IToolTip;
import mx.events.ToolTipEvent;
// holds the currently highlighted item
private var highlightedItem:Object;
// event listener to get our hands on the currently highlighted item.
private function getHighlightedItem(e:ListEvent):void {
highlightedItem = e.itemRenderer.data;
// Quick n dirty way to force the ToolTipManager to refresh our tooltip.
// We need to dispatch this by hand because the pointer never leaves myDataGrid
// between successive highlights.
myDataGrid.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OUT));
}
private function createTooltip(e:ToolTipEvent):void {
var tt:MyCustomTooltip = new MyCustomTooltip();
tt.firstName = highlightedItem.name;
tt.lastName = highlightedItem.surname;
// Contract with the tooltip manager: if it finds an IToolTip instance attached to
// the event, it uses that instance instead of creating the standard one.
e.toolTip = tt;
}
]]>
</mx:Script>
<mx:DataGrid id="myDataGrid" toolTip=" " toolTipCreate="createTooltip(event)" itemRollOver="getHighlightedItem(event)">
<mx:dataProvider>
<mx:Object name="john" surname="doe"/>
<mx:Object name="mike" surname="smith"/>
</mx:dataProvider>
</mx:DataGrid>
</mx:Application>
<!-- MyCustomTooltip.mxml -->
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" implements="mx.core.IToolTip"
backgroundColor="yellow" backgroundAlpha="0.6">
<mx:Script>
<![CDATA[
private var _firstName:String;
private var _lastName:String;
// Dummy implementations to comply with mx.core.IToolTip
public function get text():String {return null;}
public function set text(value:String):void {}
// properties and functions
public function set firstName(value:String):void {
_firstName = value;
invalidateProperties();
}
public function set lastName(value:String):void {
_lastName = value;
invalidateProperties();
}
override protected function commitProperties():void {
fName.text = _firstName;
lName.text = _lastName;
}
]]>
</mx:Script>
<mx:Label x="0" y="0" id="fName"/>
<mx:Label x="0" y="20" id="lName"/>
</mx:Canvas>
.
.
Or try something like this
.
.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="doInit();">
<mx:Script>
<!![CDATA[import mx.collections.ArrayCollection; // this holds the grid data
[Bindable]
private var myData:ArrayCollection = new ArrayCollection();private function doInit():void{
myData.addItem({fname:"Joe",lname:"Bloggs"});
myData.addItem({fname:"Joe1",lname:"Bloggs"});
}
private function buildToolTip(item:Object):String{
var myString:String = "";
if(item != null)
{
myString = myString + "Firstname : " + item.fname + "\n";
myString = myString + "Lastname : " + item.lname + "\n"
}
return myString;
}
]]>
</mx:Script>
<mx:DataGrid id="dGrid" dataProvider="{myData}" visible="true" dataTipFunction="buildToolTip">
<mx:columns>
<mx:DataGridColumn dataField="fname" headerText="FirstName" showDataTips="true" />
<mx:DataGridColumn dataField="lname" headerText="LastName" showDataTips="true" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
I figured it out: On itemRollOver handler you add event.itemRenderer.addEventListener(ToolTipEvent.TOOL_TIP_CREATE, createTT);