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.slf4j.Logger;
021    import org.slf4j.LoggerFactory;
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 Logger LOG = LoggerFactory.getLogger(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      public void resetSelected() {
050        selectedRows = new ArrayList<Integer>();
051      }
052    
053      public List<Integer> getSelectedRows() {
054        return selectedRows;
055      }
056    
057      public void setSelectedRows(List<Integer> selectedRows) {
058        this.selectedRows = selectedRows;
059      }
060    
061      public String getSortedColumnId() {
062        return sortedColumnId;
063      }
064    
065      public void setSortedColumnId(String sortedColumnId) {
066        this.sortedColumnId = sortedColumnId;
067      }
068    
069      public boolean isAscending() {
070        return ascending;
071      }
072    
073      public void setAscending(boolean ascending) {
074        this.ascending = ascending;
075      }
076    
077      public String getColumnWidths() {
078        return columnWidths;
079      }
080    
081      public void setColumnWidths(String columnWidths) {
082        this.columnWidths = columnWidths;
083      }
084    
085      public int getFirst() {
086        return first;
087      }
088    
089      public void setFirst(int first) {
090        this.first = first;
091      }
092    
093      public void updateSortState(SortActionEvent sortEvent) {
094    
095        UIColumn actualColumn = sortEvent.getColumn();
096    
097        if (actualColumn.getId().equals(sortedColumnId)) {
098          ascending = !ascending;
099        } else {
100          ascending = true;
101          sortedColumnId = actualColumn.getId();
102        }
103      }
104    
105      public Integer[] getScrollPosition() {
106        return scrollPosition;
107      }
108    
109      public void setScrollPosition(Integer[] scrollPosition) {
110        this.scrollPosition = scrollPosition;
111      }
112    
113      public static Integer[] parseScrollPosition(String value) {
114        Integer[] position = null;
115        if (!StringUtils.isBlank(value)) {
116          int sep = value.indexOf(";");
117          if (LOG.isInfoEnabled()) {
118            LOG.info("value = \"" + value + "\"  sep = " + sep + "");
119          }
120          if (sep == -1) {
121            throw new NumberFormatException(value);
122          }
123          int left = Integer.parseInt(value.substring(0, sep));
124          int top = Integer.parseInt(value.substring(sep + 1));
125          position = new Integer[2];
126          position[0] = left;
127          position[1] = top;
128        }
129        return position;
130      }
131    }