/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.topspin.ui.client;
import com.google.gwt.dom.client.OptionElement;
import com.google.gwt.dom.client.SelectElement;
import com.google.gwt.events.client.EventListenerRemover;
/**
* A widget that wraps a <select> element.
*
* TODO(jgw): multiple-selection
*/
public class Select extends Widget {
/**
* Creates a new Select widget in the given container.
*
* @param container the container in which the widget will be created
*/
public Select(Container container) {
super(container.getDocument().createSelectElement(), container);
}
/**
* Creates a new Select widget that wraps the element with the given id.
*
* @param elementId the id of the element to be wrapped
*/
public Select(String elementId) {
super(elementId);
SelectElement.as(getElement());
}
/**
* Creates a new Select widget that wraps the given element.
*
* @param element the element to be wrapped
*/
public Select(SelectElement element) {
super(element);
}
/**
* Adds a listener to be informed of blur events on this widget.
*
* @param listener the listener to be added
* @return a handle that can be used to remove the listener.
*/
public EventListenerRemover addBlurListener(final BlurListener listener) {
return BlurEvent.addBlurListener(this, getElement(), listener);
}
/**
* Adds a listener to be informed of change events on this widget.
*
* @param listener the listener to be added
* @return a handle that can be used to remove the listener.
*/
public EventListenerRemover addChangeListener(ChangeListener listener) {
return ChangeEvent.addChangeListener(this, getElement(), listener);
}
/**
* Adds a listener to be informed of focus events on this widget.
*
* @param listener the listener to be added
* @return a handle that can be used to remove the listener.
*/
public EventListenerRemover addFocusListener(final FocusListener listener) {
return FocusEvent.addFocusListener(this, getElement(), listener);
}
/**
* Adds a listener to be informed of keydown events on this widget.
*
* @param listener the listener to be added
* @return a handle that can be used to remove the listener.
*/
public EventListenerRemover addKeyDownListener(
final KeyDownListener listener) {
return KeyDownEvent.addKeyDownListener(this, getElement(), listener);
}
/**
* Adds a listener to be informed of keypress events on this widget.
*
* @param listener the listener to be added
* @return a handle that can be used to remove the listener.
*/
public EventListenerRemover addKeyPressListener(
final KeyPressListener listener) {
return KeyPressEvent.addKeyPressListener(this, getElement(), listener);
}
/**
* Adds a listener to be informed of keyup events on this widget.
*
* @param listener the listener to be added
* @return a handle that can be used to remove the listener.
*/
public EventListenerRemover addKeyUpListener(final KeyUpListener listener) {
return KeyUpEvent.addKeyUpListener(this, getElement(), listener);
}
/**
* Appends an option to the list.
*
* @param text the option text
*/
public void addOption(String text) {
getSelectElement().add(createOption(text), null);
}
/**
* Appends an option to the list.
*
* @param text the option text
* @param value the option value
*/
public void addOption(String text, String value) {
getSelectElement().add(createOption(text, value), null);
}
/**
* Removes all options from the list.
*/
public void clearOptions() {
getSelectElement().clear();
}
/**
* Gets the number of options in the list.
*
* @return the number of options
*/
public int getOptionCount() {
return getSelectElement().getOptions().getLength();
}
/**
* Gets the index of the currently selected option.
*
* @return the selected index
*/
public int getSelectedIndex() {
return getSelectElement().getSelectedIndex();
}
/**
* Gets the value of the currently selected option.
*
* @return the value of the selected option
*/
public String getSelectedValue() {
SelectElement elem = getSelectElement();
OptionElement option = elem.getOptions().getItem(elem.getSelectedIndex());
return option.getValue();
}
/**
* Inserts an option at the given position in the list. If the given index is
* equal to the size of the list, this is equivalent to
* {@link #addOption(String)}.
*
* @param text the option text
* @param index the index before which the option will be added
*/
public void insertOption(String text, int index) {
int numOptions = getOptionCount();
assert (index >= 0) && (index <= numOptions);
if (index == numOptions) {
addOption(text);
return;
}
SelectElement select = getSelectElement();
select.add(createOption(text), select.getOptions().getItem(index));
}
/**
* Inserts an option at the given position in the list. If the given index is
* equal to the size of the list, this is equivalent to
* {@link #addOption(String, String)}.
*
* @param text the option text
* @param value the option value
* @param index the index before which the option will be added
*/
public void insertOption(String text, String value, int index) {
int numOptions = getOptionCount();
assert (index >= 0) && (index <= numOptions);
if (index == numOptions) {
addOption(text, value);
return;
}
SelectElement select = getSelectElement();
select.add(createOption(text, value), select.getOptions().getItem(index));
}
/**
* Removes an option from the list.
*
* @param index the index of the option to be removed
*/
public void removeOption(int index) {
assert (index >= 0) && (index < getOptionCount());
getSelectElement().remove(index);
}
/**
* Sets the currently selected option.
*
* @param index the index of the option to select
*/
public void setSelectedIndex(int index) {
assert (index >= 0) && (index < getOptionCount());
getSelectElement().setSelectedIndex(index);
}
private OptionElement createOption(String text) {
return createOption(text, null);
}
private OptionElement createOption(String text, String value) {
OptionElement opt = getElement().getOwnerDocument().createOptionElement();
opt.setText(text);
if (value != null) {
opt.setValue(value);
}
return opt;
}
/**
* Gets this widget's element, down-cast to the correct type.
*/
protected SelectElement getSelectElement() {
return (SelectElement) getElement();
}
}