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.addins.client.subheader;
021
022import com.google.gwt.dom.client.Document;
023import com.google.gwt.user.client.DOM;
024import com.google.gwt.user.client.ui.Widget;
025import gwt.material.design.addins.client.base.constants.AddinsCssName;
026import gwt.material.design.addins.client.subheader.constants.SubHeaderType;
027import gwt.material.design.addins.client.subheader.js.JsSubHeader;
028import gwt.material.design.client.base.HasType;
029import gwt.material.design.client.base.JsLoader;
030import gwt.material.design.client.base.MaterialWidget;
031import gwt.material.design.client.base.mixin.CssTypeMixin;
032
033import java.util.ArrayList;
034import java.util.List;
035
036import static gwt.material.design.client.js.JsMaterialElement.$;
037
038//@formatter:off
039
040/**
041 * SubHeader Container will wrap your subheader items.
042 * There are two types of SubHeader Container <br/>
043 * 1. PINNED<br/>
044 * 2. STATIC
045 * <p>
046 * <h3>XML Namespace Declaration</h3>
047 * <pre>
048 * {@code
049 * xmlns:ma='urn:import:gwt.material.design.addins.client'
050 * }
051 * </pre>
052 * <p>
053 * <h3>UiBinder Usage:</h3>
054 * <pre>
055 * {@code
056 * <ma:subheader.MaterialSubHeaderContainer height="400px" type="PINNED">
057 *     <ma:subheader.MaterialSubHeader text="Subheader" textColor="pink" />
058 * </ma:subheader.MaterialSubHeaderContainer>
059 * }
060 * </pre>
061 *
062 * @author kevzlou7979
063 * @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#subheaders">Material Subheader</a>
064 */
065//@formatter:on
066public class MaterialSubHeaderContainer extends MaterialWidget implements JsLoader, HasType<SubHeaderType> {
067
068    static {
069        MaterialSubHeader.loadResources();
070    }
071
072    private CssTypeMixin<SubHeaderType, MaterialSubHeaderContainer> typeMixin;
073    private List<MaterialSubHeader> subHeaders = new ArrayList<>();
074
075    public MaterialSubHeaderContainer() {
076        super(Document.get().createDivElement(), AddinsCssName.CONTAINER1);
077    }
078
079    public MaterialSubHeaderContainer(SubHeaderType type) {
080        this();
081        setType(type);
082    }
083
084    @Override
085    protected void onLoad() {
086        super.onLoad();
087
088        load();
089    }
090
091    @Override
092    public void load() {
093        if (getType() == SubHeaderType.PINNED) {
094            String subHeaderClass = DOM.createUniqueId();
095            for (Widget w : getChildren()) {
096                if (w instanceof MaterialSubHeader) {
097                    w.addStyleName(subHeaderClass);
098                    subHeaders.add((MaterialSubHeader) w);
099                }
100            }
101
102            JsSubHeader.initSubheader("." + subHeaderClass, getElement());
103
104            if (subHeaders.size() == 0) {
105                $(getElement()).find(".top_holder").css("display", "none");
106            }
107        }
108    }
109
110    @Override
111    protected void onUnload() {
112        super.onUnload();
113
114        unload();
115    }
116
117    @Override
118    public void unload() {
119        subHeaders.clear();
120    }
121
122    @Override
123    public void reload() {
124        unload();
125        load();
126    }
127
128    @Override
129    public void setType(SubHeaderType type) {
130        getTypeMixin().setType(type);
131    }
132
133    @Override
134    public SubHeaderType getType() {
135        return getTypeMixin().getType();
136    }
137
138    protected CssTypeMixin<SubHeaderType, MaterialSubHeaderContainer> getTypeMixin() {
139        if (typeMixin == null) {
140            typeMixin = new CssTypeMixin<>(this);
141        }
142        return typeMixin;
143    }
144}