001    package com.sptci.rwt.webui.model;
002    
003    import java.util.Collection;
004    
005    import nextapp.echo2.app.Component;
006    import nextapp.echo2.app.Grid;
007    import nextapp.echo2.app.Label;
008    
009    import echopointng.GroupBox;
010    
011    import com.sptci.ReflectionUtility;
012    import com.sptci.echo2.Application;
013    import com.sptci.echo2.Configuration;
014    import com.sptci.echo2.Utilities;
015    
016    import com.sptci.rwt.ColumnMetaData;
017    import com.sptci.rwt.MetaData;
018    import com.sptci.rwt.PrimaryKeyMetaData;
019    
020    /**
021     * A view component used to display the information contained in
022     * {@link com.sptci.rwt.PrimaryKeyMetaData}.
023     *
024     * <p>&copy; Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
025     * @author Rakesh Vidyadharan 2007-10-07
026     * @version $Id: PrimaryKeyView.java 4123 2008-05-25 21:49:01Z rakesh $
027     */
028    public class PrimaryKeyView extends AbstractView
029    {
030      /** The meta data object whose details are to be displayed. */
031      private final PrimaryKeyMetaData metaData;
032    
033      /**
034       * Create a new instance of the view using the specified model object.
035       *
036       * @param metaData The {@link #metaData} model object to use.
037       */
038      public PrimaryKeyView( final PrimaryKeyMetaData metaData )
039      {
040        this.metaData = metaData;
041      }
042    
043      /**
044       * Lifecycle method used to initialise component when added to a
045       * container hierarchy.
046       *
047       * @see #createDetails
048       */
049      @Override
050      public void init()
051      {
052        removeAll();
053        add( createDetails() );
054      }
055    
056      /**
057       * Create the component used to display the default limits enforced by
058       * the database engine.
059       *
060       * @see #createLabels
061       * @return The component that displays the limits information.
062       */
063      protected Component createDetails()
064      {
065        PrimaryKeyMetaData pkmd = (PrimaryKeyMetaData) metaData;
066        int size = pkmd.getColumns().size();
067    
068        Grid grid = new Grid( size + 1 );
069    
070        createLabels( "name", metaData, grid );
071        createLabels( "type", metaData, grid );
072        createLabels( "defaultValue", metaData, grid );
073        createLabels( "size", metaData, grid );
074        createLabels( "nullable", metaData, grid );
075    
076        GroupBox box = new GroupBox( Configuration.getString( this, "title" ) );
077        box.add( grid );
078        return box;
079      }
080    
081      /**
082       * Create standard {@link nextapp.echo2.app.Label} components that
083       * represent the name of the specified field and the value in the
084       * specified model.  Over-ridden to process the
085       * java.util.Collection}&lt;ColumnMetaData&gt; and create a
086       * multi-dimensional grid.
087       *
088       * @param name The name of the field.
089       * @param metaData The model object.
090       * @param component The container component to which the labels are to
091       *   be added.
092       */
093      @Override
094      protected void createLabels( final String name,
095          final MetaData metaData, final Component component )
096      {
097        PrimaryKeyMetaData pkmd = (PrimaryKeyMetaData) metaData;
098        int size = pkmd.getColumns().size();
099    
100        final String method = "get" + name.substring( 0, 1 ).toUpperCase() +
101          name.substring( 1 );
102    
103        component.add( Utilities.createLabel(
104              getClass().getName(), name, "Title.Label" ) );
105    
106        for ( ColumnMetaData cmd : pkmd.getColumns() )
107        {
108          try
109          {
110            Object object = ReflectionUtility.execute( cmd, method );
111            component.add( new Label( String.valueOf( object ) ) );
112          }
113          catch ( Throwable t )
114          {
115            processFatalException( method, ColumnMetaData.class.getName(), t );
116          }
117        }
118      }
119    }