eu.future.earth.gwt.client.ui.popup.SelectPopup Maven / Gradle / Ivy
The newest version!
package eu.future.earth.gwt.client.ui.popup;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import eu.future.earth.gwt.client.ui.ImageResources;
import eu.future.earth.gwt.client.ui.button.CleanPopupPanel;
import eu.future.earth.gwt.client.ui.button.UiType;
import eu.future.earth.gwt.client.ui.button.ValueListItem;
import eu.future.earth.gwt.client.ui.general.UiUtil;
import eu.future.earth.gwt.client.ui.general.UnOrderedList;
public class SelectPopup extends CleanPopupPanel implements HasValueChangeHandlers, ClickHandler {
private static final String NONE = "NONE";
private final PopupRenderer renderer;
private IconRenderer imageRenderer = null;
private final UnOrderedList ul = new UnOrderedList();
private boolean allowDeselect = false;
private List choices = null;
private final HashMap> ref = new HashMap>();
public D selected = null;
private String selectedKey = NONE;
private ValueListItem noneSelectedRow = null;
public SelectPopup(PopupRenderer newRenderer) {
this(newRenderer, UiType.Touch);
}
@SuppressWarnings("unchecked")
public SelectPopup(PopupRenderer newRenderer, UiType forTouch) {
super(true);
renderer = newRenderer;
if (renderer instanceof IconRenderer>) {
imageRenderer = (IconRenderer) renderer;
}
inittWidget();
}
public void addAllowDeselect() {
this.allowDeselect = true;
if (noneSelectedRow == null) {
noneSelectedRow = new ValueListItem();
if (imageRenderer != null) {
// noneSelectedRow.add(new Image(ImageResources.s24.no()));
}
noneSelectedRow.add(new Label(ImageResources.STATIC_TEXT.nothingSelected()));
noneSelectedRow.addClickHandler(this);
}
ul.add(noneSelectedRow);
}
public void addValue(D d) {
if (choices == null) {
choices = new ArrayList();
}
choices.add(d);
addValueInternal(d);
}
@Override
public HandlerRegistration addValueChangeHandler(ValueChangeHandler handler) {
return super.addHandler(handler, ValueChangeEvent.getType());
}
private void addValueInternal(D d) {
ValueListItem newRow = new ValueListItem(d);
if (imageRenderer != null) {
newRow.add(imageRenderer.getImage(d));
}
ref.put(d, newRow);
newRow.add(new Label(renderer.getLabel(d)));
newRow.addClickHandler(this);
// newRow.addDomHandler(this, ClickEvent.getType());
ul.add(newRow);
}
public void clearItems() {
setChoices(null);
}
public void deleteValue(D d) {
if (d == null) {
return;
}
if (choices == null) {
return;
}
List newChoices = new ArrayList();
for (D da : choices) {
if (!renderer.getId(da).equals(renderer.getId(d))) {
newChoices.add(da);
}
}
setChoices(newChoices);
}
public void focusSelected() {
if (selected != null) {
ValueListItem sel = ref.get(selected);
if (sel != null) {
// sel.getElement().focus();
}
}
}
public String getSelectedKey() {
if (selected != null) {
return renderer.getId(selected);
}
return NONE;
}
public D getValue() {
return selected;
}
@SuppressWarnings("unchecked")
private void handelSelect(Object source) {
if (source instanceof ValueListItem) {
ValueListItem real = (ValueListItem) source;
selected = real.getValue();
setKeyValue();
hide();
ValueChangeEvent.fire(this, selected);
}
}
public boolean hasChoices() {
return numberChoices() > 0;
}
private void inittWidget() {
ScrollPanel scroller = new ScrollPanel(ul);
add(scroller);
setStyleName("ftr-dropdown");
scroller.addStyleName("ul-selectable");
ul.getElement().getStyle().setOverflowX(Overflow.HIDDEN);
Event.addNativePreviewHandler(new NativePreviewHandler() {
@Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
if (isAttached()) {
int type = event.getTypeInt();
switch (type) {
case Event.ONKEYDOWN: {
int keyCode = event.getNativeEvent().getKeyCode();
switch (keyCode) {
case KeyCodes.KEY_DOWN: {
// Window.alert("" + keyCode);
next();
event.cancel();
break;
}
case KeyCodes.KEY_UP: {
// Window.alert("" + keyCode);
prev();
event.cancel();
break;
}
case KeyCodes.KEY_ENTER: {
// Window.alert("" + keyCode);
hide();
event.cancel();
break;
}
}
}
}
}
}
});
}
public boolean isAllowDeselect() {
return allowDeselect;
}
private void next() {
if (selected != null) {
int current = choices.indexOf(selected);
if (current < choices.size() - 1) {
D newSelect = choices.get(current + 1);
setValue(newSelect);
scrollToValue();
ValueChangeEvent.fire(this, selected);
}
}
}
public int numberChoices() {
if (choices == null) {
return 0;
}
return choices.size();
}
@Override
public void onClick(ClickEvent event) {
handelSelect(event.getSource());
}
private void prev() {
if (selected != null) {
int current = choices.indexOf(selected);
if (current > 0) {
setValue(choices.get(current - 1));
scrollToValue();
}
ValueChangeEvent.fire(this, selected);
}
}
public void scrollToValue() {
if (selected != null) {
ValueListItem sel = ref.get(selected);
if (sel != null) {
UiUtil.getInstance().scrollIntoViewTopOnly(sel);
}
}
}
public void setChoices(List newData) {
choices = newData;
ul.clear();
ref.clear();
if (isAllowDeselect()) {
addAllowDeselect();
}
if (choices != null) {
for (D d : newData) {
addValueInternal(d);
}
}
setSelectedInternal();
}
private void setKeyValue() {
if (selected != null) {
selectedKey = renderer.getId(selected);
} else {
selectedKey = NONE;
}
setSelectedToButton();
}
private void setSelectedInternal() {
if (selectedKey == null || selectedKey.equals(NONE)) {
setValue(null);
} else {
if (choices != null) {
for (D sel : choices) {
String found = renderer.getId(sel);
if (selectedKey.equals(found)) {
setValue(sel);
}
}
}
}
}
private void setSelectedToButton() {
for (ValueListItem remSelected : ref.values()) {
remSelected.removeStyleName("selected");
}
if (noneSelectedRow != null) {
noneSelectedRow.removeStyleName("selected");
}
if (selected == null) {
if (noneSelectedRow != null) {
noneSelectedRow.addStyleName("selected");
}
} else {
if (imageRenderer != null) {
// super.setImage(imageRenderer.getImage(selected));
}
ValueListItem fo = ref.get(selected);
if (fo != null) {
fo.addStyleName("selected");
}
}
}
public void setSelectionByKey(String data) {
selectedKey = data;
if (choices != null && !choices.isEmpty()) {
setSelectedInternal();
}
}
public void setValue(D newSelected) {
this.selected = newSelected;
setKeyValue();
}
public void tellParentChanged() {
ValueChangeEvent.fire(this, selected);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy