View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.model;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.myfaces.tobago.event.SortActionEvent;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import javax.faces.component.UIColumn;
28  import java.io.Serializable;
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  public class SheetState implements Serializable {
33  
34    private static final long serialVersionUID = 7765536344426661777L;
35  
36    private static final Logger LOG = LoggerFactory.getLogger(SheetState.class);
37  
38    public static final String SEPARATOR = ",";
39  
40    private int first = -1;
41    private String sortedColumnId;
42    private boolean ascending;
43    private String columnWidths;
44    private List<Integer> selectedRows;
45    private Integer[] scrollPosition;
46    private ExpandedState expandedState;
47  
48    public SheetState() {
49      resetSelected();
50    }
51  
52    public void resetSelected() {
53      selectedRows = new ArrayList<Integer>();
54    }
55  
56    public List<Integer> getSelectedRows() {
57      return selectedRows;
58    }
59  
60    public void setSelectedRows(List<Integer> selectedRows) {
61      assert selectedRows != null;
62      this.selectedRows = selectedRows;
63    }
64  
65    public String getSortedColumnId() {
66      return sortedColumnId;
67    }
68  
69    public void setSortedColumnId(String sortedColumnId) {
70      this.sortedColumnId = sortedColumnId;
71    }
72  
73    public boolean isAscending() {
74      return ascending;
75    }
76  
77    public void setAscending(boolean ascending) {
78      this.ascending = ascending;
79    }
80  
81    public String getColumnWidths() {
82      return columnWidths;
83    }
84  
85    public void setColumnWidths(String columnWidths) {
86      this.columnWidths = columnWidths;
87    }
88  
89    public int getFirst() {
90      return first;
91    }
92  
93    public void setFirst(int first) {
94      this.first = first;
95    }
96  
97    public void updateSortState(SortActionEvent sortEvent) {
98  
99      UIColumn actualColumn = sortEvent.getColumn();
100 
101     if (actualColumn.getId().equals(sortedColumnId)) {
102       ascending = !ascending;
103     } else {
104       ascending = true;
105       sortedColumnId = actualColumn.getId();
106     }
107   }
108 
109   public Integer[] getScrollPosition() {
110     return scrollPosition;
111   }
112 
113   public void setScrollPosition(Integer[] scrollPosition) {
114     this.scrollPosition = scrollPosition;
115   }
116 
117   public ExpandedState getExpandedState() {
118     if (expandedState == null) {
119       expandedState = new ExpandedState(2);
120     }
121     return expandedState;
122   }
123 
124   public void setExpandedState(ExpandedState expandedState) {
125     this.expandedState = expandedState;
126   }
127 
128   public static Integer[] parseScrollPosition(String value) {
129     Integer[] position = null;
130     if (!StringUtils.isBlank(value)) {
131       int sep = value.indexOf(";");
132       if (LOG.isInfoEnabled()) {
133         LOG.info("value = \"" + value + "\"  sep = " + sep + "");
134       }
135       if (sep == -1) {
136         throw new NumberFormatException(value);
137       }
138       int left = Integer.parseInt(value.substring(0, sep));
139       int top = Integer.parseInt(value.substring(sep + 1));
140       position = new Integer[2];
141       position[0] = left;
142       position[1] = top;
143     }
144     return position;
145   }
146 }