001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.myfaces.tobago.model;
021
022 import org.apache.commons.logging.Log;
023 import org.apache.commons.logging.LogFactory;
024 import org.apache.commons.lang.StringUtils;
025
026 import org.apache.myfaces.tobago.event.SortActionEvent;
027
028 import javax.faces.component.UIColumn;
029 import java.io.Serializable;
030 import java.util.ArrayList;
031 import java.util.List;
032
033 // TODO find a better solution for this
034 public class SheetState implements Serializable {
035 private static final long serialVersionUID = 7765536344426661777L;
036 private static final Log LOG = LogFactory.getLog(SheetState.class);
037 public static final String SEPARATOR = ",";
038
039 private int first = -1;
040 private String sortedColumnId;
041 private boolean ascending;
042 private String columnWidths;
043 private List<Integer> selectedRows;
044 private Integer[] scrollPosition;
045
046 public SheetState() {
047 resetSelected();
048 }
049
050
051 public void resetSelected() {
052 selectedRows = new ArrayList<Integer>();
053 }
054
055 public List<Integer> getSelectedRows() {
056 return selectedRows;
057 }
058
059 public void setSelectedRows(List<Integer> selectedRows) {
060 this.selectedRows = selectedRows;
061 }
062
063 public String getSortedColumnId() {
064 return sortedColumnId;
065 }
066
067 public void setSortedColumnId(String sortedColumnId) {
068 this.sortedColumnId = sortedColumnId;
069 }
070
071 public boolean isAscending() {
072 return ascending;
073 }
074
075 public void setAscending(boolean ascending) {
076 this.ascending = ascending;
077 }
078
079 public String getColumnWidths() {
080 return columnWidths;
081 }
082
083 public void setColumnWidths(String columnWidths) {
084 this.columnWidths = columnWidths;
085 }
086
087 public int getFirst() {
088 return first;
089 }
090
091 public void setFirst(int first) {
092 this.first = first;
093 }
094
095 public void updateSortState(SortActionEvent sortEvent) {
096
097 UIColumn actualColumn = sortEvent.getColumn();
098
099 if (actualColumn.getId().equals(sortedColumnId)) {
100 ascending = !ascending;
101 } else {
102 ascending = true;
103 sortedColumnId = actualColumn.getId();
104 }
105 }
106
107 public Integer[] getScrollPosition() {
108 return scrollPosition;
109 }
110
111 public void setScrollPosition(Integer[] scrollPosition) {
112 this.scrollPosition = scrollPosition;
113 }
114
115 public static Integer[] parseScrollPosition(String value) {
116 Integer[] position = null;
117 if (!StringUtils.isBlank(value)) {
118 int sep = value.indexOf(";");
119 if (LOG.isInfoEnabled()) {
120 LOG.info("value = \"" + value + "\" sep = " + sep + "");
121 }
122 if (sep == -1) {
123 throw new NumberFormatException(value);
124 }
125 int left = Integer.parseInt(value.substring(0, sep));
126 int top = Integer.parseInt(value.substring(sep + 1));
127 position = new Integer[2];
128 position[0] = left;
129 position[1] = top;
130 }
131 return position;
132 }
133 }