001    package org.apache.myfaces.tobago.model;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.apache.commons.lang.StringUtils;
023    
024    import org.apache.myfaces.tobago.event.SortActionEvent;
025    
026    import javax.faces.component.UIColumn;
027    import java.io.Serializable;
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    // TODO find a better solution for this
032    public class SheetState implements Serializable {
033      private static final long serialVersionUID = 7765536344426661777L;
034      private static final Log LOG = LogFactory.getLog(SheetState.class);
035      public static final String SEPARATOR = ",";
036    
037      private int first = -1;
038      private String  sortedColumnId;
039      private boolean ascending;
040      private String columnWidths;
041      private List<Integer> selectedRows;
042      private Integer[] scrollPosition;
043    
044      public SheetState() {
045        resetSelected();
046      }
047    
048    
049    
050      public void resetSelected() {
051        selectedRows = new ArrayList<Integer>();
052      }
053    
054      public List<Integer> getSelectedRows() {
055        return selectedRows;
056      }
057    
058      public void setSelectedRows(List<Integer> selectedRows) {
059        this.selectedRows = selectedRows;
060      }
061    
062      public String getSortedColumnId() {
063        return sortedColumnId;
064      }
065    
066      public void setSortedColumnId(String sortedColumnId) {
067        this.sortedColumnId = sortedColumnId;
068      }
069    
070      public boolean isAscending() {
071        return ascending;
072      }
073    
074      public void setAscending(boolean ascending) {
075        this.ascending = ascending;
076      }
077    
078      public String getColumnWidths() {
079        return columnWidths;
080      }
081    
082      public void setColumnWidths(String columnWidths) {
083        this.columnWidths = columnWidths;
084      }
085    
086      public int getFirst() {
087        return first;
088      }
089    
090      public void setFirst(int first) {
091        this.first = first;
092      }
093    
094      public void updateSortState(SortActionEvent sortEvent) {
095    
096        UIColumn actualColumn = sortEvent.getColumn();
097    
098        if (actualColumn.getId().equals(sortedColumnId)) {
099          ascending = !ascending;
100        } else {
101          ascending = true;
102          sortedColumnId = actualColumn.getId();
103        }
104      }
105    
106      public Integer[] getScrollPosition() {
107        return scrollPosition;
108      }
109    
110      public void setScrollPosition(Integer[] scrollPosition) {
111        this.scrollPosition = scrollPosition;
112      }
113    
114      public static Integer[] parseScrollPosition(String value) {
115        Integer[] position = null;
116        if (!StringUtils.isBlank(value)) {
117          int sep = value.indexOf(";");
118          LOG.info("value = \"" + value + "\"  sep = " + sep + "");
119          if (sep == -1) {
120            throw new NumberFormatException(value);
121          }
122          int left = Integer.parseInt(value.substring(0, sep));
123          int top = Integer.parseInt(value.substring(sep + 1));
124          position = new Integer[2];
125          position[0] = left;
126          position[1] = top;
127        }
128        return position;
129      }
130    }