001// Copyright 2004, 2005 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
015package org.apache.tapestry.contrib.table.components;
016
017import java.io.Serializable;
018
019import org.apache.tapestry.contrib.table.model.ITableModel;
020import org.apache.tapestry.contrib.table.model.ITableSessionStateManager;
021import org.apache.tapestry.contrib.table.model.simple.SimpleTableState;
022
023/**
024 *  Acts like {@link org.apache.tapestry.contrib.table.model.common.FullTableSessionStateManager} 
025 *  if the model is provided via the tableModel parameter; 
026 *  saves only the model state otherwise. 
027 * 
028 *  @author mindbridge
029 */
030public class TableViewSessionStateManager implements ITableSessionStateManager
031{
032    private TableView m_objView;
033
034    public TableViewSessionStateManager(TableView objView)
035    {
036        m_objView = objView;
037    }
038    
039    /**
040     * @see org.apache.tapestry.contrib.table.model.ITableSessionStateManager#getSessionState(org.apache.tapestry.contrib.table.model.ITableModel)
041     */
042    public Serializable getSessionState(ITableModel objModel)
043    {
044        // if the model is provided using the 'tableModel' parameter, 
045        // emulate FullTableSessionStateManager and save everything
046        // (backward compatibility)
047        if (m_objView.getCachedTableModelValue() != null)
048            return (Serializable) objModel;
049            
050        // otherwise save only the state
051        return new SimpleTableState(objModel.getPagingState(), objModel.getSortingState());
052    }
053
054    /**
055     * @see org.apache.tapestry.contrib.table.model.ITableSessionStateManager#recreateTableModel(java.io.Serializable)
056     */
057    public ITableModel recreateTableModel(Serializable objState)
058    {
059        // if the state implements ITableModel, return itself
060        // (backward compatibility)
061        if (objState instanceof ITableModel)
062            return (ITableModel) objState;
063            
064        // otherwise have the component re-generate the model using the provided state
065        return m_objView.generateTableModel((SimpleTableState) objState);
066    }
067
068}