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;
021
022import com.google.gwt.event.logical.shared.*;
023import com.google.gwt.event.shared.HandlerRegistration;
024import com.google.gwt.user.client.ui.Widget;
025import gwt.material.design.client.base.HasPosition;
026import gwt.material.design.client.base.mixin.CssNameMixin;
027import gwt.material.design.client.constants.HideOn;
028import gwt.material.design.client.constants.Position;
029import gwt.material.design.client.ui.html.ListItem;
030import gwt.material.design.client.ui.html.UnorderedList;
031
032//@formatter:off
033
034/**
035 * <p>Material NavSection is a child of MaterialNavBar that will contain toolbar items such as link, icon and other components
036 * <h3>UiBinder Usage:</h3>
037 * <p>
038 * <pre>
039 * {@code
040 * <m:MaterialNavSection align="RIGHT">
041 *     <m:MaterialLink  iconType="ACCOUNT_CIRCLE" iconPosition="LEFT" text="Account"  textColor="WHITE" waves="LIGHT"/>
042 *     <m:MaterialLink  iconType="AUTORENEW" iconPosition="LEFT" text="Refresh" textColor="WHITE" waves="LIGHT"/>
043 *     <m:MaterialLink  iconType="SEARCH" tooltip="Menu" textColor="WHITE" waves="LIGHT"/>
044 *     <m:MaterialLink  iconType="MORE_VERT" tooltip="Starter" textColor="WHITE" waves="LIGHT"/>
045 * </m:MaterialNavSection>
046 * }
047 * </pre>
048 * </p>
049 *
050 * @author kevzlou7979
051 * @author Ben Dol
052 * @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#navbar">Material NavSection</a>
053 * @see <a href="https://material.io/guidelines/components/toolbars.html#">Material Design Specification</a>
054 */
055//@formatter:on
056public class MaterialNavSection extends UnorderedList implements HasPosition, HasSelectionHandlers<Integer> {
057
058    private CssNameMixin<MaterialNavSection, Position> positionMixin;
059
060    /**
061     * Container for App Toolbar and App Sidebar, contains Material
062     * Links, Icons or any other material components.
063     */
064    public MaterialNavSection() {
065        super();
066        setHideOn(HideOn.HIDE_ON_MED_DOWN);
067    }
068
069    @Override
070    public void add(Widget child) {
071        super.add(new ListItem(child));
072    }
073
074    @Override
075    protected void onLoad() {
076        super.onLoad();
077
078        for (Widget widget : getChildren()) {
079            if (widget instanceof ListItem) {
080                registerHandler(((ListItem) widget).addClickHandler(clickEvent -> SelectionEvent.fire(this, getWidgetIndex(widget))));
081            }
082        }
083    }
084
085    @Override
086    public Position getPosition() {
087        return getPositionMixin().getCssName();
088    }
089
090    @Override
091    public void setPosition(Position position) {
092        getPositionMixin().setCssName(position);
093    }
094
095    @Override
096    public HandlerRegistration addSelectionHandler(SelectionHandler<Integer> selectionHandler) {
097        return addHandler(selectionHandler, SelectionEvent.getType());
098    }
099
100    protected CssNameMixin<MaterialNavSection, Position> getPositionMixin() {
101        if (positionMixin == null) {
102            positionMixin = new CssNameMixin<>(this);
103        }
104        return positionMixin;
105    }
106}