Archive for the ‘Tech Tips’ Category

Why do I use Frameworks and Design Patterns?

Sunday, August 13th, 2006

I have been designing and developing for over 10 years now. I have never worked on same technology for more than 2 years. This gave me oppurtunity to learn new stuff but at the same time it made my job very challenging. Time to market is still a key differentiating factor and in this competitive world where for once money and skills are available in abundance, speed is something which can make you stand tall among the crowd. So how do you develop FAST making sure your design is extensible, your application can handle unlimited change requests, you can scale to increasing demand, you can modify look and feel every x months and lot more :) . How do you design a critical system in less time and still be confident that you are moving in the right direction?

Nothing can replace experience, now if you do not have experience yourself how can you take advantage of other people’s experience? This is where frameworks and Design patterns come.

Framework helps you start fast and Design patterns help you design right and fast. You are confident because you are using a tested and proven solution. When you have to do more in less time you have to pay more attention to issues that can become big later. Design patterns help you identify those too, it lets you identify those issues. Another side-effect of using frameworks / patterns is that it helps you communicate better. This is really helpful when you are working in a distributed enviornment. If both teams have some knowledge of both lots of times its very easy to communicate/discuss a technical problem and their solutions. Frameworks provide you some common set of features that you need in every application like session handling, logging, error/exception handling, i18n, templating etc. Most of the frameworks implement common design patterns.

more on some of the frameworks we use Struts, symfony, RCP

Enabling Java support in PHP

Saturday, March 11th, 2006

Note: This article was written after implementing Java (1.4.2_06) support on PHP 5.1.2 (cli) (built: Feb 28 2006 06:21:15)

As written on php.net there are two ways of integrating:

  1. Integrate PHP into Java
  2. Integrate Java into PHP

We will be talking about “Integrating Java into PHP”. This again can be done in two ways

  1. Re-compile php using –with-java=$JAVA_HOME (Ample of documentation availbale on net on how to compile php)
  2. Use php-java-bridge.

Enabling php-java-bridge is fairly easy and recommended as you wont have to compile php which might be already customised.

  • Get source for php-java-bridge from sourceforge
  • Decompress
  • cd to “php-java-bridge” source folder
  • phpize
  • ./configure –with-java=$JAVA_HOME
  • make
  • make install

This will install libraries in some folder eg /usr/lib/20050922 copy JavaBridge.jar java.so libnatcJavaBridge.so to php modules folder eg. /usr/lib/php/modules restart apache (service httpd restart) Look at out of phpinfo() and confirm that you see “java running” You can either write a php file and access it from your browser or quickly type echo ” | php | fgrep java at command prompt

Starting RCP application in your desired locale

Thursday, March 9th, 2006

This tip answers following questions:
1. How do you I start an RCP application in locale of my choice
2. I am starting my application in say ja_JP but still all default widgets (ok, cancel button etc), preference pages, help, update screens etc are coming in English.

Answer to both these questions is pretty simple once you know :)
In order to start an application in your desired locale you can either give a command line option (-nl=locale) or you can specify it in config.ini file (osgi.nl).
For more runtime options search for “Runtime Options” in plugin development guide.

Now answer to second question is “install language pack” and make sure they are included under dependencies of your feature/plugin. for ex to get prefence page or update screens in your language you would need org.eclipse.ui.workbench.nl1 and org.eclipse.update.ui.nl1 fragments.

Custom XUL titlebar that responds to events (almost)

Thursday, January 19th, 2006

<titlebar> tag does not have the standarf minimise, maximise or close buttons and the reference says “Any elements inside the titlebar do not receive events.” so what do you do if you want a custom titlebar with the standard buttons?

After some hair pulling Shyamal came up with a nice simple idea (which looks so obvious now :) )

<hbox>

<image id=”logo” />

<menuseparator />

<titlebar id=”myPersonalTitleBar”/>
<menuseparator />

<image id=”closeImg”/>

</hbox >

Simple huh!!

Extending FireFox menu bar

Thursday, January 19th, 2006

You can easily find information on how to create a new toolbar but if you want to add to an existing toolbar looking at the browser code was the best resource I could find. This is what it takes to add your controls on FireFox’s menu bar.

Browser.xul uses following tag hierarchy for toolbar.

<toolbox id=”">

<toolbar id=”">

<toolbaritem id=”">

Rest of the tags

</toolbaritem>

</toolbar>

</toolbox>

So if you want to extend an existing toolbar you just need to know its “id” and write your own xul. For example to appendĀ  to the menubar I would write:

<toolbar id=”toolbar-menubar”>

<toolbaritem id=”">

my stuff

</toolbaritem>

</toolbar>

Following are the ids used in firefox, names are pretty self-explanatory

  • toolbar-menubar
  • nav-bar
  • PersonalToolbar
  • status-bar

By the way, in case you want to right align your additions <toolbarspring/> will come handy.

Identifying and displaying hyperlinks in XUL

Thursday, January 12th, 2006

The other day I needed to identify URLs within a block of text and create hyperlinks out of them to be displayed in a XUL description element. First I looked for any XUL control that could do this for me. But there was none. So I turned to javascript to identify the URLs in the block of text while I decided to use the HTML namespace to add HTML tags in the XUL.

To identify URLs, I used a regular expression from the book Mastering Regular Expressions by Jeffrey Friedl. I split the block of text on spaces, parenthesis and hypens and used the regular expression to check if each token is a URL. While finalizing the regular expression, I also took help from this entry by Mark Szlazak. Co-incidentaly, it also refers to the book Mastering Regular Expressions. With this done, I was able to evaluate if each token was a URL.

Now I had to convert these URLs into hyperlinks and make them clickable in XUL. I was using a description element for showing the whole text. Since the text could be long and should wrap to multiple lines, I chose not to use the value attribute but rather add the text as value of the description tag. To do so dynamically, here’s what I did:

  • First I added the correct html namespace by including the following attribute to the window element:

xmlns:html=”http://www.w3.org/1999/xhtml”

  • Then I added the description element:

var descNode = document.createElement(”description”);

  • Subsequently add the block of text (remember I split it into tokens) one token at a time

var txtNode = document.createTextNode(token);
descNode.appendChild(txtNode);

  • The above works for adding plain text. Now to add a hyper link, the following code snippet was used to add tokens that were identified as hyperlinks:

var linkNode = document.createElementNS(”http://www.w3.org/1999/xhtml”,”a”);
linkNode.setAttribute(”href”, token);
linkNode.setAttribute(”target”, “_blank”);
link.setAttribute(”style”, “color: rgb(0,0,255); text-decoration:underline”);
var txtNode = document.createTextNode(token);
linkNode.appendChild(token);
descNode.appendChild(linkNode);

And here is how it looks:

Hyperlinks

If we were to print the XUL DOM tree, it would look like:

<vbox id=”box_id_1″ flex=”1″ style=”overflow: auto”>
<description>
This is normal text, but this link: <html:a xhref=http://www.example.com/>http://www.example.com/</html:a>
is a URL that would be converted to a hyper link.
</description>
</vbox>