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.user.client.ui.Widget;
023import gwt.material.design.client.base.HasHref;
024import gwt.material.design.client.constants.CssName;
025import gwt.material.design.client.ui.html.ListItem;
026
027//@formatter:off
028
029/**
030 * Item for Tab Component, which usually contains icons, links or other navigation component.
031 * <h3>UiBinder Usage:</h3>
032 * <pre>
033 * {@code <m:MaterialTabItem waves="YELLOW" grid="l4"><m:MaterialLink text="Tab 1" href="#tab1" textColor="WHITE"/></m:MaterialTabItem>}
034 * </pre>
035 *
036 * @author kevzlou7979
037 * @author Ben Dol
038 * @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#tabs">Material Tabs</a>
039 * @see <a href="https://material.io/guidelines/components/tabs.html">Material Design Specification</a>
040 */
041//@formatter:on
042public class MaterialTabItem extends ListItem {
043
044    private MaterialTab parent;
045
046    public MaterialTabItem() {
047        super(CssName.TAB);
048    }
049
050    @Override
051    protected void onLoad() {
052        super.onLoad();
053
054        try {
055            parent = (MaterialTab) getParent();
056        } catch (ClassCastException ex) {
057            throw new ClassCastException("MaterialTabItem must be within a MaterialTab widget.");
058        }
059    }
060
061    /**
062     * Select this tab item.
063     */
064    public void selectTab() {
065        for (Widget child : getChildren()) {
066            if (child instanceof HasHref) {
067                String href = ((HasHref) child).getHref();
068                if (!href.isEmpty()) {
069                    parent.selectTab(href.replaceAll("[^a-zA-Z\\d\\s:]", ""));
070                    break;
071                }
072            }
073        }
074    }
075}