001/*
002 *  Copyright 2012 GWT-Bootstrap
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.github.gwtbootstrap.client.ui;
017
018import com.github.gwtbootstrap.client.ui.base.IconAnchor;
019import com.github.gwtbootstrap.client.ui.constants.Constants;
020import com.google.gwt.user.client.DOM;
021import com.google.gwt.user.client.Element;
022
023/**
024 * The TabLink for {@link TabPanel}
025 * @author ohashi keisuke
026 *
027 */
028public class TabLink extends NavWidget {
029
030    private TabPane pane;
031    private boolean createTabPane = true;
032
033    /**
034     * Create widget with set Effective TabPane 
035     * @param pane effective tabPane
036     */
037    public TabLink(TabPane pane) {
038        this();
039        setText(pane.getHeading());
040        setTablePane(pane);
041    }
042
043    /**
044     * Create empty widget
045     */
046    public TabLink() {
047        super();
048        IconAnchor anchor = getAnchor();
049        anchor.getElement().setAttribute(Constants.DATA_TOGGLE, "tab");
050    }
051    
052    public void setCreateTabPane(boolean createTabPane) {
053        this.createTabPane = createTabPane;
054    }
055    
056    public boolean isCreateTabPane() {
057        return this.createTabPane;
058    }
059
060    /**
061     * Set Effective TabPane
062     * @param pane
063     */
064    public void setTablePane(TabPane pane) {
065        this.pane = pane;
066        
067        if(pane.getId() == null || pane.getId().isEmpty()) {
068            pane.setHref(DOM.createUniqueId());
069        }
070
071        setDataTarget(pane.getId());
072        
073        this.setActive(pane.isActive());
074    }
075    
076    public void setDataTarget(String id) {
077        getAnchor().getElement().setAttribute(Constants.DATA_TARGET,"#" + id);
078    }
079    
080    /**
081     * Get Effective TabPane
082     * @return effective TabPane
083     */
084    public TabPane getTabPane() {
085        return pane;
086    }
087    
088    @Override
089    protected void onAttach() {
090        super.onAttach();
091        
092        if(isActive()) {
093            show();
094        }
095    }
096    
097    @Override
098    public void setActive(boolean active) {
099        super.setActive(active);
100        
101        if(pane != null) {
102            pane.setActive(active);
103        }
104        
105    }
106    
107    /**
108     * show tab pane
109     */
110    public void show() {
111        setActive(true);
112        show(getAnchor().getElement());
113    }
114
115    //@formatter:off
116    private native void show(Element e)/*-{
117        $wnd.jQuery(e).tab('show');
118    }-*/;
119    //@formatter:on
120
121}