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 com.github.gwtbootstrap.client.ui.base.DivWidget;
019 import com.github.gwtbootstrap.client.ui.constants.Constants;
020 import com.github.gwtbootstrap.client.ui.constants.ToggleType;
021 import com.google.gwt.user.client.ui.Widget;
022
023 //@formatter:off
024 /**
025 * ButtonGroups take buttons and combine them to one optically integrated
026 * widget.
027 *
028 * <p>
029 * <h3>UiBinder Usage:</h3>
030 *
031 * <pre>
032 * {@code
033 * <b:ButtonGroup>
034 * <b:Button>First Button</b:Button>
035 * <b:Button>Second Button</b:Button>
036 * <b:Button>Third Button</b:Button>
037 * </b:ButtonGroup>}
038 * </pre>
039 *
040 * You can also use the buttons as checkboxes or radio buttons:
041 *
042 * <pre>
043 * {@code
044 * <b:ButtonGroup toggle="radio">
045 * <b:Button text="1" />
046 * <b:Button text="2" />
047 * <b:Button text="3" />
048 * <b:Button text="4" />
049 * </b:ButtonGroup>
050 * }
051 * </pre>
052 *
053 * @since 2.0.4.0
054 *
055 * @author Carlos Alexandro Becker
056 *
057 * @see <a
058 * href="http://twitter.github.com/bootstrap/components.html#buttonGroups">Bootstrap
059 * documentation</a>
060 * @see Button
061 * @see ButtonToolbar
062 */
063 // @formatter:on
064 public class ButtonGroup extends DivWidget {
065
066 /**
067 * Creates an empty ButtonGroup.
068 */
069 public ButtonGroup() {
070 setStyleName(Constants.BTN_GROUP);
071 }
072
073 /**
074 * Creates a ButtonGroup containing the provided Buttons.
075 *
076 * @param buttons
077 * the widgets to be added to the ButtonGroup
078 */
079 public ButtonGroup(Button... buttons) {
080 this();
081 for (Button btn : buttons) {
082 add(btn);
083 }
084 }
085
086 /**
087 * Adds a new {@link Button} to the group.
088 *
089 * @param widget
090 * the Button to be added.
091 */
092 @Override
093 public void add(Widget widget) {
094 if (!(widget instanceof Button))
095 throw new IllegalArgumentException(
096 "A ButtonGroup can only contain Buttons.");
097
098 super.add(widget);
099 // super.add(widget);
100 }
101
102 /**
103 * Set/unset the data-toggle behavior.
104 *
105 * @param type
106 */
107 public void setToggle(ToggleType type) {
108 if (type == null || type == ToggleType.NONE) {
109 getElement().removeAttribute(Constants.DATA_TOGGLE);
110 return;
111 }
112 getElement().setAttribute(Constants.DATA_TOGGLE, type.get());
113
114 }
115
116 /**
117 * Set/unset the data-toggle behavior.
118 *
119 * @param toggle
120 */
121 public void setToggle(String toggle) {
122 try {
123 setToggle(ToggleType.valueOf(toggle.toUpperCase()));
124 } catch (Exception e) {
125 throw new IllegalArgumentException("Invalid toggle option.");
126 }
127 }
128 }