001    package com.sptci.rwt.webui.model;
002    
003    import nextapp.echo2.app.Component;
004    import nextapp.echo2.app.Grid;
005    import nextapp.echo2.app.Label;
006    
007    import echopointng.GroupBox;
008    
009    import com.sptci.ReflectionUtility;
010    import com.sptci.echo2.Application;
011    import com.sptci.echo2.Configuration;
012    import com.sptci.echo2.Utilities;
013    
014    import com.sptci.rwt.ColumnMetaData;
015    import com.sptci.rwt.MetaData;
016    import com.sptci.rwt.KeyMetaData;
017    
018    /**
019     * An abstract view component for representing instances of {@link
020     * com.sptci.rwt.KeyMetaData}.
021     *
022     * <p>&copy; Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
023     * @author Rakesh Vidyadharan 2007-10-07
024     * @version $Id: KeyView.java 4123 2008-05-25 21:49:01Z rakesh $
025     */
026    public abstract class KeyView extends AbstractView
027    {
028      /**
029       * Create the component used to display the details of the columns that
030       * are included in the foreign key.
031       *
032       * @see #createCollectionLabels
033       * @return The component that displays the column information.
034       */
035      protected Component createColumnDetails()
036      {
037        KeyMetaData kmd = getMetaData();
038        int size = kmd.getColumns().size();
039    
040        Grid grid = new Grid( size + 1 );
041    
042        createCollectionLabels( "name", kmd, grid );
043        createCollectionLabels( "type", kmd, grid );
044        createCollectionLabels( "defaultValue", kmd, grid );
045        createCollectionLabels( "size", kmd, grid );
046        createCollectionLabels( "nullable", kmd, grid );
047    
048        GroupBox box = new GroupBox(
049            Configuration.getString( this, "column.title" ) );
050        box.add( grid );
051        return box;
052      }
053    
054      /**
055       * Create standard {@link nextapp.echo2.app.Label} components that
056       * represent the name of the specified field and the value in the
057       * specified model.  Over-loaded to process the
058       * java.util.Collection&lt;ColumnMetaData&gt; and create a
059       * multi-dimensional grid.
060       *
061       * @param name The name of the field.
062       * @param kmd The model object.
063       * @param component The container component to which the labels are to
064       *   be added.
065       */
066      protected void createCollectionLabels( final String name,
067          final KeyMetaData kmd, final Component component )
068      {
069        int size = kmd.getColumns().size();
070    
071        final String method = "get" + name.substring( 0, 1 ).toUpperCase() +
072          name.substring( 1 );
073    
074        component.add( Utilities.createLabel(
075              getClass().getName(), name, "Title.Label" ) );
076    
077        for ( ColumnMetaData cmd : kmd.getColumns() )
078        {
079          try
080          {
081            Object object = ReflectionUtility.execute( cmd, method );
082            component.add( new Label( String.valueOf( object ) ) );
083          }
084          catch ( Throwable t )
085          {
086            processFatalException( method, ColumnMetaData.class.getName(), t );
087          }
088        }
089      }
090    
091      /**
092       * Abstract method to return the model object for this view.
093       *
094       * @return The meta data object.
095       */
096      protected abstract KeyMetaData getMetaData();
097    }