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 */
016 package com.github.gwtbootstrap.client.ui;
017
018 import com.github.gwtbootstrap.client.ui.base.HasAlternateSize;
019 import com.github.gwtbootstrap.client.ui.base.HasId;
020 import com.github.gwtbootstrap.client.ui.base.HasSize;
021 import com.github.gwtbootstrap.client.ui.base.HasStyle;
022 import com.github.gwtbootstrap.client.ui.base.IsResponsive;
023 import com.github.gwtbootstrap.client.ui.base.IsSearchQuery;
024 import com.github.gwtbootstrap.client.ui.base.ResponsiveHelper;
025 import com.github.gwtbootstrap.client.ui.base.SearchQueryStyleHelper;
026 import com.github.gwtbootstrap.client.ui.base.SizeHelper;
027 import com.github.gwtbootstrap.client.ui.base.Style;
028 import com.github.gwtbootstrap.client.ui.base.StyleHelper;
029 import com.github.gwtbootstrap.client.ui.constants.AlternateSize;
030 import com.github.gwtbootstrap.client.ui.constants.Constants;
031 import com.github.gwtbootstrap.client.ui.constants.Device;
032 import com.google.gwt.dom.client.Element;
033
034 /**
035 * A ListBox for Bootstrap form.
036 *
037 * @since 2.0.4.0
038 *
039 * @author ohashi keisuke
040 *
041 */
042 public class ListBox extends com.google.gwt.user.client.ui.ListBox implements HasSize, HasAlternateSize, IsSearchQuery, HasId , IsResponsive , HasStyle{
043
044 {
045 setStyleName("");
046 }
047
048 /**
049 * Creates an empty list box in single selection mode.
050 */
051 public ListBox() {
052 super();
053 }
054
055 /**
056 * Creates an empty list box. The preferred way to enable multiple
057 * selections is to use this constructor rather than
058 * {@link #setMultipleSelect(boolean)}.
059 *
060 * @param isMultipleSelect
061 * specifies if multiple selection is enabled
062 */
063 public ListBox(boolean isMultipleSelect) {
064 super(isMultipleSelect);
065 }
066
067 /**
068 * This constructor may be used by subclasses to explicitly use an existing
069 * element. This element must be a <select> element.
070 *
071 * @param element
072 * the element to be used
073 */
074 protected ListBox(Element element) {
075 super(element);
076 }
077
078 /**
079 * {@inheritDoc}
080 */
081 @Override
082 public void setAlternateSize(AlternateSize size) {
083 StyleHelper.changeStyle(this, size, AlternateSize.class);
084 }
085
086 /**
087 * {@inheritDoc}
088 */
089 @Override
090 public void setSize(int size) {
091 SizeHelper.setSize(this, size);
092 }
093
094 /**
095 * Get Selected Value.
096 * <p>
097 * If set multiple,return first selected value.
098 * @return Selected Value.(If there is nothing selected item,return null)
099 */
100 public String getValue() {
101
102 if(getSelectedIndex() == -1) {
103 return null;
104 }
105
106 return getValue(getSelectedIndex());
107 }
108
109 /**
110 * {@inheritDoc}
111 */
112 @Override
113 public void setSearchQuery(boolean searchQuery) {
114 SearchQueryStyleHelper.setSearchQuery(this, searchQuery);
115 }
116
117 /**
118 * {@inheritDoc}
119 */
120 @Override
121 public boolean isSearchQuery() {
122 return SearchQueryStyleHelper.isSearchQuery(this);
123 }
124
125 /**
126 * {@inheritDoc}
127 */
128 @Override
129 public String getId() {
130 return getElement().getId();
131 }
132
133 /**
134 * {@inheritDoc}
135 */
136 @Override
137 public void setId(String id) {
138 getElement().setId(id);
139 }
140
141 /**
142 * {@inheritDoc}
143 */
144 @Override
145 public void setEnabled(boolean enabled) {
146 super.setEnabled(enabled);
147 if(enabled) {
148 removeStyleName(Constants.DISABLED);
149 } else {
150 addStyleName(Constants.DISABLED);
151 }
152 }
153
154 /**
155 * Selects item which has the given value. If value
156 * is not found, nothing is done.
157 * @param value to be selected (<code>null</code>-safe)
158 */
159 public void setSelectedValue(String value) {
160 if (value == null) {
161 value = "";
162 }
163 for(int i = 0; i < getItemCount(); i++) {
164 if (getValue(i).equals(value)) {
165 setSelectedIndex(i);
166 return;
167 }
168 }
169 }
170
171
172 /**
173 * {@inheritDoc}
174 */
175 @Override
176 public void setShowOn(Device device) {
177 ResponsiveHelper.setShowOn(this, device);
178 }
179
180 /**
181 * {@inheritDoc}
182 */
183 @Override
184 public void setHideOn(Device device) {
185 ResponsiveHelper.setHideOn(this, device);
186
187 }
188
189 /**
190 * {@inheritDoc}
191 */
192 @Override
193 public void setStyle(Style style) {
194 StyleHelper.setStyle(this, style);
195 }
196
197 /**
198 * {@inheritDoc}
199 */
200 @Override
201 public void addStyle(Style style) {
202 StyleHelper.addStyle(this, style);
203 }
204
205 /**
206 * {@inheritDoc}
207 */
208 @Override
209 public void removeStyle(Style style) {
210 StyleHelper.removeStyle(this, style);
211
212 }
213 }