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.DropdownBase;
019import com.github.gwtbootstrap.client.ui.base.IconAnchor;
020import com.github.gwtbootstrap.client.ui.constants.ButtonType;
021import com.github.gwtbootstrap.client.ui.constants.IconSize;
022import com.github.gwtbootstrap.client.ui.constants.IconType;
023import com.github.gwtbootstrap.client.ui.resources.ButtonSize;
024import com.google.gwt.event.dom.client.ClickHandler;
025import com.google.gwt.event.shared.HandlerRegistration;
026
027//@formatter:off
028/**
029 * Button with a dropdown menu.
030 * 
031 * <p>
032 * <h3>UiBinder Usage:</h3>
033 * 
034 * <pre>
035 * {@code
036 * <b:DropdownButton text="I am the Caption">
037 *     <b:NavHeader>Header</b:NavHeader>
038 *     <b:NavLink>Link 1</b:NavLink>
039 *     <b:NavLink>Link 2</b:NavLink>
040 * </b:DropdownButton>
041 * }
042 * </pre>
043 * </p>
044 * 
045 * @since 2.0.4.0
046 * 
047 * @author Dominik Mayer
048 * 
049 * @see <a href="http://twitter.github.com/bootstrap/components.html#buttonDropdowns">Bootstrap documentation</a>
050 * @see Dropdown
051 * @see SplitDropdownButton
052 */
053//@formatter:on
054public class DropdownButton extends DropdownBase {
055
056        private Button trigger;
057
058        /**
059         * Creates a DropdownButton without a caption.
060         */
061        public DropdownButton() {
062                super("div");
063                addStyleName("btn-group");
064        }
065
066        /**
067         * Creates a DropdownButton with the given caption.
068         * 
069         * @param caption
070         *            the button's caption
071         */
072        public DropdownButton(String caption) {
073                this();
074                setText(caption);
075        }
076
077        /**
078         * {@inheritDoc}
079         */
080        @Override
081        protected IconAnchor createTrigger() {
082                trigger = new Button();
083                trigger.setCaret(true);
084                return trigger;
085        }
086
087        /**
088         * Sets the button's size.
089         * 
090         * @param size
091         *            the button's size
092         */
093        public void setSize(ButtonSize size) {
094                trigger.setSize(size);
095        }
096
097        /**
098         * Sets the button's type.
099         * 
100         * @param type
101         *            the button's type
102         */
103        public void setType(ButtonType type) {
104                trigger.setType(type);
105        }
106
107        /**
108         * Sets the button's icon.
109         * 
110         * @param type
111         *            the icon's type
112         */
113        public void setIcon(IconType type) {
114                trigger.setIcon(type);
115        }
116
117    @Override
118    public HandlerRegistration addClickHandler(ClickHandler handler) {
119        return trigger.addClickHandler(handler);
120    }
121
122    /**
123     * {@inheritDoc}
124     */
125    @Override
126    public void setIconSize(IconSize size) {
127        trigger.setIconSize(size);
128    }
129    
130    /**
131     * {@inheritDoc}
132     */
133    @Override
134    public void setCustomIconStyle(String customIconStyle) {
135        trigger.setCustomIconStyle(customIconStyle);
136    }
137}