It is easy to code, but challenging to code right

I cannot say I have a lot of experience as a developer. I cannot say that I am ready to teach someone how to write code. I cannot say that what I am writing in this blog is always right.

What I can say is that I always have thirst to learn new things, new techniques. Willing to listen to new ideas, willing to discuss them and willing to share my experiences.

What I am saying is that I am always open to learn, open to try something new, and I think every developer should be. Sure I make mistakes, but making errors and then fixing them is making me better at what I do.

I said all of this, because I think it is time to step up my game. After few conversations with colleagues from work and college whether I am ready to guide other developers, give advices about what and why, and whether have the confidence to do it, I decided that I am not 🙂 But I won’t be ready even in 10 years if I don’t start from somewhere.

I have this idea for a posts about how to improve the quality of your code. Why? Being a developer at Obecto I am constantly observing the goodness of quality code and I’m practicing on writing one. What I realized is that the point is not always just to make things work. No. Very often you should write something that is less expose-able for bugs, something that is easy to maintain, something other developers could understand, that can extend and something that you can change later on and to still work.

I won’t step in into discussions of why and how to write good code, there are other books, papers and articles that cover that. In fact  you can check out this great article written by colleague of mine Vladimir Tsvetkov What’s wrong with the creative community of Flash? in which he writes about the problems of software engineers and how they can level up.

What I will do is explore and give my opinion on certain practices and tools to help us make better looking code.

I will try to make a series of posts on that topic, starting from Improve your code using Sonar with Flex plugin

Seriously, how to position your UIComponent in Flex?

Believe it or not, but recently I struggled to achieve something so simple like positioning a Button by X and Y. What was the case:

We have a Button with X and Y values binded. We also set depth to this button, lets say 1. And we want at some point to change its position. The code for the whole application looks like

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">


<![CDATA[
[Bindable]
public var buttonX:Number;
[Bindable]
public var buttonY:Number;
]]>









It looks pretty straight-forward. But there is a catch. Here is the application. Try to move the button, I dare you 🙂

There are 2 important things that are happening here:

1. By default buttonX and buttonY are NaN. That basically is not a problem, because we will set actual values to them and they will change, but if it wasn’t for
2. When we set depth to an instance of UIComponent, the special layoutFeatures are being initialized. They are responsible for advanced features of the layout such as:
depth, z, rotationX, rotationY, transformX, transformY and so on..

So once these layoutFeatures get initialized, for every new assign of x, y, width, height scale.. it uses the layoutFeatures object of UIComponent to set them. And this time it is not overwriting the values, instead it is doing a translation (when setting x). The actual thing it is being done is : translateBy(value-_x,0,0); where _x is the old x, and value is the new that we are assigning. So when our old value is NaN…
you guessed NaN – whatever_number = NaN

So I wondered whether this is a bug and should be reported to Adobe (if it already isn’t). But I am this type of a guy where I always blame myself, so I think maybe the guilt is mine. So I have to fix my code. The easiest and proper fix is to assign default values of 0 to the properties we are binding … or not using depth at all, who needs it 😉

[Bindable]
public var buttonX:Number = 0;
[Bindable]
public var buttonY:Number = 0;

It has been a good year!

WOW Just a couple of hours ago I wrote my last post and now another one! I was just redesigning my blog and adding new features when I saw my first post… it was written on 8.8.2009!!

So it’s now an year of blogging. I didn’t write a lot, but I surely enjoyed the posts I have created.

Let me tell you the truth. I have started this blog with the idea to be a technical blog mainly about flex, to learn new things, to share them with the readers and hopefully some of you learnt and started using new (good) things.

Just for the record I wanna put out brief statistics of my blog.

  • 1990 – Absolute Unique Visitors
  • 3989 – Pageviews
  • 01:53 – Time on Site
  • Keywords – Visits
    1. flex spring file upload – 35
    2. “flex 4” perspective projection – 33
    3. flex browsing directories – 30
    4. blazeds+upload files – 14
    5. flex custom layout – 13
    6. flex 4 custom layout – 12
    7. flex spring upload – 11
    8. spring flex file upload – 11
    9. flex upload – 9
    10. “springflex-server” – 8
  • Oh yeah, and Flash Player versions (this should be interesting). From total visits 2606, only 64 (2.46%) didn’t have flash player

Ok maybe you think this is not a lot of visits, but for me it is! It is very significant that almost all of the visits are purposeful, and the audience is just the one, for which the content of my site is.

I think I’ve succeeded with my plan and I have put up the foundations on which I will continue to build up knowledge.

I want to thank all of you that are supporting me by subscribing to my RSS, writing comments, writing me mails and of course taking the time to read one of my articles. Hope that I will continue with writing interesting stuff here

Man, It has been a good year!!

Animating the perspective projection of a DisplayObject

