Consider a scenario in which the requirement is just to create a Window with a Panel that can resize itself by clicking on the button +/- that is located in the top-right corner of the panel. One click should minimize the panel’s height to 20 pixels, and a subsequent one should maximize to 100 pixels. How efficiently do you write the code?
There are many ways to do so.
First method:- With the usage of packages
package
{
import mx.containers.Panel;
public class UpDownPanel extends Panel
{
private var isPanelMinimized:Boolean;//whether panel is minimised or maximized
public function UpDownPanel()
{
super();
}
//getter & setter for the states
public function get minimized():Boolean{
return isPanelMinimized;
}
public function set minimized(state:Boolean):void{
isPanelMinimized=state;
}
//resizing function
public function resizeMe():void{
if (minimized){
minimized=false;
height=maxHeight;
} else {
minimized=true;
height=minHeight;
}
}
}
}
And the mxml is…
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:customPanel="*">
<customPanel:UpDownPanel id="minmaxPanel" title="Height adjuster"
width="100%" height="100" headerHeight="20" minHeight="20" maxHeight="100"/>
<mx:HBox width="100%" horizontalAlign="right" paddingRight="2">
<mx:Label id="minButton" text="-"
fontSize="16" fontWeight="bold"
width="20" height="17"
click="resizePanel(minmaxPanel)"/>
</mx:HBox>
<mx:Script>
<![CDATA[
private function resizePanel(value:UpDownPanel):void{
if (value.minimized){
minButton.text="-";
value.resizeMe();
} else {
minButton.text="+";
value.resizeMe();
}
}
]]>
</mx:Script>
</mx:Application>
Good amount of coding, isn’t it?
Second method:- Simple with a few lines of code only!
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Panel id="minmaxPanel" title="Height adjuster"
width="100%" height="100" headerHeight="20"
layout="absolute"/>
<mx:HBox width="100%" horizontalAlign="right" paddingRight="2">
<mx:Label id="minButton" text="-"
fontSize="16" fontWeight="bold"
width="20" height="17"
click="{if (minButton.text=='+')
{
minButton.text='-';
minmaxPanel.height=100;
}else
{
minButton.text='+';
minmaxPanel.height=20;
}
}"/>
</mx:HBox>
</mx:Application>
That was a nice piece….read the rest too.
Third method:- Using states.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns="*">
<mx:states>
<mx:State name="minimized">
<mx:SetProperty target="{minmaxPanel}" name="height" value="20"/>
<mx:SetProperty target="{minButton}" name="text" value="+"/>
<mx:SetEventHandler target="{minButton}" name="click" handler="{this.currentState = ''}"/>
</mx:State>
</mx:states>
<mx:Panel id="minmaxPanel" title="Height adjuster"
width="100%" height="100" headerHeight="20"
layout="absolute"/>
<mx:HBox width="100%" horizontalAlign="right" paddingRight="2">
<mx:Label id="minButton" text="-"
fontSize="16" fontWeight="bold"
width="20" height="17"
click="{this.currentState = 'minimized'}"/>
</mx:HBox>
</mx:Application>
What if more controls are there on each state?
Fourth method:- with dynamic classes.
package
{
import mx.containers.Panel;
public dynamic class UpDownPanel extends Panel
{
}
}
And the mxml is…
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:customPanel="*">
<customPanel:UpDownPanel id="minmaxPanel" title="Height adjuster"
width="100%" height="100" headerHeight="20" minHeight="20" maxHeight="100"/>
<mx:HBox width="100%" horizontalAlign="right" paddingRight="2">
<mx:Label id="minButton" text="-"
fontSize="16" fontWeight="bold"
width="20" height="17"
click="resizePanel(minmaxPanel)"/>
</mx:HBox>
<mx:Script>
<![CDATA[
private function resizePanel(value:UpDownPanel):void{
if (value.minimized){
minButton.text="-";
value.minimized = false;
value.height = value.maxHeight;
} else {
minButton.text="+";
value.minimized = true;
value.height = value.minHeight;
}
}
]]>
</mx:Script>
</mx:Application>
Now, that piece of code is extensible, right? Wondering what is dynamic classes? Look here.
Fifth method:- A proper MXML with scripting.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns="*">
<mx:Component className="UpDownPanel">
<mx:Panel>
<mx:Script>
<![CDATA[
[Bindable]
public var isMinimised:Boolean = false;
]]>
</mx:Script>
</mx:Panel>
</mx:Component>
<UpDownPanel id="minmaxPanel" title="Height adjuster"
isMinimised = "false"
width="100%" headerHeight="20" minHeight="20" maxHeight="100"
height="{minmaxPanel.isMinimised?minmaxPanel.minHeight:minmaxPanel.maxHeight}"/>
<mx:HBox width="100%" horizontalAlign="right" paddingRight="2">
<mx:Label id="minButton" text="-"
fontSize="16" fontWeight="bold"
width="20" height="17"
click="{minmaxPanel.isMinimised=!minmaxPanel.isMinimised}"/>
</mx:HBox>
</mx:Application>
Finally it was simple, right?
Which of these methods do you think is good in terms of performance/stability/readability/etc? Or if you have another way of doing it, do post it in comments!
Also there are advantages/disadvantages for each method. For example, while using states, it may not become manageable if there are lots of controls in each of the state. Like wise if you can find any, please put it in comments.