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.core.client.Scheduler;
021import com.google.gwt.core.client.Scheduler.ScheduledCommand;
022import com.google.gwt.user.client.DOM;
023import com.google.gwt.user.client.Element;
024
025/**
026 * The TabLink for {@link TabPanel}
027 * @author ohashi keisuke
028 *
029 */
030public class TabLink extends NavWidget {
031
032    private TabPane pane;
033    private boolean createTabPane = true;
034
035    /**
036     * Create widget with set Effective TabPane 
037     * @param pane effective tabPane
038     */
039    public TabLink(TabPane pane) {
040        this();
041        setText(pane.getHeading());
042        setTablePane(pane);
043    }
044
045    /**
046     * Create empty widget
047     */
048    public TabLink() {
049        super();
050        IconAnchor anchor = getAnchor();
051        anchor.getElement().setAttribute(Constants.DATA_TOGGLE, "tab");
052    }
053    
054    public void setCreateTabPane(boolean createTabPane) {
055        this.createTabPane = createTabPane;
056    }
057    
058    public boolean isCreateTabPane() {
059        return this.createTabPane;
060    }
061
062    /**
063     * Set Effective TabPane
064     * @param pane
065     */
066    public void setTablePane(TabPane pane) {
067        this.pane = pane;
068        
069        if(pane.getId() == null || pane.getId().isEmpty()) {
070            pane.setHref(DOM.createUniqueId());
071        }
072
073        setDataTarget(pane.getId());
074        
075        this.setActive(pane.isActive());
076    }
077    
078    public void setDataTarget(String id) {
079        getAnchor().getElement().setAttribute(Constants.DATA_TARGET,"#" + id);
080    }
081    
082    /**
083     * Get Effective TabPane
084     * @return effective TabPane
085     */
086    public TabPane getTabPane() {
087        return pane;
088    }
089    
090    @Override
091    protected void onAttach() {
092        super.onAttach();
093        
094        if(isActive()) {
095            show();
096        }
097    }
098    
099    @Override
100    public void setActive(boolean active) {
101        super.setActive(active);
102        
103        if(pane != null) {
104            pane.setActive(active);
105        }
106        
107    }
108    
109    /**
110     * show tab pane
111     */
112    public void show() {
113        if(isOrWasAttached()) {
114            show(getAnchor().getElement());
115            return;
116        }
117        
118        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
119            
120            @Override
121            public void execute() {
122                show(getAnchor().getElement());
123            }
124        });
125    }
126
127    //@formatter:off
128    private native void show(Element e)/*-{
129        $wnd.jQuery(e).tab('show');
130    }-*/;
131    //@formatter:on
132
133}