Snippet – Disabling paste on spark TexInput

It’s a common requirement for e-mail or password inputs – the second input should not be copy pasted from the first one, to make sure it’s what the user wanted to put in. When using spark my Flex 3 method didn’t work, so just in case someone needs to do the same thing, here’s the code for spark components:

<s:TextInput
   change="if (event.operation is PasteOperation)
   (event.target as SkinnableTextBase).text = '' "

/>

It will also work with a TextArea.

Spell checking in Flex (with support for multiple languages)

A while ago the way to go, if you needed spell checking in Flash or Flex was Grant Skinners Spelling Plus Library (which still has a lot to offer, especially for Web applications, as it’s much smaller than the Adobe library). But, for a while now, Adobe has been working on their own project, called Squiggly, which I’m going to use.

The Squiggly library

You can download the library files from Adobe Labs. After uncompressing the .zip file you’ll find three files in the libs folder. The one you need for this tutorial is “AdobeSpellingUIEx.swc” – it’s the Flex 4 version for simple integration, so just put it in your libs folder. The “AdobeSpellingUI.swc” is what you would use if your target would be a Flex 3 application. You should use only one of them as putting both in your libs might cause troubles. The third file is used when working with more advanced options given by the library.


Continue reading →

Dark themes for Flash Builder

Spending a lot of time in front of a bright LCD monitor can be a real pain for the eyes. But there’s no good reason why you should keep the default white background behind your code. After a search I found that some people have already prepared configurations that will allow you to have bright text on a dark background.

Quick list of themes

fb-zenburnishhttp://www.noiseandheat.com/blog/
Dark theme for Flash Builder 4http://www.likidu.com/blog/
The Typography of My Flash Builder http://kevinsuttle.com/

After testing them for a while I’m currently using fb-zenburnish.

Installing a theme

First download the file, and unzip if necessary. You should have an .epf file. Now go to the Flash Builder, and from the File menu select Import->Other. (you might want to backup your current settings before importing the new ones, just choose Export instead of Import and it should be a straight forward process)
Import -> Other
Than choose Preferences from the General node and click Next.
Preferences
Than just browse for the epf file and click Finish.

Your brand new color scheme should be set.

Follow up – 2011-02-16

You can now find a lot of themes at www.eclipsecolorthemes.org

From photoshop design to a Flex 4 skin

When looking at some old project folders I’ve found one of the first skins I’ve created while starting with the Flex 4 skinning model, and I thought it might be an interesting case to look at, source code is included at the end of the post. It’s based on a really nice design done by Adrian Pelletier. What we’ll try to do is create a button which looks more or less like one of his button designs:

What makes it a good design is not only the way it looks but also the way the Photoshop file is created – it’s mostly based on shapes and layer effects which are easy to translate to Flex skin code.

Styling a Flex 4 Button

The Flex 4 skinning model is different from the one used in Flex 3, to keep the introduction short, let’s just say it’s now much easier to create programmatic skins, and you can do it with newly introduced MXML tags. If you ever used Degrafa to skin your components it’s should be fairly familiar, if not please check the example code. The basic usage is straight forward – you need to create a mxml file and you can start putting the new tags to design the skin.

Let’s start with the shadow behind the button, as you can see on the picture above it has 3 parts – a gradient fill, a stroke and an inner shadow, and all of them thrown on a shape of a rounded rectangle. Let’s translate this to graphic tags. First of we need that rounded rectangle, so we define a Rectangle with the corner radius using the new tags:

<s:Rect radiusY="24"
        radiusX="24"/>

That’s how easy it is to create shapes using the new syntax. Next we need to define a fill, which according to the Photoshop design is a linear gradient starting from color 0x0f2835 and ending with color 0×153445. We also need an inner drop shadow on it, so let’s add it. We also need a stroke above all this, so we need to add another rectangle which will not have the drop shadow on it, and will define that gradient stroke, just like the design. A few minutes later we have:

<s:Rect radiusY="24"
            radiusX="24"
            x="0"
            y="0"
            width="{this.width}"
            height="{this.height}"
        >
        <s:filters>
            <s:DropShadowFilter inner="true"
                                distance="0"  
                                strength="1"/>
        </s:filters>
        <s:fill>
            <mx:LinearGradient rotation="90">
                <mx:GradientEntry color="0x0f2835"/>
                <mx:GradientEntry color="0x153445"/>
            </mx:LinearGradient>
        </s:fill>
    </s:Rect>
    <s:Rect radiusY="24"
            radiusX="24"
            x="0"
            y="0"  alpha="0.8"
            width="{this.width}"
            height="{this.height}" >
        <s:stroke>
            <mx:LinearGradientStroke rotation="90"
                                     weight="2"
                                     pixelHinting="true">
                <mx:GradientEntry color="0x102a39"/>
                <mx:GradientEntry color="0x20516c"/>
            </mx:LinearGradientStroke>
        </s:stroke>
    </s:Rect>

It’s not the shortest listing, but remember that I’m only copying the settings from the design. The final flash result looks like this:

It’s almost a easy to create the rest of the design, take a look a the working button with some Flex magic thrown in to get nice transitions between states (viewsource enabled), and after the jump, I’ll quickly point to different parts of the design which are used in the ButtonSkin.mxml

Result

What makes a skin – looking at ButtonSkin.mxml

First of you should define a component for which the skin is designed.

 [HostComponent("spark.components.Button")]

Next you need to define all the states which are defined by the host component (you can check this info on livedocs) which is done in this code:

    <s:states>
        <mx:State name="up"/>
        <mx:State name="down"/>
        <mx:State name="over"/>
        <mx:State name="disabled"/>
    </s:states>

After that’s done you also need to put the parts of the skin that the host components assumes your skin will have (this is called skinning contract and I’ll write a post about it when I have some time). In the case of Button we need to display a single string (the button label as stated in livedocs) we’re going to use a Label for that and give it an id of labelDisplay according to the contract.

<s:Label left="17" right="17" color="0xffffff"
    height="{this.height}" verticalAlign="middle" id="labelDisplay" >

Than we can start putting in the graphics, using the same tags we used while creating the shadow. The only additions is that the values of the gradients change depending on the state, thanks to the new state syntax in Flex 4 it’s much more readable – there’s a different color when the button is in an over state (color.over) and a different one when the button is in the down state (color.down)

<mx:GradientEntry id="ge1" color="0x387dda"
        color.over="0x3e81de" color.down="0x88bafe" />

To add the smooth color fades between states we also define a bunch of transitions which work on our gradient entries.

<s:Transition   fromState="up" toState="over" autoReverse="true" >
   <s:AnimateColor targets="{[ge1, ge2, ge3, ge4]}"  duration="250"/>
 </s:Transition>

Feel free to download the source code and play with it, hopefully it will be a good starting point.

Download source code.

How not to use “services_config.xml”

The most common way to use RemoteServices in flex is by creating a separate file to keep the configuration, and adding the “-services” switch to the compiler arguments. It’s a good way to do it, but not always, and is especially painful when explaining remoting to someone. But you can skip the whole file and configure the same thing using MXML tags. I’ll take a sample “services_config” file (this one is for zend_amf) and translate it to MXML.

<?xml version="1.0" encoding="UTF-8"?>
<services-config>
    <services>
        <service id="amfphp-flashremoting-service"
        class="flex.messaging.services.RemotingService"
        messageTypes="flex.messaging.messages.RemotingMessage">
            <destination id="zend">
                <channels>
                    <channel ref="library"/>
                </channels>
                <properties>
                    <source>*</source>
                </properties>
            </destination>
        </service>      
    </services>  
    <channels>  
         <channel-definition id="library"
            class="mx.messaging.channels.AMFChannel" >
         <endpoint uri="http://localhost/librarySample/amf.php"
            class="flex.messaging.endpoints.AMFEndpoint" />
        </channel-definition>        
    </channels>
</services-config>

translates to:

<mx:ChannelSet id="chanelSet" >
      <mx:AMFChannel
      uri="http://localhost/librarySample/sampleamf.php" />
</mx:ChannelSet>

<mx:RemoteObject  
      channelSet="{chanelSet}" >       
 </mx:RemoteObject>

So what you need to do is create a ChannelSet, put a channel inside (in this case AMFChannel) and set its uri to the one you would usually put as endpoint uri in the config file.
Once you do that you can start using your RemoteObjects without the services_config.

Of course you should add the rest of the things you usually put inside a remote object. For people who would like an example:

<mx:RemoteObject  
        channelSet="{chanelSet}"
        source="myServerSideClassName"  
        destination="zend" >
        <mx:method
            name="serverSideFunctionName"
            result="someResultFunction()" />           
</mx:RemoteObject>
/