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.component;
021
022import com.google.gwt.user.client.ui.HasEnabled;
023import gwt.material.design.client.data.DataView;
024import gwt.material.design.jquery.client.api.JQuery;
025import gwt.material.design.jquery.client.api.JQueryElement;
026import gwt.material.design.client.ui.table.TableRow;
027
028import java.util.ArrayList;
029import java.util.List;
030
031/**
032 * @author Ben Dol
033 */
034public class RowComponent<T> extends Component<TableRow> implements HasEnabled {
035    private T data;
036    private int index;
037    private final String categoryName;
038    private final DataView dataView;
039
040    public RowComponent(RowComponent<T> clone) {
041        super(clone.getWidget(), clone.isRedraw());
042        addAll(clone.getChildren());
043        data = clone.data;
044        index = clone.index;
045        categoryName = clone.categoryName;
046        dataView = clone.dataView;
047    }
048
049    public RowComponent(T data, DataView dataView, String categoryName) {
050        this.data = data;
051        this.dataView = dataView;
052        this.categoryName = categoryName;
053    }
054
055    public T getData() {
056        return data;
057    }
058
059    public void setData(T data) {
060        this.data = data;
061    }
062
063    public int getIndex() {
064        return index;
065    }
066
067    public void setIndex(int index) {
068        this.index = index;
069    }
070
071    public DataView getDataView() {
072        return dataView;
073    }
074
075    public CategoryComponent getCategory() {
076        return dataView.getCategory(categoryName);
077    }
078
079    public String getCategoryName() {
080        return categoryName;
081    }
082
083    @Override
084    public boolean isEnabled() {
085        return getWidget().isEnabled();
086    }
087
088    @Override
089    public void setEnabled(boolean enabled) {
090        getWidget().setEnabled(enabled);
091    }
092
093    @Override
094    protected void clearWidget() {
095        TableRow row = getWidget();
096        if(row != null) {
097            clearRowExpansion();
098        }
099        super.clearWidget();
100    }
101
102    public void clearRowExpansion() {
103        JQueryElement next = JQuery.$(getWidget()).next();
104        if(next.is("tr.expansion")) {
105            next.remove();
106        }
107    }
108
109    public static <T> List<T> extractData(List<RowComponent<T>> rows) {
110        List<T> data = new ArrayList<>();
111        for(RowComponent<T> row : rows) {
112            if(row != null) {
113                data.add(row.getData());
114            }
115        }
116        return data;
117    }
118
119    @Override
120    public String toString() {
121        return "RowComponent{" +
122                "data=" + data +
123                ", index=" + index +
124                ", categoryName='" + categoryName + '\'' +
125                '}';
126    }
127}