Archive for February, 2009

24
Feb
09

Flex “Workspace in use…” error – arg!

Most annoying thing about Flex Builder. If I work for a while in Flex, then quit and try to start Flex again it says “Workspace in use or cannot be created, blah blah”.

I can then either restart the pc  or use task manager to quit process “Javaw”.  I’m running FB 3.2 on Win XP (all patches and service packs thank you). It’s not a huge hassle to quit that process, it just seems like an unnecessary bug in a 3 point 2 version of a major application.

Has anyone found a workaround or perhaps knows what the cause is? A search showed that I’m not the only one annoyed by it, but there doesn’t seem to be a fix yet.

Advertisements
23
Feb
09

Web Services are suprisingly easy with Flex

Last time I had to work with web services, ActionScript2 was still hot stuff. Handling complex data was an absolute pain, mainly because of the woefully primitive XML implementation of AS2. Manually looping through children’s lists is not my idea of fulfilling programming tasks. Not that I realised how bad it was until AS3 showed up with E4X. “You mean I can just read the XML like an object or array? No way, that’s sci-fi!”

It was with a great deal of apprehension that I started on a new project that required integration with an external web service. But it turned out to be an absolute breeze. A small, hitherto ignored, menu item called “Data: Import Web Service (WSDL)” turned out to be very close to magic and became my new best friend.

Import Web Service takes an URI to the WSDL (web service definition language), loads and parses it and then lets you choose the methods you want to generate code for. It creates complete proxies with typed data classes for both in- and output. You can reload and regenerate classes if the web service changes, and you can add and remove methods at any time. All the generated code is accessible and well documented – although all is well documented with examples in the Flex help, I didn’t need more than the comments in the generated code.

I’m not claiming to have made a big discovery here, after all it’s part of the standard Flex Builder. But it just works so much better than I expected. Web Services have gone from being a royal pain to the easiest and most structured way of communicating with the server.

17
Feb
09

Dynamic classes for settings objects

Sometimes real world programming requires solutions that aren’t lovely  OOP-type code. For instance if you have a base class that is extended by a number of children, it’s not always possible to foresee all the types of arguments you need to send them, but you still need to define an interface.

In old-school AS2 code, we often used untyped objects as parameters for constructors. Bad OOP, but as AS2 doesn’t have typing, and the editor we used (SEPY) didn’t show us the arguments for methods and constructors, the code was more readable that way.

In AS3, classes are sealed by default, meaning that no attributes or methods can be added at run-time. This saves memory and increases performance. If you set a class as dynamic, it behaves like classes did in AS2 – you can add whatever you like at run-time.

We found that a variant of this AS2 technique was handy for those indeterminate settings objects. But instead of just passing a generic Object, we can create a dynamic class and define the settings that the base class uses and have those typed strictly. And we still allow future implementations to add their parameters. The downside of dynamic classes is that they use more memory than sealed (default) classes. But as they only live for a short time, it’s not a big real-world problem.

An example class from our framework. Notice the ‘dynamic’ declaration before ‘class’.

canDelete, pageStyle and pageLinkedToNextCount are types and cannot be set with other types. The parseRaw method is a way to take a generic object and insert all its attributes into this class.

package no.papirfly.msp.ptp.templates
{
    /**
     * Defined pageTypeSettings as used in <code>BaseTemplate.initPageTypes</code>  
     * @param canDelete user can delete page. Default is false
     * @param pageStyle
     * @param pageLinkedToNextCount how many pages will be locked to this. 1 = next page is locked to this.package no.papirfly.msp.ptp.templates
     *
     */        
    public dynamic class PageTypeSettings
    {
        public var canDelete:Boolean
        public var pageStyle:int
        public var pageLinkedToNexCount:uint;

        public function PageTypeSettings(canDelete:Boolean = false, pageStyle:int = 0, pageLinkedToNexCount:uint = 0)
        {
            this.canDelete = canDelete
            this.pageStyle = pageStyle
            this.pageLinkedToNexCount = pageLinkedToNexCount
        }
       /**        @private         */
        public function parseRaw(o:Object):void {
            for (var item:String in o) {
                this[item] = o[item];
            }
            if (o.canDelete == null) canDelete = false
        }
    }
}

We can instantiate this class:

var mySettings:PageTypeSettings = new PageTypeSettings(true);

and add new attributes that aren’t declared in the class:

mySettings.specialColorForMyPage = 0xFF00FF;

We lose strict typing, but it’s manageable when you use it for a few classes. If needed, the PageTypeSettings could be extended to type more attributes.