eu.future.earth.gwt.client.ui.button.TouchButton 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.uibinder.client.UiConstructor;
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;
import eu.future.earth.gwt.client.ui.event.ClickTouchEvent;
import eu.future.earth.gwt.client.ui.event.ClickTouchHandler;
import eu.future.earth.gwt.client.ui.event.HasClickTouchHandlers;
public class TouchButton extends Composite implements HasClickHandlers, Focusable, HasClickTouchHandlers {
protected FocusPanel main = new FocusPanel();
protected InlineLabel text = new InlineLabel();
@UiConstructor
public TouchButton() {
this(null, null, UiType.Touch);
}
public TouchButton(UiType forTouch) {
this(null, null, forTouch);
}
public TouchButton(String newText) {
this(newText, null, UiType.Touch);
}
public TouchButton(String newText, UiType forTouch) {
this(newText, null, forTouch);
}
public TouchButton(Image newImage) {
this(null, newImage);
}
public TouchButton(Image newImage, UiType forTouch) {
this(null, newImage, forTouch);
}
public TouchButton(String newText, Image newImage) {
this(newText, newImage, UiType.Touch);
}
public TouchButton(String newText, Image newImage, UiType forTouch) {
this(newText, newImage, forTouch, true);
}
public TouchButton(String newText, Image newImage, UiType forTouch, boolean setTouch) {
super();
initWidget(main);
super.addStyleName("ftr-button");
if (newImage == null || setTouch) {
if (UiType.Touch.equals(forTouch)) {
super.addStyleName("touch");
} else {
super.addStyleName("notouch");
}
}
main.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
if (enabled) {
handleClick();
}
}
});
setImage(newImage);
if (newText != null) {
main.getElement().appendChild(text.getElement());
text.setText(newText);
}
}
public void handleClick() {
ClickTouchEvent.fire(this);
}
public void addStyleNameToMain(String newStyle) {
main.addStyleName(newStyle);
}
protected void removeStyleNameToMain(String newStyle) {
main.removeStyleName(newStyle);
}
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) {
text.setText(newText);
if (!main.getElement().isOrHasChild(text.getElement())) {
main.getElement().appendChild(text.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) {
// if (DOM.eventGetType(event) == Event.ONCLICK) {
//
// }
super.onBrowserEvent(event);
// handleClick();
} else {
event.preventDefault();
}
}
@Override
public HandlerRegistration addClickHandler(ClickHandler handler) {
return addHandler(handler, ClickEvent.getType());
}
@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);
}
@Override
public HandlerRegistration addClickHandler(ClickTouchHandler handler) {
return super.addHandler(handler, ClickTouchEvent.getType());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy