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