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     */
016    package com.github.gwtbootstrap.client.ui;
017    
018    import java.util.List;
019    
020    import com.github.gwtbootstrap.client.ui.base.DivWidget;
021    import com.github.gwtbootstrap.client.ui.resources.Bootstrap;
022    import com.google.gwt.core.client.GWT;
023    import com.google.gwt.user.client.ui.IsWidget;
024    import com.google.gwt.user.client.ui.Widget;
025    
026    //@formatter:off
027    /**
028     * The container for a tabbable nav.
029     * 
030     * @since 2.0.4.0
031     * @author Dominik Mayer
032     * @author ohashi keisuke
033     */
034    //@formatter:on
035    public class TabPanel extends DivWidget {
036    
037        private static class TabContent extends DivWidget {
038    
039            public TabContent() {
040                setStyleName(Bootstrap.tab_content);
041            }
042        }
043    
044        private NavTabs tabs = new NavTabs();
045    
046        private TabContent tabContent = new TabContent();
047    
048        public TabPanel() {
049            this(Bootstrap.Tabs.ABOVE);
050        }
051    
052        public TabPanel(Bootstrap.Tabs position) {
053            setTabPosition(position.get().toLowerCase());
054        }
055    
056        public void setTabPosition(String position) {
057            if(tabs.getParent() != null) {
058                remove(tabs);
059                remove(tabContent);
060            }
061            
062            if (position.equalsIgnoreCase("below")) {
063                setStyle(Bootstrap.Tabs.BELOW);
064                super.add(tabContent);
065                super.add(tabs);
066            } else if (position.equalsIgnoreCase("left")) {
067                setStyle(Bootstrap.Tabs.LEFT);
068                super.add(tabs);
069                super.add(tabContent);
070            } else if (position.equalsIgnoreCase("right")) {
071                setStyle(Bootstrap.Tabs.RIGHT);
072                super.add(tabs);
073                super.add(tabContent);
074            } else {
075                setStyle(Bootstrap.Tabs.ABOVE);
076                super.add(tabs);
077                super.add(tabContent);
078            }
079        }
080    
081        @Override
082        public void add(Widget child) {
083    
084            if (child instanceof TabPane) {
085                add((TabPane) child);
086                return;
087            }
088    
089            if (child instanceof TabLink) {
090                add((TabLink) child);
091                return;
092            }
093            
094            if(child instanceof DropdownTab) {
095                add((DropdownTab) child);
096                return;
097            }
098            
099            if(GWT.isProdMode()) {
100                throw new IllegalArgumentException("TabPanel can add only TabPane or TabLink or Tab or DorpdownTab. you added " + child);
101            }
102        }
103        
104        private void add(DropdownTab dropdownTab) {
105            
106            tabs.add(dropdownTab);
107            
108            List<Tab> tabList = dropdownTab.getTabList();
109            for (Tab tab : tabList) {
110                TabPane tabPane = tab.getTabPane();
111                tabContent.add(tabPane);
112            }
113        }
114    
115        private void add(TabPane child) {
116    
117            if(child.isCreateTabLink()) {
118                TabLink tabLink = new TabLink(child);
119                tabs.add(tabLink);
120            }
121            tabContent.add(child);
122        }
123        
124        private void add(final TabLink child) {
125            
126            if(child.isCreateTabPane() && child.getTabPane() == null){
127                TabPane pane = new TabPane(child.getText());
128                child.setTablePane(pane);
129                tabContent.add(pane);
130            } else if(child.getTabPane() != null) {
131                tabContent.add(child.getTabPane());
132            }
133            tabs.add(child);
134        }
135        
136        @Override
137        public void clear() {
138            tabContent.clear();
139            tabs.clear();
140        }
141        
142        /**
143         * Remove tab or tabpane.
144         * <p>
145         * If Tablink has TabPane,romve TabPane with TabLink.
146         * </pre>
147         * {@inheritDoc}
148         */
149        @Override
150        public boolean remove(int index) {
151            Widget widget = tabs.getWidget(index);
152            
153            if (widget instanceof TabLink) {
154                TabLink link = (TabLink) widget;
155                if(link.getTabPane() != null) {
156                    link.getTabPane().removeFromParent();
157                }
158                return tabs.remove(index);
159            } else if(widget instanceof TabPane) {
160                return tabContent.remove(widget);
161            }
162            
163            return super.remove(widget);
164        }
165        
166        /**
167         * remove TabLink or TabPane.
168         * <p>
169         * </p>
170         * {@inheritDoc}
171         */
172        @Override
173        public boolean remove(Widget w) {
174            
175            if (w instanceof TabLink) {
176                TabLink link = (TabLink) w;
177                
178                if(link.getTabPane() != null) {
179                    link.getTabPane().removeFromParent();
180                }
181                return tabs.remove(w);
182            } else if(w instanceof TabPane) {
183                return tabContent.remove(w);
184            }
185            
186            return super.remove(w);
187        }
188        
189        /**
190         * {@inheritDoc}
191         */
192        @Override
193        public boolean remove(IsWidget child) {
194            
195            if (child instanceof Tab) {
196                Tab tab = (Tab) child;
197                
198                TabLink link = tab.asTabLink();
199                
200                if(link.getTabPane() != null) {
201                    link.getTabPane().removeFromParent();
202                }
203                return tabs.remove(link);
204            } else if(child instanceof DropdownTab) {
205                DropdownTab tab = (DropdownTab)child;
206                
207                List<Tab> tabList = tab.getTabList();
208                
209                for (Tab tab2 : tabList) {
210                    tabContent.remove(tab2.getTabPane());
211                }
212                return super.remove(child);
213            }
214            
215            return super.remove(child);
216        }
217    }