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.lang.StringUtils;
021    import org.apache.myfaces.tobago.event.SortActionEvent;
022    import org.slf4j.Logger;
023    import org.slf4j.LoggerFactory;
024    
025    import javax.faces.component.UIColumn;
026    import java.io.Serializable;
027    import java.util.ArrayList;
028    import java.util.List;
029    
030    // TODO find a better solution for this
031    public class SheetState implements Serializable {
032      private static final long serialVersionUID = 7765536344426661777L;
033      private static final Logger LOG = LoggerFactory.getLogger(SheetState.class);
034      public static final String SEPARATOR = ",";
035    
036      private int first = -1;
037      private String sortedColumnId;
038      private boolean ascending;
039      private String columnWidths;
040      private List<Integer> selectedRows;
041      private Integer[] scrollPosition;
042    
043      public SheetState() {
044        resetSelected();
045      }
046    
047      public void resetSelected() {
048        selectedRows = new ArrayList<Integer>();
049      }
050    
051      public List<Integer> getSelectedRows() {
052        return selectedRows;
053      }
054    
055      public void setSelectedRows(List<Integer> selectedRows) {
056        assert selectedRows != null;
057        this.selectedRows = selectedRows;
058      }
059    
060      public String getSortedColumnId() {
061        return sortedColumnId;
062      }
063    
064      public void setSortedColumnId(String sortedColumnId) {
065        this.sortedColumnId = sortedColumnId;
066      }
067    
068      public boolean isAscending() {
069        return ascending;
070      }
071    
072      public void setAscending(boolean ascending) {
073        this.ascending = ascending;
074      }
075    
076      public String getColumnWidths() {
077        return columnWidths;
078      }
079    
080      public void setColumnWidths(String columnWidths) {
081        this.columnWidths = columnWidths;
082      }
083    
084      public int getFirst() {
085        return first;
086      }
087    
088      public void setFirst(int first) {
089        this.first = first;
090      }
091    
092      public void updateSortState(SortActionEvent sortEvent) {
093    
094        UIColumn actualColumn = sortEvent.getColumn();
095    
096        if (actualColumn.getId().equals(sortedColumnId)) {
097          ascending = !ascending;
098        } else {
099          ascending = true;
100          sortedColumnId = actualColumn.getId();
101        }
102      }
103    
104      public Integer[] getScrollPosition() {
105        return scrollPosition;
106      }
107    
108      public void setScrollPosition(Integer[] scrollPosition) {
109        this.scrollPosition = scrollPosition;
110      }
111    
112      public static Integer[] parseScrollPosition(String value) {
113        Integer[] position = null;
114        if (!StringUtils.isBlank(value)) {
115          int sep = value.indexOf(";");
116          if (LOG.isInfoEnabled()) {
117            LOG.info("value = \"" + value + "\"  sep = " + sep + "");
118          }
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    }