Flex actionscript extending DateChooser, events in calendar - apache-flex

ExtendedDateChooser class is great solution for simple event calendar used in my flex project. You can find it if google for "Adding-Calendar-Event-Entries-to-the-Flex-DateChooser-Component"
with a link of updated solution in comments of the post. I posted files below.
Problem in that calendar is text events are missing when month is changed.
Is there updateCompleted event in Actionscript just like in dateChooser flex component? Like in:
<mx:DateChooser id="dc" updateCompleted="goThroughDateChooserCalendarLayoutAndSetEventsInCalendarAgain()"</mx>
When scroll event is added, which is
available in Actionscript, it gets
dispatched but after
updateDisplayList() is fired, so
didn't manage to answer, why are
calendar events erased?
Any suggestions, what to add in code, maybe override some function?
ExtendedDateChooserClass.mxml
<?xml version='1.0' encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:mycomp="cyberslingers.controls.*"
layout="absolute"
creationComplete="init()">
<mx:Script>
<![CDATA[
import cyberslingers.controls.ExtendedDateChooser;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
public var mycal:ExtendedDateChooser = new ExtendedDateChooser();
// collection to hold date, data and label
[Bindable]
public var dateCollection:XMLList = new XMLList();
private function init():void
{
eventList.send();
}
private function readCollection(event:ResultEvent):void
{
dateCollection = event.result.calendarevent;
//Position and size the calendar
mycal.width = 400;
mycal.height = 400;
//Add the data from the XML file to the calendar
mycal.dateCollection = dateCollection;
//Add the calendar to the canvas
this.addChild(mycal);
}
private function readFaultHandler(event:FaultEvent):void
{
Alert.show(event.fault.message, "Could not load data");
}
]]>
</mx:Script>
<mx:HTTPService id="eventList"
url="data.xml"
resultFormat="e4x"
result="readCollection(event);"
fault="readFaultHandler(event);"/>
</mx:Application>
ExtendedDateChooser.as
package cyberslingers.controls
{
import flash.events.Event;
import flash.events.TextEvent;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.CalendarLayout;
import mx.controls.DateChooser;
import mx.core.UITextField;
import mx.events.FlexEvent;
public class ExtendedDateChooser extends DateChooser
{
public function ExtendedDateChooser()
{
super();
this.addEventListener(TextEvent.LINK, linkHandler);
this.addEventListener(FlexEvent.CREATION_COMPLETE, addEvents);
}
//datasource
public var dateCollection:XMLList = new XMLList();
//--------------------------------------
// Add events
//--------------------------------------
/**
* Loop through calendar control and add event links
* #param e
*/
private function addEvents(e:Event):void
{
// loop through all the calendar children
for(var i:uint = 0; i < this.numChildren; i++)
{
var calendarObj:Object = this.getChildAt(i);
// find the CalendarLayout object
if(calendarObj.hasOwnProperty("className"))
{
if(calendarObj.className == "CalendarLayout")
{
var cal:CalendarLayout = CalendarLayout(calendarObj);
// loop through all the CalendarLayout children
for(var j:uint = 0; j < cal.numChildren; j++)
{
var dateLabel:Object = cal.getChildAt(j);
// find all UITextFields
if(dateLabel.hasOwnProperty("text"))
{
var day:UITextField = UITextField(dateLabel);
var dayHTML:String = day.text;
day.selectable = true;
day.wordWrap = true;
day.multiline = true;
day.styleName = "EventLabel";
//TODO: passing date as string is not ideal, tough to validate
//Make sure to add one to month since it is zero based
var eventArray:Array = dateHelper((this.displayedMonth+1) + "/" + dateLabel.text + "/" + this.displayedYear);
if(eventArray.length > 0)
{
for(var k:uint = 0; k < eventArray.length; k++)
{
dayHTML += "<br><A HREF='event:" + eventArray[k].data + "' TARGET=''>" + eventArray[k].label + "</A>";
}
day.htmlText = dayHTML;
}
}
}
}
}
}
}
//--------------------------------------
// Events
//--------------------------------------
/**
* Handle clicking text link
* #param e
*/
private function linkHandler(event:TextEvent):void
{
// What do we want to do when user clicks an entry?
Alert.show("selected: " + event.text);
}
//--------------------------------------
// Helpers
//--------------------------------------
/**
* Build array of events for current date
* #param string - current date
*
*/
private function dateHelper(renderedDate:String):Array
{
var result:Array = new Array();
for(var i:uint = 0; i < dateCollection.length(); i++)
{
if(dateCollection[i].date == renderedDate)
{
result.push(dateCollection[i]);
}
}
return result;
}
}
}
data.xml
<?xml version="1.0" encoding="utf-8"?>
<rss>
<calendarevent>
<date>8/22/2009</date>
<data>This is a test 1</data>
<label>Stephens Test 1</label>
</calendarevent>
<calendarevent>
<date>8/23/2009</date>
<data>This is a test 2</data>
<label>Stephens Test 2</label>
</calendarevent>
</rss>

Just Change the FlexEvent.CREATION_COMPLETE in the ExtendedDateChooser.as to FlexEvent.UPDATE_COMPLETE
That should do.

Related

Spark Mobile Callout with List does not position / resize correctly

I've created a simple Callout with a List in it.
Like this:
<s:Callout xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
autoLayout="true" >
<fx:Declarations>
<!-- Platzieren Sie nichtvisuelle Elemente (z. B. Dienste, Wertobjekte) hier -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import com.skill.flextensions.factories.StyledClassFactory;
import mx.collections.IList;
import spark.components.IconItemRenderer;
import spark.components.List;
import spark.events.IndexChangeEvent;
import spark.layouts.HorizontalAlign;
import spark.layouts.VerticalLayout;
private var _list:List;
//
// PUBLIC PROPERTIES
//
private var _dataProvider:IList;
private var _dataProviderChanged:Boolean = false;
public function get dataProvider():IList
{
return _dataProvider;
}
public function set dataProvider(value:IList):void
{
_dataProvider = value;
_dataProviderChanged = true;
this.invalidateProperties();
}
private var _itemRenderer:IFactory;
private var _itemRendererChanged:Boolean = false;
public function get itemRenderer():IFactory
{
return _itemRenderer;
}
public function set itemRenderer(value:IFactory):void
{
_itemRenderer = value;
_itemRendererChanged = true;
this.invalidateProperties();
}
//
// # SUPER
//
override protected function commitProperties():void
{
super.commitProperties();
if(_dataProviderChanged)
{
_dataProviderChanged = false;
_list.dataProvider = _dataProvider;
// TODO
// we have to remeasure, after dataprovider updated
// unfortunately, this doesn't work:
/*
_list.invalidateSize();
_list.invalidateDisplayList();
_list.validateNow();
invalidateSize();
invalidateDisplayList();
validateNow();
*/
// so we will have to find a workaround for this situation.
}
if(_itemRendererChanged)
{
_itemRendererChanged= false;
_list.itemRenderer = getItemRenderer();
}
}
override protected function createChildren():void
{
_list = new List;
_list.top = _list.bottom = 0;
_list.itemRenderer = getItemRenderer();
_list.addEventListener( IndexChangeEvent.CHANGE , onChange , false , 0 , true );
var l:VerticalLayout = new VerticalLayout;
l.gap = 0;
l.requestedMinRowCount = 0;
l.horizontalAlign = HorizontalAlign.CONTENT_JUSTIFY;
_list.layout = l;
this.addElement( _list );
}
//
// # LIST
//
protected function onChange(e:IndexChangeEvent):void
{
var obj:Object = _list.selectedItem;
this.removeAllElements();
_list = null;
this.close(true , obj);
}
private function getItemRenderer():IFactory
{
if( ! _itemRenderer )
{
var fac:StyledClassFactory = new StyledClassFactory( IconItemRenderer );
var props:Object = new Object;
props.messageField = "message";
props.labelField = "";
props.styleName = "itemName";
props.iconField = "icon";
var styles:Object = new Object;
styles.messageStyleName = "itemHeadline";
fac.properties = props;
fac.styles = styles;
return fac;
}
return _itemRenderer;
}
]]>
</fx:Script>
The problem here is, that my Callout does not measure correctly. When the dataProvider is added to the List, it always resizes the List to the first item. When some user-interaction happens with the List, it suddenly resizes correctly (adjusting to the largest item).
Unfortunaltely, the CallOut-Position does not change, leading to a misplaced Callout, sometimes it's even half off screen.
So I want to make sure, List has the right size, before I open the Callout.
How can I do this? Many thx for your input.
I had the same problem. I'm sure there's a more elegant way to do it but this was my solution.
Listen for Callout FlexEvent.CREATION_COMPLETE:
<s:Callout xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
autoLayout="true"
creationComplete="init()">
Force the callout to redo its layout/sizing:
function init():void
{
validateNow();
updatePopUpPosition();
}
In my app, i handle list data population slightly differently than you, so you may need to call init() after setting data instead.

