Home » tapestry-src-5.0.19 » org.apache.tapestry5.corelib.components » [javadoc | source]

    1   // Copyright 2007, 2008 The Apache Software Foundation
    2   //
    3   // Licensed under the Apache License, Version 2.0 (the "License");
    4   // you may not use this file except in compliance with the License.
    5   // You may obtain a copy of the License at
    6   //
    7   //     http://www.apache.org/licenses/LICENSE-2.0
    8   //
    9   // Unless required by applicable law or agreed to in writing, software
   10   // distributed under the License is distributed on an "AS IS" BASIS,
   11   // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12   // See the License for the specific language governing permissions and
   13   // limitations under the License.
   14   
   15   package org.apache.tapestry5.corelib.components;
   16   
   17   import org.apache.tapestry5.Asset;
   18   import org.apache.tapestry5.Block;
   19   import org.apache.tapestry5.ComponentResources;
   20   import org.apache.tapestry5.PropertyOverrides;
   21   import org.apache.tapestry5.annotations;
   22   import org.apache.tapestry5.beaneditor.PropertyModel;
   23   import org.apache.tapestry5.grid.ColumnSort;
   24   import org.apache.tapestry5.grid.GridConstants;
   25   import org.apache.tapestry5.grid.GridModel;
   26   import org.apache.tapestry5.grid.GridSortModel;
   27   import org.apache.tapestry5.internal.InternalConstants;
   28   import org.apache.tapestry5.internal.TapestryInternalUtils;
   29   import org.apache.tapestry5.ioc.Messages;
   30   import org.apache.tapestry5.ioc.annotations.Inject;
   31   import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
   32   
   33   import java.util.List;
   34   
   35   /**
   36    * Renders out the column headers for the grid, including links (where appropriate) to control column sorting.
   37    */
   38   @SupportsInformalParameters
   39   public class GridColumns
   40   {
   41       /**
   42        * The object that provides access to bean and data models, which is typically the enclosing Grid component.
   43        */
   44       @Parameter(value = "componentResources.container")
   45       private GridModel gridModel;
   46   
   47       /**
   48        * If true, then the CSS class on each <TH> element will be omitted, which can reduce the amount of output
   49        * from the component overall by a considerable amount. Leave this as false, the default, when you are leveraging
   50        * the CSS to customize the look and feel of particular columns.
   51        */
   52       @Parameter
   53       private boolean lean;
   54   
   55       /**
   56        * Where to look for informal parameter Blocks used to override column headers.  The default is to look for such
   57        * overrides in the GridColumns component itself, but this is usually overridden.
   58        */
   59       @Parameter("this")
   60       private PropertyOverrides overrides;
   61   
   62       /**
   63        * If not null, then each link is output as a link to update the specified zone.
   64        */
   65       @Parameter
   66       private String zone;
   67   
   68       @SuppressWarnings("unused")
   69       @Component(
   70               parameters = {"event=sort", "disabled=sortDisabled", "context=columnContext", "class=sortLinkClass",
   71                       "zone=inherit:zone"})
   72       private EventLink sort, sort2;
   73   
   74       @Inject
   75       @Path("sort-asc.png")
   76       private Asset ascendingAsset;
   77   
   78       @Inject
   79       @Path("sort-desc.png")
   80       private Asset descendingAsset;
   81   
   82       @Inject
   83       @Path("sortable.png")
   84       private Asset sortableAsset;
   85   
   86       @Inject
   87       private Messages messages;
   88   
   89       @Inject
   90       private Block standardHeader;
   91   
   92       /**
   93        * Optional output parameter that stores the current column index.
   94        */
   95       @Parameter
   96       @Property
   97       private int index;
   98   
   99       /**
  100        * Caches the index of the last column.
  101        */
  102       private int lastColumnIndex;
  103   
  104       @Property(write = false)
  105       private PropertyModel columnModel;
  106   
  107       @Inject
  108       private ComponentResources resources;
  109   
  110       void setupRender()
  111       {
  112           lastColumnIndex = gridModel.getDataModel().getPropertyNames().size() - 1;
  113       }
  114   
  115       public boolean isSortDisabled()
  116       {
  117           return !columnModel.isSortable();
  118       }
  119   
  120       public String getSortLinkClass()
  121       {
  122           switch (getSortForColumn())
  123           {
  124               case ASCENDING:
  125                   return GridConstants.SORT_ASCENDING_CLASS;
  126   
  127               case DESCENDING:
  128                   return GridConstants.SORT_DESCENDING_CLASS;
  129   
  130               default:
  131                   return null;
  132           }
  133       }
  134   
  135       private ColumnSort getSortForColumn()
  136       {
  137           GridSortModel sortModel = gridModel.getSortModel();
  138   
  139           String columnId = columnModel.getId();
  140   
  141           return sortModel.getColumnSort(columnId);
  142       }
  143   
  144       public String getHeaderClass()
  145       {
  146           List<String> classes = CollectionFactory.newList();
  147   
  148           if (!lean) classes.add(columnModel.getId());
  149   
  150           String sort = getSortLinkClass();
  151   
  152           if (sort != null) classes.add(sort);
  153   
  154           if (index == 0) classes.add(GridConstants.FIRST_CLASS);
  155   
  156           if (index == lastColumnIndex) classes.add(GridConstants.LAST_CLASS);
  157   
  158           return TapestryInternalUtils.toClassAttributeValue(classes);
  159       }
  160   
  161       public boolean isActiveSortColumn()
  162       {
  163           return getSortForColumn() != ColumnSort.UNSORTED;
  164       }
  165   
  166       /**
  167        * Normal, non-Ajax event handler.
  168        */
  169   
  170       void onSort(String columnId)
  171       {
  172           gridModel.getSortModel().updateSort(columnId);
  173       }
  174   
  175       /**
  176        * Ajax event handler, which carries the zone id.
  177        */
  178       boolean onSort(String columnId, String zone)
  179       {
  180           onSort(columnId);
  181   
  182           resources.triggerEvent(InternalConstants.GRID_INPLACE_UPDATE, new Object[] {zone}, null);
  183   
  184           // Event is handled, don't trigger further event handler methods.
  185   
  186           return true;
  187       }
  188   
  189       public Asset getIcon()
  190       {
  191           switch (getSortForColumn())
  192           {
  193               case ASCENDING:
  194                   return ascendingAsset;
  195   
  196               case DESCENDING:
  197                   return descendingAsset;
  198   
  199               default:
  200                   return sortableAsset;
  201           }
  202       }
  203   
  204       public Object getColumnContext()
  205       {
  206           if (zone == null) return columnModel.getId();
  207   
  208           return new Object[] {columnModel.getId(), zone};
  209       }
  210   
  211       public String getIconLabel()
  212       {
  213           switch (getSortForColumn())
  214           {
  215               case ASCENDING:
  216                   return messages.get("ascending");
  217               case DESCENDING:
  218                   return messages.get("descending");
  219               default:
  220                   return messages.get("sortable");
  221           }
  222       }
  223   
  224       public List<String> getColumnNames()
  225       {
  226           return gridModel.getDataModel().getPropertyNames();
  227       }
  228   
  229   
  230       public void setColumnName(String columnName)
  231       {
  232           columnModel = gridModel.getDataModel().get(columnName);
  233       }
  234   
  235   
  236       public Block getBlockForColumn()
  237       {
  238           Block override = overrides.getOverrideBlock(columnModel.getId() + "Header");
  239   
  240           if (override != null) return override;
  241   
  242           return standardHeader;
  243       }
  244   }

Home » tapestry-src-5.0.19 » org.apache.tapestry5.corelib.components » [javadoc | source]