Advanced Visual Component in ActionScript

<?xml version="1.0"?>
<!-- asAdvanced/ASAdvancedMainModalTextEvent.mxml -->

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:MyComp="myComponents.*" >

    <mx:Script>
      <![CDATA[

        import flash.events.Event;

        private function placementChangedListener(event:Event):void {

          myEvent.text="placementChanged event occurred - textPlacement = "
              + myMT.textPlacement as String;
        }

      ]]>
    </mx:Script>

    <MyComp:ModalText id="myMT"
        textPlacement="left"
        height="40"

        placementChanged="placementChangedListener(event);"/>
    <mx:TextArea id="myEvent" width="50%"/>    

    <mx:Label text="Change Placement" />

    <mx:Button label="Set Text Placement Right"
        click="myMT.textPlacement='right';" />
    <mx:Button label="Set Text Placement Left"
        click="myMT.textPlacement='left';" />

</mx:Application>


package myComponents
{
    import mx.core.UIComponent;
    import mx.controls.Button;
    import mx.controls.TextArea;
    import flash.events.Event;
    import flash.text.TextLineMetrics;

    [(name="change", type="flash.events.Event")]

    [(name="textChanged", type="flash.events.Event")]
    [(name="placementChanged", type="flash.events.Event")]

    public class ModalText extends UIComponent
    {

        public function ModalText()

        {
            super();
        }

        private var text_mc:TextArea;
        private var mode_mc:Button;

        [Embed(source="Modal2.swf", symbol="blueCircle")]

        public var modeUpSkinName:Class;

        [Embed(source="Modal2.swf", symbol="blueCircle")]
        public var modeOverSkinName:Class;

        [Embed(source="Modal2.swf", symbol="greenSquare")]

        public var modeDownSkinName:Class;

        override protected function createChildren():void

        {
            super.createChildren();

            if (!text_mc)
            {

                text_mc = new TextArea();
                text_mc.explicitWidth = 80;
                text_mc.editable = false;
                text_mc.text= _text;
                text_mc.addEventListener("change", handleChangeEvent);
                addChild(text_mc);
            }

            if (!mode_mc)
            {   mode_mc = new Button();
                mode_mc.label = "Toggle Editing Mode";
                // If you do not have skins available, 

                // comment out these lines.
                mode_mc.setStyle('overSkin', modeOverSkinName);
                mode_mc.setStyle('upSkin', modeUpSkinName);
                mode_mc.setStyle('downSkin', modeDownSkinName);
                mode_mc.addEventListener("click", handleClickEvent);
                addChild(mode_mc);
            }

        }

        /*** f) Implement the commitProperties() method. ***/
        override protected function commitProperties():void

        {
            super.commitProperties();

            if (bTextChanged) {

                bTextChanged = false;
                text_mc.text = _text;
                invalidateDisplayList();
            }

        }       

        override protected function measure():void {
            super.measure();

            // Since the Button control uses skins, get the 

            // measured size of the Button control.
            var buttonWidth:Number = mode_mc.getExplicitOrMeasuredWidth();
            var buttonHeight:Number = mode_mc.getExplicitOrMeasuredHeight();

            // The default and minimum width are the measuredWidth 

            // of the TextArea control plus the measuredWidth
            // of the Button control.
            measuredWidth = measuredMinWidth =
                text_mc.measuredWidth + buttonWidth;

            // The default and minimum height are the larger of the 

            // height of the TextArea control or the measuredHeight of the
            // Button control, plus a 10 pixel border around the text.
            measuredHeight = measuredMinHeight =
                Math.max(mode_mc.measuredHeight,buttonHeight) + 10;
        }

        override protected function updateDisplayList(unscaledWidth:Number,
                unscaledHeight:Number):void

        {
            super.updateDisplayList(unscaledWidth, unscaledHeight);         

            // Subtract 1 pixel for the left and right border,
            // and use a 3 pixel margin on left and right.
            var usableWidth:Number = unscaledWidth - 8;

            // Subtract 1 pixel for the top and bottom border, 

            // and use a 3 pixel margin on top and bottom.
            var usableHeight:Number = unscaledHeight - 8;

            // Calculate the size of the Button control based on its text.

            var lineMetrics:TextLineMetrics = measureText(mode_mc.label);
            // Add a 10 pixel border area around the text.

            var buttonWidth:Number = lineMetrics.width + 10;
            var buttonHeight:Number = lineMetrics.height + 10;
            mode_mc.setActualSize(buttonWidth, buttonHeight);       

            // Calculate the size of the text

            // Allow for a 5 pixel gap between the Button
            // and the TextArea controls.
            var textWidth:Number = usableWidth - buttonWidth - 5;
            var textHeight:Number = usableHeight;
            text_mc.setActualSize(textWidth, textHeight);

            // Position the controls based on the textPlacement property.

            if (textPlacement == "left") {
                text_mc.move(4, 4);
                mode_mc.move(4 + textWidth + 5, 4);
            }

            else {
                mode_mc.move(4, 4);
                text_mc.move(4 + buttonWidth + 5, 4);
            }           

            // Draw a simple border around the child components.

            graphics.lineStyle(1, 0x000000, 1.0);
            graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
        }

        /*** i) Add methods, properties, and metadata. ***/
        // The general pattern for properties is to specify a private
        // holder variable.
        private var _textPlacement:String = "left";

        // Create a getter/setter pair for the textPlacement property.

        public function set textPlacement(p:String):void {

            _textPlacement = p;
            invalidateDisplayList();
            dispatchEvent(new Event("placementChanged"));
        }

        // The textPlacement property supports data binding.
        [(event="placementChanged")]
        public function get textPlacement():String {

            return _textPlacement;
        }

        private var _text:String = "ModalText";
        private var bTextChanged:Boolean = false;

        // Create a getter/setter pair for the text property.

        public function set text(t:String):void {

            _text = t;
            bTextChanged = true;
            invalidateProperties();
            dispatchEvent(new Event("textChanged"));
        }

        [(event="textChanged")]
        public function get text():String {

                return text_mc.text;
        }

        // Handle events that are dispatched by the children.
        private function handleChangeEvent(eventObj:Event):void {

                dispatchEvent(new Event("change"));
        }

        // Handle events that are dispatched by the children.
        private function handleClickEvent(eventObj:Event):void {

                text_mc.editable = !text_mc.editable;
        }
    }

}

