Posts Tagged ‘ Flex ’

wootHackwoot ?

Posted in Flex, Opinions on September 13th, 2010 by sinica – Be the first to comment Tags: , ,

If you know what  wootHackwoot means,we would like to ask a question: Why is so buggy the “Classes” tab from “Flex Library build Path” ? Why we should manually check those radios  when we are updating code from the source control? I imagine that “Include all classes from all source projects” is an easter egg that will popup with some nice sexy clips when we click for the millionth time? I’m not upset,just annoyed a bit. Flash Builder and Flex are such nice tools but  some features within Eclipse integration are just staying in our way. Adobe,could you fix these annoyances please?

Format the text inside a Spark RichEditableText control

Posted in Best practices, Flex on August 26th, 2010 by bogdan – 5 Comments Tags: , , , , , , , , , , ,

Lately I’ve been working on a code editor, based on Flex 4.1, that should be similar to a modern
editor with the so needed syntax highlighting feature. In order to highlight reserved words, strings
and comments, the text is transformed into tokens, which contain useful information for coloring
such as its position in text, length and the type, used to decide which color should be used to
highlight the token.

Everything went well until formatting RichEditableText to highlight the tokens, which was tricky.
My first implementation was something like the code below :

	for (var i:int = 0; i < tokens.length; i++)
	{
		var tlf:TextLayoutFormat = new TextLayoutFormat();
		tlf.color = tokens[i].color;
		richEditableText.setFormatOfRange(tlf,
			tokens[i].beginIndex, tokens[i].endIndex);
	}

This implementation was way too slow: if I pasted a text with around 125 of words to be colored, it would take up to 19 seconds. That was unbelievable, so I started searching about the performance of setFormatOfRange method. By reading some forums and the source code for RichEditableText class, I noticed that this method triggers update on all the elements of textFlow, which happened for each of those 125 words and dropped so much the speed of formatting and displaying the text.

So to solve this issue, all the format operations should be wrapped into a single one to be executed, and when this is done, trigger the update only once.

Fortunately, the Text Layout Framework provides an API for doing just this through flashx.textLayout.operations package. So I wrapped each format operation into an ApplyFormatOperation, which was added to a CompositeOperation object and executed the latter one.
Below is a sample implementation for my improved solution :

	var compositeFormatOperation:CompositeOperation =
		new CompositeOperation();
	for (var i:int = 0; i < tokens.length; i++)
	{
		var operationState:SelectionState =
			new SelectionState(richEditableText.textFlow,
				tokens[i].beginIndex, tokens[i].endIndex);
		var leafFormat:TextLayoutFormat = new TextLayoutFormat();
		leafFormat.color = tokens[i].color;
		var formatOperation:ApplyFormatOperation =
			new ApplyFormatOperation(operationState,leafFormat,null);
		compositeFormatOperation.addOperation(formatOperation);
	}
	var success:Boolean = compositeFormatOperation.doOperation();
	if (success)
	{
		richEditableText.textFlow.flowComposer.updateAllControllers();
	}

Notice that after the composite operation is executed, I update the textFlow only once.
This improved solution dropped the time from 19 seconds to 0.3 seconds, an 63x improvement that makes
my code editor usable again.

I didn’t use an EditManager because it dispatches TextOperationEvent.CHANGE, which I listen only for text changes (insert, paste, cut, delete) on my RichEditableText. So if text changed, I would set color on some tokens by calling applyFormat on EditManager, which would send a new TextOperationEvent.CHANGE and I would ended up in a infinite loop.

I’m sure there might be better ways of doing this (I just haven’t discovered yet), so I’m open to new suggestions and I hope this post would serve as a starting point for others having similar task with the new text controls inside Flex Spark framework.

For more information on Text Layout Framework you may have a look at this PDF file

Flex unit testing without a Serial Number for Flash Builder 4 PREMIUM

Posted in Flex, Q&A on August 17th, 2010 by bogdan – 1 Comment Tags: , , , , ,

Flash Builder 4 comes in 2 flavors: Standard Edition and Premium. With Standard Edition you can’t run unit tests inside Eclipse. If you’re reading this post then I guess you’re looking for an easy and free way of unit testing your flex application. There is a easy way of doing this. All you have to do is create a project, add some swc libraries and set up unit testing.

For a Flex 4.1 environment, you’ll need to add flexunit-core-flex-4.1.0 and flexunit-uilistener-4.1.0 libraries to your project, write test classes and suite class to include the tests, and in the end you should write a little code to Default Application file.

Here is a sample unit testing project you can look over.

The whole post was meant to be short and provide a simple, but reliable way of unit testing in Flex.
For more details and more advanced features you can check out the official website.

DictionaryCollection – a mix between Dictionary and ArrayCollection

Posted in Flex, Quark framework on August 16th, 2010 by bogdan – 2 Comments Tags: , , ,

One day I came across a task which involved using some sort of collection that was bindable
and could store and retrieve data like a dictionary does. And soon I got a new task : to build
such a data type.

Practically I needed a mix between Dictionary and ArrayCollection, so my first idea was to
extend Proxy, in order to access and retrieve data using square brackets, and after that to
create similar methods to ArrayCollection. But after looking over the source code of ArrayCollection,
I’ve noticed it inherits Proxy, due to ListCollectionView inheritance, which saved me from the classic
‘reinventing the wheel’ and led me to improving the wheel instead.

My plan was to modify the methods of ArrayCollection so it adds/removes links to the added/removed
objects and this way they could be retrieved later.It was pretty straightforward to override the methods
and add/remove references to items,which were stored in a Dictionary. It was also easy because I didn’t have to override each method as some of them are reused. For example, addItem  just calls addItemAt.

Besides this basic functionality, I thought it would be useful to add one more feature : adding and
removing references to items based on given properties. Let’s say you add objects with
quantity, price and label properties, using the plain addItem method and you still want to retrieve
them using square brackets. You can do this, but first you have to decide which properties should be
the keys that references to the object. Below is an example for more clarity.

// here I declare an empty DictionaryCollection and set the label property as a key field
var a:DictionaryCollection = new DictionaryCollection(null,"label");
a.addItem({label: "Bread", price: "2", quantity: "1"});
a.addItem({label: "Butter", price: "3", quantity: "2"});
a.addItem({label: "Tea bags", price: "1", quantity: "4"});
a["Jam"] = {label: "Jam", price: "4", quantity: "1"};
// now I can retrieve each object based on its label
trace(a.getItemByKey("Butter").price);   // 3
trace(a["Tea bags"].quantity);             // 4
trace(a["Jam"].label); 			     //	Jam

I hope this code snippet explains pretty well how a DictionaryCollection can be used and you can download it following the link below. I also included the test class for unit testing.

DictionaryCollection

As a conclusion, I’m happy that reading the source code of Flex saved me hours of work and helped me to build a custom data type that should blend seamlessly with the framework.