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.HasType;
020import com.github.gwtbootstrap.client.ui.base.IconAnchor;
021import com.github.gwtbootstrap.client.ui.constants.*;
022import com.github.gwtbootstrap.client.ui.resources.ButtonSize;
023import com.google.gwt.event.dom.client.ClickHandler;
024import com.google.gwt.event.dom.client.HasClickHandlers;
025import com.google.gwt.event.shared.HandlerRegistration;
026import com.google.gwt.uibinder.client.UiChild;
027import com.google.gwt.user.client.ui.Widget;
028
029//@formatter:off
030/**
031 * Split Dropdown button.
032 * 
033 * @author Dominik Mayer
034 * @since 2.0.4.0
035 * @see <a
036 *      href="http://getbootstrap.com/2.3.2/components.html#buttonDropdowns">Bootstrap
037 *      documentation</a>
038 * 
039 */
040//@formatter:on
041public class SplitDropdownButton extends DropdownBase implements
042                HasClickHandlers , HasType<ButtonType> {
043
044        private Button button;
045
046        private Button trigger;
047
048        /**
049         * Create an Empty Split Dropdown Button
050         */
051        public SplitDropdownButton() {
052                super("div");
053                addStyleName("btn-group");
054        }
055
056        /**
057         * Create an Empty Split Dropdown Button with text.
058         * @param text
059         */
060        public SplitDropdownButton(String text) {
061                this();
062                setText(text);
063        }
064
065        /**
066         * {@inheritDoc}
067         */
068        @Override
069        public void setText(String text) {
070                button.setText(text);
071        }
072
073        /**
074         * {@inheritDoc}
075         */
076        @Override
077        public String getText() {
078                return button.getText();
079        }
080
081        @Override
082        protected IconAnchor createTrigger() {
083                button = new Button();
084                addWidget(button);
085                trigger = new Button();
086                trigger.setCaret(true);
087                return trigger;
088        }
089
090        /**
091         * Set Button size
092         * @param size button size
093         */
094        public void setSize(ButtonSize size) {
095                trigger.setSize(size);
096                button.setSize(size);
097        }
098
099        /**
100         * {@inheritDoc}
101         */
102        @Override
103        public void setType(ButtonType type) {
104                trigger.setType(type);
105        button.setType(type);
106        }
107
108    /**
109     * {@inheritDoc}
110     */
111    @Override
112    public void setIcon(IconType type) {
113        setBaseIcon(type);
114    }
115
116    /**
117     * {@inheritDoc}
118     */
119    @Override
120    public void setBaseIcon(BaseIconType type) {
121        this.button.setBaseIcon(type);
122    }
123
124        /**
125         * {@inheritDoc}
126         */
127        public HandlerRegistration addClickHandler(ClickHandler handler) {
128                return button.addClickHandler(handler);
129        }
130
131    /**
132     * {@inheritDoc}
133     */
134    @Override
135    public void setIconSize(IconSize size) {
136        button.setIconSize(size);
137    }
138    
139    @Override
140    @UiChild(tagname="customTrigger" , limit=1)
141    public void addCustomTrigger(Widget w) {
142        button.insert(w, 0);
143    }
144    
145    /**
146     * {@inheritDoc}
147     */
148    @Override
149    public void setCustomIconStyle(String customIconStyle) {
150        button.setCustomIconStyle(customIconStyle);
151    }
152
153    /**
154     * {@inheritDoc}
155     */
156    @Override
157    public void setIconPosition(IconPosition position) {
158        button.setIconPosition(position);
159    }
160
161}