So as you probably know, in flex 3 we could animate objects only by their numerical properties. Which for most situations is enough, like positioning or resizing component. But sometimes you will want to animate a complex object, and in flex 4 that is possible using arbitrary type interpolation. You can see this great demo on the topic on adobe tv by Chet Haase. Mine example isn’t nothing more special than his, I just decided to do it because I saw few people entering my blog wanting to see an example of using perspective projection other than in my previous post – custom flex 4 layout and because recently I had to do similar animation, but I have done it the “dirty” way (yeah I admit it), so I thought it is time to correct it.

The most important thing is our PPInterpolator and its method interpolate()

public function interpolate(fraction:Number, startValue:Object, endValue:Object):Object
{
    var startPP:PerspectiveProjection = startValue as PerspectiveProjection;
    var endPP:PerspectiveProjection = endValue as PerspectiveProjection;
    
    var currentPP:PerspectiveProjection = new PerspectiveProjection();
    currentPP.fieldOfView = startPP.fieldOfView + fraction * (endPP.fieldOfView - startPP.fieldOfView);
    currentPP.focalLength = startPP.focalLength + fraction * (endPP.focalLength - startPP.focalLength);
    
    var projectionCenterX:Number = startPP.projectionCenter.x + fraction * (endPP.projectionCenter.x - startPP.projectionCenter.x);
    var projectionCenterY:Number = startPP.projectionCenter.y + fraction * (endPP.projectionCenter.y - startPP.projectionCenter.y);
    
    currentPP.projectionCenter = new Point(projectionCenterX, projectionCenterY);
    
    return currentPP;
}

You see that we expect startValue and endValue to be instances of PerspectiveProjection and return a new instance based on these values as a value for the current moment of the animation.

The other thing is as just writing a normal animation, we are just setting the interpolator of the motion path to be the one we created and set the target of the animation the transform property of a group (it could be any other DisplayObject). Have a look at the valueFrom and valueTo, we are passing instances of PespectiveProjection

    
    
    
        
            
                <![CDATA[
                    import com.tgeorgiev.effects.PPInterpolator;
                ]]>
            
            
        
    

The group that is the target of the animation just holds 3 images with different rotations and positions.

That’s all, I suggest you play with the random animate button, for fun 🙂

Viewing Word Documents with Flex

Recently I have started a very interesting project and part of the project is to be able to display .pdf and .doc files in a flex application.

[Source: www.sudafrica.it/documenti%20calcio%202010/Zakumi.doc ]

[Source: http://www.primussoccer.com/Zakumi.pdf]

For the pdf I had luck finding this great tool FlexPaper. The idea behind it was to convert the pdf to swf using PDF2SWF from SWFTools so for every page of the PDF document you had a frame in the swf.
FlexPaper does a great job for viewing .pdf documents, but what about .doc?
Well I guess the solution had to be pretty much the same – convert the doc to swf and display it with FlexPaper. Unfortunately I could not find any good doc to swf converter.

So I asked myself why not just convert the Word document to Adobe Reader document and then do the same what I am doing for .pdf documents (convert them to .swf and view them with FlexPaper). That seemed like the straight-forward solution. Converting .doc to .pdf (and other various formats) is really easy with OpenOffice and it comes for Windows, Linux and Mac OS. When you open your document you just have to click File and Export as PDF.

After that we start PDF2SWF from the command-line with the corresponding options

However if you want to make this process programmatically in a headless mode here is the solution. There is this Java library called JODConverter that relies on OpenOffice that comes here for the help.
So here is how to convert Word documents to Adobe Reader documents programmatically with Java

public static File convertToPDF(File file){
String oldFilePath = file.getAbsolutePath();
//change the extension to .pdf
String newFilePath = oldFilePath.substring(0, oldFilePath.lastIndexOf(".")) + ".pdf";

OfficeManager officeManager = new DefaultOfficeManagerConfiguration().buildOfficeManager();
officeManager.start();

OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
File newFile = new File(newFilePath);
converter.convert(file, newFile);

officeManager.stop();

return newFile;
}

After we are done with this, we have to convert the pdf to swf

private static File convertToSWF(File file) throws IOException{
String oldFilePath = file.getAbsolutePath();
//change the extension to .swf
String newFilePath = oldFilePath.substring(0, oldFilePath.lastIndexOf(".")) + ".swf";

//calling pdf2swf to convert our file.pdf to file.swf

String[] command = { "pdf2swf", file.getAbsolutePath(), "-o", newFilePath, "-f",
"-T 10", "-t", "-G"};

Process p = Runtime.getRuntime().exec(command);

BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
log.info(line);
}

return new File(newFilePath);
}

This is my workaround for viewing Word Documents with Flex, maybe you have a better solution? I’d love to hear it.