com.github.gwtbootstrap.client.ui.ButtonGroup Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gwt-bootstrap Show documentation
Show all versions of gwt-bootstrap Show documentation
A GWT Library that provides the widgets of Twitter Bootstrap.
The newest version!
/*
* Copyright 2012 GWT-Bootstrap
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.gwtbootstrap.client.ui;
import com.github.gwtbootstrap.client.ui.base.DivWidget;
import com.github.gwtbootstrap.client.ui.base.DropdownBase;
import com.github.gwtbootstrap.client.ui.base.MarkupWidget;
import com.github.gwtbootstrap.client.ui.constants.Constants;
import com.github.gwtbootstrap.client.ui.constants.ToggleType;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
//@formatter:off
/**
* ButtonGroups take buttons and combine them to one optically integrated
* widget.
*
*
*
UiBinder Usage:
*
*
* {@code
*
* First Button
* Second Button
* Third Button
* }
*
*
* You can also use the buttons as checkboxes or radio buttons:
*
*
* {@code
*
*
*
*
*
*
* }
*
*
* @since 2.0.4.0
*
* @author Carlos Alexandro Becker
* @author ohashi keisuke
*
* @see Bootstrap
* documentation
* @see Button
* @see ButtonToolbar
*/
// @formatter:on
public class ButtonGroup extends DivWidget {
/**
* Creates an empty ButtonGroup.
*/
public ButtonGroup() {
setStyleName(Constants.BTN_GROUP);
}
/**
* Creates a ButtonGroup containing the provided Buttons.
*
* @param buttons
* the widgets to be added to the ButtonGroup
*/
public ButtonGroup(Button... buttons) {
this();
for (Button btn : buttons) {
add(btn);
}
}
@Override
public void add(IsWidget child) {
Widget widget = asWidgetOrNull(child);
if(child instanceof MarkupWidget && widget instanceof DropdownButton) {
MarkupWidget markup = (MarkupWidget) child;
DropdownButton dropdownBase = (DropdownButton) widget;
markup.setWidget(dropdownBase.getTriggerWidget());
markup.asWidget();
}
this.add(widget);
}
/**
* Adds a new {@link Button} to the group.
*
* @param widget
* the Button to be added.
*/
@Override
public void add(Widget widget) {
if(widget instanceof Button) {
super.add(widget);
return;
}
if(widget instanceof DropdownButton) {
this.add((DropdownButton)widget);
return;
}
throw new IllegalArgumentException(
"A ButtonGroup can only contain Buttons or DropdownButton. You added " + widget);
}
/**
* Add dropdown widget
* @param dropdown dropdown widget
*/
private void add(DropdownBase dropdown) {
super.add(dropdown.getTriggerWidget());
super.add(dropdown.getMenuWiget());
this.setStyleName(Constants.DROPUP, dropdown.isDropup());
}
@Override
public boolean remove(Widget w) {
if(!(w instanceof DropdownBase)) {
return super.remove(w);
}
DropdownBase dropdown = (DropdownBase) w;
super.remove(dropdown.getTriggerWidget());
return super.remove(dropdown.getMenuWiget());
}
/**
* Set/unset the data-toggle behavior.
*
* @param type
*/
public void setToggle(ToggleType type) {
if (type == null || type == ToggleType.NONE) {
getElement().removeAttribute(Constants.DATA_TOGGLE);
return;
}
getElement().setAttribute(Constants.DATA_TOGGLE, type.get());
}
/**
* Set/unset the data-toggle behavior.
*
* @param toggle
*/
public void setToggle(String toggle) {
try {
setToggle(ToggleType.valueOf(toggle.toUpperCase()));
} catch (Exception e) {
throw new IllegalArgumentException("Invalid toggle option.");
}
}
/**
* Set vertical style
* @param vertical true:Set , false:Unset
*/
public void setVertical(boolean vertical) {
setStyleName(Constants.BTN_GROUP_VERTICAL, vertical);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy