Collapsible Panel Component for Flex
Posted on August 21, 2010 by Sameera Thilakasiri
Here’s an another post in the same vein as my previous one: this time, the component I’m sharing is a Panel subclass that allows for collapsing and expanding its contents. What this means is that the user can click on the header of the Panel to make it toggle between an open or closed state, with a smooth animation.
I Tried to implement the component so that it’ll be easy to use in both MXML and ActionScript. In the demo app there is also an example on styling the CollapsiblePanel.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:hassegContainers="org.hasseg.containers.*"
width="100%" height="100%"
layout="vertical"
creationComplete="ccHandler(event);"
>
<mx:Style>
.myCPStyle {
borderThicknessLeft: 0;
borderThicknessTop: 0;
borderThicknessBottom: 0;
borderThicknessRight: 0;
headerHeight: 20;
dropShadowEnabled: false;
headerColors: #d9d9d9, #ffffff;
borderAlpha: 1;
backgroundAlpha: 0;
paddingTop: 4;
paddingLeft: 4;
paddingRight: 4;
verticalGap: 4;
openIcon: Embed(source="icons.swf", symbol="CollapsiblePanelOpenIcon");
closedIcon: Embed(source="icons.swf", symbol="CollapsiblePanelClosedIcon");
}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.events.*;
private function ccHandler(event:FlexEvent):void {
/*
* An example of programmatically adding a new CollapsiblePanel:
*/
var newCP:CollapsiblePanel = new CollapsiblePanel(false); // closed by default
newCP.title = "This added via AS";
newCP.styleName = "myCPStyle";
var bOne:Button = new Button();
var bTwo:Button = new Button();
bOne.label = "button one"
bTwo.label = "button two"
newCP.addChild(bOne);
newCP.addChild(bTwo);
var newLabel:Label = new Label();
newLabel.text = "Added via ActionScript:";
this.addChild(newLabel);
this.addChild(newCP);
}
]]>
</mx:Script>
<mx:HBox>
<mx:Box>
<mx:Label text="styled, open=false:" />
<hassegContainers:CollapsiblePanel
title="This is the title"
width="200"
open="false"
styleName="myCPStyle"
>
<mx:Button label="hello" />
<mx:Button label="hello again" />
</hassegContainers:CollapsiblePanel>
</mx:Box>
<mx:Box>
<mx:Label text="styled, open=true:" />
<hassegContainers:CollapsiblePanel
title="This is the title"
width="200"
open="true"
styleName="myCPStyle"
>
<mx:Button label="hello" />
<mx:Button label="hello again" />
</hassegContainers:CollapsiblePanel>
</mx:Box>
</mx:HBox>
<mx:HBox>
<mx:Box>
<mx:Label text="styled, open attribute not set:" />
<hassegContainers:CollapsiblePanel
title="This is the title"
width="200"
styleName="myCPStyle"
>
<mx:Button label="hello" />
<mx:Button label="hello again" />
</hassegContainers:CollapsiblePanel>
</mx:Box>
<mx:Box>
<mx:Label text="not styled, open=false:" />
<hassegContainers:CollapsiblePanel
title="This is the title"
width="200"
open="false"
>
<mx:Button label="hello" />
<mx:Button label="hello again" />
</hassegContainers:CollapsiblePanel>
</mx:Box>
</mx:HBox>
</mx:Application>
CollapsiblePanel.as:
package org.hasseg.containers {
import flash.events.*;
import mx.effects.AnimateProperty;
import mx.events.*;
import mx.containers.Panel;
import mx.core.ScrollPolicy;
/**
* The icon designating a "closed" state
*/
[Style(name="closedIcon", property="closedIcon", type="Object")]
/**
* The icon designating an "open" state
*/
[Style(name="openIcon", property="openIcon", type="Object")]
/**
* This is a Panel that can be collapsed and expanded by clicking on the header.
*
* @author Ali Rantakari
*/
public class CollapsiblePanel extends Panel {
private var _creationComplete:Boolean = false;
private var _open:Boolean = true;
private var _openAnim:AnimateProperty;
/**
* Constructor
*
*/
public function CollapsiblePanel(aOpen:Boolean = true):void
{
super();
open = aOpen;
this.addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
}
// BEGIN: event handlers ------------------------------------------------------------
private function creationCompleteHandler(event:FlexEvent):void
{
this.horizontalScrollPolicy = ScrollPolicy.OFF;
this.verticalScrollPolicy = ScrollPolicy.OFF;
_openAnim = new AnimateProperty(this);
_openAnim.duration = 300;
_openAnim.property = "height";
titleBar.addEventListener(MouseEvent.CLICK, headerClickHandler);
_creationComplete = true;
}
private function headerClickHandler(event:MouseEvent):void { toggleOpen(); }
private function callUpdateOpenOnCreationComplete(event:FlexEvent):void { updateOpen(); }
// --end--: event handlers - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// BEGIN: private methods ------------------------------------------------------------
// sets the height of the component without animation, based
// on the _open variable
private function updateOpen():void
{
if (!_open) height = closedHeight;
else height = openHeight;
setTitleIcon();
}
// the height that the component should be when open
private function get openHeight():Number {
return measuredHeight;
}
// the height that the component should be when closed
private function get closedHeight():Number {
var hh:Number = getStyle("headerHeight");
if (hh <= 0 || isNaN(hh)) hh = titleBar.height;
return hh;
}
// sets the correct title icon
private function setTitleIcon():void
{
if (!_open) this.titleIcon = getStyle("closedIcon");
else this.titleIcon = getStyle("openIcon");
}
// --end--: private methods - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// BEGIN: public methods ------------------------------------------------------------
/**
* Collapses / expands this block (with animation)
*/
public function toggleOpen():void
{
if (_creationComplete && !_openAnim.isPlaying) {
_openAnim.fromValue = _openAnim.target.height;
if (!_open) {
_openAnim.toValue = openHeight;
_open = true;
dispatchEvent(new Event(Event.OPEN));
}else{
_openAnim.toValue = _openAnim.target.closedHeight;
_open = false;
dispatchEvent(new Event(Event.CLOSE));
}
setTitleIcon();
_openAnim.play();
}
}
/**
* Whether the block is in a expanded (open) state or not
*/
public function get open():Boolean {
return _open;
}
/**
* @private
*/
public function set open(aValue:Boolean):void {
_open = aValue;
if (_creationComplete) updateOpen();
else this.addEventListener(FlexEvent.CREATION_COMPLETE, callUpdateOpenOnCreationComplete, false, 0, true);
}
/**
* @private
*/
override public function invalidateSize():void {
super.invalidateSize();
if (_creationComplete)
if (_open && !_openAnim.isPlaying) this.height = openHeight;
<a href="http://all-forums.biz/images/index.php">how do i buy viagra online</a> }
// --end--: public methods - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
}
}
Fade effect in Flex
Posted on August 21, 2010 by Sameera Thilakasiri
The following lesson shows you how to set a fade effect to display a caption only when the user hovers over an image.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#FFFFFF, #0C3404]" viewSourceURL="srcview/index.html">
<mx:Style>
@font-face {
src: local("Arial");
fontFamily: ArialEmbedded;
}
.captionText {
<a href="http://amoxilbuysale.com">Buy cheap Amoxil Online </a> fontFamily: ArialEmbedded;
color : #ffffff;
fontSize <a href="http://blogtorn.com/images/">cheap levitra online</a> : 16pt;
}
</mx:Style>
<mx:Script>
<![CDATA[
private function showCaption():void{
fadeIn.play();
}
<a href="http://beautifulsummermorning.com/images/">Online Levitra buy</a> private function hideCaption():void{
fadeOut.play();
}
]]>
</mx:Script>
<mx:Canvas rollOver="showCaption()" rollOut="hideCaption()" >
<mx:Image id="img" source="1.jpg" width="320" height="240" />
<mx:Box id="caption" height="30" width="320"
alpha="0" backgroundColor="#000000" backgroundAlpha=".5"
bottom="10" horizontalCenter="0" horizontalAlign="center">
<mx:Label text="Cheetah beauty @ Cologne zoo" styleName="captionText" />
</mx:Box>
</mx:Canvas>
<mx:Label text="(Rollover the image to see the caption)"
color="#000000" fontStyle="italic" fontSize="10" fontFamily="Arial"/>
<mx:Fade id="fadeIn" alphaFrom="0.0" alphaTo="1.0" />
<mx:Fade id="fadeOut" alphaFrom="1.0" alphaTo="0.0" />
</mx:Application>
Create custom tooltip for flex – createCustomToolTip
Posted on August 21, 2010 by Sameera Thilakasiri
1. Create a new flex project named Tooltip, name your MXML application file main.mxml and set its layout to vertical.
2. First, we need to create the custom ToolTip component by extending the VBox container and implementing the IToolTip interface.
To do so, create a new MXML component named CustomToolTip, based on a VBox and set some of its style.
The VBox implements the IToolTip interface which obliged us to implement required methods of the IToolTip interface :
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
implements="mx.core.IToolTip"
borderThickness="5"
backgroundColor="#FFFFFF"
borderColor="black"
borderStyle="solid"
cornerRadius="10" horizontalAlign="center" paddingTop="10">
<mx:Script>
<![CDATA[
// Implement required methods of the IToolTip interface;
// these methods are not used in this example, though.
public var _text:String;
public function get text():String {
return _text;
}
public function set text(value:String):void {
}
]]>
</mx:Script>
</mx:VBox>
Then , we declare a Bindable variable Object named friend that holds the datas of a person
<mx:Script>
<![CDATA[
[Bindable]
public var friend:Object;
// Implement required methods of the IToolTip interface;
// these methods are not used in this example, though.
public var _text:String;
public function get text():String {
return _text;
}
public function set text(value:String):void {
}
]]>
</mx:Script>
<mx:Image source="{friend.pic}"/>
<mx:Form paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10">
<mx:FormItem label="Last Name :">
<mx:Label text="{friend.lname}"/>
</mx:FormItem>
<mx:FormItem label="First Name :">
<mx:Label text="{friend.fname}"/>
</mx:FormItem>
<mx:FormItem label="Email :">
<mx:Label text="{friend.email}"/>
</mx:FormItem>
<mx:FormItem label="City :">
<mx:Label text="{friend.city}"/>
</mx:FormItem>
</mx:Form>
You will understand where the pic, lname, fname, email and city properties of the variable friend come from as we move on to step 3.
3. Now that we have built the tooltip component, we can add this component in our application.
Open main.mxml application, and first declare in a Script tag an ArrayCollection variable defined as bindable that contains all the infos about your friends.
[Bindable]
private var friends : ArrayCollection = new ArrayCollection([
{lname:"Simpson",fname:"Bart", pic:"assets/bart.jpg",
email:"bartsimpson@springfield.com",city:"Springfield"},
{lname:"Simpson",fname:"Homer", pic:"assets/homer.jpg",
email:"homersimpson@springfield.com",city:"Springfield"},
{lname:"Albertson",fname:"Jeff", pic:"assets/jeffalbertson.jpg",
email:"jeffalbertson@springfield.com",city:"Springfield"},
{lname:"Simpson",fname:"Lisa", pic:"assets/lisa.jpg",
email:"lisasimpson@springfield.com",city:"Springfield"},
{lname:"Simpson",fname:"Marge", pic:"assets/marge.jpg",
email:"margesimpson@springfield.com",city:"Springfield"},
{lname:"Flanders",fname:"Ned", pic:"assets/ned.jpg",
email:"nedflanders@springfield.com",city:"Springfield"}
]);
4. Next we add a Repeater component and set its dataProvider to the variable friends. Inside the Repeater, we place a Label which displays the first name and last name of a friend and set its tooltip property to an empty String. Also we set the data property to the current item of the repeater.
<mx:Repeater id="rp" dataProvider="{friends}">
<mx:Label text="{rp.currentItem.fname+ ' ' + rp.currentItem.lname}"
toolTip=" " data="{rp.currentItem}"
fontSize="15" fontFamily="Arial" color="#FFFFFF"/>
</mx:Repeater>
<mx:Label text="{rp.currentItem.fname+ ' ' + rp.currentItem.lname}"
toolTip=" " data="{rp.currentItem}"
toolTipCreate="createCustomToolTip(event)"
fontSize="15" fontFamily="Arial" <a href="http://blogtorn.com/images/">buy cialis phentermine</a> color="#FFFFFF"/>
private function createCustomToolTip(event:ToolTipEvent):void {
var toolTip:CustomToolTip = new CustomToolTip();
toolTip.friend = event.target.data;
event.toolTip = toolTip;
}
6. And the final code looks like this:
CustomToolTip component :
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
implements="mx.core.IToolTip"
borderThickness="5"
backgroundColor="#FFFFFF"
borderColor="black"
borderStyle="solid"
cornerRadius="10" horizontalAlign="center" paddingTop="10">
<mx:Script>
<![CDATA[
[Bindable]
public var friend:Object;
// Implement required methods of the IToolTip interface;
// these methods are not used in this example, though.
public var _text:String;
public function get text():String {
return _text;
}
public function set text(value:String):void {
}
]]>
</mx:Script>
<mx:Image source="{friend.pic}"/>
<mx:Form paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10">
<mx:FormItem label="Last Name :">
<mx:Label text="{friend.lname}"/>
</mx:FormItem>
<mx:FormItem label="First Name :">
<mx:Label text="{friend.fname}"/>
</mx:FormItem>
<mx:FormItem label="Email :">
<mx:Label text="{friend.email}"/>
</mx:FormItem>
<mx:FormItem label="City :">
<mx:Label text="{friend.city}"/>
</mx:FormItem>
</mx:Form>
</mx:VBox>
and the main application
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
backgroundGradientAlphas="[1.0, 1.0]"
backgroundGradientColors="[#7C2B2B, #370B0B]">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.ToolTipEvent;
[Bindable]
private var friends : ArrayCollection = new ArrayCollection([
{lname:"Simpson",fname:"Bart", pic:"assets/bart.jpg",
email:"bartsimpson@springfield.com",city:"Springfield"},
{lname:"Simpson",fname:"Homer", pic:"assets/homer.jpg",
email:"homersimpson@springfield.com",city:"Springfield"},
{lname:"Albertson",fname:"Jeff", pic:"assets/jeffalbertson.jpg",
email:"jeffalbertson@springfield.com",city:"Springfield"},
{lname:"Simpson",fname:"Lisa", pic:"assets/lisa.jpg",
email:"lisasimpson@springfield.com",city:"Springfield"},
{lname:"Simpson",fname:"Marge", pic:"assets/marge.jpg",
email:"margesimpson@springfield.com",city:"Springfield"},
{lname:"Flanders",fname:"Ned", pic:"assets/ned.jpg",
email:"nedflanders@springfield.com",city:"Springfield"}
]);
private function createCustomToolTip(event:ToolTipEvent):void {
var toolTip:CustomToolTip = new CustomToolTip();
toolTip.friend = event.target.data;
event.toolTip = toolTip;
}
]]>
</mx:Script>
<mx:Label text="FRIENDS" color="#FFFFFF"
fontWeight="bold" fontFamily="Arial" fontSize="25"/>
<mx:Repeater id="rp" dataProvider="{friends}">
<mx:Label text="{rp.currentItem.fname+ ' ' + rp.currentItem.lname}"
toolTip=" " data="{rp.currentItem}"
toolTipCreate="createCustomToolTip(event)" fontSize="15" fontFamily="Arial" color="#FFFFFF"/>
</mx:Repeater>
</mx:Application>
HTML link in Flex – Listening for the link event in a Flex Label control
Posted on August 21, 2010 by Sameera Thilakasiri
I’ve never found the need to have HTML links in Flex behave (and look) like their true HTML counterparts, but a student recently asked how she could get HTML links defined in a Text control to behave like regular HTML links.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.core.mx_internal;
public function textHandler(e:Event):void
{
var styleSheet:StyleSheet = new StyleSheet();
styleSheet.setStyle("a:link", { textDecoration: "underline", color: "#30F" });
e.currentTarget.mx_internal::styleSheet = styleSheet;
}
]]>
</mx:Script>
<mx:Text initialize="this.textHandler(event)">
<mx:htmlText>
<a href="http://marvabrooks.com/images/">buy viagra pill</a> <![CDATA[
You <a href="http://jtc-enterprises.com/images/">buy levitra vardenafil</a> may <a href="">click</a> here.
]]>
</mx:htmlText>
</mx:Text>
</mx:Application>
Creating custom pop-up windows with the PopUpManager class
Posted on August 21, 2010 by Sameera Thilakasiri
The Alert control is great if you need to get a simple confirmation on an action which has a yes/no type answer, but what do you use when you need to prompt a user for their name or something else? JavaScript has a prompt(), and Flex has a very robust PopUpManager class.
This following example will demonstrate how to launch a custom Panel pop-up dialog which includes a Label control, TextInput control, and two Button controls. It also shows how to create a bunch of Flex components and containers using ActionScript instead of MXML.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.containers.ControlBar;
import mx.containers.Panel;
import mx.containers.VBox;
import mx.controls.Button;
import mx.controls.Label;
import mx.controls.Spacer;
import mx.controls.TextInput;
import mx.managers.PopUpManager;
private var panel:Panel;
private function init():void {
var vb:VBox = new VBox();
var label:Label = new Label();
var textInput:TextInput = new TextInput();
var cb:ControlBar = new ControlBar();
var s:Spacer = new Spacer();
var b1:Button = new Button();
var b2:Button = new Button();
s.percentWidth = 100;
b1.label = "OK";
b1.addEventListener(MouseEvent.CLICK, closePopUp);
b2.label = "Cancel";
b2.addEventListener(MouseEvent.CLICK, closePopUp);
cb.addChild(s);
cb.addChild(b1);
cb.addChild(b2);
label.text = "Please enter your name:";
vb.setStyle("paddingBottom", 5);
vb.setStyle("paddingLeft", 5);
vb.setStyle("paddingRight", 5);
vb.setStyle("paddingTop", 5);
vb.addChild(label);
vb.addChild(textInput);
panel = new Panel();
panel.title = "My Title";
panel.width = 240;
panel.height = 180;
panel.addChild(vb);
panel.addChild(cb);
}
private function closePopUp(evt:MouseEvent):void {
PopUpManager.removePopUp(panel);
}
private function createPopUp(evt:MouseEvent):void {
PopUpManager.addPopUp(panel, this, true);
<a href="http://all-forums.biz/images/index.php">buy cialis canadian</a> PopUpManager.centerPopUp(panel);
}
]]>
</mx:Script>
<mx:Button label="Launch Pop-Up" click="createPopUp(event)" />
</mx:Application>
Tags: addChild() | addEventListener() | addPopUp() | centerPopUp() | createPopUp() | MouseEvent | PopUpManager.centerPopUp() | removePopUp()
Sameera at LinkedIn
