Tuesday, July 15, 2008

help me raise money for battle-knight , its a game :)

Please click the link below and help me earn 3 silver coins.

http://s6.battleknight.co.uk/index.php?loc=hire&ref=MTMwNzAx

Wednesday, June 04, 2008

Pasting Code in Blogspot

I hate writing html by hand, and to write html to display java/as/flex code in a page, is very painful. I was searching for a tool which would generate the html for me, escape all symbols to html tags. And i found this, and it works like a charm. I thought i would list it here, would be helpful for a trespasser.

I used this tool to paste code for my blog "programmtic skins in Flex Builder" and it worked great. Just a matter of 5 seconds.

Wednesday, May 21, 2008

Programmatic Skinning in Flex Builder 3

Flex is the "HOT" technology these days. Apart from the technology, performance and other advantages behind it, for end users it is very pleasing to look at. When it comes to look and feel, pretty much everything can be done using Javascript too, but flex was developed particularly for "Rich" applications, so it makes creating rich user interfaces easy for the developer. Not to mention, its more efficient and more maintainable for the developer. Flex Builder for Flex is pretty much the Visual Studio for ASP.NET (lot of differences and lot of similarities here). Flex Builder really makes working with Flex easier. And by default, adobe provides a lot of components, which you can use to layout using Flex Builder. Using these components is fine, until you need a different component. Flex also provides support to create custom components and skin them programatically (there are also other ways).

There are a lot of articles and blogs online which talk about how to write programmatic skins, but these skins can only be seen when the Flex application actually runs. What do I need to do if I want to see the applied programmatic skin in the Flex Builder Design view, so i can play around with my layout?

There are some steps that need to be followed. And what better way there is to show the examples? I wish this blogging site supported code snippets, but it doesnt , so i will provide links to code.

The Application mxml is ProgrammaticSkinning.mxml and the skin classes are FlatColorRectangleSkin.as , FlatColorRoundedRectangleSkin.as, GradientRectangleSkin.as and NodeSkin.mxml.

The custom class is Node.as and the style sheet is styles.css. The code in these classes is straight forward, so i wont explain them all. But Node.as and NodeSkin.mxml are special.

1. Node.as should be put in a different library project, if this class exists in the same project, the skins wont be rendered in Flex Builder design view. I dont know the reason for this, if anybody knows, please let me know.

2. Node class in order to support skins, has to override updateDisplayList method, get the SkinClass, create an instance of the class, set the skin Name , set stylename on the skin to this(for the skin to inherit all style properties from the component), set the size of the skin to the component or appropriate (we want the size of skin to be grater than 0, to be able to see it), and add the skin as a child.

3. If anyone wants to use Degrafa for skinning, look at NodeSkin.mxml. This is my favourite. We can use Degrafa tags to define our tags.

That said, we can create our custom components, style them in any way we want, use them in Flex Builder, drag-n-drop in Flex Builder design view and play with the layout.

Also, here is a component you can extend form if you want your component to be skinnable.


package charts
{
import flash.display.DisplayObject;
import flash.events.MouseEvent;

import mx.core.IFlexDisplayObject;
import mx.core.UIComponent;
import mx.effects.Zoom;
import mx.effects.easing.Bounce;
import mx.styles.ISimpleStyleClient;

public class SkinnableComponent extends UIComponent
{
private var zoomAll:Zoom = new Zoom();

public function SkinnableComponent()
{
scaleX = 0.5;
scaleY = 0.5;
addEventListener(MouseEvent.ROLL_OVER, doZoom);
addEventListener(MouseEvent.ROLL_OUT, doZoom);

zoomAll.zoomWidthTo = 1;
zoomAll.zoomHeightTo =1;
zoomAll.zoomWidthFrom = 0.5;
zoomAll.zoomHeightFrom = 0.5;
}

protected override function updateDisplayList(unscaledWidth:Number,
unscaledHeight:Number):void
{
showSkin("upSkin");
}

protected function showSkin(skinName:String):void
{
var skinClass:Class = Class(getStyle(skinName));
var skin:IFlexDisplayObject = IFlexDisplayObject(getChildByName(skinName));

if (!skin)
{
if (skinClass)
{
skin = IFlexDisplayObject(new skinClass());
skin.name = skinName;
(skin as ISimpleStyleClient).styleName = this;
addChild(DisplayObject(skin));
skin.setActualSize(unscaledWidth, unscaledHeight);
}
}

}

protected function doZoom(event:MouseEvent):void
{
if (zoomAll.isPlaying)
zoomAll.reverse();
else
zoomAll.play([event.target], event.type == MouseEvent.ROLL_OUT ? true : false);
}

}
}


