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