Paste event on GWT - gwt

I found an example of how to catch the Paste event on a TextArea on GWT but it doesn't work.
public MyTextArea() {
super();
sinkEvents(Event.ONPASTE);
}
#Override
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
switch (event.getTypeInt()) {
case Event.ONPASTE:
System.out.println("Paste Detected");
Window.alert("Paste Works!!! Yippie!!!");
break;
}
}
The problem is that I never enter to onBrowserEvent ... Any suggestion ?
Thnx

Works for me as intended:
public class Starter implements EntryPoint {
#Override
public void onModuleLoad() {
RootPanel.get().add(new MyTextArea());
}
class MyTextArea extends TextArea {
public MyTextArea() {
super();
sinkEvents(Event.ONPASTE);
}
#Override
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
switch (event.getTypeInt()) {
case Event.ONPASTE:
System.out.println("Paste Detected");
Window.alert("Paste Works!!! Yippie!!!");
break;
}
}
}
}
On what browser are you testing it?

Related

How to add ValueChangeHandler to the customize DateBox?

I want to add the functionality of value change handler to my customize class that extends the DateBox. I had already added the key handler but I also want to implement the value change handler to the same class.
I want my DateBox to work properly when I take input from DatePicker or Keyboard.
Any other suggestion will also be helpful.
public class ValidationDateBoxInternal extends DateBox implements KeyUpHandler{
private List<String> regexList;
public ValidationDateBoxInternal(){
super();
this.getTextBox().addKeyUpHandler(this);
regexList = new ArrayList<String>();
}
public void setAsNotEmpty() {
regexList.add(DateValidation.isNotEmpty);
isValid();
}
public void setAsValidDate(){
regexList.add(DateValidation.isValidDate);
isValid();
}
public void onKeyUp(KeyUpEvent event) {
isValid();
}
public void setText(String text) {
super.getTextBox().setText(text);
isValid();
}
public void clearRegexList() {
regexList.clear();
}
public String getText() {
if (regexList.size() == 0) {
return super.getTextBox().getText();
}
if (isValid()) {
for (String regex: regexList) {
if (regex.equals(DateValidation.isValidDate)) {
return super.getTextBox().getText().toLowerCase().trim();
}
}
return super.getTextBox().getText().trim();
}
else {
return null;
}
}
public boolean isValid() {
for (String regex: regexList) {
if (!DateValidation.isValid(super.getTextBox().getText(), regex)) {
this.addStyleName("invalid");
return false;
}
}
this.removeStyleName("invalid");
return true;
}
}
Your class extends DateBox, which implements the HasValueChangeHandlers interface. So you can call:
DateBox.addValueChangeHandler(ValueChangeHandler<java.util.Date> handler)
For example:
public class MyDateBox extends DateBox implements ValueChangeHandler<Date> {
void foo() {
addValueChangeHandler(this);
}
void onValueChange(ValueChangeEvent<Date> event) {
// validate the value of the DateBox
}
}

Multiple Click handlers

I have different 3 Different Buttons with different onclick events :
add.addClickHandler(new ClickHandler()
{
#Override
public void onClick(ClickEvent event)
{
add();
}
});
set.addClickHandler(new ClickHandler()
{
#Override
public void onClick(ClickEvent event)
{
set();
}
});
get.addClickHandler(new ClickHandler()
{
#Override
public void onClick(ClickEvent event)
{
get();
}
});
So now if i extend this up to 10 Buttons my Script would be far to long,
is there a way to pass the Methode or do seperate the Handlers?
Suppose you have some view:
customview.ui.xml
<g:VerticalPanel>
<style:Button ui:field="addButton" text="Add"/>
<style:Button ui:field="setButton" text="Set"/>
<style:Button ui:field="getButton" text="Get"/>
</g:VerticalPanel>
In your View class define 3 fields and 3 handlers:
CustomView.java
public class CustomView extends ViewWithUiHandlers<CustomUiHandlers>
implements CustomPresenter.MyView {
#UiField
Button addButton;
#UiField
Button setButton;
#UiField
Button getButton;
// Here constructor and other code
#UiHandler("addButton")
void onAddButtonClicked(ClickEvent event) {
if (getUiHandlers() != null) {
getUiHandlers().onAddClicked();
}
}
#UiHandler("setButton")
void onSetButtonClicked(ClickEvent event) {
if (getUiHandlers() != null) {
getUiHandlers().onSetClicked();
}
}
#UiHandler("getButton")
void onGetButtonClicked(ClickEvent event) {
if (getUiHandlers() != null) {
getUiHandlers().onGetClicked();
}
}
}
CustomUiHandlers.java
public interface CustomUiHandlers extends UiHandlers {
void onAddClicked();
void onSetClicked();
void onGetClicked();
}
CustomPresenter.java
public class CustomPresenter extends
Presenter<CustomPresenter.MyView, CustomPresenter.MyProxy>
implements CustomUiHandlers {
// Some code
#Override
public void onAddClicked() {
// Here your code
}
#Override
public void onSetClicked() {
// Here your code
}
#Override
public void onGetClicked() {
// Here your code
}
}
You can bind event handler to a method by UiBinder, or alternatively wait for lambda support for GWT.

