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.Element;
023import com.google.gwt.user.client.DOM;
024import com.google.gwt.user.client.ui.Widget;
025import gwt.material.design.client.base.MaterialWidget;
026import gwt.material.design.client.data.component.CategoryComponent;
027
028public class TableRow extends MaterialWidget {
029
030    protected CategoryComponent category;
031
032    public TableRow() {
033        this(DOM.createTR());
034    }
035
036    public TableRow(Element element) {
037        super(element);
038    }
039
040    public TableRow(CategoryComponent category) {
041        this();
042        setDataCategory(category);
043    }
044
045    public void add(TableData tableData) {
046        super.add(tableData);
047    }
048
049    public void insert(TableData child, int beforeIndex) {
050        super.insert(child, beforeIndex);
051    }
052
053    public void clearColumnsFromIndex(int colIndex) {
054        for(int i = colIndex; i < getChildren().size(); i++) {
055            TableData column = getColumn(i);
056            if(column != null) {
057                column.clear();
058            }
059        }
060    }
061
062    public TableData getColumn(int index) {
063        try {
064            return (TableData) getWidget(index);
065        } catch (IndexOutOfBoundsException ex) {
066            return null;
067        }
068    }
069
070    public void setDataCategory(CategoryComponent category) {
071        this.category = category;
072        setDataAttribute("data-category", category.getName());
073    }
074
075    public CategoryComponent getDataCategory() {
076        return category;
077    }
078
079    public void copy(TableRow tableRow) {
080        if(tableRow != null) {
081            this.category = tableRow.category;
082        }
083    }
084
085    public TableData getExpansionColumn() {
086        for(Widget column : getChildren()) {
087            if(column.getElement().getId().equals("colex") && column instanceof TableData) {
088                return (TableData)column;
089            }
090        }
091        return null;
092    }
093
094    public boolean hasExpansionColumn() {
095        return getExpansionColumn() != null;
096    }
097
098    public void removeExpansionColumn() {
099        for(Widget column : getChildren()) {
100            if(column.getElement().getId().equals("colex")) {
101                column.removeFromParent();
102            }
103        }
104    }
105}