001package org.gwtbootstrap3.client.ui.base;
002
003/*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 2013 - 2015 GwtBootstrap3
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import org.gwtbootstrap3.client.ui.base.helper.StyleHelper;
024import org.gwtbootstrap3.client.ui.base.mixin.DataToggleMixin;
025import org.gwtbootstrap3.client.ui.base.mixin.PullMixin;
026import org.gwtbootstrap3.client.ui.constants.ButtonGroupSize;
027import org.gwtbootstrap3.client.ui.constants.DeviceSize;
028import org.gwtbootstrap3.client.ui.constants.Pull;
029import org.gwtbootstrap3.client.ui.constants.Styles;
030import org.gwtbootstrap3.client.ui.constants.Toggle;
031import org.gwtbootstrap3.client.ui.gwt.FlowPanel;
032
033import com.google.gwt.user.client.ui.HasName;
034import com.google.gwt.user.client.ui.Widget;
035
036/**
037 * Abstract base class for button groups.
038 *
039 * @author Sven Jacobs
040 * @author Joshua Godi
041 * @see org.gwtbootstrap3.client.ui.ButtonGroup
042 * @see org.gwtbootstrap3.client.ui.VerticalButtonGroup
043 */
044public abstract class AbstractButtonGroup extends FlowPanel implements HasName, HasSize<ButtonGroupSize>,
045        HasDataToggle, HasJustified, HasPull, HasResponsiveness {
046
047    private final PullMixin<AbstractButtonGroup> pullMixin = new PullMixin<AbstractButtonGroup>(this);
048    private final DataToggleMixin<AbstractButtonGroup> toggleMixin = new DataToggleMixin<AbstractButtonGroup>(this);
049    private String name;
050
051    protected AbstractButtonGroup(final String styleName) {
052        setStyleName(styleName);
053    }
054
055    /**
056     * Convenience method that will set the name of all child widgets that can have a name
057     *
058     * @param name Name of group
059     * @see #add(com.google.gwt.user.client.ui.Widget)
060     */
061    @Override
062    public void setName(final String name) {
063        this.name = name;
064
065        if (name == null) {
066            return;
067        }
068
069        for (final Widget w : getChildren()) {
070            if (w instanceof HasName) {
071                ((HasName) w).setName(name);
072            }
073        }
074    }
075
076    @Override
077    public String getName() {
078        return this.name;
079    }
080
081    @Override
082    public void setSize(ButtonGroupSize size) {
083        StyleHelper.addUniqueEnumStyleName(this, ButtonGroupSize.class, size);
084    }
085
086    @Override
087    public ButtonGroupSize getSize() {
088        return ButtonGroupSize.fromStyleName(getStyleName());
089    }
090
091    @Override
092    public void setDataToggle(final Toggle toggle) {
093        toggleMixin.setDataToggle(toggle);
094    }
095
096    @Override
097    public Toggle getDataToggle() {
098        return toggleMixin.getDataToggle();
099    }
100
101    /**
102     * Make a group of buttons stretch at the same size to span the entire width of its parent.
103     * <p/>
104     * <strong>Note:</strong> Justified button groups only work with {@link org.gwtbootstrap3.client.ui.AnchorButton} child elements!
105     *
106     * @param justified Stretch button group
107     */
108    @Override
109    public void setJustified(final boolean justified) {
110        if (justified) {
111            addStyleName(Styles.BTN_GROUP_JUSTIFIED);
112        } else {
113            removeStyleName(Styles.BTN_GROUP_JUSTIFIED);
114        }
115    }
116
117    @Override
118    public boolean isJustified() {
119        return StyleHelper.containsStyle(getStyleName(), Styles.BTN_GROUP_JUSTIFIED);
120    }
121
122    @Override
123    public void setPull(final Pull pull) {
124        pullMixin.setPull(pull);
125    }
126
127    @Override
128    public Pull getPull() {
129        return pullMixin.getPull();
130    }
131
132    @Override
133    public void setVisibleOn(final DeviceSize deviceSize) {
134        StyleHelper.setVisibleOn(this, deviceSize);
135    }
136
137    @Override
138    public void setHiddenOn(final DeviceSize deviceSize) {
139        StyleHelper.setHiddenOn(this, deviceSize);
140    }
141
142    /**
143     * Makes this a "drop up" container for dropdown menus where the menu opens upwards.
144     *
145     * @param dropUp display up or not
146     */
147    public void setDropUp(final boolean dropUp) {
148        if (dropUp) {
149            addStyleName(Styles.DROP_UP);
150        } else {
151            removeStyleName(Styles.DROP_UP);
152        }
153    }
154
155    @Override
156    public void add(final Widget w) {
157        super.add(w);
158
159        if (name == null) {
160            return;
161        }
162
163        // Add group's name to child widgets that can have a name
164        if (w instanceof HasName) {
165            ((HasName) w).setName(name);
166        }
167    }
168}