eu.future.earth.gwt.client.ui.button.TouchToggleButton Maven / Gradle / Ivy
The newest version!
package eu.future.earth.gwt.client.ui.button;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;
public class TouchToggleButton extends Composite implements HasClickHandlers, ClickHandler, Focusable {
protected FocusPanel main = new FocusPanel();
private InlineLabel textSpan = new InlineLabel();
public TouchToggleButton() {
this(null, null, UiType.Touch);
}
public TouchToggleButton(UiType forTouch) {
this(null, null, forTouch);
}
public TouchToggleButton(String newText) {
this(newText, null, UiType.Touch);
}
public TouchToggleButton(String newText, UiType forTouch) {
this(newText, null, forTouch);
}
public TouchToggleButton(Image newImage) {
this(null, newImage);
}
public TouchToggleButton(Image newImage, UiType forTouch) {
this(null, newImage, forTouch);
}
public TouchToggleButton(String newText, Image newImage) {
this(newText, newImage, UiType.Touch);
}
public TouchToggleButton(String newText, Image newImage, UiType forTouch) {
this(newText, null, newImage, forTouch);
}
public TouchToggleButton(String newText, String newSelectedText) {
this(newText, newSelectedText, null, UiType.Regular);
}
private final String text;
private final String deselected;
public TouchToggleButton(String newText, String newDeselected, Image newImage, UiType forTouch) {
this(newText, newDeselected, newImage, null, forTouch);
}
private final Image image;
private final Image deselectImage;
public TouchToggleButton(String newText, String newDeselected, Image newImage, Image newNotImage, UiType forTouch) {
super();
initWidget(main);
text = newText;
image = newImage;
deselectImage = newNotImage;
deselected = newDeselected;
super.addStyleName("ftr-button");
super.addStyleName("ftr-toggle");
if (UiType.Touch.equals(forTouch)) {
super.addStyleName("touch");
} else {
super.addStyleName("notouch");
}
main.addClickHandler(this);
setImage(newImage);
if (newText != null) {
main.getElement().appendChild(textSpan.getElement());
textSpan.setText(newText);
}
setState();
}
protected void addStyleNameToMain(String newStyle) {
main.addStyleName(newStyle);
}
protected void removeStyleNameToMain(String newStyle) {
main.removeStyleName(newStyle);
}
private boolean selected = false;
private Element imagePlaced = null;
public void setImage(Image newImage) {
if (imagePlaced != null) {
main.getElement().removeChild(imagePlaced);
}
if (newImage != null) {
imagePlaced = newImage.getElement();
main.getElement().insertFirst(imagePlaced);
}
}
public void setText(String newText) {
textSpan.setText(newText);
if (!main.getElement().isOrHasChild(textSpan.getElement())) {
main.getElement().appendChild(textSpan.getElement());
}
}
private boolean enabled = true;
public void setEnabled(boolean newState) {
if (enabled != newState) {
if (enabled) {
super.addStyleName("ftr-disabled");
} else {
super.removeStyleName("ftr-disabled");
}
enabled = newState;
}
}
public boolean isEnabled() {
return enabled;
}
@Override
public void onBrowserEvent(Event event) {
if (enabled) {
selected = !selected;
setState();
super.onBrowserEvent(event);
} else {
event.preventDefault();
}
}
@Override
public HandlerRegistration addClickHandler(ClickHandler handler) {
return addHandler(handler, ClickEvent.getType());
}
@Override
public void onClick(ClickEvent event) {
}
@Override
public int getTabIndex() {
return main.getTabIndex();
}
@Override
public void setAccessKey(char key) {
main.setAccessKey(key);
}
@Override
public void setFocus(boolean focused) {
main.setFocus(focused);
}
@Override
public void setTabIndex(int index) {
main.setTabIndex(index);
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean newSelected) {
if (selected != newSelected) {
selected = newSelected;
setState();
}
}
private void setState() {
if (selected) {
super.addStyleName("ftr-active");
if (text != null) {
textSpan.setText(text);
}
if (deselectImage != null) {
setImage(image);
}
} else {
super.removeStyleName("ftr-active");
if (deselected != null) {
textSpan.setText(deselected);
}
if (deselectImage != null) {
setImage(deselectImage);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy