GWT Delaying an action after an event trigger - gwt

I want to have the user click a button, then they will see a "Toast" message popup, which fades away , then the action that the button click performs should happen. What is the best way to do this? Should I trigger a Timer on catching the click event and let it timeout (While the toast is being displayed ) and then call the handling code or is there any built in delay mechanism in event handling I can use?
I don't want my toast to be involved at all in the event handling

If I really follow your requirements the following code should do:
// interface of the "Toast" no matter what the implementation actually is
public interface Toast
{
void open( String message );
void closeFadingAway();
}
// calling code
public class ClientCode
{
private static final int myDelay = 1000; // 1 second in millis
private Toast myToast;
void onMyAction()
{
myToast.open( "Your action is being handled..." );
Scheduler.get().scheduleFixedDelay( new RepeatingCommand()
{
#Override
public boolean execute()
{
myToast.closeFadingAway();
performAction();
return false; // return false to stop the "repeating" = executed only once
}
}, myDelay );
}
void performAction()
{
// do something interesting
}
}
Now, if you actually mean to be able to interrupt the action when the user presses some button in the toast this is a different story.

If you are using a popup panel you could use the addCloseHandler on the popup panel and from here call the method that would have otherwise been called by the button.
popUpPanel.addCloseHandler(new CloseHandler<PopupPanel>(){
#Override
public void onClose(CloseEvent<PopupPanel> event) {
// TODO Do the closing stuff here
}
});
So when the PopupPanel disappears and the close event triggers you can do your magic there.

Related

GWT Button Click Enable/Disable Pattern -- GwtEvent assertLive() in dev mode

To avoid users clicking repetitively on the same button and by the same token send multiple requests to server, I have used the following pattern:
In button ClickHandler.onClick, disable the button.
In call back, re-enable the button.
See pattern in code below. The "rpcCall" function below basically is the core implementation of the Button onClick(final ClickEvent event).
private void rpcCall(final ClickEvent event)
{
final AsyncCallback<Void> callback = new AsyncCallback<Void>()
{
#Override
public void onSuccess(Void result)
{
final Button source = (Button) event.getSource(); // Dev mode isLive assertion failure.
source.setEnabled(true);
// Process success...
}
#Override
public void onFailure(Throwable caught)
{
final Button source = (Button) event.getSource();
source.setEnabled(true);
// Process error...
}
};
// Disable sender.
final Button source = (Button) event.getSource();
source.setEnabled(false);
// RPC call.
final RpcAsync rpcAsync = getRpcAsync();
RpcAsync.rpcCall(..., callback);
}
I just noticed a "This event has already finished being processed by its original handler manager, so you can no longer access it" exception caused by an isLive assertion failure in dev mode when the onSuccess async function calls event.getSource().
It seems to work in production/javascript mode though.
This dev mode assertion failure makes me question this pattern.
Is it a good pattern? Why do I get the exception only in dev mode? What would be a better pattern?
Obviously, I could bypass the call to event.getSource() by passing the source Button as an argument of the rpc wrapper call function, but it seems redundant with the event object already carrying such a reference.
Historically, the way you got the event object in IE was to use window.event, which only lasted the time to process the event. GWT's Event object therefore had to put guards so you're discouraged to keep a hold on an event instance, as it could suddenly reflect another event being processed, or no event at all (weird!)
Fortunately, Microsoft has since fixed their browser, and this is why it works when you test it (I bet you didn't test in IE6 ;-) ).
The correct way to handle that situation is to extract all the data you need from the event and keep them in final variables:
private void rpcCall(final ClickEvent event)
{
final Button source = (Button) event.getSource();
final AsyncCallback<Void> callback = new AsyncCallback<Void>()
{
#Override
public void onSuccess(Void result)
{
source.setEnabled(true);
// Process success...
}
#Override
public void onFailure(Throwable caught)
{
source.setEnabled(true);
// Process error...
}
};
// Disable sender.
source.setEnabled(false);
// RPC call.
final RpcAsync rpcAsync = getRpcAsync();
RpcAsync.rpcCall(..., callback);
}

GWT event handling. Needs to block the event till form submit finish

I'm developing a wizard by using GWT. In the Wizard first page i have a form component to upload the file. In the wizard panel i have the next button when i press the next button the validation method will be triggered if the validation is passed then i'm calling the form.submit(); but before form.submit() handler starts the functionality the validation methods completes it. After it completes the validation method only the form submit it's really taking part. How can i controll this event behavior, when i submit the form using form.submit() the remaining actions has to wait till this form returns to it's handler.
Advance Thanks.
Assuming you have a FormPanel:
FormPanel form = new FormPanel();
You can add a handler:
form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
public void onSubmitComplete(SubmitCompleteEvent event) {
// TODO: Do the next step in the wizard
// Use event.getResults() to get the text of the response
}
});
Write a boolean which you'll set to true once the validation is complete. Then start a timer that waits on that boolean. But consider using events and callbacks for repeated use. Using timer will make your code messy if you overuse them.
#UiHandler("submit")
protected void onSubmit() {
validated=false;
validate();
Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
public boolean execute() {
if (validated) {
if (validationSucceeded()) {
submit();
}
return false;
}
return true;
}
}, 250);
}
private boolean validated = false;
private void validate() {
// do validation
validated=true;
}

continuous actions to be performed when I long tapped MGWT button until the touch end event fired

I want to invoke a method continuously when I used MGWT button long tap handler, this should be done until I release the button. i.e; until the touch end event fired. For this I had written Timer inside the MGWT Button's long tap handler.I continuously calling the my task method inside the run method of the Timer. my code:
upButton.addLongTapHandler(new LongTapHandler() {
#Override
public void onLongTap(LongTapEvent event) {
upBtnTimer = new Timer() {
#Override
public void run() {
if(getValue() >= maxValue){
Window.alert("max val reached");
upBtnTimer.cancel();
}else{
setValue(getValue() + RATE);
}
}
};
upBtnTimer.scheduleRepeating(100);
}
});
And I also wrote touch end handler to the upButton. this is:
upButton.addTouchEndHandler(new TouchEndHandler() {
#Override
public void onTouchEnd(TouchEndEvent event) {
if(upBtnTimer!=null){
upBtnTimer.cancel();
upBtnTimer = null;
}
}
});
this is OK when I'm testing my mobile application on browser, but when I installed my application in iOS/Android device, this is not working. Only single tap event firing.
If you are clear with my requirement please tell me if there is another approach to do this. Thanks in advance.

GWT onResize & mouse up

I have a window resizeHandler, it is working fine. Except that I'm just interested in the final dimension of the window, in other words, I'm interested in the dimension of the window when the mouse button is released. Is there a way I can listen to window mouse events?
I've written a piece of code that accomplishes my goal but I'd prefer something more obvious. resizeRequestId is a field:
private void processResize() {
final Timer timer = new Timer() {
final Size windowSize = getWindowDimention();
final int requestId = ++resizeRequestId;
#Override
public void run() {
final boolean isLatestRequest = requestId == resizeRequestId;
if (isLatestRequest) {
//DO SOMETHING WITH windowSize
}
}
};
timer.schedule(100);
}
The browser doesn't pass along events that happen outside of the page, and the window resize counts as outside the page. That said, you still get a hook for resize actions of the entire browser window:
Window.addResizeHandler(new ResizeHandler() {
public void onResize(ResizeEvent event) {
//get, process window resize behavior
}
});
For some browsers and some resizes, you'll get lots of events as the mouse moves, and for others, you'll only get the complete one. In firefox, for example, the resize handle in the corner sends every change that is made, while the side handles each send only once the user releases the mouse. Minimizing and maximizing the window also result in a single event.
Colin is right.
Moreover, if you do a lot of calculations "on resize" (e.g. forceLayout), it is a good idea to add a Timer. This way the calculations will be fired once every...10th of second?
final Timer resizeTimer = new Timer() {
#Override
public void run() {
mainPanel.forceLayout();
}
};
Window.addResizeHandler(new ResizeHandler() {
public void onResize(ResizeEvent event) {
int height = event.getHeight();
mainPanel.setHeight(height + "px");
resizeTimer.cancel();
resizeTimer.schedule(100);
}
});
That's it

gwt Popup is not centered

I use gwt popup to show some messages, but it is not displayed in the
center
of the display event if i call popup.center(). Actually it is not centered
only the first time, if i close it and open it again every thing is ok,
but not the first time. How to fix that?
GWT.runAsync(new RunAsyncCallback() {
#Override
public void onSuccess() {
Image fullImage = new Image(optionImageName);
fullImage.setAltText("Loading image ...");
imagePopup.setWidget(fullImage);
imagePopup.center();
}
});
I found this question on http://www.devcomments.com/gwt-Popup-is-not-centered-at107182.htm, and today I have had this problem too. I found the answer and i will post it here for future reference.
I found that the problem is that the image is not loaded completed when you center the popup. This happens the first time only because the second time the image is somehow cashed by the browser.
The solution is to center it on the onLoad event as well.
GWT.runAsync(new RunAsyncCallback() {
#Override
public void onSuccess() {
Image fullImage = new Image(optionImageName);
fullImage.setAltText("Loading image ...");
fullImage.addLoadHandler(new LoadHandler() {
#Override
public void onLoad(LoadEvent event) {
imagePopup.center();
}
});
imagePopup.setWidget(fullImage);
imagePopup.center();
}
});
I had this problem as well. The reason that you have to call center twice is because the popup container is actually removed from the DOM when the popup is "hidden". This is problematic because your popup has to now "show" the contents of the popup before you can check that the image is loaded.
The issue with the implementation recommended is that the first call to center() will be done incorrectly if the image is not cached. The second call to center will correct it. On the browser, this causes a shift of the popup dialog box (which looks pretty bad).
I would recommend the following:
1. Put a waiting spinner in the same display and show that initially.
2. One the loadHandler is called, display the image, hide the spinner, and recenter.
public void onClick(ClickEvent event) {
// TODO Auto-generated method stub
final ADPopup popup = new ADPopup();
popup.setHeight("300px");
popup.setWidth("500px");
popup.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
public void setPosition(int offsetWidth, int offsetHeight) {
// TODO Auto-generated method stub
int left = (Window.getClientWidth() - offsetWidth) / 3;
int top = (Window.getClientHeight() - offsetHeight) / 3;
popup.setPopupPosition(left, top);
}
});
popup.show();
}
Hope this Help.