Thursday, January 22, 2009

Creating a splash screen effect with Dojo

Many professionally designed applications provide a splash screen while the main application loads in background, so that I thought I would learn how to do it using Dojo 1.2.

Starting from an external blog entry with a working example I had to spend a few extra minutes on the Dojo documentation to find the right fade-in call in Dojo 1.2, which turned out to be “dojo.anim”.

The final technique is summarized in this modified version of the original blog entry:

function show_main_gui() {
    dojo.byId(“splash”).style.display = “none”;
    dojo.anim(“real”, { opacity: 100 }, 100);
}
dojo.addOnLoad(show_main_gui);



<div id="splash" style="z-index: 100; width: 100%; position: absolute; top: 200px">
<div align="center">
<img src="images/throbber.gif" />
</div>
</div>



<div id="real" style="filter: progid:dximagetransform.microsoft.alpha(opacity=1); opacity: 0.01" dojotype="LayoutContainer">
...
</div>


The original entry also lists this nifty generator for the “loading” icon.

Monday, January 19, 2009

Getting your numbers straight when programming in Java

While going through a cursory code inspection, some common patterns emerged above the throng. A few of them tend to trick even the more experienced programmers, less on the account of their skill than on the account of being syntactically too small to spot:

Automatic type promotion

Programmers tend to rely on the compiler to automatically promote smaller types into larger types, which works well **during the assignment** from one to another. If you are multiplying two smaller types that may result in a number that needs a larger type to be represented, **there is no automatic type promotion**.

int appleValue= 55;
int appleCount = 2000 * 1000 * 1000;
long appleNetWorth …;
appleNetWorth = appleValue * appleCount; // WRONG RESULT, TYPE OVERFLOW!
appleNetWorth = (long) (appleValue * appleCount); // WRONG RESULT, TYPE OVERFLOW!
appleNetWorth = (long) appleValue * appleCount; // Nefariously correct!
long appleNetWorth = (long)appleValue * (long)appleCount; // Verbosely correct!

Use “Number.toString” to convert numbers to strings

Whether you should be always using java.text.NumberFormat depends on your particular purpose at the time, but otherwise use the static “toString” method for the corresponding numeric type. Avoid constructs like this:

String appleValueStr…;
appleValueStr = "" + appleValue; // BAD: It works...at a cost…
appleValueStr = Integer.toString(appleValue); // GOOD

This kind of construct has two obvious problems, the first being that I will be mad at you due to the second problem, which is the wasteful creation of an additional String to contain the result of the concatenation.

Avoid constructing Number instances.

Number is the base type for Integer, Long, Float, and other numeric types. Creating a new number using its constructor inhibits JRE optimizations where the most common numbers (-128 to 128) are cached.

Integer appleValueObject = ...;
appleValueObject = new Integer(appleValue); // BAD: Prevents memory optimization
appleValueObject = Integer.valueOf(appleValue); // GOOD: May save you some memory for small numbers

Monday, January 12, 2009

Configuring Windows Live Writer to post on IBM Communities

IBM Communities is an interesting experiment on bridging its internal networks of subject-matter experts and its external network of customers and partners (disclaimer, I am an IBM software engineer.)

As a heavy internal blogger sold on the concept of blogging as a personal empowerment tool over centrally controlled knowledge distribution, I was initially skeptical of the notion of corporate sponsored blogging, but I accepted the challenge of giving it a go for a a few months.

Enough with the philosophical observations, which I reserve for my other blog, the sourcepatch is about coding hacks and technical tips. In this entry I wanted to help people setup their favorite off-line editors to work with IBM Communities, starting first with Windows Live Writer (in fairness, employee-employeer loyalty and all that, WLW is a remarkable tool) .

Setup steps

  1. I started with the latest version: 14.0.8050.1202 en with the intention of pointing at my newly created blog, the Energy Scrolls, at https://www-951.ibm.com/blogs/energyscrolls.
  2. Select Blogs –> Add blog account…
  3. In the “What blog service do you use?”, select “Other blog service”
  4. In the next dialog, use “https://www-951.ibm.com/” for the Web address of your blog and provide your username and password, click on “Next”
    image
  5. In the “Select blog type” dialog, select “Atom Publishing Protocol” as the type of blog and use “https://www-951.ibm.com/blogs/api” as the service document URL, click on “Next”.
     image
  6. Select your blog for the account being created (you can create other accounts for eventual additional blogs) and click on “Next”.
    image
  7. I could not get it to detect the blog theme or upload images, but I pointed to the “Energy Scrolls – Media Entries” when prompted for a place to upload my files, then click on “Next”
  8. Just pick a nickname of your choice in the next panel and you are done.
    image

Two more disclaimers:

  1. When I post an entry and check the “View in browser after publishing”, the browser does not open the entry correctly as it does for posting to other blogging platforms.
  2. I could not get the Blogger and MetaWeblog APIs to work.

Now onto figuring out how to do the same with w.bloggar and ScribeFire.