001:// Copyright 2007 The Apache Software Foundation
002://
003:// Licensed under the Apache License, Version 2.0 (the "License");
004:// you may not use this file except in compliance with the License.
005:// You may obtain a copy of the License at
006://
007:// http://www.apache.org/licenses/LICENSE-2.0
008://
009:// Unless required by applicable law or agreed to in writing, software
010:// distributed under the License is distributed on an "AS IS" BASIS,
011:// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012:// See the License for the specific language governing permissions and
013:// limitations under the License.
014:
015:package org.apache.tapestry.corelib.components;
016:
017:import java.util.List;
018:
019:import org.apache.tapestry.Asset;
020:import org.apache.tapestry.annotations.Component;
021:import org.apache.tapestry.annotations.Inject;
022:import org.apache.tapestry.annotations.Parameter;
023:import org.apache.tapestry.annotations.Path;
024:import org.apache.tapestry.beaneditor.PropertyModel;
025:import org.apache.tapestry.grid.GridModelProvider;
026:import org.apache.tapestry.ioc.Messages;
027:
028:/**
029: * Renders out the column headers for the grid. Eventually, this will include control over column
030: * sorting, perhaps even column ordering.
031: */
032:public class GridColumns
033:{
034: /**
035: * The object that provides access to bean and data models, which is typically the enclosing
036: * Grid component.
037: */
038: @Parameter(value="componentResources.container")
039: private GridModelProvider _dataProvider;
040:
041: private PropertyModel _columnModel;
042:
043: /**
044: * The column which is currently being sorted. This value is the column's
045: * {@link PropertyModel#getId() id}, not its {@link PropertyModel#getPropertyName() name}.
046: * This parameter may be null, in which case no column is being used for sorting.
047: */
048: @Parameter(required=true)
049: private String _sortColumnId;
050:
051: /** If true, then the sort is ascending (A - Z), if false the descending (Z - A). */
052: @Parameter(required=true)
053: private boolean _sortAscending;
054:
055: @SuppressWarnings("unused")
056: @Component(parameters={"disabled=sortDisabled","context=columnModel.id","class=sortLinkClass"})
057: private ActionLink _sort, _sort2;
058:
059: @Inject @Path("sort-asc.png")
060: private Asset _ascendingAsset;
061:
062: @Inject @Path("sort-desc.png")
063: private Asset _descendingAsset;
064:
065: @Inject @Path("sortable.png")
066: private Asset _sortableAsset;
067:
068: @Inject
069: private Messages _messages;
070:
071: public boolean isSortDisabled()
072: {
073: return !_columnModel.isSortable();
074: }
075:
076: public String getSortLinkClass()
077: {
078: if (isActiveSortColumn())
079: return _sortAscending ? "t-sort-column-ascending" : "t-sort-column-descending";
080:
081: return null;
082: }
083:
084: public boolean isActiveSortColumn()
085: {
086: return _columnModel.getId().equals(_sortColumnId);
087: }
088:
089: void onActionFromSort(String columnId)
090: {
091: if (columnId.equals(_sortColumnId))
092: {
093: _sortAscending = !_sortAscending;
094: }
095: else
096: {
097: _sortColumnId = columnId;
098: _sortAscending = true;
099: }
100: }
101:
102: void onActionFromSort2(String columnId) { onActionFromSort(columnId); }
103:
104: public Asset getIcon()
105: {
106: if (isActiveSortColumn())
107: return _sortAscending ? _ascendingAsset : _descendingAsset;
108:
109: return _sortableAsset;
110: }
111:
112: public String getIconLabel()
113: {
114: String key = isActiveSortColumn() ? (_sortAscending ? "ascending" : "descending" ) : "sortable";
115:
116: return _messages.get(key);
117: }
118:
119: public List<String> getColumnNames()
120: {
121: return _dataProvider.getDataModel().getPropertyNames();
122: }
123:
124: public PropertyModel getColumnModel()
125: {
126: return _columnModel;
127: }
128:
129: public void setColumnName(String columnName)
130: {
131: _columnModel = _dataProvider.getDataModel().get(columnName);
132: }
133:
134: public String getCellClass()
135: {
136: return _columnModel.getId() + "-header";
137: }
138:}
|