Embedding FXG document by variable name in Flex

I've been trying to get FXG to work in my Flex app, it works and renders fine but what I'm trying to accomplish is a sort of a gallery with data about the images in a database. I used to be able to use <s:Image source=/path/{variable_name}> but now I have to import the FXG files and can't use <s:Image> anymore. Here I can display a static FXG image:
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:fxg="assets.fxg.*"
tabBarVisible="false" title="{data.name}">
<fxg:megapicture001 x="118" y="27" width="338" height="519"/>
<s:Label x="78" y="43" text="{data.name}"/>
<s:navigationContent>
<s:Button icon="#Embed('assets/home.png')" click="navigator.popView()"/>
</s:navigationContent>
</s:View>
Trying to do <fxg:{data.picturename} /> blows up.
You can't import and use the FXG elements stand alone since they aren't display objects. My take was to wrap them in a UIComponent container. This class will probably end up as part of the Flextras Mobile Component set in our next update sometime early next year most likely:
package com.dotcomit.utils
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import mx.core.UIComponent;
public class FXGImage extends UIComponent
{
public function FXGImage(source:Class = null)
{
if(source){
this.source = source;
}
super();
}
// this will tell us the class we want to use for the display
// most likely an fxgClass
private var _source : Class;
protected var sourceChanged :Boolean = true;
public function get source():Class
{
return _source;
}
public function set source(value:Class):void
{
_source = value;
sourceChanged = true;
this.commitProperties();
}
public var imageInstance : DisplayObject;
// if you want to offset the position of the X and Y values in the
public var XOffset :int = 0;
public var YOffset :int = 0;
// if you want to offset the position of the X and Y values in the
public var heightOffset :int = 0;
public var widthOffset :int = 0;
override protected function createChildren():void{
super.createChildren();
if(this.sourceChanged){
if(this.imageInstance){
this.removeChild(this.imageInstance);
this.imageInstance = null;
}
if(this.source){
this.imageInstance = new source();
this.imageInstance.x = 0 + XOffset;
this.imageInstance.y = 0 + YOffset;
this.addChild(this.imageInstance);
}
this.sourceChanged = false;
}
}
override protected function commitProperties():void{
super.commitProperties();
if(this.sourceChanged){
// if the source changed re-created it; which is done in createChildren();
this.createChildren();
}
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
super.updateDisplayList(unscaledWidth, unscaledHeight);
if(unscaledHeight != 0){
this.imageInstance.height = unscaledHeight + this.heightOffset;
}
if(unscaledWidth != 0){
this.imageInstance.width = unscaledWidth + this.widthOffset;
}
}
}
}
You can use it something like this:
<utils:FXGImage id="fxgImage" source="assets.images.mainMenu.MainMenuBackground" height="100%" width="100%" />

