BBCode

Bulletin Board Code or BBCode is a lightweight markup language used to format posts in many message boards. The available tags are usually indicated by square brackets surrounding a keyword, and they are parsed by the message board system before being translated into a markup language that web browsers understand—usually HTML or XHTML.

Merantau – Imam Syafii

Orang berilmu dan beradab tidak akan diam di kampung halaman
Tinggalkan negerimu dan merantaulah ke negeri orang
Merantaulah, kau akan dapatkan pengganti dari kerabat dan kawan
Berlelah-lelahlah, manisnya hidup terasa setelah lelah berjuang

Aku melihat air menjadi rusak karena diam tertahan
Jika mengalir menjadi jernih, jika tidak, kan keruh menggenang

Singa jika tak tinggalkan sarang tak akan dapat mangsa
Anak panah jika tak tinggalkan busur tidak akan kena sasaran

Jika matahari di orbitnya tidak bergerak dan terus diam
Tentu manusia bosan padanya dan enggan memandang

Bijih emas bagaikan tanah biasa sebelum digali dari tambang
Kayu gaharu tak ubahnya seperti kayu biasa
Jika didalam hutan

Author : Imam Syafii
(Dikutip dari novel Negeri 5 Menara. By: Ahmad Fuadi)

Eclipse Helios’ Java Formatter Improvement

A new milestone of Eclipse 3.6 just released with a long new and noteworthy list.

Instead of discussing what’s new from this release, I want to discuss a set of improvement from its Java Formatter. I still love Eclipse’s Java Formatter and with these new improvements, I even love it more. This improvements unfortunately didn’t make their way to list of new and noteworthy, hence the reason why I wrote this blog post.

First, we can now turn off the formatter for some part of your code that you don’t want to format automatically. This is achieved by defining special tags.

This is very useful to format a part of your code that will if formatted will be uglier. For example:

		// [format off]
		foo(           "|------------|\n"            +
		    "-----------|------------|-----------\n" +
	         "----------|   FORMAT   |----------\n"  +
	          "--------- |          | ---------\n"  +
	            "-------  |        |  -------\n"  +
	                      "--------");

No tools can keep this formatting no matter what are you trying, right? 😉

The next improvement is very beautiful. This is result of bug entry 59891 which is posted five years ago (! just to make sure that you understand its complexity).

Basically the problem is if you have nested method calls, Eclipse can’t group the arguments based on their call level. So if you have something like:

public class Main {
    public static void main(
            String[] args) {
        foo(bar(1, 2, 3), baz(4, 5, 6, 7));
    }
}

In case the editor set to wrap on 40th characters, you will get:

public class Main {
    public static void main(
            String[] args) {
        foo(bar(1, 2, 3), baz(4, 5, 6,
                7));
    }
}

With the improvement, you will get:

public class Main {
    public static void main(
            String[] args) {
        foo(bar(1, 2, 3),
                baz(4, 5, 6, 7));
    }
}

Very nice!

A longer example (based on post on StackOverflow.com).

With old formatter, the best you can get is:

		Options options = new Options().addOption(OPTION_HELP_SHORT,
				OPTION_HELP, false, "print usage information").addOption(
				OPTION_VERSION_SHORT, OPTION_VERSION, false,
				"print version and exit").addOption(
				OptionBuilder.withLongOpt(OPTION_PROPERTIES)
						.hasArg()
						.withArgName("FILE")
						.withType(File.class)
						.withDescription("specify a user properties file")
						.create());

Or anything explained on the original post. With the new improvement, you can set the formatter to get something like this:

		Options options = new Options()
				.addOption(OPTION_HELP_SHORT, OPTION_HELP, false,
					"print usage information")
				.addOption(OPTION_VERSION_SHORT, OPTION_VERSION, false,
					"print version")
				.addOption(	OptionBuilder
									.withLongOpt(OPTION_PROPERTIES)
									.hasArg()
									.withArgName("FILE")
									.withType(File.class)
									.withDescription("specify a user properties file")
									.create());

Beautiful, isn’t it? Just one bug maybe since there is a white space before OptionBuilder.

Beside those two, Helios also bring further supports for formatting annotations and various bug fixes. You can check the whole list by clicking this link.

Visual Editor in Eclipse Galileo

For a long time I have tried to install Visual Editor into my Eclipse Galileo and never get it work until recently.

The secret is explained in the Wiki:

With “Eclipse IDE for Java EE Developers”, you should NOT check the Java EMF Model Utilities (org.eclipse.jem.util) plugins since there are already installed.

Ugh… I think I have tried it before but why only now it is working? Anyway, I’m glad that I have it.

For those who are not aware, Visual Editor is an GUI editor for Eclipse. It can be used to assist Swing or SWT application creation. I never like Netbeans Matisse or SWT Designer because I can’t modify the code like I want. I know that Visual Editor is pretty slow, but to get a code that I can enhance manually tastes better than the alternative.

And more importantly, I like the way it codes my Swing application. Here’s an example:

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class TTT extends JFrame {

   private static final long serialVersionUID = 1L;
   private JPanel jContentPane = null;
   private JLabel jLabel = null;
   private JButton jButton = null;
   private JTextField jTextField = null;
   private JSlider jSlider = null;

   /**
    * This is the default constructor
    */
   public TTT() {
      super();
      initialize();
   }

   /**
    * This method initializes this
    *
    * @return void
    */
   private void initialize() {
      this.setSize(300, 196);
      this.setContentPane(getJContentPane());
      this.setTitle("JFrame");
   }

   /**
    * This method initializes jContentPane
    *
    * @return javax.swing.JPanel
    */
   private JPanel getJContentPane() {
      if (jContentPane == null) {
         jLabel = new JLabel();
         jLabel.setText("JLabel");
         jContentPane = new JPanel();
         jContentPane.setLayout(new BorderLayout());
         jContentPane.add(jLabel, BorderLayout.CENTER);
         jContentPane.add(getJButton(), BorderLayout.EAST);
         jContentPane.add(getJTextField(), BorderLayout.SOUTH);
         jContentPane.add(getJSlider(), BorderLayout.NORTH);
      }
      return jContentPane;
   }

   /**
    * This method initializes jButton
    *
    * @return javax.swing.JButton
    */
   private JButton getJButton() {
      if (jButton == null) {
         jButton = new JButton();
         jButton.setHorizontalAlignment(SwingConstants.TRAILING);
         jButton.setText("Test");
         jButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
               System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
            }
         });
      }
      return jButton;
   }

   /**
    * This method initializes jTextField
    *
    * @return javax.swing.JTextField
    */
   private JTextField getJTextField() {
      if (jTextField == null) {
         jTextField = new JTextField();
         jTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            @Override public void keyTyped(java.awt.event.KeyEvent e) {
               System.out.println("keyTyped()"); // TODO Auto-generated Event stub keyTyped()
            }
         });
      }
      return jTextField;
   }

   /**
    * This method initializes jSlider
    *
    * @return javax.swing.JSlider
    */
   private JSlider getJSlider() {
      if (jSlider == null) {
         jSlider = new JSlider();
      }
      return jSlider;
   }

} // @jve:decl-index=0:visual-constraint="10,10"

See how it creates a getter for every components? The getter should prepare a component with its properties and also its listener initialization. It makes every method pretty small and readable. There is no long methods with several components and listeners initialization.