Chitika

Showing posts with label IntelliJ IDEA. Show all posts
Showing posts with label IntelliJ IDEA. Show all posts

Thursday, September 16, 2004

sample IDEA live template

Following is a sample of IDEA Live Template that I'd like to share and discuss:

/**
 * Getter method for the text version of a double field
 * named $DECAPITALIZED_FIELD_NAME$ with a precision of
 * $PRECISION$.
 */
public String get$FIELD_NAME$Text () {
  Double d$FIELD_NAME$ = get$FIELD_NAME$();
  return StringConversionUtil.doubleToString (
    d$FIELD_NAME$.doubleValue(), $PRECISION$);
}

/**
 * Setter method for the text version of a double field
 * named $FIELD_NAME$.
 */
public void set$FIELD_NAME$Text (
    String $DECAPITALIZED_FIELD_NAME$Text) {
  double d$FIELD_NAME$ =
    StringConversionUtil.stringToDouble (
      $DECAPITALIZED_FIELD_NAME$Text);
  set$FIELD_NAME$ (d$FIELD_NAME$ == 0.0 ?
    StringConversionUtil.ZERO :
    new Double (d$FIELD_NAME$));
}

The above Live Template is trying to provide additional methods in the ActionForm class to provide semi-automatic data type conversion for the ActionForm auto-population mechanics in Struts (see my previous post, limitation of ActionForm in Struts).

For every double field in the class (or its subclass), you can just type in the abbreviation, e.g. "setgetdouble", and hit the expand key, e.g. "Tab", boom!

The setter & getter method will be generated automatically. All you need to provide is just the DECAPITALIZED_FIELD_NAME & PRECISION. Following is an example of what's the generation result if the DECAPITALIZED_FIELD_NAME is 'ratio' and the PRECISION is '2':

/**
 * Getter method for the text version of a double field
 * named ratio with a precision of
 * 2.
 */
public String getRatioText () {
  Double dRatio = getRatio();
  return StringConversionUtil.doubleToString (
    dRatio.doubleValue(), 2);
}

/**
 * Setter method for the text version of a double field
 * named Ratio.
 */
public void setRatioText (
    String ratioText) {
  double dRatio =
    StringConversionUtil.stringToDouble (
      ratioText);
  setRatio (dRatio == 0.0 ?
    ScoringCommonConstants.ZERO :
    new Double (dRatio));
}

Here's what I put for the template variables:
- DECAPITALIZED_FIELD_NAME
    Expression: variableOfType("java.lang.Double")
    Default value: "aDoubleField"
- FIELD_NAME
    Expression: capitalize(DECAPITALIZED_FIELD_NAME)
    Skip if defined: Checked
- PRECISION
    Default value: "2"

The variableOfType() expression will show you a selection of all variables within the current scope whose type is the same as you specified.
The capitalize() expression will capitalize the first character of the variable's value which you specified.
The rest is quite straightforward.

What do you think? It helped me.. :D
I'll share more of my live templates next time.. :D

Tuesday, September 14, 2004

live templates in IntelliJ IDEA

Three years ago, I was not a fan of any IDE tools. My favourite Java editor was UltraEdit. I've switched to EditPlus once a while, but mostly it's UltraEdit that got me through many Java projects. Two years ago, most of the developers in my company started using IntelliJ IDEA, but I wasn't impressed enough to even give it a try.

Until last year, when I had a chance, I took a look at IntelliJ IDEA 3.0. It's quite impressive actually. One thing for sure, I really like the refactoring tools that came with it.

Recently, when I played around with the latest IntelliJ IDEA 4.5 which already includes the Inspection Gadget 2.0, I was more impressed, and making it my favourite IDE for Java. Since then, I'm trying to use more and more features in it, and try to minimize my time to develop any Java application.

With the spirit of leveraging my productivity, I'm currently testing the Live Templates in IDEA. I was hoping it could help me boost my productivity. And, apparently it did. I have been using most of the pre-constructed Live Templates (as listed in Ctrl-J). And now, I have even tried to create some Live Templates for my daily usage.

It's working great. It can be even copied and imported to my PC at home, since it's created in an XML file. This could very well be a good step to enrich my experience with IDEA.

If you had not tried it, it's highly recommended..
If you had created any user-defined live templates which can be shared, I'd really like to take a look.. :D
I'll share mine once it's not so application-specific, and more generic instead..