How to check the user selections on dynamically generated radio buttons and check boxes in Flex?

The following is my codes. This is still work in progress; so, you will see some functions with empty contents. Plus, this is my first Flex application; please bear with me.
This is a quiz application that gets the questions and answers to each questions from a ColdFusion web service. There are three types of questions, True or False, Multiple Choice with single selection, and Multiple Choice with multiple selections. So, based upon the question type, the application would dynamically generate the appropriate amount of radio buttons or check boxes for the users to select. I got these working. The problem that I am having is, I am not sure how to check what the users have actually selected. In some other forums and posts on other web site, it said that I can use event.currentTarget.selectedValue to get the user selection. But when I actually do it, I got a run-time error saying, "Property selectedValue not found on mx.controls.FormItem and there is no default value." My question is, what do I need to do to capture the user selections?
Thanks in advance,
Monte
<?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"
creationComplete="initVars()">
<fx:Declarations>
<s:RemoteObject id="CFCertService" destination="ColdFusion" source="CFCertExam.cfquiz">
<s:method name="returnQuestions" result="resultHandler(event)"/>
<s:method name="returnAnswers" result="answerHandler(event)"/>
</s:RemoteObject>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.containers.FormItem;
import mx.controls.Alert;
import mx.controls.CheckBox;
import mx.controls.RadioButton;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;
[Bindable]
private var questionArray:ArrayCollection;
private var questionType:String;
private var answerItem:FormItem;
[Bindable]
private var currentQuestionCounter:int;
[Bindable]
private var answerArray:ArrayCollection;
private var myOptionButton:RadioButton = new RadioButton();
private var myOptionButton2:RadioButton = new RadioButton();
private function initVars():void {
currentQuestionCounter = 0;
btnPrev.enabled = false;
btnNext.enabled = false;
}
private function answerHandler(event:ResultEvent):void {
answerArray = event.result as ArrayCollection;
var i:int;
answerForm.removeAllChildren();
answerItem = new FormItem();
answerForm.addChild(answerItem);
switch (questionType) {
case "True or False":
{
myOptionButton.label = "True";
if (answerArray.getItemAt(0).Answer_Choice == "True") {
myOptionButton.value = 1;
} else {
myOptionButton.value = 0;
}
answerItem.addChild(myOptionButton);
myOptionButton2.label = "False";
if (answerArray.getItemAt(0).Answer_Choice == "False") {
myOptionButton2.value = 1;
} else {
myOptionButton2.value = 0;
}
answerItem.addChild(myOptionButton2);
answerItem.addEventListener(MouseEvent.CLICK, selectionHandler);
break;
}
case "Multiple Choice (Single Selection)":
{
for (i=0; i<answerArray.length; i++) {
var myOptionButton1:RadioButton = new RadioButton();
myOptionButton1.label = answerArray.getItemAt(i).Answer_Choice;
if (answerArray.getItemAt(i).Corect_Flag == "1") {
myOptionButton1.value = 1;
} else {
myOptionButton1.value = 0;
}
answerItem.addChild(myOptionButton1);
}
break;
}
case "Multiple Choice (Multiple Selection)":
{
for (i=0; i<answerArray.length; i++) {
var myCheckBox:CheckBox = new CheckBox();
myCheckBox.label = answerArray.getItemAt(i).Answer_Choice;
answerItem.addChild(myCheckBox);
}
break;
}
}
answerForm.x = 380;
answerForm.y = 200;
}
private function selectionHandler(event:MouseEvent):void {
Alert.show(event.currentTarget.toString());
}
private function resultHandler(event:ResultEvent):void {
questionArray = event.result as ArrayCollection;
txt1Questions.htmlText = questionArray.getItemAt(currentQuestionCounter).Question_Text;
questionType = questionArray.getItemAt(currentQuestionCounter).Question_Type;
btnNext.enabled = true;
CFCertService.returnAnswers(questionArray.getItemAt(currentQuestionCounter).Question_ID);
}
private function buttonEventHandler():void {
CFCertService.returnQuestions();
btnStartExam.enabled = false;
}
private function btnPrevEventHandler():void {
currentQuestionCounter--;
if (currentQuestionCounter == 0) {
btnPrev.enabled = false;
}
if (currentQuestionCounter < questionArray.length) {
btnNext.enabled = true;
}
txt1Questions.htmlText = questionArray.getItemAt(currentQuestionCounter).Question_Text;
questionType = questionArray.getItemAt(currentQuestionCounter).Question_Type;
CFCertService.returnAnswers(questionArray.getItemAt(currentQuestionCounter).Question_ID);
}
private function answerReturnHandler(questionIndex:int):void {
}
private function btnNextEventHandler():void {
currentQuestionCounter++;
if (currentQuestionCounter > 0) {
btnPrev.enabled = true;
}
if (currentQuestionCounter >= (questionArray.length - 1)) {
btnNext.enabled = false;
}
txt1Questions.htmlText = questionArray.getItemAt(currentQuestionCounter).Question_Text;
questionType = questionArray.getItemAt(currentQuestionCounter).Question_Type;
CFCertService.returnAnswers(questionArray.getItemAt(currentQuestionCounter).Question_ID);
}
]]>
</fx:Script>
<mx:Text id="txt1Questions" x="129" y="124"/>
<s:Button id="btnStartExam" label="Start Exam" click="buttonEventHandler()" x="370" y="54"/>
<mx:Form id="answerForm"/>
<s:Button x="129" y="436" label="Previous" id="btnPrev" click="btnPrevEventHandler()" enabled="false"/>
<s:Button x="642" y="436" label="Next" id="btnNext" click="btnNextEventHandler()" enabled="false"/>
</s:Application>
The problem in your code is that currentTarget references the UIComponent that you add the event listener to, which you added to the FormItem and not the RadioButtons.
Two Options
If you want to continue to add the event listener to the FormItem, you should use target instead of currentTarget to obtain the reference to the actual item clicked, rather than the UIComponent with the listener on it. However, you should be aware that if you add anything else to the FormItem (e.g., Labels, RichText, etc), those items will also trigger the event listener when clicked.
The other option is to add event listeners (they can all use selectionHandler) to each of the RadioButtons and then currentTarget will work fine.
Also, you might want to use a RadioButtonGroup for those questions which only allow a single selection. Then you would only need to use the Event.CHANGE on the RadioButtonGroup to trigger your selectionHandler.
Additional Resource
Check out the video on event bubbling from the Flex in a Week series.

