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    public class SimpleTableStateAdaptor implements SqueezeAdaptor
028    {
029    
030        private static final String PREFIX = "t";
031    
032        /**
033         * @see SqueezeAdaptor#getPrefix()
034         */
035        public String getPrefix()
036        {
037            return PREFIX;
038        }
039    
040        /**
041         * @see SqueezeAdaptor#getDataClass()
042         */
043        public Class getDataClass()
044        {
045            return SimpleTableState.class;
046        }
047    
048        public String squeeze(DataSqueezer squeezer, Object data)
049        {
050            SimpleTableState objState = (SimpleTableState) data;
051    
052            StringBuffer buf = new StringBuffer();
053            buf.append(objState.getPagingState().getPageSize());
054            buf.append(":");
055            buf.append(objState.getPagingState().getCurrentPage());
056            buf.append(":");
057            String strSortColumn = objState.getSortingState().getSortColumn();
058            if (strSortColumn == null)
059                strSortColumn = "";
060            buf.append(strSortColumn);
061            buf.append(":");
062            buf.append(objState.getSortingState().getSortOrder() ? 'T' : 'F');
063    
064            return buf.toString();
065        }
066    
067        public Object unsqueeze(DataSqueezer squeezer, String string)
068        {
069            StringTokenizer strTok = new StringTokenizer(string, ":");
070            if (strTok.countTokens() != 4)
071                throw new ApplicationRuntimeException(TableMessages.invalidTableStateFormat(string));
072            int nPageSize = Integer.parseInt(strTok.nextToken());
073            int nCurrentPage = Integer.parseInt(strTok.nextToken());
074            String strSortColumn = strTok.nextToken();
075            if (strSortColumn.equals(""))
076                strSortColumn = null;
077            boolean bSortOrder = strTok.nextToken().equals("T");
078    
079            return new SimpleTableState(nPageSize, nCurrentPage, strSortColumn, bSortOrder);
080        }
081    
082    }