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.constants.Constants;
020import com.google.gwt.user.client.ui.Widget;
021
022//@formatter:off
023/**
024 * Toolbar that places different Buttons in a horizontal line.
025 * 
026 * <p>
027 * <h3>UiBinder Usage:</h3>
028 * 
029 * <pre>
030 * {@code 
031 * <b:ButtonToolbar>
032 *     <b:ButtonGroup>
033 *         <b:Button>1</b:Button>
034 *         <b:Button>2</b:Button>
035 *         <b:Button>3</b:Button>
036 *     </b:ButtonGroup>
037 *     <b:Button>4</b:Button>
038 *     <b:Button>5</b:Button>
039 *     <b:DropdownButton text="6">
040 *         <b:NavLink>Some NavLink</b:NavLink>
041 *         <b:NavLink>Another NavLink</b:NavLink>
042 *     </b:DropdownButton>
043 *     <b:SplitDropdownButton text="7">
044 *         <b:NavLink>Some NavLink</b:NavLink>
045 *         <b:NavLink>Another NavLink</b:NavLink>
046 *     </b:SplitDropdownButton>
047 * </b:ButtonToolbar>
048 * }
049 * </pre>
050 * 
051 * @since 2.0.4.0
052 *
053 * @author Carlos Alexandro Becker
054 * 
055 * @author Dominik Mayer
056 * 
057 * @see <a href="http://getbootstrap.com/2.3.2/components.html#buttonGroups">Bootstrap documentation</a>
058 * @see Button
059 * @see ButtonGroup
060 * @see DropdownButton
061 * @see SplitDropdownButton
062 */
063//@formatter:on
064public class ButtonToolbar extends DivWidget {
065
066        /**
067         * Creates an empty toolbar.
068         */
069        public ButtonToolbar() {
070                setStyleName(Constants.BTN_TOOLBAR);
071        }
072
073        /**
074         * Adds a new widget to the toolbar.
075         * 
076         * @param widget
077         *            the widget to be added
078         */
079        @Override
080        public void add(Widget widget) {
081
082                Widget addingWidget = widget;
083
084                if (!canBeAdded(widget))
085                        throw new IllegalArgumentException("A widget of "
086                                        + widget.getClass() + " cannot be added to the toolbar.");
087
088                if (widget instanceof Button)
089                        addingWidget = new ButtonGroup((Button) widget);
090
091                super.add(addingWidget);
092        }
093
094        /**
095         * This method decides whether a widget can be added to the toolbar or not.
096         * <p>
097         * Override it to allow other than the default widgets.
098         * 
099         * @param widget
100         *            the widget that should be added to the toolbar.
101         * @return <code>true</code> if the widget can be added to the toolbar.
102         */
103        protected boolean canBeAdded(Widget widget) {
104                return (widget instanceof ButtonGroup) || (widget instanceof Button)
105                                || (widget instanceof DropdownButton)
106                                || (widget instanceof SplitDropdownButton);
107        }
108
109}