cedric      01/12/27 09:37:46

  Added:       contrib/tiles/src/tutorial/org/apache/struts/example/tiles/portal
                        UserPortalSettingsAction.java UserPortalAction.java
                        UserMenuSettingsAction.java UserMenuAction.java
                        PortalSettingsForm.java PortalSettings.java
                        PortalChoices.java MenuSettingsForm.java
                        MenuSettings.java
  Log:
  New examples directories associated classes
  
  Revision  Changes    Path
  1.1                  
jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/portal/UserPortalSettingsAction.java
  
  Index: UserPortalSettingsAction.java
  ===================================================================
  /*
   */
  
  
  package org.apache.struts.example.tiles.portal;
  
  import org.apache.struts.tiles.*;
  import org.apache.struts.tiles.actions.TilesAction;
  
  import java.io.IOException;
  import java.util.Locale;
  import java.util.Vector;
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpSession;
  import javax.servlet.http.HttpServletResponse;
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
  import org.apache.struts.action.ActionServlet;
  import org.apache.struts.util.MessageResources;
  
  import org.apache.struts.util.RequestUtils;
  
  
  /**
   * Implementation of <strong>Action</strong> that populates an instance of
   * <code>SubscriptionForm</code> from the currently specified subscription.
   *
   * This action is used as controller for portal settings editor.
   * It does folowing :
   * <ul>
   * <li>Load or create user portal settings</li>
   * <li>Read web form, and set user portal setting accordingly</li>
   * <li>Prepare portal editor needed attributes</li>
   * <li></li>
   * </ul>
   *
   * @author Cedric Dumoulin
   * @version $Revision: 1.1 $ $Date: 2001/12/27 17:37:46 $
   */
  
  public final class UserPortalSettingsAction extends TilesAction {
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Process the specified HTTP request, and create the corresponding HTTP
       * response (or forward to another web component that will create it).
       * Return an <code>ActionForward</code> instance describing where and how
       * control should be forwarded, or <code>null</code> if the response has
       * already been completed.
       *
       * @param servlet The ActionServlet making this request
       * @param mapping The ActionMapping used to select this instance
       * @param actionForm The optional ActionForm bean for this request (if any)
       * @param request The HTTP request we are processing
       * @param response The HTTP response we are creating
       *
       * @exception IOException if an input/output error occurs
       * @exception ServletException if a servlet exception occurs
       */
      public ActionForward perform( ComponentContext context,
                                   ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response)
                            throws IOException, ServletException
    {
    //System.out.println("Enter action UserPortalSettingsAction");
        HttpSession session = request.getSession();
    PortalSettingsForm prefsForm = (PortalSettingsForm)form;
  
        // Get user portal settings from user context
    PortalSettings settings = UserPortalAction.getSettings( context, session );
    PortalChoices choices = UserPortalAction.getPortalChoices( context, 
getServlet().getServletContext() );
  
    if( prefsForm.isSubmitted() )
      {  // read arrays
      //System.out.println("form submitted");
  
        // Set settings cols according to user choice
      for( int i=0;i<prefsForm.getNumCol(); i++)
        {
        settings.setListAt( i, choices.checkKeys( prefsForm.getNewCol(i)) );
        } // end loop
  
      //System.out.println( "settings : " +settings.toString() );
      prefsForm.reset();
          //return null; // (mapping.findForward("viewPortal"));
      }
  
        // Set lists values to be shown
      for( int i=0;i<settings.getNumCols(); i++ )
        {
        prefsForm.addCol(settings.getListAt(i) );
        prefsForm.addColLabels(choices.getChoiceLabelsForKeys( settings.getListAt(i)) 
);
        } // end loop
  
      prefsForm.setChoices(choices.getChoices() );
      prefsForm.setChoiceLabels(choices.getChoiceLabels() );
  
      //System.out.println("Exit action UserPortalSettingsAction");
          return null; //(mapping.findForward("editPortal"));
      }
  
  }
  
  
  
  1.1                  
jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/portal/UserPortalAction.java
  
  Index: UserPortalAction.java
  ===================================================================
  /*
   */
  
  
  package org.apache.struts.example.tiles.portal;
  
  import org.apache.struts.tiles.*;
  import org.apache.struts.tiles.actions.TilesAction;
  
  import java.io.IOException;
  import java.util.Locale;
  //import java.util.Vector;
  import java.util.List;
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpSession;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.ServletContext;
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
  import org.apache.struts.action.ActionServlet;
  import org.apache.struts.util.MessageResources;
  
  import org.apache.struts.util.RequestUtils;
  
  
  /**
   * This controller load user portal settings and put them in tile context.
   * If portal settings are not defined for user, defined them based on tiles
   * attributes used as default.
   *
   * @author Cedric Dumoulin
   * @version $Revision: 1.1 $ $Date: 2001/12/27 17:37:46 $
   */
  
  public final class UserPortalAction extends TilesAction {
  
        /** Name used to store settings in session context */
      public static String DEFAULT_CONTEXT_ID = "portal.USER_PORTAL_SETTINGS";
        /** Tile parameter name */
      public static String PARAM_NUMCOLS = "numCols";
        /** Tile parameter name */
      public static String PARAM_LIST = "list";
        /** Tile parameter name */
      public static String PARAM_LIST_LABELS = "listLabels";
        /** Tile parameter name */
      public static String PARAM_ID = "userContextId";
        /** Tile parameter name */
      public static String PORTAL_CHOICES_ID = "tiles.portal.PortalChoices";
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Process the specified HTTP request, and create the corresponding HTTP
       * response (or forward to another web component that will create it).
       * Return an <code>ActionForward</code> instance describing where and how
       * control should be forwarded, or <code>null</code> if the response has
       * already been completed.
       *
       * @param servlet The ActionServlet making this request
       * @param mapping The ActionMapping used to select this instance
       * @param actionForm The optional ActionForm bean for this request (if any)
       * @param request The HTTP request we are processing
       * @param response The HTTP response we are creating
       *
       * @exception IOException if an input/output error occurs
       * @exception ServletException if a servlet exception occurs
       */
      public ActionForward perform( ComponentContext context,
                                   ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response)
                            throws IOException, ServletException
      {
      //System.out.println("Enter action UserPortalAction");
        // Get current session.
          HttpSession session = request.getSession();
  
        // Get user portal list from user context
      PortalSettings settings = getSettings( context, session );
        // Set parameters for tiles
      context.putAttribute( "numCols", Integer.toString(settings.getNumCols()) );
      for( int i=0; i<settings.getNumCols(); i++ )
        context.putAttribute( "list"+i, settings.getListAt(i) );
  
      //System.out.println( "settings=" + settings );
      //System.out.println("Exit action UserPortalAction");
          return null; //(mapping.findForward("success"));
      }
  
      /**
       * Retrieve user setting from session.
       * If settings are not found, initialized them.
       */
    public static PortalSettings getSettings( ComponentContext context, HttpSession 
session )
    {
        // Retrieve user context id used to store settings
      String userSettingsId = (String)context.getAttribute( PARAM_ID );
      if( userSettingsId == null )
        userSettingsId = DEFAULT_CONTEXT_ID;
  
        // Get user portal list from user context
      PortalSettings settings = (PortalSettings)session.getAttribute( userSettingsId );
  
      if( settings == null )
        { // List doesn't exist, create it and initialize it from Tiles parameters
        settings = new PortalSettings();
        settings.setNumCols( (String)context.getAttribute( PARAM_NUMCOLS ) );
        for( int i=0; i<settings.getNumCols(); i++ )
          {
          List col = (List)context.getAttribute( ((String)PARAM_LIST+i) );
          //List labels = (List)context.getAttribute( ((String)PARAM_LIST_LABELS+i) );
          settings.setListAt( i, col );
          } // end loop
  
          // Save user settings in session
        session.setAttribute( userSettingsId, settings);
        } // end if
  
    return settings;
    }
  
        /**
         * Retrieve portal choices object.
         * Create it from default values if needed.
         */
      static public PortalChoices getPortalChoices( ComponentContext context, 
ServletContext servletContext)
        {
        PortalChoices choices = (PortalChoices)servletContext.getAttribute( 
PORTAL_CHOICES_ID );
        if( choices == null )
          { // Initialize choices
          choices = new PortalChoices();
          int numCols = Integer.parseInt( (String)context.getAttribute( PARAM_NUMCOLS 
) );
          for( int i=0; i<numCols; i++ )
            {
            List col = (List)context.getAttribute( ((String)PARAM_LIST+i) );
            List labels = (List)context.getAttribute( ((String)PARAM_LIST_LABELS+i) );
            choices.addChoices( col, labels );
            } // end loop
          servletContext.setAttribute( PORTAL_CHOICES_ID, choices );
          } // end if
  
        return choices;
        }
  
  }
  
  
  
  
  1.1                  
jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/portal/UserMenuSettingsAction.java
  
  Index: UserMenuSettingsAction.java
  ===================================================================
  package org.apache.struts.example.tiles.portal;
  
  import org.apache.struts.action.Action;
  import org.apache.struts.tiles.*;
  import org.apache.struts.tiles.actions.TilesAction;
  import org.apache.struts.tiles.beans.MenuItem;
  
  import java.io.IOException;
  import java.util.List;
  import java.util.Iterator;
  import java.util.ArrayList;
  
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpSession;
  import javax.servlet.http.HttpServletResponse;
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
  import org.apache.struts.action.ActionServlet;
  import org.apache.struts.util.MessageResources;
  
  import org.apache.struts.util.RequestUtils;
  
    /**
     * Tiles controller as Struts Action.
     * This controller take a list of lists of MenuItems, and arrange them
     * to be shown by appropriate jsp view.
     * Create and set following attribute in Tile context :
     * <ul>
     *   <li>names : list of names to display</li>
     *   <li>returnedValues : list of corresponding key, or values to return</li>
     *   <li>selecteds : list of boolean indicating whether or not a name is 
selected</li>
     * </ul>
   * Tiles input attributes :
   * <ul>
   *   <li>title : menu title</li>
   *   <li>items : Menu entries used as default when user settings is created</li>
   *   <li>defaultChoice : Menus or menu entries porposed as choice to user</li>
   *   <li>storeUnderName : Store user settings under provided name in session context 
[optional]</li>
   *   <li></li>
   * </ul>
   * Tiles output attributes :
   * <ul>
   *   <li>choiceItems : List of menu items proposed as a choice</li>
   *   <li>userItems : List of user actual menu items</li>
   * </ul>
   *
     */
  public class UserMenuSettingsAction extends TilesAction
  {
        /** debug flag */
      public static boolean debug = false;
  
        /**
       * Process the specified HTTP request, and create the corresponding HTTP
       * response (or forward to another web component that will create it).
       * Return an <code>ActionForward</code> instance describing where and how
       * control should be forwarded, or <code>null</code> if the response has
       * already been completed.
       *
       * @param servlet The ActionServlet making this request
       * @param mapping The ActionMapping used to select this instance
       * @param actionForm The optional ActionForm bean for this request (if any)
       * @param request The HTTP request we are processing
       * @param response The HTTP response we are creating
       *
       * @exception IOException if an input/output error occurs
       * @exception ServletException if a servlet exception occurs
       */
      public ActionForward perform( ComponentContext context,
                                   ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response)
                            throws IOException, ServletException
      {
      if(debug)
        System.out.println("Enter action UserMenuSettingsAction");
  
      HttpSession session = request.getSession();
      MenuSettingsForm actionForm = (MenuSettingsForm)form;
  
          // Load user menu settings and available list of choices
      MenuSettings settings = UserMenuAction.loadUserSettings( context, session );
      List choices = UserMenuAction.loadMenuChoices( context, request, 
getServlet().getServletContext() );
  
        // Check if form is submitted
        // If true, read, check and store new values submitted by user.
      if( actionForm.isSubmitted() )
        {  // read arrays
        if(debug)
          System.out.println("form submitted");
  
        settings.reset();
        settings.addItems( checkSelected(actionForm.getSelected(), choices) );
        if(debug)
          System.out.println( "settings : " +settings.toString() );
        actionForm.reset();
        //return null; // (mapping.findForward("viewPortal"));
        } // end if
  
        // Prepare data for view tile
      context.putAttribute( "userItems", settings.getItems() );
      context.putAttribute( "choiceItems", choices );
  
      if(debug)
        System.out.println("Exit action UserMenuSettingsAction");
          return null; //(mapping.findForward("editPortal"));
      }
  
        /**
         * Check selected items, and return apppropriate list of items.
         * For each item in selected list, check if it exist in available choice.
         * Also check for double.
         * @param selectedKey Key of selected items (generally, link url)
         * @param choices List of avalaible items choice to compare against.
         */
    static protected List checkSelected( String[] selectedKey, List choices )
      {
      List selectedList = java.util.Arrays.asList( selectedKey );
      List result = new ArrayList(selectedList.size());
  
        ////////////////
      Iterator iter = selectedList.iterator();
      while( iter.hasNext() )
        {
        MenuItem item = getItemByKey( iter.next(), choices);
        if( item != null )
          result.add(item );
        } // end loop
      return result;
      }
        /**
         * Get item by its key in list of choices
         * @param key Key of selected items (generally, link url)
         * @param choices List of avalaible items choice to compare against.
         * @return corresponding item or null if not found.
         */
    static protected MenuItem getItemByKey( Object key, List choices )
      {
      Iterator iter = choices.iterator();
      while( iter.hasNext() )
        {
        MenuItem item = (MenuItem)iter.next();
        if( item.getLink().equals( key) )
          return item;
        } // end loop
      return null;
      }
  }
  
  
  1.1                  
jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/portal/UserMenuAction.java
  
  Index: UserMenuAction.java
  ===================================================================
  /*
   */
  
  
  package org.apache.struts.example.tiles.portal;
  
  import org.apache.struts.tiles.*;
  import org.apache.struts.tiles.actions.TilesAction;
  import org.apache.struts.tiles.Controller;
  import org.apache.struts.tiles.beans.MenuItem;
  import org.apache.struts.tiles.DefinitionsUtil;
  
  import java.io.IOException;
  import java.util.Iterator;
  import java.util.List;
  import java.util.ArrayList;
  
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpSession;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.ServletContext;
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
  import org.apache.struts.action.ActionServlet;
  import org.apache.struts.util.MessageResources;
  
  import org.apache.struts.util.RequestUtils;
  
  
  /**
   * This controller load user menu settings and put them in tile context.
   * If menu settings are not defined for user, defined them based on tiles
   * attributes used as default.
   *
   * This implementation extends Struts Action, and also define Tiles Controller.
   * This allows to use it as well as a controller type or a controller url. If used
   * as controller type, Struts Action functionality are not availables.
   *
   * Tiles input attributes :
   * <ul>
   *   <li>title : menu title</li>
   *   <li>items : Menu entries used as default when user settings is created</li>
   *   <li>defaultChoice : Menus or menu entries porposed as choice to user</li>
   *   <li>storeUnderName : Store user settings under provided name in session context 
[optional]</li>
   *   <li></li>
   * </ul>
   * Tiles output attributes :
   * <ul>
   *   <li>title : menu title</li>
   *   <li>items : Menu items to display</li>
   *   <li></li>
   * </ul>
   *
   * @author Cedric Dumoulin
   * @version $Revision: 1.1 $ $Date: 2001/12/27 17:37:46 $
   */
  
  public final class UserMenuAction extends TilesAction implements Controller {
  
        /** debug flag */
      public static boolean debug = false;
  
        /** Tile attribute name. Value of this tile's attribute is the name used to 
store settings in user context */
      public static String STORE_UNDER_ATTRIBUTE = "storeUnderName";
        /** Default name used to store settings in session context */
      public static String DEFAULT_STORE_UNDER_NAME = "menu.DEFAULT_STORE_UNDER_NAME";
  
        /** Name under which item choices are stored in application context */
      public static final String CHOICES_ITEMS_NAME = "menu.AvailableItems";
  
        /** Tile attribute name. List of menu or items used as choices */
      public static final String CHOICES_ITEMS_ATTRIBUTE = "defaultChoice";
  
        /** Tile attribute name. User items are stored under this name in tile's 
context  */
      public static String USER_ITEMS_ATTRIBUTE = "items";
  
      /**
       * Struts' action perform().
       * Process the specified HTTP request, and create the corresponding HTTP
       * response (or forward to another web component that will create it).
       * Return an <code>ActionForward</code> instance describing where and how
       * control should be forwarded, or <code>null</code> if the response has
       * already been completed.
       *
       * @param servlet The ActionServlet making this request
       * @param mapping The ActionMapping used to select this instance
       * @param actionForm The optional ActionForm bean for this request (if any)
       * @param request The HTTP request we are processing
       * @param response The HTTP response we are creating
       *
       * @exception IOException if an input/output error occurs
       * @exception ServletException if a servlet exception occurs
       */
      public ActionForward perform( ComponentContext context,
                                   ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response)
                            throws IOException, ServletException
      {
      perform( context, request, response, getServlet().getServletContext() );
          return null; //(mapping.findForward("success"));
      }
  
     /**
      * Method associated to a tile and called immediately before tile is included.
      * @param tileContext Current tile context.
      * @param request Current request
      * @param response Current response
      * @param servletContext Current servlet context
      */
     public void perform(ComponentContext context,
                         HttpServletRequest request, HttpServletResponse response,
                         ServletContext servletContext)
          throws ServletException, IOException
      {
      if(debug)
        System.out.println("Enter action UserMenuAction");
        // Get current session.
          HttpSession session = request.getSession();
  
        // Load user settings from user context
      MenuSettings settings = loadUserSettings( context, session );
        // Set parameters for rendering page
      context.putAttribute( USER_ITEMS_ATTRIBUTE, settings.getItems() );
  
      if(debug)
        {
        System.out.println( "settings=" + settings );
        System.out.println("Exit action UserMenuAction");
        }
      }
  
      /**
       * Load user setting.
       * This implementation load setting from user context.
       * If settings are not found, initialized them from default items defined
       * in Tile's context.
       * If settings are not found, initialized them.
       */
    public static MenuSettings loadUserSettings( ComponentContext context, HttpSession 
session )
      throws ServletException
    {
        // Retrieve attribute name used to store settings.
      String userSettingsName = (String)context.getAttribute( STORE_UNDER_ATTRIBUTE );
      if( userSettingsName == null )
        userSettingsName = DEFAULT_STORE_UNDER_NAME;
  
        // Get user list from user context
      MenuSettings settings = (MenuSettings)session.getAttribute( userSettingsName );
  
        // If settings don't exist, create and initialize them
        // Initialization is done from context attribute denoted by ITEMS
      if( settings == null )
        { // List doesn't exist, create it and initialize it from Tiles parameters
        settings = new MenuSettings();
        try {
          settings.addItems( (List)context.getAttribute( USER_ITEMS_ATTRIBUTE ) );
          }
         catch( ClassCastException ex )
          {
          throw new ServletException( "Can't initialize user menu : default items must 
be a list of items" );
          }
          // Save user settings in session
        session.setAttribute( userSettingsName, settings);
        } // end if
  
    return settings;
    }
  
        /**
         * Load list of menu entries available as choices to user.
         * Look in servlet context, or create it from list of object provided
         * as Tiles attribute.
         * Create it from default values if needed.
         */
      static public List loadMenuChoices( ComponentContext context, HttpServletRequest 
request, ServletContext servletContext)
        throws ServletException
        {
          // Get list of choices from context
        List choices = (List)servletContext.getAttribute( CHOICES_ITEMS_NAME );
          // If not found, initialize it from provided default menu
        if( choices == null )
          {
          Object dflt = context.getAttribute( CHOICES_ITEMS_ATTRIBUTE );
          if( dflt == null )
            throw new ServletException( "Can't find default menu item choices under 
name '" + CHOICES_ITEMS_ATTRIBUTE + "'" );
          choices = new ArrayList();
          extractItems(choices, dflt, request, servletContext);
          if( choices.size() == 0 )
            throw new ServletException( "Can't initialize menu items choices" );
            // save it for future use
          servletContext.setAttribute( CHOICES_ITEMS_NAME, choices );
          } // end if
          return choices;
        }
        /**
         * Extract menu items from passed object. Items are stored in parameter 
'result'.
         * This method allows to create a list of menu entries from existing menus.
         * Check object type class :
         * <li>
         *   <ul>MenuItems : add it</ul>
         *   <ul>ComponentDefinition : get attribute items, or list if not found.
         *       Call ExtractItems with resulting attribute.
         *   </ul>
         *   <ul>List : iterate on list, and call ExtractItems for each element.
         * </li>
         * @param result result list (should be initialized)
         * @param object object to add (MenuItems, Definition, ...)
         * @param request current request
         * @param servletContext current servlet context.
         */
      static private void extractItems( List result, Object object, HttpServletRequest 
request, ServletContext servletContext)
        {
        if(debug)
          System.out.println( "Extract menu item from '" + object + "'" );
        if( object instanceof String )
          {  // definition name
          try {
            ComponentDefinition def = DefinitionsUtil.getDefinition( (String)object, 
request, servletContext );
            extractItems( result, def, request, servletContext );
            }
           catch( Exception ex )
            { // silently fail
            }
  
          }
        else if( object instanceof List )
          {
          List list = (List)object;
          Iterator iter = list.iterator();
          while( iter.hasNext() )
            {
            extractItems( result, iter.next(), request, servletContext );
            } // end loop
          }
         else if( object instanceof ComponentDefinition )
          {
          ComponentDefinition definition = (ComponentDefinition)object;
          Object attribute = definition.getAttribute( "items" );
          if( attribute == null )
            attribute = definition.getAttribute( "list" );
          if( attribute == null )
            {
            return;
            }
          extractItems( result, attribute, request, servletContext );
          }
         else if( object instanceof MenuItem )
          {
          result.add( object );
          }
        }
  
  }
  
  
  
  
  1.1                  
jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/portal/PortalSettingsForm.java
  
  Index: PortalSettingsForm.java
  ===================================================================
  /*
   */
  
  
  package org.apache.struts.example.tiles.portal;
  
  
  import java.util.List;
  import java.util.ArrayList;
  import org.apache.struts.action.ActionForm;
  
  
  /**
   * Action form used to read data from web page form.
   */
  public final class PortalSettingsForm extends ActionForm  {
  
      /** Validate value */
    protected String validate;
      /** empty list used by reset */
    protected String[] empty = {};
      /** list of "remaining" choices */
    protected String[] remaining = empty;
      /** list of user columns */
    protected List columns = new ArrayList();
      /** list of user columns labels */
    protected List columnLabels = new ArrayList();
      /** list of columns selected by user */
    protected List newCols = new ArrayList();
  
      /** Choice list */
    protected List choice;
      /** Choice list labels */
    protected List choiceLabels;
      /** Is initialized ? */
    protected boolean initialized = false;
  
  
      /**
       * Set col
       */
    public void setCol( int index, List col )
      {
      columns.set( index, col);
      }
  
      /**
       * Add col
       */
    public void addCol( List col )
      {
      columns.add( col);
      }
  
      /**
       * Get col Labels
       */
    public List getColLabels(int index)
      {
      return (List)columnLabels.get(index);
      }
  
      /**
       * Set col Labels
       */
    public void setColLabels( int index, List col )
      {
      columnLabels.set( index, col);
      }
  
      /**
       * Add col Labels
       */
    public void addColLabels( List col )
      {
      columnLabels.add( col);
      }
  
      /**
       * Get col
       */
    public List getCol(int index)
      {
      return (List)columns.get(index);
      }
  
      /**
       * Set col Labels
       */
    public void setNewCol( int index, String list[] )
      {
        // ensure capacity
      while( index>=newCols.size())newCols.add(null);
      newCols.set( index, list);
      }
  
      /**
       * Get col
       */
    public String[] getNewCol(int index)
      {
      if(newCols==null || index>=newCols.size())
        return null;
      return (String[])newCols.get(index);
      }
  
      /**
       * Get number of columns
       */
    public int getNumCol()
      {
      return newCols.size();
      }
  
      /**
       * Set list1
       */
    public void setL1( String array[] )
      {
      setNewCol(1, array);
      }
      /**
       * Set list1
       */
    public String[] getL1()
      {
      return getNewCol(1);
      }
  
      /**
       * Set list1
       */
    public void setL0( String array[] )
      {
      setNewCol(0, array);
      }
      /**
       * Set list1
       */
    public String[] getL0()
      {
      return getNewCol(0);
      }
      /**
       * Set list1
       */
    public void setRemaining( String array[] )
      {
      remaining = array;
      }
      /**
       * Set list1
       */
    public String[] getRemaining()
      {
      return remaining;
      }
  
  
      /**
       * Set list1
       */
    public void setChoices( List list )
      {
      choice = list;
      }
      /**
       * Set list1
       */
    public void setChoiceLabels( List list )
      {
      choiceLabels = list;
      }
      /**
       * Set list1
       */
    public List getChoices()
      {
      return choice;
      }
      /**
       * Set list1
       */
    public List getChoiceLabels()
      {
      return choiceLabels;
      }
  
     /**
      * Is this form submitted ?
      */
    public boolean isSubmitted()
      {
      return validate != null;
      }
  
     /**
      * Is this form submitted ?
      */
    public void setValidate( String value)
      {
      this.validate = value;
      }
  
      /**
       * Reset properties
       */
    public void reset()
      {
      remaining =  empty;
      validate = null;
      columns.clear();
      columnLabels.clear();
      newCols.clear();
      }
      /**
       * Initialized flag
       */
    public boolean isInitialized()
      {
      return initialized;
      }
      /**
       * Initialized flag
       */
    public void setInitialized( boolean isInitialized)
      {
      initialized = isInitialized;
      }
  }
  
  
  
  
  1.1                  
jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/portal/PortalSettings.java
  
  Index: PortalSettings.java
  ===================================================================
  package org.apache.struts.example.tiles.portal;
  
  import java.util.List;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.HashMap;
  
    /**
     * Objects of this class hold portal settings for one user.
     */
     public class PortalSettings
     {
         /** Number of columms*/
       protected int numCols;
         /** List of lists (one per column) */
       protected List lists = new ArrayList();
  
         /**
          * Get number of columns.
          */
       public int getNumCols()
         {
         return numCols;
         }
         /**
          * Set number of columns
          */
       public void setNumCols( String numCols )
         {
         setNumCols( Integer.parseInt(numCols) );
         }
         /**
          * Set number of columns.
          * Ensure capacity for internal list.
          */
       public void setNumCols( int numCols )
         {
         this.numCols = numCols;
         }
         /**
          * Get list at specified index
          */
       public List getListAt( int index )
         {
         return (List)lists.get(index);
         }
  
         /**
          * Add a list without checking
          */
       public void addList( List list )
         {
         lists.add( list);
         }
  
         /**
          * Set list at specified index. Previous list is disguarded.
          * Add empty list if necessary.
          * Indexes go from 0 to numCols-1
          * @param index index of the list to add.
          * @param list list to set.
          */
       public void setListAt( int index, List list )
         {
           // First, ensure capacity
         while( index>lists.size() ) lists.add(new ArrayList());
         lists.add( index, list);
         }
  
         /**
          * Reset settings
          */
       public void reset()
         {
         numCols = 0;
         lists.clear();
         }
  
       public String toString()
         {
         return "colCount=" + numCols
                + " , lists=" + lists;
         }
     }
  
  
  
  1.1                  
jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/portal/PortalChoices.java
  
  Index: PortalChoices.java
  ===================================================================
  package org.apache.struts.example.tiles.portal;
  
  import java.util.List;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.HashMap;
  
  
  /**
   * Instances of this classe hold available Tiles for a portal page.
   *
   * @author Cedric Dumoulin
   */
  public class PortalChoices
  {
         /** Choices Tiles */
       protected List choices = new ArrayList();
         /** Choices Tiles labels */
       protected List choiceLabels = new ArrayList();
  
         /**
          * Set list of choices.
          * Labels come from tiles names
          * @param list list of tiles
          */
       public void setChoices( List list)
         {
         setChoices(list, list);
         }
  
         /**
          * add list to list of choices Tiles
          * Labels come from tiles names
          * @param list list of tiles
          */
       public void addChoices( List list)
         {
         addChoices( list, list);
         }
  
         /**
          * Set list of choices Tiles.
          * Previous list is disguarded.
          * @param list list of tiles
          * @param labels corresponding labels. List size must be the same as list.
          * If labels is null, use list of tiles.
          * @throws ArrayIndexOutOfBoundsException if list and labels aren't the same 
size.
          */
       public void setChoices( List list, List labels)
           throws ArrayIndexOutOfBoundsException
         {
           // If no labels, use list keys
         if( labels == null )
           labels = list;
           // Check sizes
         if( list.size() != labels.size() )
           {// error
           System.out.println( "Error : list and labels size must be the same." );
           throw new java.lang.ArrayIndexOutOfBoundsException( "List of choices and 
list of labels must be of the same size" );
           }
         this.choices = list;
         choiceLabels = labels;
         }
  
         /**
          * add list and labels to list of choices Tiles.
          * If labels is null, use keys list as labels.
          * @list list of choice keys to add
          * @param labels corresponding labels. List size must be the same as list.
          * If labels is null, use list of tiles.
          * @throws ArrayIndexOutOfBoundsException if list and labels aren't the same 
size.
          */
       public void addChoices( List list, List labels)
           throws ArrayIndexOutOfBoundsException
         {
           // If no labels, use list keys
         if( labels == null )
           labels = list;
           // Check sizes
          if(choices== null)
           {
           setChoices(list, labels);
           return;
           }
  
         if( list.size() != labels.size() )
           {// error
           System.out.println( "Error : list and labels size must be the same." );
           throw new java.lang.ArrayIndexOutOfBoundsException( "List of choices and 
list of labels must be of the same size" );
           }
         choices.addAll(list);
         choiceLabels.addAll(labels);
         }
  
         /**
          * Get list of choices Tiles
          */
       public List getChoices( )
         {
         return choices;
         }
  
         /**
          * Get list of labels for Tiles
          */
       public List getChoiceLabels( )
         {
         return choiceLabels;
         }
  
         /**
          * Get label for specified Tile, identified by its key.
          * @param key Tile key
          */
       public String getChoiceLabel( Object key )
         {
         int index = choices.indexOf( key );
         if(index==-1)
           return null;
         return (String)choiceLabels.get(index);
         }
  
         /**
          * Get list of labels for Tile keys passes as parameters
          * @param choiceKeys List of keys to search for labels.
          */
       public List getChoiceLabelsForKeys( List choiceKeys )
         {
         List listLabels = new ArrayList();
  
         Iterator i = choiceKeys.iterator();
         while(i.hasNext())
           {
           Object key = i.next();
           listLabels.add( getChoiceLabel(key) );
           } // end loop
         return listLabels;
         }
  
         /**
          * Check keys passed in parameters, and add it in returned list if available
          * in choices.
          * If a key denote a special key, create appropriate 'definition'.
          * @keys array of keys to add to list.
          */
       public List checkKeys( String keys[] )
         {
         List list = new ArrayList();
  
           // add keys to list
         for(int i=0;i<keys.length;i++)
           {
           String key = keys[i];
           if( key.indexOf( '@' )>0 )
             { // special key
             }
           if( choices.contains( key ) )
             { // ok, add it
             list.add( key );
             }
           } // end loop
         return list;
         }
  
         /**
          * Set labels for choices Tiles.
          */
       protected void setChoiceLabels( List list)
         {
         this.choiceLabels = list;
         }
         /**
          * add list to list of choices Tiles
          */
       protected void addChoiceLabels( List list)
         {
         if(choiceLabels== null)
           {
           setChoiceLabels(list);
           return;
           }
         choiceLabels.addAll(list);
         }
  
  }
  
  
  1.1                  
jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/portal/MenuSettingsForm.java
  
  Index: MenuSettingsForm.java
  ===================================================================
  /*
   */
  
  
  package org.apache.struts.example.tiles.portal;
  
  
  import java.util.List;
  import java.util.ArrayList;
  import org.apache.struts.action.ActionForm;
  
  
  /**
   * Action form used to read data from web page form.
   */
  public final class MenuSettingsForm extends ActionForm  {
  
      /** Validate value */
    protected String validate;
      /** empty list used by reset */
    protected String[] empty = {};
      /** list of items selected by user */
    protected String[] selected;
  
      /**
       * Set selected items
       */
    public void setSelected( String array[] )
      {
      selected = array;
      }
  
      /**
       * Get selected items
       */
    public String[] getSelected()
      {
      return selected;
      }
  
      /**
       * Get selected items
       */
    public String[] getSelectedChoices()
      {
      return empty;
      }
  
     /**
      * Is this form submitted ?
      */
    public boolean isSubmitted()
      {
      return validate != null;
      }
  
     /**
      * Is this form submitted ?
      */
    public void setValidate( String value)
      {
      this.validate = value;
      }
  
      /**
       * Reset properties
       */
    public void reset()
      {
      selected =  empty;
      validate = null;
      }
  }
  
  
  
  
  1.1                  
jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/portal/MenuSettings.java
  
  Index: MenuSettings.java
  ===================================================================
  package org.apache.struts.example.tiles.portal;
  
  import java.util.List;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.HashMap;
  
    /**
     * Objects of this class hold menu settings for one user.
     */
     public class MenuSettings
     {
         /** List of items */
       protected List items = new ArrayList();
  
         /**
          * Get list of items
          */
       public List getItems( )
         {
         return items;
         }
  
         /**
          * Add an item to the list
          */
       public void addItem( Object item )
         {
         items.add( item );
         }
  
         /**
          * Add all items to the list.
          */
       public void addItems( List list )
         {
         items.addAll( list );
         }
  
         /**
          * Reset settings
          */
       public void reset()
         {
         items.clear();
         }
  
       public String toString()
         {
         return "items=" + items;
         }
     }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to