Chitika

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

No comments:

Post a Comment