Comments, advices and help are always welcome.

Tuesday, May 20, 2008

Comet Style Requests and continuations

Let me start the introduction to this post by quoting an excerpt from an article "

So what makes these apps special? What makes them different from other things that might at first glance appear similar? Fundamentally, they all use long-lived HTTP connections to reduce the latency with which messages are passed to the server. In essence, they do not poll the server occasionally. Instead the server has an open line of communication with which it can push data to the client.

The architecture relies on a view of data which is event driven on both sides of the HTTP connection"

The above style of communication is now widely used in Flex, but it is not "long-lived Http". So is this style of communication possible in Http? Sure with Comet style requests we can have long lived requests so that ajax clients, apart form polling, can also enjoy the "server-push" data. But what about the effects of this long lived Http connections? How does the server (servlet container) handle these requests?

It is considered that "thread-per-request" is a better model than "thread-per-connection", when serving thousands of clients simulataneously (not for serving max requests per second). But with long lived connections, the "thread-per-request" model doesnt hold good. Consider a thousand ajax clients haing long lived connections toa server. With
"thread-per-request" model, the server will run out of threads to process new requests. So a new approach is needed on the server side, if Comet style requests are to be supported scalably.

One of the new approaches is "Continuations". If continuations is a new term for any, whats the best site to check for its meaning other than wikipedia? This link gives an excellent introduction to continuations (atleast to me, i didnt find any page which describes the concept in such accurate simple words) .

Now the greedy question is "Can i use it in Java? Can i use it to develop web applications in Java?". Yes! Jetty supports continuations on the server side. For more information, visit their wiki site.

The basic idea is to have long lived connections, but to used threads effeciently. Continuations seem to support that, they only allocate threads to these long-lived connections when an event occurs, threads don't waste time waiting for blocking IO etc.

I also found an article on continuations in Rife and scalability with Terracota.
Also Scala, has support for continuations. But more on that in an other post.


Saturday, May 03, 2008

FlexSprite

FlexSprite is defined like this "public class FlexSprite extends Sprite". As far as Sprite goes, the docs quote that

" The Sprite class is a basic display list building block: a display list node that can display graphics and can also contain children.

A Sprite object is similar to a movie clip, but does not have a timeline. Sprite is an appropriate base class for objects that do not require timelines. For example, Sprite would be a logical base class for user interface (UI) components that typically do not use the timeline.

The Sprite class is new in ActionScript 3.0. It provides an alternative to the functionality of the MovieClip class, which retains all the functionality of previous ActionScript releases to provide backward compatibility."

It would be handy to know the difference between movieclip, timelines and Sprite, but thats for an another post.

And FlexSprite adds extends Sprite to override the toString() method. It overrides the toString() method to return a string indicating the location of the object within the hierarchy of DisplayObjects in the application.

It iterates through the parent heirarchy untill it reaches the stage object and returns the object location heirarchy.


Tuesday, March 04, 2008

Migration to Flex 3

declaration of style conflicts with previous version !

If you see the above error when compiling your flex 3 application, then it is most probably because you are compiling your project linked to a swc file that is compiled in flex 2. To solve the above problem, find out the swc's or library project's your project is depending on, and compile those projects and files using flex 3.

Monday, February 25, 2008

Flex MXML, Functions and Nested Functions

In my previous post Anonymous Functions in Flex, I mentioned about the inconsistent behaviour of nested functions. I decided to dvelve deeper and see if it was a bug or if i was missing something. I started out with the language specification, which was very disappointing, still, had some information on how functions in actionscript behave.

The following lines are taken from the specification document :