Flex + Image + Disable Drag

How do I diable the drag-drop of an image. I've tried to stopPropagation, but that didn't help.
Here is the snippet of the code that I've written
<mx:Image width="24" height="24" complete="init()" dragStart="disableMove(event)"
source="{(data.id==null)?'': (data.id.search('\\.') > 0) ? 'assets/icons/teacher.png' : 'assets/icons/student.png'}"
toolTip="{data.data}" doubleClick="itemDoubleClick(event, data.id)" doubleClickEnabled="true">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import flash.events.MouseEvent;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
private var allCurrentItems: Array = new Array();
private function itemDoubleClick(event: Event, id: String): void {
Alert.show("Clicked = "+id);
}
private function init(): void {
var menuLabel:String = "About School\u00A0";
var cm:ContextMenu = new ContextMenu();
cm.hideBuiltInItems();
var item:ContextMenuItem = new ContextMenuItem(menuLabel);
this.addEventListener(MouseEvent.MOUSE_DOWN, showClick);
//add eventlisteners to the menu item and provide functions
cm.customItems.push(item);
//cm.customItems = [item];
this.contextMenu = cm;
}
private function showClick(event:MouseEvent): void {
if (event.buttonDown) {
Alert.show(String(event.buttonDown));
}
}
private function disableMove(event: MouseEvent): void {
event.stopImmediatePropagation();
}
]]>
</mx:Script>
</mx:Image>
I got it, instead of calling disableMove(event) on dragStart(), I called it on mouseDown() it worked.

