Package org.gwtoolbox.widget.client.panel.tab

Source Code of org.gwtoolbox.widget.client.panel.tab.SimpleTabPanel$TabAndContent

package org.gwtoolbox.widget.client.panel.tab;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import org.gwtoolbox.widget.client.panel.LayoutComposite;
import org.gwtoolbox.widget.client.panel.LayoutUtils;
import org.gwtoolbox.widget.client.panel.FixedVerticalPanel;
import org.gwtoolbox.widget.client.panel.layout.VerticalLayoutData;
import org.gwtoolbox.widget.client.panel.layout.tab.TabSpec;
import org.gwtoolbox.widget.client.support.setter.ValueSetter;

import java.util.HashMap;
import java.util.Map;

/**
* @author Uri Boness
* @deprecated use {@link org.gwtoolbox.widget.client.panel.layout.tab.TabLayout instead}
*/
@Deprecated
public class SimpleTabPanel extends LayoutComposite {

    private DeckPanel deckPanel;
    private SimpleTabBar tabBar;

    private boolean hideTabBarWithSingleTab;

    private ValueSetter<Boolean> tabBarVisibilitySetter;

    private Map<String, TabAndContent> tabAndContentById = new HashMap<String, TabAndContent>();

    public SimpleTabPanel() {
        this(false);
    }

    public SimpleTabPanel(boolean fixedSize) {
        deckPanel = new DeckPanel()  {
            @Override
            public void showWidget(int index) {

                if (index < 0) {
                    int visibleIndex = getVisibleWidget();
                    Element parent = DOM.getParent(getWidget(visibleIndex).getElement());
                    UIObject.setVisible(parent, false);
                    return;
                }

                super.showWidget(index);
            }
        };
        tabBar = new SimpleTabBar();

        tabBar.addCloseHandler(new CloseHandler<SimpleTabBar.Tab>() {
            public void onClose(CloseEvent<SimpleTabBar.Tab> event) {
                TabAndContent tac = tabAndContentById.remove(event.getTarget().getId());
                if (tac == null) {
                    return;
                }
                deckPanel.remove(tac.getContent());
                if (tabBar.getSelectedTab() == null) {
                    selectNextTab(event.getTarget().getIndex());
                }
                updateTabBarVisibility();
            }
        });

        tabBar.addSelectionHandler(new SelectionHandler<SimpleTabBar.Tab>() {
            public void onSelection(SelectionEvent<SimpleTabBar.Tab> event) {
                if (event.getSelectedItem() == null) {
                    int index = deckPanel.getVisibleWidget();
                    if (index < 0) {
                        return;
                    }
                    deckPanel.getWidget(index).setVisible(false);
                    return;
                }
                String id = event.getSelectedItem().getId();
                int index = deckPanel.getWidgetIndex(tabAndContentById.get(id).getContent());
                deckPanel.showWidget(index);
                if (index > 0) {
                    LayoutUtils.notifyLayout(deckPanel.getWidget(index));
                }
            }
        });

        if (fixedSize) {
            final FixedVerticalPanel main = new FixedVerticalPanel();
            main.addWidget(tabBar, new VerticalLayoutData().setHeight("27px"));
            main.addWidget(deckPanel, new VerticalLayoutData().setHeight("*").setExpandToFit(true).setOverflow("hidden"));
            tabBarVisibilitySetter = new ValueSetter<Boolean>() {
                public void set(Boolean visible) {
                    setVisible(main.getWidgetContainer(tabBar), visible);
                }
            };
            initWidget(main);
        } else {
            final Grid main = new Grid(2, 1);
            main.setCellPadding(0);
            main.setCellSpacing(0);
            main.getCellFormatter().setHeight(0, 0, "27px");
            main.setWidget(0, 0, tabBar);
            tabBar.setSize("100%", "20px");
            main.setWidget(1, 0, deckPanel);
            deckPanel.setSize("100%", "100%");

            SimplePanel sp = new SimplePanel();
            sp.setWidget(main);
            main.setWidth("100%");

            tabBarVisibilitySetter = new ValueSetter<Boolean>() {
                public void set(Boolean visible) {
                    main.getCellFormatter().setVisible(0, 0, visible);
                }
            };

            initWidget(sp);
        }

        setStylePrimaryName("SimpleTabPanel");
        deckPanel.setStylePrimaryName("TabContent");
    }

