001/* 002 * #%L 003 * GwtMaterial 004 * %% 005 * Copyright (C) 2015 - 2017 GwtMaterialDesign 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package gwt.material.design.client.data; 021 022import gwt.material.design.client.base.constants.TableCssName; 023import gwt.material.design.client.ui.MaterialIcon; 024import gwt.material.design.client.ui.table.TableHeader; 025import gwt.material.design.client.ui.table.cell.Column; 026 027/** 028 * @author Ben Dol 029 */ 030public class SortContext<T> { 031 private TableHeader tableHeader; 032 protected Column<T, ?> sortColumn; 033 protected SortDir sortDir = SortDir.ASC; 034 protected boolean sorted; 035 036 protected SortContext(SortContext<T> sortContext) { 037 if(sortContext != null) { 038 tableHeader = sortContext.tableHeader; 039 sortColumn = sortContext.sortColumn; 040 sortDir = sortContext.sortDir; 041 sorted = sortContext.sorted; 042 } 043 } 044 045 protected SortContext(Column<T, ?> sortColumn, TableHeader tableHeader) { 046 this.sortColumn = sortColumn; 047 if(!this.sortColumn.isDefaultSortAscending()) { 048 sortDir = SortDir.DESC; 049 } 050 setTableHeader(tableHeader); 051 } 052 053 public Column<T, ?> getSortColumn() { 054 return sortColumn; 055 } 056 057 public void setSortDir(SortDir sortDir) { 058 this.sortDir = sortDir; 059 } 060 061 public SortDir getSortDir() { 062 return sortDir; 063 } 064 065 protected void setSortColumn(Column<T, ?> sortColumn) { 066 reset(); 067 this.sortColumn = sortColumn; 068 069 if(sortColumn != null && !sortColumn.isDefaultSortAscending()) { 070 sortDir = SortDir.DESC; 071 } else { 072 sortDir = SortDir.ASC; 073 } 074 } 075 076 protected void setTableHeader(TableHeader tableHeader) { 077 if(tableHeader.getSortIcon() == null) { 078 tableHeader.setSortIcon(new MaterialIcon()); 079 } 080 this.tableHeader = tableHeader; 081 } 082 083 public TableHeader getTableHeader() { 084 return tableHeader; 085 } 086 087 protected void reverse() { 088 sortDir = sortDir.reverse(); 089 } 090 091 protected void reset() { 092 tableHeader.removeSortIcon(); 093 tableHeader.removeStyleName(TableCssName.SELECTED); 094 sortDir = SortDir.ASC; 095 sortColumn = null; 096 } 097 098 public boolean isSorted() { 099 return sorted; 100 } 101 102 public void setSorted(boolean sorted) { 103 this.sorted = sorted; 104 } 105}