Flex: Combobox loses is focus when data-provider updated?

It seems that ComboBoxes loose their selected item after their dataProvider updates, even if the same selected item is still in the dataProvider. They convert back to the first item being selected. Is there anyway to prevent this? So that if the same object is in the dataProvider it keeps the same object selected and only reverts back to the first index if the selected object is not in the updated dataProvider?
Thanks!
If the ComboBox looses its selected item, it means the dataProvider isn't updated - it is replaced. If you bind a ComboBox to an ArrayCollection and then add an item to the AC, The ComboBox is updated without loosing its selectedItem.
Sometimes you have to replace the dataProvider and in those cases, you have to listen for the updateComplete-event and reset the selectedItem. You can try this code:
<mx:Script>
<![CDATA[
import mx.controls.ComboBox;
import mx.events.ListEvent;
import mx.events.FlexEvent;
import mx.collections.ArrayCollection;
[Bindable]
private var dp:ArrayCollection = new ArrayCollection(["Item 1", "Item 2", "Item 3"]);
private var selectedItem:*;
private var dataProvider:*;
private function onChange(event:ListEvent):void {
selectedItem = (event.currentTarget as ComboBox).selectedItem;
}
private function onUpdateComplete(event:FlexEvent):void {
trace(event);
var cb:ComboBox = event.currentTarget as ComboBox;
if(dataProvider == null || cb.dataProvider != dataProvider) {
if(selectedItem != null && cb.selectedItem != selectedItem) cb.selectedItem = selectedItem;
if(cb.selectedIndex < 0) cb.selectedIndex = 0;
dataProvider = cb.dataProvider;
}
}
private function extendDP():void {
dp.addItem("Item " + (dp.length +1));
var ac:ArrayCollection = new ArrayCollection(dp.source);
dp = ac;
}
private function reduceDP():void {
dp.removeItemAt(dp.length -1);
var ac:ArrayCollection = new ArrayCollection(dp.source);
dp = ac;
}
]]>
</mx:Script>
<mx:VBox>
<mx:ComboBox dataProvider="{dp}" change="onChange(event)" updateComplete="onUpdateComplete(event)" />
<mx:Button label="Extend dp" click="extendDP()" />
<mx:Button label="Reduce dp" click="reduceDP()" />
</mx:VBox>
It creates a ComboBox and binds it to an ArrayCollection. The two buttons adds and removes items from the collection.
Well I was able to extend the ComboBox with this class, which just looks for the selected label and compares with labels from the new dataProvider. It seems to work, although kinda ad-hoc. I was hoping for a scalable solution.
package
{
import mx.controls.ComboBox;
import mx.collections.ArrayCollection;
public class SelectionKeepingComboBox extends ComboBox
{
public function SelectionKeepingComboBox()
{
super();
}
override public function set dataProvider(value:Object):void
{
var curSelectedLabel:String;
if(this.selectedItem)
{
curSelectedLabel = this.selectedLabel;
}
super.dataProvider = value;
if(curSelectedLabel == null)
{
return;
}
var dp:Array;
if(this.dataProvider is ArrayCollection)
{
dp = this.dataProvider.toArray();
}
else
{
dp = this.dataProvider as Array;
}
for(var i:uint = 0; i<dp.length; i++)
{
var obj:Object = dp[i];
var dpLabel:String = obj.label;
if(dpLabel == curSelectedLabel)
{
this.selectedItem = obj;
}
}
}
}
}

Resources