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.ui.table;
021
022import com.google.gwt.dom.client.Style.Float;
023import com.google.gwt.user.client.DOM;
024import gwt.material.design.client.base.constants.TableCssName;
025import gwt.material.design.client.constants.Display;
026import gwt.material.design.client.constants.IconSize;
027import gwt.material.design.client.ui.MaterialIcon;
028import gwt.material.design.client.ui.html.Div;
029import gwt.material.design.client.ui.html.Span;
030
031/**
032 * @author Ben Dol
033 */
034public class TableHeader extends TableData {
035
036    private Span headerLbl;
037    private MaterialIcon sortIcon;
038    private Div headerWrap = new Div();
039
040    public TableHeader() {
041        super(DOM.createTH());
042    }
043
044    public TableHeader(String classNames) {
045        super(DOM.createTH(), classNames);
046    }
047
048    public TableHeader(MaterialIcon sortIcon) {
049        this();
050        setSortIcon(sortIcon);
051    }
052
053    @Override
054    protected void onLoad() {
055        super.onLoad();
056
057        headerWrap.setHeight("100%");
058        headerWrap.setDisplay(Display.FLEX);
059        add(headerWrap);
060    }
061
062    public void setHeader(String header) {
063        if(headerLbl == null) {
064            headerLbl = new Span(header);
065            headerLbl.setStyleName(TableCssName.TABLE_HEADER);
066            headerWrap.add(headerLbl);
067        } else {
068            headerLbl.setText(header);
069        }
070    }
071
072    public String getHeader() {
073        return headerLbl != null ? headerLbl.getText() : "";
074    }
075
076    public MaterialIcon getSortIcon() {
077        return sortIcon;
078    }
079
080    public void setSortIcon(MaterialIcon sortIcon) {
081        removeSortIcon();
082        this.sortIcon = sortIcon;
083
084        if(sortIcon != null) {
085            IconSize iconSize = this.sortIcon.getIconSize();
086            if(iconSize == null) {
087                this.sortIcon.setIconSize(IconSize.SMALL);
088            }
089            this.sortIcon.getElement().getStyle().setFloat(Float.LEFT);
090            headerWrap.insert(this.sortIcon, 0);
091        }
092    }
093
094    public void removeSortIcon() {
095        if(sortIcon != null) {
096            sortIcon.setInnerText("");
097        }
098    }
099}