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