KickJava   Java API By Example, From Geeks To Geeks.

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


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
28 /**
29  * A component that renders the default column header in a form.
30  *
31  * If the current column is sortable, it renders the header as a link.
32  * Clicking on the link causes the table to be sorted on that column.
33  * Clicking on the link again causes the sorting order to be reversed.
34  *
35  * This component renders links that cause the form to be submitted.
36  * This ensures that the updated data in the other form fields is preserved.
37  *
38  * @author mindbridge
39  */

40 public abstract class SimpleTableColumnFormComponent
41     extends BaseComponent
42     implements ITableRendererListener
43 {
44
45     public abstract ITableColumn getTableColumn();
46     public abstract void setTableColumn(ITableColumn objColumn);
47
48     public abstract ITableModelSource getTableModelSource();
49     public abstract void setTableModelSource(ITableModelSource objSource);
50
51     public abstract String JavaDoc getSelectedColumnName();
52
53     /**
54      * @see org.apache.tapestry.contrib.table.model.ITableRendererListener#initializeRenderer(IRequestCycle, ITableModelSource, ITableColumn, Object)
55      */

56     public void initializeRenderer(
57         IRequestCycle objCycle,
58         ITableModelSource objSource,
59         ITableColumn objColumn,
60         Object JavaDoc objRow)
61     {
62         setTableModelSource(objSource);
63         setTableColumn(objColumn);
64     }
65
66     public ITableModel getTableModel()
67     {
68         return getTableModelSource().getTableModel();
69     }
70
71     public boolean getColumnSorted()
72     {
73         return getTableColumn().getSortable();
74     }
75
76     public String JavaDoc getDisplayName()
77     {
78         ITableColumn objColumn = getTableColumn();
79         
80         if (objColumn instanceof SimpleTableColumn) {
81             SimpleTableColumn objSimpleColumn = (SimpleTableColumn) objColumn;
82             return objSimpleColumn.getDisplayName();
83         }
84         return objColumn.getColumnName();
85     }
86
87     public boolean getIsSorted()
88     {
89         ITableSortingState objSortingState = getTableModel().getSortingState();
90         String JavaDoc strSortColumn = objSortingState.getSortColumn();
91         return getTableColumn().getColumnName().equals(strSortColumn);
92     }
93
94     public IAsset getSortImage()
95     {
96         IAsset objImageAsset;
97
98         IRequestCycle objCycle = getPage().getRequestCycle();
99         ITableSortingState objSortingState = getTableModel().getSortingState();
100         if (objSortingState.getSortOrder()
101             == ITableSortingState.SORT_ASCENDING)
102         {
103             objImageAsset =
104                 (IAsset) objCycle.getAttribute(
105                     TableColumns.TABLE_COLUMN_ARROW_UP_ATTRIBUTE);
106             if (objImageAsset == null)
107                 objImageAsset = getAsset("sortUp");
108         }
109         else
110         {
111             objImageAsset =
112                 (IAsset) objCycle.getAttribute(
113                     TableColumns.TABLE_COLUMN_ARROW_DOWN_ATTRIBUTE);
114             if (objImageAsset == null)
115                 objImageAsset = getAsset("sortDown");
116         }
117
118         return objImageAsset;
119     }
120
121     public void columnSelected(IRequestCycle objCycle)
122     {
123         String JavaDoc strColumnName = getSelectedColumnName();
124         ITableSortingState objState = getTableModel().getSortingState();
125         if (strColumnName.equals(objState.getSortColumn()))
126             objState.setSortColumn(strColumnName, !objState.getSortOrder());
127         else
128             objState.setSortColumn(
129                 strColumnName,
130                 ITableSortingState.SORT_ASCENDING);
131
132         // ensure that the change is saved
133
getTableModelSource().fireObservedStateChange();
134     }
135
136 }
137
Popular Tags