Click handler of button

I want to add a button and add click handler to it after entering the values in database.
I want that button to be on onSucess of greeting service.
help me
public static void edit1(String fnme,String lnme,String clgn,String scn){
greetingService.enter(fnme,lnme,clgn,scn, new AsyncCallback<String>()
{
public void onSuccess(String result)
{
Window.alert("successfully entered");
// TODO Auto-generated method stub
Button bt =new Button("submit");
RootPanel.get().add(bt);
bt.addClickHandler(new MyClickHandler);
}
public void onFailure(Throwable caught)
{
Window.alert("fail");
}
});
}
class MyClickHandler implements ClickHandler
{
public void onClick(ClickEvent e)
{
//create();
}
}
but this is not working.
Do you need the click handler?
Anyway this is what I think you're trying to do:
Button bt =new Button("submit");
RootPanel.get().add(bt);
bt.addClickHandler(new MyClickHandler);
public static void edit1(String fnme,String lnme,String clgn,String scn)
{
greetingService.enter(fnme,lnme,clgn,scn, new AsyncCallback<String>()
{
public void onSuccess(String result)
{
Window.alert("successfully entered");
// TODO Auto-generated method stub
create();
}
public void onFailure(Throwable caught)
{
Window.alert("fail");
}
});
}
class MyClickHandler implements ClickHandler
{
public void onClick(ClickEvent e)
{
// create();
}
}
Your MyClickHandler does nothing inside the onClick() method. It should work as long as you put code. Try this:
public static void edit1(String fnme,String lnme,String clgn,String scn){
greetingService.enter(fnme,lnme,clgn,scn, new AsyncCallback<String>() {
public void onSuccess(String result) {
Window.alert("successfully entered");
// TODO Auto-generated method stub
Button bt =new Button("submit");
RootPanel.get().add(bt);
bt.addClickHandler(new MyClickHandler() {
public void onClick(ClickEvent e) {
//DO SOMETHING HERE
}
});
}
public void onFailure(Throwable caught) {
Window.alert("fail");
}
});
}
class MyClickHandler implements ClickHandler {
public void onClick(ClickEvent e) {
/* OR DO SOMETHING HERE, BUT THAT WILL AFFECT ALL
* INSTANCES OF MyClickHandler
*/
}
}

GWT Platform UiHandlers not working

I have followed the GettingStarted on the GWTP tutorial
http://code.google.com/p/gwt-platform/wiki/GettingStarted
but unfortunately handlers not working, getUiHandlers() return null and exception stacktrace is same as in:
How to use UiHandlers of GWT Platform?
.
View Class
public class AView extends ViewWithUiHandlers<AUiHandlers> implements APresenter.Display {
#UiTemplate("AView.ui.xml")
interface AViewUiBinder extends UiBinder<Widget, AView> {}
private static AViewUiBinder uiBinder = GWT.create(AViewUiBinder.class);
#UiField Button saveBtn;
#UiField Button cancelBtn;
#UiField DivElement errorDiv;
private Widget widget;
#Inject
public AssetView() {
widget = uiBinder.createAndBindUi(this);
}
public Widget asWidget() {
return widget;
}
// Implementation: Presenter's Display methods
public void setErrorDivText(String msg) {
errorDiv.getStyle().setDisplay(Display.BLOCK);
errorDiv.setInnerText(msg);
}
// Handlers
#UiHandler("saveBtn")
void onSaveButtonClick(ClickEvent event) {
if(getUiHandlers() != null) {
getUiHandlers().onSaveButtonClick();
}
}
#UiHandler("cancelBtn")
void onCancelButtonClick(ClickEvent event) {
if(getUiHandlers() != null) {
getUiHandlers().onCancelButtonClick();
}
}
}
Handler Interface
public interface AUiHandlers extends UiHandlers {
void onSaveButtonClick();
void onCancelButtonClick();
}
Presenter
public class APresenter extends Presenter<APresenter.Display, APresenter.AssetProxy> implements AUiHandlers {
public interface Display extends View, HasUiHandlers<AUiHandlers> {
public void setErrorDivText(String msg);
}
#ProxyStandard
#NameToken(NameTokens.ASSET)
public interface AssetProxy extends ProxyPlace<AssetPresenter> {}
#Inject
public AssetPresenter(EventBus eventBus, Display view, AssetProxy proxy) {
super(eventBus, view, proxy);
getView().setUiHandlers(this);
}
#Override
protected void onBind() {
super.onBind();
}
#Override
protected void revealInParent() {
RevealRootContentEvent.fire( this, this );
}
public void onSaveButtonClick() {
getView().setErrorDivText("Save clicked.");
}
public void onCancelButtonClick() {
getView().setErrorDivText("Cancel clicked.");
}
}
Unable to understand where i am making mistake, implementation regarding UiHandlers is same as told in the above mentioned tutorial's link.
UiHandlers is not generic; it cannot be parameterized with arguments
As I see your handler interface, you have passed AUiHandlers type. I don't understand the package structure of UiHandlers . it should be com.gwtplatform.mvp.client.UiHandlers.
Please check import of it.
Update:
Remove private static AViewUiBinder uiBinder = GWT.create(AViewUiBinder.class);
and Pass as constructor argument
#Inject
public AssetView(AViewUiBinder uiBinder) {
widget = uiBinder.createAndBindUi(this);
}

Register KeyDownHandler on GWT VerticalPanel

I have a gwt VerticalPanel class that i need to handel KeyDown events for it.
the method i used to implement keyboard handler in my class is:
i add :
this.sinkEvents(Event.ONKEYDOWN);
to constructor
then i override method onBrowserEvent() to handle key down event.
#Override
public void onBrowserEvent(Event event) {
// TODO Auto-generated method stub
super.onBrowserEvent(event);
int type = DOM.eventGetType(event);
switch (type) {
case Event.ONKEYDOWN:
//call method to handle this keydown event
onKeyDownEvent(event);
break;
default:
return;
}
}
however this method doesn’t work for this VerticalPanel class.no KeyDown Event is fired when i press a key!
there are specific gwt widgets that support KeyDownHandler like Button etc..VerticalPanel is not one of them..so we need a work around to register a KeyDownHandler on a class extending VerticalPanel.
can you suggest an idea or hint?
thanks
You could create a Composite that wrappes a FocusPanel and a VerticalPanel. This way you can catch all key events provided the FocusPanel is focused. Simply delegate the needed methods to the panels:
public void onModuleLoad() {
ExtendedVerticalPanel panel = new ExtendedVerticalPanel();
panel.add(new Label("some content"));
panel.addKeyDownHandler(new KeyDownHandler() {
#Override
public void onKeyDown(KeyDownEvent event) {
if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
Window.alert("enter hit");
}
}
});
RootPanel.get().add(panel);
}
private class ExtendedVerticalPanel extends Composite implements HasWidgets, HasAllKeyHandlers {
private VerticalPanel fVerticalPanel;
private FocusPanel fFocusPanel;
public ExtendedVerticalPanel() {
fVerticalPanel = new VerticalPanel();
fFocusPanel = new FocusPanel();
fFocusPanel.setWidget(fVerticalPanel);
initWidget(fFocusPanel);
}
#Override
public void add(Widget w) {
fVerticalPanel.add(w);
}
#Override
public void clear() {
fVerticalPanel.clear();
}
#Override
public Iterator<Widget> iterator() {
return fVerticalPanel.iterator();
}
#Override
public boolean remove(Widget w) {
return fVerticalPanel.remove(w);
}
#Override
public HandlerRegistration addKeyUpHandler(KeyUpHandler handler) {
return fFocusPanel.addKeyUpHandler(handler);
}
#Override
public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
return fFocusPanel.addKeyDownHandler(handler);
}
#Override
public HandlerRegistration addKeyPressHandler(KeyPressHandler handler) {
return fFocusPanel.addKeyPressHandler(handler);
}
}
UPDATE
Your question on how to prevent the browser from scrolling when the arrow keys are pressed. Here a small example that works for me:
public void onModuleLoad() {
ExtendedVerticalPanel panel = new ExtendedVerticalPanel();
// make panel reeeeaally big
panel.setHeight("3000px");
panel.add(new TextBox());
panel.addKeyDownHandler(new KeyDownHandler() {
#Override
public void onKeyDown(KeyDownEvent event) {
if (event.getNativeKeyCode() == KeyCodes.KEY_DOWN) {
Window.alert("down hit");
event.preventDefault();
}
}
});
RootPanel.get().add(panel);
}
Add the handlers you need and call preventDefault() on the events the browser must not take care of.