Leave a comment

Alice Gomez With Madalyn Blanchett and Marilyn Rife song downloads Angra new mp3 Chris Hinze and Gyuto Monks music to download City Boy song downloads Curtis Mayfield-The Impressions mp3 music Dere album Jack Dejohnette - Pat Metheny - Herbie Hancock - Dave Holland buy mp3 John Acquaviva and Madox Ft Tommie Sunshine mp3 downloads Leon Tomas music Love Spirals Downwards music macka b and the royale roots band mp3s Melodica music Michail Anthimos Dissos all mp3 oceanlab featuring justine suissa mp3 search Pat Monahan album Peter Cincotti mp3 music Planet P Project mp3 Sly and Robbie mp3 online Straight Outta Junior High english mp3 Vibrasphere downloadable music 18 Summers mp3 downloads Alexi Delano and Tony Rohr song downloads Alicja Janosz english mp3 Arrested Development download David Amo And Julio Navas Vs Maw mp3 songs Dresden Dolls new mp3 Forgotten Woods music Foxhole best mp3 Ictus mp3 downloads Igor S VS Lady Brian instrumental Ina Deter song downloads Jan Garbarek - Ustad Fateh Ali Khan new mp3 John Travolta get mp3 Jorge Palma review Kyaw Kyaw Naing best mp3 Laika mp3s Octex get mp3 Pete Johnson mp3 search Rocio Durcal download Santa Geracao 11 downloads Silent Voices music search Slagerij Van Kampen mp3 search Stadio download mp3 Steve Angello mp3 song Welladay pop Accused download mp3 Barkays VS Double Dose downloadable music Blind Dog downloads Cafe Quijano music download Calibre top mp3 Crowbar mp3 downloads Dies Ater mp3 online Dosh download songs Feelies album Good For Cows dowland James Yorkston music search Jan Garbarek and Gary Peacock downloads Jive Bunnys mp3 songs Last Resort mp3 music Liberto and Mihalis Safras music downloading Marisa Monte downloadable music Night of the Brain all mp3 Nishat Khan downloads Rezerection download mp3 Robin Williamson and Mike Heron mp3 songs Satriani, Malmsteen, Vai mp3 downloads Sengir downloads Squad 21 instrumental Swingle Singers And Modern Jazz Quartet top mp3 Waylon Jennings mp3 online Aceyalone top mp3 Afrika Bambaataa mp3 online Akcent pop Ancient Winds music downloading Anthropolatri albums Dave Ellesmere and Polder pop Dizzy Mizz Lizzy music to download Dreams Of Sanity dowland God Dethroned song downloads Grendel best mp3 Jacka And Lee Majors instrumental Jessica Williams download songs Kevin Rudolf dowland La Petite Bande, con. Gustav Leonhardt top mp3 Little Wolf Band mp3 online Nautilis mp3 song Night in Gales music Orkiestra Osmego Dnia dowland Outlines mp3 song Popa Chubby download mp3 Radiation 4 mp3 song Smash TV mp3 search Sorin Pavelescu albums Tatsuhiko Asano all mp3 Tom Russell music download Ustad Imrat Khan and his sons review Usura song downloads Waterdown new mp3 Wendy James albums Xenomorph get mp3