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 = new ArrayList<Integer>();
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 boolean isDefinedColumnWidths() {
118     for (Integer columnWidth : columnWidths) {
119       if(columnWidth < 0) {
120         return false;
121       }
122     }
123     return columnWidths.size() > 0;
124   }
125 
126   public int getFirst() {
127     return first;
128   }
129 
130   public void setFirst(final int first) {
131     this.first = first;
132   }
133 
134   public void updateSortState(final SortActionEvent sortEvent) {
135 
136     final UIColumn actualColumn = sortEvent.getColumn();
137 
138     if (actualColumn.getId().equals(sortedColumnId)) {
139       setAscending(!isAscending());
140     } else {
141       setAscending(true);
142       setSortedColumnId(actualColumn.getId());
143     }
144   }
145 
146   @Override
147   public ScrollPosition getScrollPosition() {
148     return scrollPosition;
149   }
150 
151   public void setScrollPosition(final ScrollPosition scrollPosition) {
152     this.scrollPosition = scrollPosition;
153   }
154 
155   public ExpandedState getExpandedState() {
156     if (expandedState == null) {
157       expandedState = new ExpandedState(2);
158     }
159     return expandedState;
160   }
161 
162   public void setExpandedState(final ExpandedState expandedState) {
163     this.expandedState = expandedState;
164   }
165 
166   public SelectedState getSelectedState() {
167     if (selectedState == null) {
168       selectedState = new SelectedState();
169     }
170     return selectedState;
171   }
172 
173   public void setSelectedState(final SelectedState selectedState) {
174     this.selectedState = selectedState;
175   }
176 
177   public boolean isToBeSorted() {
178     return toBeSorted;
179   }
180 
181   public void setToBeSorted(boolean toBeSorted) {
182     this.toBeSorted = toBeSorted;
183   }
184 }