001package gwt.material.design.client.ui;
002
003/*
004 * #%L
005 * GwtMaterial
006 * %%
007 * Copyright (C) 2015 GwtMaterialDesign
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import com.google.gwt.user.client.ui.Widget;
024import gwt.material.design.client.base.HasHref;
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"><i:Link text="Tab 1" href="#tab1" textColor="white"/></m:MaterialTabItem>}
034 * </pre>
035 * @see <a href="http://gwt-material-demo.herokuapp.com/#tabs">Material Tabs</a>
036 * @author kevzlou7979
037 * @author Ben Dol
038 */
039//@formatter:on
040public class MaterialTabItem extends ListItem {
041
042    private MaterialTab parent;
043
044    public MaterialTabItem() {
045        super("tab");
046    }
047
048    @Override
049    protected void onLoad() {
050        super.onLoad();
051
052        try {
053            parent = (MaterialTab)getParent();
054        } catch (ClassCastException ex) {
055            throw new ClassCastException(
056                "MaterialTabItem must be within a MaterialTab widget.");
057        }
058    }
059
060    public void selectTab() {
061        for(Widget child : getChildren()) {
062            if(child instanceof HasHref) {
063                String href = ((HasHref) child).getHref();
064                if(!href.isEmpty()) {
065                    parent.selectTab(href.replaceAll("[^a-zA-Z\\d\\s:]", ""));
066                    break;
067                }
068            }
069        }
070    }
071}