Monday, July 26, 2010

Customizing Which Form Fields Are Displayed: Part 7

Following on from parts 1, 2, 3, 4, 5 and 6, I thought I'd blog some of the other field ordering preferences I've encountered.

This time I want to look at 'universal' ordering that sits not just above per-screen, but also above per-domain. An obvious one, though rarely useful, is alphabetical ordering. This is what JavaBeanPropertyStyle implements by default. Another interesting one is 'source code line number' ordering. Source code line numbers are not usually available in JVM class files, but if you have a technology like Javassist on your classpath you can plug that into Metawidget to get field ordering for free!

Example implementation below. Some points to note:
  • It's in Swing so you can just cut and paste and run it

  • It uses JavassistPropertyStyle (no annotations, Inspectors or InspectionResultProcessors this time!)

  • You must have Javassist on your classpath

  • You must compile your code in debug mode so that line numbering is included
package com.myapp;

import javax.swing.*;

import org.metawidget.inspector.annotation.*;
import org.metawidget.inspector.composite.*;
import org.metawidget.inspector.impl.*;
import org.metawidget.inspector.impl.propertystyle.javassist.*;
import org.metawidget.inspector.propertytype.*;
import org.metawidget.swing.*;

public class Main {

   public static void main( String[] args ) {

      Person person = new Person();

      SwingMetawidget metawidget = new SwingMetawidget();
      BaseObjectInspectorConfig config = new BaseObjectInspectorConfig().setPropertyStyle( new JavassistPropertyStyle() );
      metawidget.setInspector( new CompositeInspector( new CompositeInspectorConfig()
               new PropertyTypeInspector( config ),
               new MetawidgetAnnotationInspector( config )
      metawidget.setToInspect( person );

      JFrame frame = new JFrame( "Metawidget Tutorial" );
      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      frame.getContentPane().add( metawidget );
      frame.setSize( 400, 250 );
      frame.setVisible( true );

   static class Person {

      private String mName;
      private int mAge;
      private boolean mRetired;
      private String mNotes;

      public String getName() {
         return mName;

      public void setName( String name ) {
         mName = name;

      public int getAge() {
         return mAge;

      public void setAge( int age ) {
         mAge = age;

      public boolean isRetired() {
         return mRetired;

      public void setRetired( boolean retired ) {
         mRetired = retired;

      public String getNotes() {
         return mNotes;

      public void setNotes( String notes ) {
         mNotes = notes;