Daily Archive for December 17th, 2007

Kampuang nan jauh di mato

Great play, sir!

Here is the lyric:

Kampuang den jauh di mato
Gunung sansay baku liliang
Takana jo kawan – kawan den lamo
Sangke basuliang – suliang

Pan duduk nyo nan elok
Nan suko bagotong royong
Sakit sanang samo-samo di raso
Den takana jo kampuang.

Takana jo kampuang
Ibu, ayah, adiak sadonyo
Rasu ma imbau-imbau den pulang
Den takana jo kampuang.

Java Tips: MessageFormat

Writing a working code is not enough. There is so wide spectrum from where we can distinguish good programmer and a bad one. This is one example of it.

Suppose we need to write a String that concatenate several predefined substrings with several variables. What do we do? Obviously, the most natural thing is to write:

String c = "The book with title " + title + " is sold with price "
              + price + " to " + buyername;

Nothing wrong with the code but obviously if the sentence build many times, you already know that it’s not that efficient. The use of StringBuffer or StringBuilder in Java 5 can make it much better.

String c = new StringBuilder("The book with title ").append(title)
                    .append(" is sold with price ").append(price)
                    .append(" to ").append(buyername);

Great! But not for future development. The code looks cryptic and hard to be changed. The use of MessageFormat is the best solution for such problem (which unfortunately, also the most inefficient).

String result = MessageFormat.format(
     "The book with title {0} is sold with price {1} to {2}",
      new Object[] {title, price, buyername});

This is even better since we can customize the format of the price, for example:

String result = MessageFormat.format(
     "The book with title {0} is sold with price {1,number,currency} to {2}",
      new Object[] {title, price, buyername});

JavaScript – Applet communication

I don’t like applet… and I don’t like javascript. But some time you just need to use them. It is bad if it happens but even worse if you have to make them communicate to each other.

Java Script call applet function
JavaScript can call applet function. Suppose we have an applet with name “appletsample” and main class have a public method name register()

public Main extend Applet {

   public void start() {
      -- applet code --
   }

   public void register() {
      -- do something here --
   }

}

We can call register() from javascript like this:

   document.appletsample.register();

Just remember that the code that is called from applet is working on different context, which means:

  1. We have to work with security issue
  2. If the code generate UI, it will use standard Look&Feel of the OS

To work around the security issue, there is two suggestions can be chosen:

  • javascript only change a boolean variable, and create one thread that check this variable all the time, and if there is some changes, trigger the real action which need security permission.
    public Main extend Applet implements Runnable {
    
       boolean generate = false;
    
       public void start() {
          Thread thread = new Thread(this);
          thread.start();
          -- applet code --
       }
    
       public void register() {
          generate = true;
       }
    
       public void run() {
          while (true) {
              if (generate) {
                 -- do something here --
              }
          }
       }
    
    }
    
  • wrap the code that needs security permission by java.security.PrivilegedAction
    Example:

    public Main extend Applet {
    
       public void start() {
          -- applet code --
       }
    
       public void register() {
          AccessController.doPrivileged(new PrivilegedAction() {
             public Object run() {
                -- do something here --
             }
          });
       }
    
    }
    

The first solution, however, takes a lot of CPU processes, where in a while, will also solve problem with Look&Feel. The second solution is not solving the Look&Feel problem, but uses CPU efficiently.

Call javascript from applet
We need classes from netscape. This is located in file plugin.jar, you can find it in JRE-DIR/lib/. After you find the file, include it with your application classpath. Then write code in your applet like this:

JSObject win = (JSObject) JSObject.getWindow(this);
(String) win.eval("alert('test javascript');");

Change alert(‘test javascript’); to whatever javascript code you want to call from applet.

InterCityExpress

ice.jpg

For a long time, Germany inter city train connection was mainly operated by InterCity train. When Japan introduced Shinkansen and France produced TGV, some industrial companies were also planning to build new faster train for Germany. The proposal was approved with one condition… it should able to use old railways so that Germany didn’t have to put a lot of money to build new railway (which is the case for Shinkansen and TGV).

This approach is proofed to be very effective. Germany had already famous for its dense railways and using the approach, in a short time, the train became the most popular rapid mass transport between cities in Germany. In the same time, more and more better railways are build throughout Germany to achieve the velocity’s limit of train. Currently, its top speed can only be achieved on the line between Frankfurt-Cologne and Nuremberg-Munich, where it can run until 300 km/h.

ICE Network

Over the time, several types of the train has been developed. InterCityExpress is picked as the name and the model is named as ICE 1, ICE 2, and ICE 3. Several variations of the models are also developed afterward.

Most of the train has signal amplifier for mobile phone so people can still use their phone despite of the speed of the train. In the modern model, every two seats in ICE has access to the electrical power so its very convenient for businessmen or anybody that want to use laptop or charge battery. And recently, several ICEs are also equipped with WLAN courtesy of T-Mobile.

This train is also considered as very attractive for many train fans. This can be concluded from many fan pages about the train over the internet. Every information about the train can also be read in the fan pages. Two of the most interesting sites are http://www.ice-fanpage.de/ and http://www.railfaneurope.net/pix/de/electric/emu/ICE/pix.html.

Having memory problem with Firefox? Use Flock!

Several months ago when I first tried Flock, I was pretty disappointed. The browser is buggy and even has bigger memory problem as Firefox. Not long after the installation, I uninstall it. And I never intended to try it one more time. Not even when it escaped the beta. A social browser just doesn’t get my attention.

This is somehow changed when I see the post in Techcrunch. According to the post:

Then came Flock 1.0. I’d never been a Flock fan before, always believing it to be nothing more than Firefox with plugins (Flock is based on the Firefox engine). Having watched the demo at TechCrunch 40 I downloaded the beta of Flock 1.0 and surfed away without incident. Some how the folks at Flock had tweaked the underlying Firefox engine to stop the memory issues.

So I give Flock another try. And yes, it seems to me that it is working better than Firefox. The previous post shows an image how much memory taken by Flock compared to Firefox.

Great work Flock team. Sadly I won’t still use your social features. I certainly will use Firefox more often if the solution can be implemented in Firefox, but I guess it will just be a dream since Firefox’s memory leak is a feature.