/**********************************************************************************

Function objects have a property named prototype whose value is used to initialize the intrinsic delegate property of the objects it creates. The prototype property has a default value of a new instance of the class Object. Building on the previous example:

function A() { this.x = 10 }
function B() {}
B.prototype = new A
var o = new B
trace(o.x) // traces 10

The value of o is an instance of B, which delegates to an instance of A, which has a property named x with value of 10.

Constructor methods inside of a class are also used to create objects. But, unlike constructor functions, constructor methods create objects with a set of fixed properties (traits) associated with its class and a delegate that is also an instance of its class.

class A {
var x
function A() { this.x = 10 }
}
var o = new A
trace(o.x) // traces 10

There are some subtle differences between the preceding example and the one involving a function constructor:

  • x is a fixed property of each instance of A rather than a dynamic property.
  • A.prototype is an instance of A rather than an instance of Object.
  • The expression A(expr) does not call the function A defined in class A. It results in an explicit conversion of the value of expr to the type A.

Class methods are functions that are defined with the static attribute inside of a class definition. A class method cannot be used as a constructor and does not define the this reference. Class methods are in the scope of the class object in which they are defined.

Instance methods are functions that are defined without the static attribute and inside a class definition. Instance methods are associated with an instance of the class in which they are defined. Instance methods can override or implement inherited class or interface methods and always have a value bound to this.

The value of this in an instance method is the value of the instance the method belongs to.
************************************************************************************/
Having said that, consider the example below : (The examples below have syntax errors, i had to do so, r else the page would not display these tags properly, please correct them, if you r copy/pasting)


mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

mx:Button id="ti" click="getText()" label="uhuh"/>
mx:Script>
ADD CDATA TAG HERE WHEN COMPILING
import mx.core.UIComponent;
import mx.controls.Alert;

private var global_var:String = "global_var";

private function getText():void
{
Alert.show("Level 1 : "+global_var+"\n\n"+this.ti.label);
var inner1:Function = function():void
{
Alert.show("Level 2 : "+global_var+"\n\n"+this.ti.label)
var inner2:Function = function():void
{
Alert.show("Level 3 : "+global_var+"\n\n"+this.ti.label);
}
inner2();
};
inner1();
}

]]>
/mx:Script>
/mx:Application>


In the above example, getText() is a instance method, and the flex compiler sets this method's this reference to the Application instance. So, Level 1 is good, but the functions inner2 and inner3 are not instance methods and their this reference is not set to anything, we have to set it.
And how do we do it? We do it by calling the function, specifying the prototype object/target object for that function using call method. Below is a working example.


mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

mx:Button id="ti" click="getText()" label="uhuh"/>
mx:Script>
ADD CDATA TAG HERE WHEN COMPILING
import mx.core.UIComponent;
import mx.controls.Alert;

private var global_var:String = "global_var";

private function getText():void
{
Alert.show("Level 1 : "+global_var+"\n\n"+this.ti.label);
var inner1:Function = function():void
{
Alert.show("Level 2 : "+global_var+"\n\n"+this.ti.label)
var inner2:Function = function():void
{
Alert.show("Level 3 : "+global_var+"\n\n"+this.ti.label);
}
inner2.call(this);
};
inner1.call(this);
}

]]>
/mx:Script>
/mx:Application>


Notice inner2.call(this) and inner1.call(this)? But how come the inner functions access global_var variable, any explanation? In the Adobe Documentation, programming_actionscript3.pdf, Under section "Understanding variable scope", these lines are specified :

/*******************************************************************************
ActionScript variables, unlike variables in C++ and Java, do not have block-level scope. A
block of code is any group of statements between an opening curly brace ( { ) and a closing
curly brace ( } ). In some programming languages, such as C++ and Java, variables declared
inside a block of code are not available outside that block of code. This restriction of scope is
called block-level scope, and does not exist in ActionScript. If you declare a variable inside a
block of code, that variable will be available not only in that block of code, but also in any
other parts of the function to which the code block belongs.
*******************************************************************************/

The above answers our question, but the specification talks nothing about variable scopes. Its a relief to know there is a neat solution for the nested functions problem.