Skip to content

Instantly share code, notes, and snippets.

@jfuerth
Created December 12, 2014 23:04
Show Gist options
  • Select an option

  • Save jfuerth/e859e32cea1d7fd4c919 to your computer and use it in GitHub Desktop.

Select an option

Save jfuerth/e859e32cea1d7fd4c919 to your computer and use it in GitHub Desktop.
Dropdown tab support for GwtBootstrap3
TabPanelWithDropdowns tabPanel = new TabPanelWithDropdowns();
RootLayoutPanel.get().add(tabPanel);
tabPanel.addItem("First", new Text("This is the content in pane 1"));
tabPanel.addItem("Second", new Text("This is the content in pane 2"));
tabPanel.addItem("Third", new Text("This is the content in pane 3"));
tabPanel.addItem("Really really really really really long tab name", new Text("This is the content in pane 4"));
tabPanel.addItem("Fifth", new Text("This is the content in pane 5"));
DropDownContents dropDownContents = tabPanel.addDropdownTab("Dropdown");
dropDownContents.addItem("Dropdown item 1", new Text("This is the content in dropdown item 1"));
dropDownContents.addItem("Dropdown item 2", new Text("This is the content in dropdown item 2"));
package ca.fuerth.gwt.playground.client;
import java.util.HashSet;
import java.util.Set;
import org.gwtbootstrap3.client.shared.event.TabShowEvent;
import org.gwtbootstrap3.client.shared.event.TabShowHandler;
import org.gwtbootstrap3.client.shared.event.TabShownEvent;
import org.gwtbootstrap3.client.shared.event.TabShownHandler;
import org.gwtbootstrap3.client.ui.AnchorListItem;
import org.gwtbootstrap3.client.ui.DropDownMenu;
import org.gwtbootstrap3.client.ui.NavTabs;
import org.gwtbootstrap3.client.ui.TabContent;
import org.gwtbootstrap3.client.ui.TabListItem;
import org.gwtbootstrap3.client.ui.TabPane;
import org.gwtbootstrap3.client.ui.TabPanel;
import org.gwtbootstrap3.client.ui.constants.IconPosition;
import org.gwtbootstrap3.client.ui.constants.IconType;
import org.gwtbootstrap3.client.ui.constants.Styles;
import org.gwtbootstrap3.client.ui.constants.Toggle;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
public class TabPanelWithDropdowns extends Composite {
private final NavTabs tabBar;
private final TabContent tabContent;
private final Set<Widget> activatableWidgets = new HashSet<Widget>();
private final TabShowHandler clearActiveStyles = new TabShowHandler() {
@Override
public void onShow(TabShowEvent showEvent) {
for (Widget w : activatableWidgets ) {
w.removeStyleName(Styles.ACTIVE);
}
}
};
public TabPanelWithDropdowns() {
tabBar = new NavTabs();
tabContent = new TabContent();
TabPanel root = new TabPanel();
root.add(tabBar);
root.add(tabContent);
initWidget(root);
}
/**
* Adds a normal tab (not a dropdown) with the given label and contents.
*
* @param label
* the label for the tab itself.
* @param content
* the contents that should appear in the content area when the tab is selected.
*/
public void addItem(String label, Widget content) {
TabPane contentPane = new TabPane();
contentPane.add(content);
TabListItem tab = new TabListItem(label);
tab.setDataTargetWidget(contentPane);
tab.addShowHandler(clearActiveStyles);
activatableWidgets.add(tab);
tabBar.add(tab);
tabContent.add(contentPane);
}
/**
* Adds a new tab to this panel that doesn't have any contents itself, but can contain multiple items that appear in a
* dropdown menu when the tab is clicked. This dropdown menu is initially empty. Items can be added and removed using
* the {@link DropDownContents#addItem(String, Widget)} and {@link DropDownContents#removeItem(String, Widget)}
* methods.
*
* @param label The text that should appear on the dropdown tab.
* @return the container for the items that appear when the tab is clicked.
*/
public DropDownContents addDropdownTab(String label) {
AnchorListItem tab = new AnchorListItem(label);
// gets set to active when one of the menu items is selected
activatableWidgets.add(tab);
// FIXME should actually subclass AnchorListItem and add a <b class=caret/> to the anchor elem
tab.setIcon(IconType.CARET_DOWN);
tab.setIconPosition(IconPosition.RIGHT);
tab.addStyleName(Styles.DROPDOWN_TOGGLE);
tab.setDataToggle(Toggle.DROPDOWN);
DropDownContents dropDownContents = new DropDownContents();
tab.add(dropDownContents);
tabBar.add(tab);
return dropDownContents;
}
/**
* Container for the menu items that appear when the dropdown tab is clicked.
* Normally, should only be created by {@link TabPanelWithDropdowns#addDropdownTab(String)}.
*/
public class DropDownContents extends DropDownMenu {
public void addItem(String label, Widget content) {
TabPane contentPane = new TabPane();
contentPane.add(content);
TabListItem dropDownMenuItem = new DropDownTabListItem(label);
activatableWidgets.add(dropDownMenuItem);
dropDownMenuItem.setDataTargetWidget(contentPane);
dropDownMenuItem.addShowHandler(clearActiveStyles);
dropDownMenuItem.addShownHandler(new TabShownHandler() {
@Override
public void onShown(TabShownEvent event) {
DropDownContents.this.getParent().addStyleName(Styles.ACTIVE);
}
});
add(dropDownMenuItem);
tabContent.add(contentPane);
}
}
/**
* Widget for an individual menu item entry that appears when the dropdown tab is clicked.
* Normally, should only be created by {@link DropDownContents#addItem(String, Widget)}.
*/
public static class DropDownTabListItem extends TabListItem {
public DropDownTabListItem(String label) {
super(label);
anchor.setTabIndex(-1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment