001 package org.gwtbootstrap3.client.ui.base;
002
003 /*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 2013 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
023 import org.gwtbootstrap3.client.ui.base.helper.StyleHelper;
024 import org.gwtbootstrap3.client.ui.base.mixin.DataToggleMixin;
025 import org.gwtbootstrap3.client.ui.base.mixin.PullMixin;
026 import org.gwtbootstrap3.client.ui.constants.DeviceSize;
027 import org.gwtbootstrap3.client.ui.constants.Pull;
028 import org.gwtbootstrap3.client.ui.constants.Styles;
029 import org.gwtbootstrap3.client.ui.constants.Toggle;
030 import org.gwtbootstrap3.client.ui.gwt.FlowPanel;
031
032 import com.google.gwt.user.client.ui.HasName;
033 import com.google.gwt.user.client.ui.Widget;
034
035 /**
036 * Abstract base class for button groups.
037 *
038 * @author Sven Jacobs
039 * @author Joshua Godi
040 * @see org.gwtbootstrap3.client.ui.ButtonGroup
041 * @see org.gwtbootstrap3.client.ui.VerticalButtonGroup
042 */
043 public abstract class AbstractButtonGroup extends FlowPanel implements HasName, HasDataToggle, HasJustified, HasPull, HasResponsiveness {
044
045 private final PullMixin<AbstractButtonGroup> pullMixin = new PullMixin<AbstractButtonGroup>(this);
046 private final DataToggleMixin<AbstractButtonGroup> toggleMixin = new DataToggleMixin<AbstractButtonGroup>(this);
047 private String name;
048
049 protected AbstractButtonGroup(final String styleName) {
050 setStyleName(styleName);
051 }
052
053 /**
054 * Convenience method that will set the name of all child widgets that can have a name
055 *
056 * @param name Name of group
057 * @see #add(com.google.gwt.user.client.ui.Widget)
058 */
059 @Override
060 public void setName(final String name) {
061 this.name = name;
062
063 if (name == null) {
064 return;
065 }
066
067 for (final Widget w : getChildren()) {
068 if (w instanceof HasName) {
069 ((HasName) w).setName(name);
070 }
071 }
072 }
073
074 @Override
075 public String getName() {
076 return this.name;
077 }
078
079 @Override
080 public void setDataToggle(final Toggle toggle) {
081 toggleMixin.setDataToggle(toggle);
082 }
083
084 @Override
085 public Toggle getDataToggle() {
086 return toggleMixin.getDataToggle();
087 }
088
089 /**
090 * Make a group of buttons stretch at the same size to span the entire width of its parent.
091 * <p/>
092 * <strong>Note:</strong> Justified button groups only work with {@link org.gwtbootstrap3.client.ui.AnchorButton} child elements!
093 *
094 * @param justified Stretch button group
095 */
096 @Override
097 public void setJustified(final boolean justified) {
098 if (justified) {
099 addStyleName(Styles.BTN_GROUP_JUSTIFIED);
100 } else {
101 removeStyleName(Styles.BTN_GROUP_JUSTIFIED);
102 }
103 }
104
105 @Override
106 public boolean isJustified() {
107 return StyleHelper.containsStyle(getStyleName(), Styles.BTN_GROUP_JUSTIFIED);
108 }
109
110 @Override
111 public void setPull(final Pull pull) {
112 pullMixin.setPull(pull);
113 }
114
115 @Override
116 public Pull getPull() {
117 return pullMixin.getPull();
118 }
119
120 @Override
121 public void setVisibleOn(final DeviceSize deviceSize) {
122 StyleHelper.setVisibleOn(this, deviceSize);
123 }
124
125 @Override
126 public void setHiddenOn(final DeviceSize deviceSize) {
127 StyleHelper.setHiddenOn(this, deviceSize);
128 }
129
130 /**
131 * Makes this a "drop up" container for dropdown menus where the menu opens upwards.
132 *
133 * @param dropUp display up or not
134 */
135 public void setDropUp(final boolean dropUp) {
136 if (dropUp) {
137 addStyleName(Styles.DROP_UP);
138 } else {
139 removeStyleName(Styles.DROP_UP);
140 }
141 }
142
143 @Override
144 public void add(final Widget w) {
145 super.add(w);
146
147 if (name == null) {
148 return;
149 }
150
151 // Add group's name to child widgets that can have a name
152 if (w instanceof HasName) {
153 ((HasName) w).setName(name);
154 }
155 }
156 }