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