KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > table > components > inserted > SimpleTableColumnComponent


1 // Copyright 2004, 2005 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.tapestry.contrib.table.components.inserted;
16
17 import org.apache.tapestry.BaseComponent;
18 import org.apache.tapestry.IAsset;
19 import org.apache.tapestry.IRequestCycle;
20 import org.apache.tapestry.contrib.table.components.TableColumns;
21 import org.apache.tapestry.contrib.table.model.ITableColumn;
22 import org.apache.tapestry.contrib.table.model.ITableModel;
23 import org.apache.tapestry.contrib.table.model.ITableModelSource;
24 import org.apache.tapestry.contrib.table.model.ITableRendererListener;
25 import org.apache.tapestry.contrib.table.model.ITableSortingState;
26 import org.apache.tapestry.contrib.table.model.simple.SimpleTableColumn;
27 import org.apache.tapestry.event.PageDetachListener;
28 import org.apache.tapestry.event.PageEvent;
29 import org.apache.tapestry.util.ComponentAddress;
30
31 /**
32  * A component that renders the default column header.
33  *
34  * If the current column is sortable, it renders the header as a link.
35  * Clicking on the link causes the table to be sorted on that column.
36  * Clicking on the link again causes the sorting order to be reversed.
37  *
38  * @author mindbridge
39  */

40 public abstract class SimpleTableColumnComponent
41     extends BaseComponent
42     implements ITableRendererListener, PageDetachListener
43 {
44     // transient
45
private ITableColumn m_objColumn;
46     private ITableModelSource m_objModelSource;
47
48     public SimpleTableColumnComponent()
49     {
50         init();
51     }
52
53     /**
54      * @see org.apache.tapestry.event.PageDetachListener#pageDetached(PageEvent)
55      */

56     public void pageDetached(PageEvent arg0)
57     {
58         init();
59     }
60
61     private void init()
62     {
63         m_objColumn = null;
64         m_objModelSource = null;
65     }
66
67
68     /**
69      * @see org.apache.tapestry.contrib.table.model.ITableRendererListener#initializeRenderer(IRequestCycle, ITableModelSource, ITableColumn, Object)
70      */

71     public void initializeRenderer(
72         IRequestCycle objCycle,
73         ITableModelSource objSource,
74         ITableColumn objColumn,
75         Object JavaDoc objRow)
76     {
77         m_objModelSource = objSource;
78         m_objColumn = objColumn;
79     }
80
81     public ITableModel getTableModel()
82     {
83         return m_objModelSource.getTableModel();
84     }
85
86     public boolean getColumnSorted()
87     {
88         return m_objColumn.getSortable();
89     }
90
91     public String JavaDoc getDisplayName()
92     {
93         if (m_objColumn instanceof SimpleTableColumn) {
94             SimpleTableColumn objSimpleColumn = (SimpleTableColumn) m_objColumn;
95             return objSimpleColumn.getDisplayName();
96         }
97         return m_objColumn.getColumnName();
98     }
99
100     public boolean getIsSorted()
101     {
102         ITableSortingState objSortingState = getTableModel().getSortingState();
103         String JavaDoc strSortColumn = objSortingState.getSortColumn();
104         return m_objColumn.getColumnName().equals(strSortColumn);
105     }
106
107     public IAsset getSortImage()
108     {
109         IAsset objImageAsset;
110
111         IRequestCycle objCycle = getPage().getRequestCycle();
112         ITableSortingState objSortingState = getTableModel().getSortingState();
113         if (objSortingState.getSortOrder()
114             == ITableSortingState.SORT_ASCENDING)
115         {
116             objImageAsset =
117                 (IAsset) objCycle.getAttribute(
118                     TableColumns.TABLE_COLUMN_ARROW_UP_ATTRIBUTE);
119             if (objImageAsset == null)
120                 objImageAsset = getAsset("sortUp");
121         }
122         else
123         {
124             objImageAsset =
125                 (IAsset) objCycle.getAttribute(
126                     TableColumns.TABLE_COLUMN_ARROW_DOWN_ATTRIBUTE);
127             if (objImageAsset == null)
128                 objImageAsset = getAsset("sortDown");
129         }
130
131         return objImageAsset;
132     }
133
134     public Object JavaDoc[] getColumnSelectedParameters()
135     {
136         return new Object JavaDoc[] {
137             new ComponentAddress(m_objModelSource),
138             m_objColumn.getColumnName()};
139     }
140
141     public void columnSelected(IRequestCycle objCycle)
142     {
143         Object JavaDoc[] arrArgs = objCycle.getListenerParameters();
144         ComponentAddress objAddr = (ComponentAddress) arrArgs[0];
145         String JavaDoc strColumnName = (String JavaDoc) arrArgs[1];
146
147         ITableModelSource objSource =
148             (ITableModelSource) objAddr.findComponent(objCycle);
149         ITableModel objModel = objSource.getTableModel();
150
151         ITableSortingState objState = objModel.getSortingState();
152         if (strColumnName.equals(objState.getSortColumn()))
153             objState.setSortColumn(strColumnName, !objState.getSortOrder());
154         else
155             objState.setSortColumn(
156                 strColumnName,
157                 ITableSortingState.SORT_ASCENDING);
158
159         // ensure that the change is saved
160
objSource.fireObservedStateChange();
161     }
162
163 }
164
Popular Tags