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.myfaces.tobago.event.SortActionEvent;
23  import org.apache.myfaces.tobago.internal.util.StringUtils;
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, ScrollPositionState {
33  
34    private static final long serialVersionUID = 2L;
35  
36    private static final Logger LOG = LoggerFactory.getLogger(SheetState.class);
37  
38    public static final String SEPARATOR = ",";
39  
40    private int first;
41    private String sortedColumnId;
42    private boolean ascending;
43    private boolean toBeSorted;
44    private List<Integer> columnWidths;
45    private List<Integer> selectedRows;
46    private ScrollPosition scrollPosition;
47    private ExpandedState expandedState;
48    private SelectedState selectedState;
49  
50    public SheetState() {
51      reset();
52    }
53  
54    public void reset() {
55      first = -1;
56      sortedColumnId = null;
57      ascending = true;
58      toBeSorted = false;
59      columnWidths = null;
60      resetSelected();
61      if (expandedState != null) {
62        expandedState.reset();
63      }
64      if (selectedState != null) {
65        selectedState.clear();
66      }
67      if (scrollPosition != null) {
68        scrollPosition.clear();
69      } else {
70        scrollPosition = new ScrollPosition();
71      }
72    }
73  
74    public void resetSelected() {
75      selectedRows = new ArrayList<Integer>();
76    }
77  
78    public List<Integer> getSelectedRows() {
79      return selectedRows;
80    }
81  
82    public void setSelectedRows(final List<Integer> selectedRows) {
83      assert selectedRows != null;
84      this.selectedRows = selectedRows;
85    }
86  
87    public String getSortedColumnId() {
88      return sortedColumnId;
89    }
90  
91    public void setSortedColumnId(final String sortedColumnId) {
92      if (StringUtils.notEquals(this.sortedColumnId, sortedColumnId)) {
93        this.sortedColumnId = sortedColumnId;
94        toBeSorted = true;
95      }
96    }
97  
98    public boolean isAscending() {
99      return ascending;
100   }
101 
102   public void setAscending(final boolean ascending) {
103     if (this.ascending != ascending) {
104       this.ascending = ascending;
105       toBeSorted = true;
106     }
107   }
108 
109   public List<Integer> getColumnWidths() {
110     return columnWidths;
111   }
112 
113   public void setColumnWidths(final List<Integer> columnWidths) {
114     this.columnWidths = columnWidths;
115   }
116 
117   public int getFirst() {
118     return first;
119   }
120 
121   public void setFirst(final int first) {
122     this.first = first;
123   }
124 
125   public void updateSortState(final SortActionEvent sortEvent) {
126 
127     final UIColumn actualColumn = sortEvent.getColumn();
128 
129     if (actualColumn.getId().equals(sortedColumnId)) {
130       setAscending(!isAscending());
131     } else {
132       setAscending(true);
133       setSortedColumnId(actualColumn.getId());
134     }
135   }
136 
137   @Override
138   public ScrollPosition getScrollPosition() {
139     return scrollPosition;
140   }
141 
142   public void setScrollPosition(final ScrollPosition scrollPosition) {
143     this.scrollPosition = scrollPosition;
144   }
145 
146   public ExpandedState getExpandedState() {
147     if (expandedState == null) {
148       expandedState = new ExpandedState(2);
149     }
150     return expandedState;
151   }
152 
153   public void setExpandedState(final ExpandedState expandedState) {
154     this.expandedState = expandedState;
155   }
156 
157   public SelectedState getSelectedState() {
158     if (selectedState == null) {
159       selectedState = new SelectedState();
160     }
161     return selectedState;
162   }
163 
164   public void setSelectedState(final SelectedState selectedState) {
165     this.selectedState = selectedState;
166   }
167 
168   public boolean isToBeSorted() {
169     return toBeSorted;
170   }
171 
172   public void setToBeSorted(boolean toBeSorted) {
173     this.toBeSorted = toBeSorted;
174   }
175 
176   /**
177    * @deprecated since 2.0.0
178    */
179   @Deprecated
180   public static Integer[] parseScrollPosition(final String value) {
181     Integer[] position = null;
182     if (!StringUtils.isBlank(value)) {
183       final int sep = value.indexOf(";");
184       if (LOG.isInfoEnabled()) {
185         LOG.info("value = \"" + value + "\"  sep = " + sep + "");
186       }
187       if (sep == -1) {
188         throw new NumberFormatException(value);
189       }
190       final int left = Integer.parseInt(value.substring(0, sep));
191       final int top = Integer.parseInt(value.substring(sep + 1));
192       position = new Integer[2];
193       position[0] = left;
194       position[1] = top;
195     }
196     return position;
197   }
198 }