I have prepared a very simple test case to demo my problem.
Please just place the 2 files below into a Flash Builder 4.6 project and they will run instantly.
My problem is that the menu attached to my (slightly modified) custom PopUpButton never changes - even though the underlying Array data provider is changed when you click one of the 3 buttons on the right side of it:
AuxButtonTest.mxml:
<?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"
xmlns:comps="*"
creationComplete="init()">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
private const XML1:XML =
<pref>
<aux event="1">One</aux>
<aux event="2">Two</aux>
<aux event="3">Three</aux>
<aux event="4">Four</aux>
<aux event="5">Five</aux>
</pref>;
private const XML2:XML =
<pref>
<aux event="1">One</aux>
<aux event="2">Two</aux>
</pref>;
private const XML3:XML =
<pref>
<aux event="3">Three</aux>
</pref>;
public function init():void {
_auxBtn.update(XML1.aux);
}
//private function handleAuxChosen(event:PrefEvent):void {
//Alert.show(event.toString());
//}
]]>
</fx:Script>
<s:controlBarContent>
<!-- commented: aux_chosen="handleAuxChosen(event)" -->
<comps:AuxButton id="_auxBtn" />
<s:Button id="_btn1" label="XML 1" click="_auxBtn.update(XML1.aux);" />
<s:Button id="_btn2" label="XML 2" click="_auxBtn.update(XML2.aux);" />
<s:Button id="_btn3" label="XML 3" click="_auxBtn.update(XML3.aux);" />
</s:controlBarContent>
</s:Application>
AuxButton.mxml (my custom component based on PopUpButton):
<?xml version="1.0" encoding="utf-8"?>
<mx:PopUpButton
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
popUp="{_menu}"
creationComplete="init(event)">
<fx:Metadata>
<!-- [Event(name="aux_chosen", type="PrefEvent")] -->
</fx:Metadata>
<fx:Script>
<![CDATA[
import mx.controls.Menu;
import mx.events.MenuEvent;
import mx.events.FlexEvent;
private var _str:String;
[Bindable]
private var _data:Array = new Array();
[Bindable]
private var _menu:Menu = new Menu();
private function init(event:FlexEvent):void {
_menu.dataProvider = _data;
_menu.addEventListener('itemClick', handleMenu);
addEventListener('click', handleClick);
}
public function update(xlist:XMLList):void {
_data.length = 0;
for each (var xml:XML in xlist) {
_data.push({label: xml, event: xml.#event});
}
label = _data[0].label;
_str = _data[0].event;
enabled = true;
}
private function handleMenu(event:MenuEvent):void {
label = event.label;
_str = event.item.event;
}
private function handleClick(event:MouseEvent):void {
enabled = false;
//dispatchEvent(new PrefEvent(PrefEvent.AUX_CHOSEN, _str));
}
]]>
</fx:Script>
</mx:PopUpButton>
I've commented my custom event out - it doesn't matter here.
Please just click at the buttons few times and then look at the menu - it always has 5 items and that is wrong.
UPDATE: Thank you for the answers - the following code works now for me. I still wonder why isn't ArrayList supported, but ArrayCollection works fine.
AuxButtonText.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:PopUpButton
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
popUp="{_menu}"
creationComplete="init(event)">
<fx:Script>
<![CDATA[
import mx.controls.*;
import mx.events.*;
import mx.collections.*;
private var _str:String;
[Bindable]
private var _data:ArrayCollection = new ArrayCollection();
//private var _data:ArrayList = new ArrayList();
[Bindable]
private var _menu:Menu = new Menu();
private function init(event:FlexEvent):void {
_menu.dataProvider = _data;
_menu.addEventListener('itemClick', handleMenu);
addEventListener('click', handleClick);
}
public function update(xlist:XMLList):void {
_data.removeAll();
if (xlist == null || xlist.length() == 0) {
enabled = false;
return;
}
for each (var xml:XML in xlist)
_data.addItem({label: xml, event: xml.#event});
label = _data.getItemAt(0).label;
_str = _data.getItemAt(0).event;
enabled = true;
}
private function handleMenu(event:MenuEvent):void {
label = event.label;
_str = event.item.event;
}
private function handleClick(event:MouseEvent):void {
enabled = false;
//dispatchEvent(new PrefEvent(PrefEvent.AUX_CHOSEN, _str));
}
]]>
</fx:Script>
</mx:PopUpButton>
AuxButton.mxml:
<?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"
xmlns:comps="*"
initialize="init()">
<fx:Script>
<![CDATA[
private const XML1:XML =
<pref>
<aux event="1">One</aux>
<aux event="2">Two</aux>
<aux event="3">Three</aux>
<aux event="4">Four</aux>
<aux event="5">Five</aux>
</pref>;
private const XML2:XML =
<pref>
<aux event="1">One</aux>
<aux event="2">Two</aux>
</pref>;
private const XML3:XML =
<pref>
<aux event="3">Three</aux>
</pref>;
private const XML4:XML =
<pref>
</pref>;
public function init():void {
_auxBtn.update(XML1.aux);
}
]]>
</fx:Script>
<s:controlBarContent>
<comps:AuxButton id="_auxBtn" />
<s:Button id="_btn1" label="XML 1" click="_auxBtn.update(XML1.aux);" />
<s:Button id="_btn2" label="XML 2" click="_auxBtn.update(XML2.aux);" />
<s:Button id="_btn3" label="XML 3" click="_auxBtn.update(XML3.aux);" />
<s:Button id="_btn4" label="LEN=0" click="_auxBtn.update(XML4.aux);" />
<s:Button id="_btn5" label="NULL" click="_auxBtn.update(null);" />
</s:controlBarContent>
</s:Application>
Arrays do not dispatch any events when their content changes. As a rule of thumb: Using [Bindable] on an Array and using it as some kind of dataProvider is usually a bad idea since adding/removing doesn't dispatch any events and therefore won't be handled by any component.
Instead of Array use an ArrayCollection which dispatches events of type CollectionEvent.COLLECTION_CHANGE whenever its content changes. Most components in the Flex SDK handle those events and update itself accordingly. So, the following code works since assigning a new source to the ArrayCollection dispatches a CollectionEvent which causes the Menu to update its menu items.
// you don't need [Bindable] on _data
private var _data:ArrayCollection = new ArrayCollection();
public function update(xlist:XMLList):void
{
var items:Array = [];
for each (var xml:XML in xlist)
{
items.push({label: xml, event: xml.#event});
}
_data.source = items;
if (items.length > 0)
{
label = items[0].label;
_str = items[0].event;
}
enabled = true;
}
My problem is that the menu attached to my (slightly modified) custom
PopUpButton never changes - even though the underlying Array data
provider is changed
This herein lies your problem. You have no code to change the menu's dataProvider or sync it with your private _data variable. The _data array does indeed change, but you have not coded any relation between _data and _menu.dataProvider.
The initial value is set in your init method:
private function init(event:FlexEvent):void {
_menu.dataProvider = _data;
_menu.addEventListener('itemClick', handleMenu);
addEventListener('click', handleClick);
}
But, it is never changed as data changes.
I'll do one little side note here to add that your init() method is called on creationComplete. Which means the component goes through it's full lifecycle once; then you change the dataProvider of the _menu, forcing it go through it's lifecycle again to redraw with the new dataProvider. This is usually undesirable. You should read up on the Flex Component Lifecycle to understand what events are fired and when. I would probably recommend setting the dataProvider on initialize, which is caleld after createChildren() is run, but before the children will be sized and measured.
Anyway, a quick fix is to just change the _menu.dataProvider on the fly in your update:
public function update(xlist:XMLList):void {
_data.length = 0;
for each (var xml:XML in xlist) {
_data.push({label: xml, event: xml.#event});
}
label = _data[0].label;
_str = _data[0].event;
enabled = true;
// new code
_menu.dataProvider = _data;
}
Related
I don't even know how to explain this behavior but I'll try. I am loading images from an external url that requires basic auth so I am using URLLoader to load the image from a unique ID. The ID gets passed to the itemrenderer which then proceeds to load the image. But the images switch around on their own when I scroll. If I load more than 7 images or so it starts repeating images....
Youtube video of error:
http://www.youtube.com/watch?v=ZYoqlS14gWQ
Relevant code:
<s:ItemRenderer name="RandomItemRenderer" creationComplete="init();"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
autoDrawBackground="false">
<s:states>
<s:State name="normal" />
<s:State name="hovered" />
<s:State name="selected" />
</s:states>
<fx:Script>
<![CDATA[
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import mx.utils.ObjectProxy;
import customclasses.Settings;
[Bindable] private var coverArtImage:Image;
private var myCoverArtLoader:URLLoader;
[Bindable] private var coverArtSource:String;
private function init():void {
get_coverArt();
}
private function get_coverArt(): void {
if (!data.coverArt) {
set_nullCoverArt();
} else {
var requestString:String = "/rest/getCoverArt.view?v=1.5.0&c=AirSub&id=" + data.coverArt;
var requestURL:String = Settings.ServerURL + requestString;
myCoverArtLoader = new URLLoader();
var myRequest:URLRequest = new URLRequest();
var authHeader:URLRequestHeader = new URLRequestHeader();
authHeader.name = 'Authorization';
authHeader.value = 'Basic ' + Settings.EncryptedCreds();
myRequest.requestHeaders.push(authHeader);
myRequest.url = requestURL;
myRequest.method = URLRequestMethod.GET;
myCoverArtLoader.dataFormat = URLLoaderDataFormat.BINARY;
myCoverArtLoader.addEventListener(Event.COMPLETE, set_coverArt);
myCoverArtLoader.addEventListener(IOErrorEvent.IO_ERROR, set_failedCoverArt);
myCoverArtLoader.load(myRequest);
}
}
private function set_coverArt(evt:Event) : void {
coverArtImage = new Image();
coverArtImage.source = myCoverArtLoader.data;
myCoverArtLoader.removeEventListener(Event.COMPLETE, set_coverArt);
}
private function set_nullCoverArt() : void {
coverArtImage = new Image();
coverArtImage.source = "assets/nullCoverArt.jpg";
}
private function set_failedCoverArt() : void {
coverArtImage = new Image();
coverArtImage.source = "assets/nullCoverArt.jpg";
myCoverArtLoader.addEventListener(IOErrorEvent.IO_ERROR, set_nullCoverArt);
}
]]>
</fx:Script>
<s:Image source.normal="assets/coverOutline.png" source.selected="assets/coverOutlineYellow.png" source.hovered="assets/coverOutlineYellow.png"
height="226" width="226" />
<s:VGroup top="4.5" bottom="5" width="200" horizontalAlign="center" letterSpacing="10"
paddingBottom="5" paddingTop="9" verticalAlign="middle" x="13.5">
<s:Image id="ui_imgCoverArt" width="200" height="200" source="{coverArtImage.source}"/>
<s:Label text="{data.title}" width="160" styleName="RandomList" />
</s:VGroup>
ItemRenderers are reusable and cached, i.e. there are only limited count created in List to fill its area (rowCount +- couple). And when you scroll, new renderers are not instantiated, instead the one renderer that was scrolled out goes up or down and is filled with new data.
That's why you can not rely on creationComplete event, it will be fired only once for each instance of renderer.
The solution is to override data setter and build there the behaviour needed:
override public function set data(value:Object):void
{
super.data = value;
get_coverArt();
}
Useful link: How flex itemRenderer works ? (their life cycle)
I'd be extremely grateful if somebody could help me, or point me in the right direction.
I've been trying to get an adobe air application start in system tray, so far I've used this snippet: http://www.swamicharan.com/blog/air/minimizing-an-air-app-to-systemtray/ which works as described, however no matter what I do I can't seem to make it start, minimized, in the system tray. This is the code I have so far:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="300" height="100" creationComplete="initApp()" layout="horizontal">
<fx:Script>
<![CDATA[
import mx.events.CloseEvent;
private var trayIcon:BitmapData;
public function initApp():void{
loadTrayIcon();
this.addEventListener(Event.CLOSING, minToTray);
}
public function loadTrayIcon():void{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, readyToTray);
loader.load(new URLRequest("assets/icon.PNG"));
}
private function minToTray(event:Event):void{
event.preventDefault();
dock();
}
public function readyToTray(event:Event):void{
trayIcon = event.target.content.bitmapData;
var myMenu:NativeMenu = new NativeMenu();
var openItem:NativeMenuItem = new NativeMenuItem("Options");
var closeItem:NativeMenuItem = new NativeMenuItem("Close");
openItem.addEventListener(Event.SELECT, unDock);
closeItem.addEventListener(Event.SELECT, closeApp);
myMenu.addItem(openItem);
myMenu.addItem(new NativeMenuItem("", true));
myMenu.addItem(closeItem);
if(NativeApplication.supportsSystemTrayIcon){
SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip = "Notifier";
SystemTrayIcon(NativeApplication.nativeApplication.icon).
addEventListener(MouseEvent.CLICK, unDock);
stage.nativeWindow.addEventListener(
NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING, winMinimized);
SystemTrayIcon(NativeApplication.nativeApplication.icon).menu = myMenu;
}
}
private function winMinimized(displayStateEvent:NativeWindowDisplayStateEvent):void{
if(displayStateEvent.afterDisplayState == NativeWindowDisplayState.MINIMIZED){
displayStateEvent.preventDefault();
dock();
}
}
public function dock():void{
stage.nativeWindow.visible = false;
NativeApplication.nativeApplication.icon.bitmaps = [trayIcon];
}
public function unDock(event:Event):void{
stage.nativeWindow.visible = true;
stage.nativeWindow.orderToFront();
NativeApplication.nativeApplication.icon.bitmaps = [];
}
private function closeApp(event:Event):void{
stage.nativeWindow.close();
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Style>
#namespace s "library://ns.adobe.com/flex/spark";
s|WindowedApplication
{
skinClass:ClassReference("spark.skins.spark.SparkChromeWindowedApplicationSkin");
background-color:#999999;
background-alpha:"0.7";
}
</fx:Style>
<s:Label text="Hello AIR"/>
</mx:WindowedApplication>
Many Thanks.
I think you'll manage by calling dock() at the end of readyToTray(event:Event).
To make sure your initialWindow is invisible when it launches you can set it's visible property to false in the application descriptor file.
Many thanks to www.Flextras.com who has been helping me with this question the last couple of days, and I almost have it. I have a main.mxml, child.mxml and headermenu.mxml. I click a button on the headermenu that dispatches an event up to the main.mxml which then executes a method in child.mxml. I know this works because I put an AlertDialog in the function I'm calling inside of the child.mxml. The child.mxml contains a drawingArea object that has an erase(). When I call this directly from child.xml it executes, however if I put drawingArea.erase() inside of the function being called by child's parent (main.mxml) nothing happens.
Here is my code:
Telestrator.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:MobileApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" firstView="views.TelestratorHome" creationComplete="creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.effects.Fade;
import mx.events.ChildExistenceChangedEvent;
import mx.events.EffectEvent;
import mx.events.FlexEvent;
import mx.events.ResizeEvent;
import mx.managers.PopUpManager;
import qnx.dialog.AlertDialog;
import qnx.events.QNXApplicationEvent;
import qnx.system.QNXApplication;
import views.TelestratorHome;
private var headerTimer:Timer = new Timer(5000,1);
[Bindable] private var headerMenu:HeaderMenu;
[Bindable] private var headerMenuDisplayed:Boolean = false;
private var tele:TelestratorHome;
private var alert2:AlertDialog = new AlertDialog();
protected function creationCompleteHandler(event:FlexEvent):void
{
QNXApplication.qnxApplication.addEventListener(QNXApplicationEvent.SWIPE_DOWN, onApplicationSwipeDown);
headerMenu = new HeaderMenu();
headerMenu.addEventListener(MouseEvent.CLICK, onHeaderMenuClick);
this.tele = new TelestratorHome();
this.tele.width = 100;
this.tele.height = 100;
this.tele.x = 10;
this.tele.y = 10;
this.addChild( tele );
headerMenu.addEventListener('parentDoSomething', onHeaderBarToldMeTo);
headerTimer.addEventListener(TimerEvent.TIMER, onHeaderMenuTimerEvent);
}
protected function onApplicationSwipeDown(event:QNXApplicationEvent) : void
{
showHeaderMenuPopup();
}
private function showHeaderMenuPopup() : void
{
if (!headerMenuDisplayed)
{
headerMenu.x = 0;
headerMenu.y = 0;
PopUpManager.addPopUp(headerMenu,this, false);
headerPopupEffectIn.end();
headerPopupEffectIn.play();
headerMenuDisplayed = true;
}
headerTimer.reset();
headerTimer.start();
}
private function onHeaderMenuTimerEvent(event:TimerEvent) : void
{
hideHeaderMenuPopup();
headerTimer.stop();
}
private function onHeaderMenuClick(event:MouseEvent) : void
{
hideHeaderMenuPopup();
}
private function hideHeaderMenuPopup() : void
{
headerPopupEffectOut.end();
headerPopupEffectOut.play();
headerPopupEffectOut.addEventListener(EffectEvent.EFFECT_END, headerMenuHidden);
}
private function headerMenuHidden(event:EffectEvent) : void
{
PopUpManager.removePopUp(headerMenu);
headerPopupEffectIn.removeEventListener(EffectEvent.EFFECT_END, headerMenuHidden);
headerMenuDisplayed = false;
}
protected function onHeaderBarToldMeTo(event:Event):void{
tele.eraseCanvas(event);
}
]]>
</fx:Script>
<fx:Declarations>
<s:Parallel id="headerPopupEffectIn" target="{headerMenu}">
<s:Move duration="250" yFrom="-75" xTo="0" yTo="0" easer="{sineEaser}" />
</s:Parallel>
<s:Parallel id="headerPopupEffectOut" target="{headerMenu}">
<s:Move duration="250" yFrom="0" xTo="0" yTo="-75" easer="{sineEaser}" />
</s:Parallel>
<s:Sine id="sineEaser" easeInFraction="0.2" />
</fx:Declarations>
</s:MobileApplication>
TelestratorHome.mxml
------------------
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark"
actionBarVisible="true" creationComplete="init()" overlayControls="false"
tabBarVisible="true" title="Telestrator">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:actionContent>
<s:Button label="Erase" click="drawingArea.erase(event)"/>
<s:Button id="saveBtn" label="Save Image" click="save()" visible="false"/>
</s:actionContent>
<s:titleContent>
<s:Button label="Take Photo" click="takeSnapshot()"/>
</s:titleContent>
<s:Panel id="p" top="1" width="1024" height="600" backgroundAlpha="0.0" horizontalCenter="0">
<s:Panel id="imageViewer" top="-32" width="1024" height="600" backgroundAlpha="0.0" horizontalCenter="0"/>
<s:Panel id="preview" top="-32" width="1024" height="600" backgroundAlpha="0.0" horizontalCenter="0"/>
<DrawingArea xmlns="*" id="drawingArea" y="-32" width="100%" height="509"/>
</s:Panel>
<fx:Script>
<![CDATA[
import com.tinkerlog.WebCam;
import com.tinkerlog.util.Base64;
import flash.media.Camera;
import mx.core.UIComponent;
import mx.graphics.codec.JPEGEncoder;
import mx.graphics.codec.PNGEncoder;
import qnx.dialog.AlertDialog;
private var webCam:WebCam;
private var alert3:AlertDialog = new AlertDialog();
private function init():void {
webCam = new WebCam(1024, 600);
var ref:UIComponent = new UIComponent();
preview.removeAllElements();
preview.addElement(ref);
ref.addChild(webCam);
}
private function takeSnapshot():void {
imageViewer.visible = true;
preview.visible=false;
saveBtn.visible = true;
imageViewer.width = preview.width;
imageViewer.height = preview.height;
var uiComponent : UIComponent = new UIComponent();
uiComponent.width = webCam.width;
uiComponent.height = webCam.height;
var photoData:Bitmap = webCam.getSnapshot();
var photoBitmap:BitmapData = photoData.bitmapData;
uiComponent.addChild(photoData);
imageViewer.removeAllElements();
imageViewer.addElement(uiComponent);
}
private function deleteSnapshot():void {
imageViewer.removeAllElements();
}
public function save():void
{
var bd:BitmapData = new BitmapData(p.width, p.height);
bd.draw(this);
var ba:ByteArray = (new PNGEncoder()).encode(bd);
(new FileReference()).save(ba, "doodle.png");
}
public function eraseCanvas(event:Event):void{
drawingArea.erase(event);
}
]]>
</fx:Script>
</s:View>
HeaderMenu.mxml
--------------
<?xml version="1.0" encoding="utf-8"?>
<s:BorderContainer xmlns:fx="http://ns.adobe.com/mxml/2009" borderVisible="false"
xmlns:s="library://ns.adobe.com/flex/spark" width="1024" height="75" xmlns:mx="library://ns.adobe.com/flex/mx" >
<fx:Script>
<![CDATA[
import mx.managers.PopUpManager;
import qnx.dialog.AlertDialog;
private var tele:Telestrator = new Telestrator();
private var alert:AlertDialog = new AlertDialog();
protected function btn_clickHandler(event:MouseEvent):void
{
// Figure out which button was pressed, and load appropriate view.
}
protected function onButtonInheaderbarClick(event:Event):void{
dispatchEvent(new Event('parentDoSomething'));
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:backgroundFill>
<s:SolidColor alpha="0.9" color="#000000" />
</s:backgroundFill>
<s:layout>
<s:HorizontalLayout horizontalAlign="right" verticalAlign="middle" paddingTop="5" paddingBottom="5" paddingRight="5" paddingLeft="5" gap="5"/>
</s:layout>
<s:Button id="btn_refresh" label="Refresh" height="60" fontSize="18" click="onButtonInheaderbarClick(event)"/>
<s:Label text="APPLICATION NAME" fontSize="32" styleName="FeedItemTitle" textAlign="center" width="100%" height="100%" verticalAlign="middle" />
<s:Button id="btn_about" label="About" height="60" fontSize="18" click="btn_clickHandler(event)"/>
</s:BorderContainer>
OK, still looking it over but the first mistake I see is you nested function which could be causing scoping issues so lets start with that and see if that helps.
Fix your DrawingArea class like so.
package{
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.utils.ByteArray;
import mx.core.UIComponent;
import mx.events.FlexEvent;
import mx.graphics.codec.PNGEncoder;
public class DrawingArea extends UIComponent{
private var isDrawing:Boolean = false;
private var x1:int;
private var y1:int;
private var x2:int;
private var y2:int;
public var drawColor:uint = 0x000000;
public function DrawingArea(){
this.super();
this.addEventListener(Event.ADDED_TO_STAGE, this.erase );
this.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDown);
this.addEventListener(MouseEvent.MOUSE_MOVE, this.mouseMove);
this.addEventListener(MouseEvent.MOUSE_UP, this.mouseUp);
}
private function mouseDown( e:MouseEvent ):void{
this.x1 = mouseX;
this.y1 = mouseY;
this.isDrawing = true;
}
private function mouseMove(e:MouseEvent ):void{
if (!event.buttonDown){
this.isDrawing = false;
}
this.x2 = mouseX;
this.y2 = mouseY;
if (this.isDrawing){
this.graphics.lineStyle(2, this.drawColor);
this.graphics.moveTo(this.x1, this.y1);
this.graphics.lineTo(this.x2, this.y2);
this.x1 = this.x2;
this.y1 = this.y2;
}
}
private function mouseUp( e:MouseEvent ):void{
this.isDrawing = false;
}
public function erase( e:Event ):void{
this.graphics.clear();
this.graphics.beginFill(0xffffff, 0.00001);
this.graphics.drawRect(0, 0, this.width, this.height);
this.graphics.endFill();
}
}
}
In main.mxml do this
private var child:Child;
function init(){
this.child = = new Child();
this.child.width = 100;
this.child.height = 100;
this.child.x = 10;
this.child.y = 10;
this.addChild( child );
headerMenu.addEventListener('parentDoSomething', onHeaderBarToldMeTo);
}
I have a datagrid which contains a Spark dropdownlist that needs to obtain dynamic data. The datagrid uses a separate dataProvider.
When I use a static ArrayCollection within my ItemRenderer, it works (please see listing 1).
However, when I use Swiz to mediate a 'list complete' event to load the ArrayCollection, the dropdownlist does not show the new data (please see listing 2).
Using the debugger, I inspected the dropdownlist ItemRenderer and have confirmed the new data is being loaded into the ArrayCollection. The new data is not shown in the UI control. I have tried invalidateProperties() + validateNow() and dispatching events on both the control and the renderer (this), but nothing seems to make the new data appear in the control on the datagrid.
Please help !!!
Listing 1: Datagrid and static ArrayCollection (this works):
<mx:DataGrid x="10" y="25" width="98%" id="dgInventory" paddingLeft="25" paddingRight="25" paddingTop="25" paddingBottom="25"
editable="true"
itemClick="dgInventory_itemClickHandler(event)" dataProvider="{acInventory}"
creationComplete="dgInventory_creationCompleteHandler(event)"
height="580">
<mx:columns>
<mx:DataGridColumn headerText="Item" dataField="itemName" itemRenderer="components.ItemRendererItem"
rendererIsEditor="true" editorDataField="selection" editable="true"/>
<mx:DataGridColumn headerText="Quantity Required" dataField="quantityReq" itemRenderer="components.ItemRendererQuantityRequired"
rendererIsEditor="true" editorDataField="selection" editable="true"/>
</mx:columns>
</mx:DataGrid>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import spark.events.IndexChangeEvent;
public var selection:int;
[Bindable]
protected var acItem:ArrayCollection = new ArrayCollection(
[ { itemName: "Item1"},
{ itemName: "Item2"},
{ itemName: "Item3"},
]);
//
protected function dropdownlist1_changeHandler(e:IndexChangeEvent):void
{
selection = e.newIndex;
}
]]>
</fx:Script>
<s:DropDownList id="ddlItem" prompt="Select Item" dataProvider="{acItem}" labelField="itemName"
selectedIndex="{int(dataGridListData.label)}"
change="dropdownlist1_changeHandler(event)"
width="80%" top="5" bottom="5" left="5" right="5"/>
Listing 2: Dynamic ArrayCollection (does not work):
<?xml version="1.0" encoding="utf-8"?>
<s:MXDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
focusEnabled="true">
<fx:Script>
<![CDATA[
import event.ItemEvent;
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
import spark.events.IndexChangeEvent;
public var selection:int;
//
[Bindable]
protected var acItem:ArrayCollection = new ArrayCollection();
//
protected function dropdownlist1_changeHandler(e:IndexChangeEvent):void
{
selection = e.newIndex;
}
//
protected function ddlItem_creationCompleteHandler(event:FlexEvent):void
{
var eve : ItemEvent = new ItemEvent( ItemEvent.LIST_ITEM_REQUESTED );
dispatchEvent( eve );
}
//
[Mediate( event="ItemEvent.LIST_ITEM_COMPLETE", properties="acItem" )]
public function refreshness( _acItem : ArrayCollection ):void
{
acItem.removeAll();
var len:int = _acItem.length;
if (len > 0)
{
for (var i:int=0; i < len; i++)
{
var newItem:Object = new Object;
newItem["itemName"] = _acItem[i].itemName;
acItem.addItem(newItem);
}
}
this.invalidateProperties();
this.validateNow();
//dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
}
]]>
</fx:Script>
<s:DropDownList id="ddlItem" prompt="Select Item" dataProvider="{acItem}" labelField="itemName"
selectedIndex="{int(dataGridListData.label)}"
creationComplete="ddlItem_creationCompleteHandler(event)"
change="dropdownlist1_changeHandler(event)"
width="80%" top="5" bottom="5" left="5" right="5"/>
</s:MXDataGridItemRenderer>
After re-reading Peter Ent's ItemRenderer series, this turned out to be quite simple.
I extended DataGrid to have the ArrayCollection property I needed, then added this to my renderer:
[Bindable]
protected var acItem:ArrayCollection = new ArrayCollection();
//
override public function set data( value:Object ) : void
{
super.data = value;
acItem = (listData.owner as MyDataGrid).itemList; // get the data from the renderer's container (by extending it to add a property, if necessary)
}
I've had a good rummage around the interweb and can't seem to find any examples of a tri-state checkbox. It doesn't look to be supported in the SDK and I can't find any examples online.
I would imagine this is a common problem, before I embark on writing my own does anyone know of a good flex tri-state checkbox component somewhere I can use :)
Cheers,
Jawache.
There is an example posted here on Flex Cookbook. You can easily create your own component from this code.
For others who come across this later, I made my own custom component that just hides the checkbox for the third state and replaces it with an image that looks like a checkbox in the third state. This is really easy to implement:
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
public static const BOX_CHECKED:int = 1;
public static const BOX_REJECTED:int = -1;
public static const BOX_UNCHECKED:int = 0;
[Bindable] public var boxVisible:Boolean = true;
[Bindable] public var enum_id:int = -1;
protected function checkbox_clickHandler(event:MouseEvent):void
{
if(!checkbox.selected)
boxVisible = false;
}
protected function image_clickHandler(event:MouseEvent):void
{
boxVisible = true;
}
public function set label(str:String):void {
checkbox.label = str;
xLabel.text = str;
}
public function get boxState():int {
if(checkbox.selected)
return BOX_CHECKED;
else if(redX.visible)
return BOX_REJECTED;
return BOX_UNCHECKED;
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:CheckBox id="checkbox" click="checkbox_clickHandler(event)" visible="{boxVisible}" includeInLayout="{boxVisible}"/>
<s:HGroup>
<s:Image id="redX" source="#Embed('assets/icons/redX.png')"
width="16" height="16"
click="image_clickHandler(event)"
visible="{!boxVisible}" includeInLayout="{!boxVisible}"/>
<s:Label id="xLabel" visible="{!boxVisible}" includeInLayout="{!boxVisible}" paddingTop="4"/>
</s:HGroup>
</s:Group>
Here is my own version inspired by James' one. You can enable / disable tristate to get a classic checkbox with the same component, and the mechanism used is a bit different, with less boilerplate. Embedded picture is here.
Use at your own risks ! :)
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init()">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
// STATES :
/** The intermediate state value. */
public static const INTERMEDIATE:int = -1;
/** The unchecked state value. */
public static const UNCHECKED:int = 0;
/** The checked state value. */
public static const CHECKED:int = 1;
// TRISTATE :
private var _isTristate:Boolean;
/** Whether this checkbox is tristate or a normal checkbox. */
[Bindable] public function get isTristate():Boolean { return _isTristate; }
public function set isTristate(value:Boolean):void {
_isTristate = value;
if(!_isTristate && state == INTERMEDIATE)
state = UNCHECKED;
}
// LABEL :
/** The checkbox label. */
[Bindable] public var label:String;
// STATE CHANGE :
private var _state:int;
/** The current state of the box. */
public function get state():int { return _state; }
public function set state(value:int):void {
_state = value;
switch(_state) {
case INTERMEDIATE:
if(!_isTristate)
throw new Error("Setting state to INTERMEDIATE on a non tristate checkbox !");
tristateImg.visible = true;
checkbox.selected = false;
break;
case UNCHECKED:
tristateImg.visible = false;
checkbox.selected = false;
break;
case CHECKED:
tristateImg.visible = false;
checkbox.selected = true;
break;
}
}
/**
* Simply sets the state according to the current state.
*/
protected function changeState(ev:Event):void {
if(isTristate) ev.preventDefault();
switch(state) {
case INTERMEDIATE: state = CHECKED; break;
case UNCHECKED: state = isTristate ? INTERMEDIATE : CHECKED; break;
case CHECKED: state = UNCHECKED; break;
}
}
/**
* Initial state.
*/
protected function init():void {
state = UNCHECKED;
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:layout>
<s:BasicLayout />
</s:layout>
<s:CheckBox id="checkbox" label="{label}"
change="changeState(event)"/>
<s:Image id="tristateImg"
mouseEnabled="false" mouseChildren="false"
source="#Embed('assets/icons/tristate_checkbox.png')"
x="2" y="5" />
</s:Group>
Take a look at http://srinichekuri.wordpress.com/2011/05/20/3-state-checkbox-for-headerrenderer-in-datagrid/.
It has the code for 3 state check box.