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