001    // Copyright 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    
015    /*
016     * Created on Jul 6, 2005
017     */
018    package org.apache.tapestry.contrib.table.model.simple;
019    
020    import java.util.StringTokenizer;
021    
022    import org.apache.hivemind.ApplicationRuntimeException;
023    import org.apache.tapestry.contrib.table.components.TableMessages;
024    import org.apache.tapestry.services.DataSqueezer;
025    import org.apache.tapestry.util.io.SqueezeAdaptor;
026    
027    /**
028     * 
029     * @author mb
030     */
031    public class SimpleTableStateAdaptor implements SqueezeAdaptor
032    {
033    
034        private static final String PREFIX = "t";
035    
036        /**
037         * @see SqueezeAdaptor#getPrefix()
038         */
039        public String getPrefix()
040        {
041            return PREFIX;
042        }
043    
044        /**
045         * @see SqueezeAdaptor#getDataClass()
046         */
047        public Class getDataClass()
048        {
049            return SimpleTableState.class;
050        }
051    
052        public String squeeze(DataSqueezer squeezer, Object data)
053        {
054            SimpleTableState objState = (SimpleTableState) data;
055    
056            StringBuffer buf = new StringBuffer();
057            buf.append(objState.getPagingState().getPageSize());
058            buf.append(":");
059            buf.append(objState.getPagingState().getCurrentPage());
060            buf.append(":");
061            String strSortColumn = objState.getSortingState().getSortColumn();
062            if (strSortColumn == null)
063                strSortColumn = "";
064            buf.append(strSortColumn);
065            buf.append(":");
066            buf.append(objState.getSortingState().getSortOrder() ? 'T' : 'F');
067    
068            return buf.toString();
069        }
070    
071        public Object unsqueeze(DataSqueezer squeezer, String string)
072        {
073            StringTokenizer strTok = new StringTokenizer(string, ":");
074            if (strTok.countTokens() != 4)
075                throw new ApplicationRuntimeException(TableMessages.invalidTableStateFormat(string));
076            int nPageSize = Integer.parseInt(strTok.nextToken());
077            int nCurrentPage = Integer.parseInt(strTok.nextToken());
078            String strSortColumn = strTok.nextToken();
079            if (strSortColumn.equals(""))
080                strSortColumn = null;
081            boolean bSortOrder = strTok.nextToken().equals("T");
082    
083            return new SimpleTableState(nPageSize, nCurrentPage, strSortColumn, bSortOrder);
084        }
085    
086    }