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