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.Widget;
023import gwt.material.design.client.ui.table.TableSubHeader;
024
025/**
026 * @author Ben Dol
027 */
028public class CategoryComponent extends Component<TableSubHeader> {
029
030    public static class OrphanCategoryComponent extends CategoryComponent {
031        public OrphanCategoryComponent() {
032            super(null);
033        }
034
035        @Override
036        protected void render(TableSubHeader subheader) {
037            super.render(subheader);
038
039            subheader.setName("No Category");
040        }
041    }
042
043    private String name;
044    private String height;
045    private boolean openByDefault;
046
047    private int currentIndex = -1;
048    private int rowCount = 0;
049
050    public CategoryComponent(String name) {
051        this(name, false);
052    }
053
054    public CategoryComponent(String name, boolean openByDefault) {
055        this.name = name;
056        this.openByDefault = openByDefault;
057    }
058
059    public String getName() {
060        return name;
061    }
062
063    /**
064     * Render the data category row element.
065     * Customization to the element can be performed here.
066     */
067    protected void render(TableSubHeader subheader) {
068        // Do nothing by default
069    }
070
071    /**
072     * Render the data category row element.
073     *
074     * @return a fully formed {@link TableSubHeader} object.
075     */
076    public final TableSubHeader render() {
077        TableSubHeader element = getWidget();
078        if(element == null) {
079            element = new TableSubHeader(this);
080            setWidget(element);
081        }
082        render(element);
083        return element;
084    }
085
086    public boolean isOpen() {
087        return isRendered() && getWidget().isOpen();
088    }
089
090    public int getCurrentIndex() {
091        return currentIndex;
092    }
093
094    public void setCurrentIndex(int currentIndex) {
095        this.currentIndex = currentIndex;
096    }
097
098    public int getRowCount() {
099        return rowCount;
100    }
101
102    public void setRowCount(int rowCount) {
103        this.rowCount = rowCount;
104    }
105
106    public boolean isOpenByDefault() {
107        return openByDefault;
108    }
109
110    public void setOpenByDefault(boolean openByDefault) {
111        this.openByDefault = openByDefault;
112    }
113
114    public String getHeight() {
115        return height;
116    }
117
118    public void setHeight(String height) {
119        this.height = height;
120
121        Widget widget = getWidget();
122        if(widget != null && widget.isAttached()) {
123            widget.setHeight(height);
124        }
125    }
126
127    @Override
128    public void setWidget(TableSubHeader widget) {
129        // copy the elements style and info
130        widget.copy(getWidget());
131
132        super.setWidget(widget);
133    }
134
135    @Override
136    public boolean equals(Object o) {
137        if (this == o) return true;
138        if (o == null || getClass() != o.getClass()) return false;
139
140        CategoryComponent that = (CategoryComponent) o;
141        return name.equals(that.name);
142    }
143
144    @Override
145    public int hashCode() {
146        return name.hashCode();
147    }
148}