    public void addTab(String text, Widget content) {
        addTab(text, text, content);
    }

    public void addTab(String id, String text, Widget content) {
        addTab(new TabSpec(id, text, null, content, false));
    }

    public void addTab(TabSpec tabSpec) {
        addTab(tabSpec, tabBar.getTabCount());
    }

    public void addTab(TabSpec tabSpec, int index) {
        TabAndContent tac = tabAndContentById.remove(tabSpec.getId());
        if (tac != null) {
            index = tabBar.getTabIndex(tabSpec.getId());
            tabBar.remove(tabSpec.getId());
            deckPanel.remove(tac.getContent());
        }
        tabAndContentById.put(tabSpec.getId(), new TabAndContent(tabSpec.getText(), tabSpec.getContent()));
        tabBar.insertTab(tabSpec, index);
        deckPanel.insert(tabSpec.getContent(), index);
        updateTabBarVisibility();
    }

    public void removeTab(String id) {
        TabAndContent tac = tabAndContentById.remove(id);
        if (tac == null) {
            return;
        }
        SimpleTabBar.Tab tab = tabBar.remove(id);
        deckPanel.remove(tac.getContent());
        selectNextTab(tab.getIndex());
        updateTabBarVisibility();
    }

    public int getTabIndex(String id) {
        return tabBar.getTabIndex(id);
    }

    public int getTabCount() {
        return tabBar.getTabCount();
    }

    public void clear() {
        tabAndContentById.clear();
        tabBar.clear();
        deckPanel.clear();
        updateTabBarVisibility();
    }

    public int getSelectedTab() {
        return tabBar.getSelectedTabIndex();
    }

    public void setSelectedTab(String id) {
        tabBar.selectTab(id);
    }

    public void setAnimationEnabled(boolean enabled) {
        deckPanel.setAnimationEnabled(enabled);
    }

    public HandlerRegistration addCloseHandler(CloseHandler<SimpleTabBar.Tab> handler) {
        return tabBar.addCloseHandler(handler);
    }

    public HandlerRegistration addSelectionHandler(SelectionHandler<SimpleTabBar.Tab> handler) {
        return tabBar.addSelectionHandler(handler);
    }

    public void setTabEnabled(String id, boolean enabled) {
        tabBar.setTabEnabled(id, enabled);
    }

    public boolean isTabEnabled(String id) {
        return tabBar.isTabEnabled(id);
    }

    public boolean isHideTabBarWithSingleTab() {
        return hideTabBarWithSingleTab;
    }

    public void setHideTabBarWithSingleTab(boolean hideTabBarWithSingleTab) {
        this.hideTabBarWithSingleTab = hideTabBarWithSingleTab;
        updateTabBarVisibility();
    }

    @Override
    public void layout() {
        super.layout();
        LayoutUtils.notifyLayout(deckPanel);
    }

    //================================================ Helper Methods ==================================================

    private void updateTabBarVisibility() {
        boolean visible = !hideTabBarWithSingleTab || getTabCount() > 1;
        tabBarVisibilitySetter.set(visible);
        if (!visible) {
            deckPanel.addStyleDependentName("fullscreen");
        } else {
            deckPanel.removeStyleDependentName("fullscreen");
        }
        layout();
    }

    private void selectNextTab(int index) {
        if (tabBar.getTabCount() <= index) {
            index = tabBar.getTabCount() - 1;
        }
        if (index < 0) {
            return;
        }
        String id = tabBar.getTabId(index);
        setSelectedTab(id);
    }

    //================================================= Inner Classes ==================================================

    private class TabAndContent {

        private String text;
        private Widget content;

        private TabAndContent(String text, Widget content) {
            this.text = text;
            this.content = content;
        }

        public String getText() {
            return text;
        }

        public Widget getContent() {
            return content;
        }
    }
   
}
TOP

Related Classes of org.gwtoolbox.widget.client.panel.tab.SimpleTabPanel$TabAndContent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.