Custom Formatters

If you want your code formatter added to this list, send me an email (scott@javadude.com) and I'll add it!

Note: Custom Code Formatters are only supported in VisualAge 3.5 and above using VA Assist 2.0.7.

VA Assist 2.0.7 now supports custom code formatters for use in VisualAge for Java. If you don't have VA Assist, I highly recommend it! You can get it from http://www.instantiations.com. (It's free if you're using VisualAge for Java, professional edition!)

VA Assist Custom Code Formatter Documentation

You can read the documentation on VA Assist's Custom Code formatter support at

http://www.instantiations.com/assist/docs/browser_custom_formatter.htm

http://www.instantiations.com/assist/docs/options_formatter_custom.htm

Creating a Custom Code Formatter

To create a custom code formatter:

  1. Create a project in your workspace named LFC.
     
  2. Import the lfc.jar file into your LFC project. This is typically found in c:\Program Files\IBM\VisualAge for Java\ide\program\lib\lfc.jar, but that location may vary depending on where you installed VisualAge for Java.
     
  3. Create a new project to contain your custom formatter. You can name this whatever you'd like. For purposes of explanation, we'll assume you name it My Formatter.
     
  4. Create a class (any name, any package) to use as your custom formatter. It should look as follows. The code inside the format method can do anything you would like. This example just calls the default formatter and adds a comment (just to prove the custom formatter was called.)
    package your-package-name;
    
    
    
    import com.ibm.jdt.formatter.api.CodeFormatter;
    
    import com.ibm.jdt.compiler.api.ConfigurableOption;
    
    
    
    /** <p>A sample custom code formatter class. All you need to do is put your
    
     *    formatting code in the format method.</p>
    
     *
    
     *  <p>This sample formatter calls the default IBM formatter and adds a
    
     *    comment at the top of the formatted code.</p>
    
     *
    
     *  <p>We recommend that you come up with an alternate means of providing
    
     *    formatting options, but this demonstration might be useful
    
     *    to show the options.</p>
    
     *
    
     *  <p><i>Note: to edit this class, you'll need to import the VisualAge
    
     *        formatter code. You should create a project named LFC and
    
     *        import the vajinstall\ide\program\lib\lfc.jar file into it.</i></p>
    
     * 
    
     * <p><b>DISCLAIMER</b></p>
    
     * 
    
     *   <p>This Java code sample is provided to you on an 'as-is' basis
    
     *      without warranty or condition of any kind, either express or
    
     *      implied, including, but not limited to, warranty or condition of
    
     *      merchantable quality or fitness for a particular purpose.</p>
    
     * 
    
     *   <p>This sample code is provided to you solely for the purpose of 
    
     *      assisting you in the development of your applications. 
    
     *      Neither Instantiations nor Scott Stanchfield shall be liable for 
    
     *      any damages arising out of your use of this sample code, even 
    
     *      if they have been advised of the possibility of such damages.</p>
    
     * 
    
     *   <p><i>Copyright (c) 2001, Instantiations and Scott Stanchfield, 
    
     *         All Rights Reserved</i></p>
    
     *
    
     * @author Scott Stanchfield
    
     * @version [SS - 2001-05-18] 1.0.0 
    
     */
    
    
    
    public class YourFormatterClassName {
    
      /** <p>This method performs the formatting. It's called for
    
       *    <i>all</i> formatting inside VisualAge when control-w (or
    
       *    whatever key you assign) is pressed in the editor. This applies
    
       *    for class definitions or method definitions. <i>There is no way
    
       *    to tell the difference between a call to format a class definition
    
       *    or a method definition.</i></p>
    
       *
    
       *  <p>Depending on the VA Assist/J settings, this may also be called
    
       *    when importing, exporting, or viewing the entire class source.</p>
    
       *
    
       *  <p>This sample version of the format method calls the default
    
       *     VisualAge formatter and adds a comment at the top of the code.</p>
    
       *
    
       *  @param code    (<code>java.lang.String</code>)<dl><dd> 
    
       *                 The source code to be formatted</dd></dl>
    
       *
    
       *  @param n       (<code>int</code>)<dl><dd> 
    
       *                 The function of this parameter is unknown, 
    
       *                 but it's always 0...</dd></dl>
    
       *
    
       *  @param options (<code>com.ibm.jdt.compiler.api.ConfigurableOption[]</code>)<dl><dd> 
    
       *                 An array of option objects representing the settings 
    
       *                 in VisualAge's Window->Options dialog.</dd></dl>
    
       * 
    
       *  @return        (<code>java.lang.String</code>)<dl><dd> 
    
       *                 The formatted code. You should use the appropriate line 
    
       *                 separation character for your platform in place of 
    
       *                 newlines. You can obtain this by calling 
    
       *                 <pre>System.getProperty("line.separator")</pre></dd></dl>
    
       */
    
      public static String format(String code, int n, ConfigurableOption[] options) {
    
        // Ask VisualAge's default formatter to format the code
    
        String formatted = CodeFormatter.format(code, 0, options);
    
    
    
        // determine the appropriate new line sequence for this platform
    
        String nl = System.getProperty("line.separator");
    
    
    
        // do our own formatting (in this case, just add a message to
    
        //   prove we came here)
    
        return "// *** I'VE BEEN FORMATTED! ***" + nl + formatted;
    
      }
    
    }
  5. Create code in your class' format() method to format the passed-in code string and return it. We recommend you do any String modification in a StringBuffer rather than using Strings, as it's more efficient.

    For a simple example of a custom formatter, see the JavaDude Sample Code Formatters below. You can call the default IBM VisualAge for Java formatter if you'd like (see the sample formatters for an example), or you can do all of the formatting yourself.
      

  6. Change the name of the Custom Formatter class under Window->Options->Coding->Formatter->Custom. (This page is added by VA Assist and is not available in the standard VisualAge for Java installation.) If your custom formatter is in the workspace, it will automatically appear in the drop-down list.
     
  7. When you press control-w in the editor, your custom formatter will be called! (Note that it may also be called when importing, exporting, or viewing your entire class, depending on the other VA Assist options that you have set.
     
  8. After testing your custom formatter, you can export it to any directory or jar on your workspace classpath (See Window->Options->Resources). Once you have exported the custom formatter, you can delete it and and the LFC project from your workspace.
     
  9. Once you have a formatter you like, send me a note at scott@javadude.com and I'll add it to the list at the bottom of the page!

Sample Custom Code Formatters

The file sample-code-formatters.zip contains some sample custom formatters. These formatters do not have any real functionality in them, but can be used as templates when creating your own custom code formatters.

To use them:

  1. Stop VisualAge for Java
  2. Download sample-code-formatters.zip from this site.
  3. Unzip it into the directory in which you installed VisualAge for Java. This will typically be c:\Program Files\IBM\VisualAge for Java
  4. Restart VisualAge for Java (You may see a brief message that the JavaDude Custom Code Formatter Samples feature is being installed.
  5. Make sure you have the LFC project set up (follow steps 1 and 2 under Creating a Custom Code Formatter above).
  6. Choose File->Quick Start->Features->Add Feature.
  7. Select the JavaDude Custom Code Formatter Samples feature and press ok.

You can try these sample formatters using the Window->Options->Coding->Formatter->Custom options.

Useful Custom Code Formatters

JIndent Plug-in

If you own JIndent, you can find an adapter for it at JIndent Plugin for VisualAge for Java.