Detecting refresh event in GWT - gwt

We need to detect the Refresh event (f5) / reload of the web application.
Is there an event in GWT that can detect this?
thanks,
mj

Add a Window Closing Handler:
HandlerRegistration registration = Window.addClosingHandler(new ClosingHandler() {
void onWindowClosing(ClosingEvent event) {
// call the server, or whatever
}
});
EDIT
The HandlerRegistration is used to deregister the Handler if you don't need it anymore.

Related

GWT Bootstrap typeahead event listener?

I am using GWTBootstrap3 Typeahead widget. The main problem is the event is not getting deregistered and the events are bundling up as many times i load the component. The components are cached by default. The first component load triggers event 1 time and in second time component load triggers 2 times and so on. It's causing a lot of problem. I have tried HandlerRegistration and removeHandler() its not working. If any body found a solution please let me know.
Here is the bit of code where the event is registered:
HandlerRegistration typeAheadListener =
productSelect.addTypeaheadSelectedHandler(new TypeaheadSelectedHandler<Part>() {
#Override public void onSelected(TypeaheadSelectedEvent<Part> event) {
selectedPart = event.getSuggestion().getData(); // Handling the event
}
});
Thanks
I can think of two options there:
You can register the event handler in the productSelect's constructor, or in the code where you call the constructor. Not when the component is loaded.
You can check the HandlerRegistration API, it gives a tip on how a handler can deregister itself:
new MyHandler() {
HandlerRegistration reg = MyEvent.register(eventBus, this);
public void onMyThing(MyEvent event) {
/* do your thing * /
reg.removeHandler();
}
};

Hide Dialog from inside in LWUIT

I have created a Dialog with two buttons Yes, No, and then I have add action listener to them, my problem is that I want no button to hide the Dialog that I have created
the code is looks like:
dialog = new Dialog(title);
dialog.setDialogType(Dialog.TYPE_CONFIRMATION);
ta = new TextArea(text);
ta.getStyle().setBorder(Border.createEmpty());
ta.setEditable(false);
yesCommand = new Button("YES");
noCommand = new Button("NO");
yesCommand.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
LGBMainMidlet.getLGBMidlet().notifyDestroyed();
}
});
noCommand.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Logger.Log("Bye Bye");
dialog = null;
System.gc();
}
});
dialog.addComponent(ta);
dialog.addComponent(yesCommand);
dialog.addComponent(noCommand);
dialog.show();
the code is not working for me, can anyone told me what is the problem?
B.N. I have used dialog.dispose(), but it exit the whole application
It is better to use
dialog.setTimeout(1000); the number show the time limit the dialog box wait in milliseconds. So by doing this you can exit the dialog form automatically.
Dialog.dispose() does not exit the whole application, it just closes the dialog.
If you have nothing in your application you might see nothing if you dispose the dialog.

Block gwt DisclosurePanel on open state

How may I block a gwt DisclosurePanel on the open state ?
I mean, how can I prevent this DisclosurePanel to close if the user click the header more than once ?
(My header is a textBox, I want the user to enter a text, and the panel should remain open if the user unfocus the textBox and focus newly by clicking it. The DisclosurePanel content has a "cancel" button that closes the panel)
Thank you very much.
I edit my question after 2 first answers: I would like to avoid to reopen the DisclosurePanel once closed to avoid flashing effect. I actually want to prevent the DisclosurePanel to close. Maybe sinkEvents can help me... if so, how? Thanks.
A NativePreviewHandler receives all events before they are fired to their handlers. By registering a nativePreviewHandler the first time your disclosurePanel is opened, you can cancel the click event. You can later decide to remove this handler by preventClose.removeHandler();
HandlerRegistration preventClose = null;
....
panel.addOpenHandler(new OpenHandler<DisclosurePanel>() {
#Override
public void onOpen(OpenEvent<DisclosurePanel> event) {
if (preventClose == null){
preventClose = Event.addNativePreviewHandler(new NativePreviewHandler() {
#Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
if (event.getTypeInt()==Event.ONCLICK && event.getNativeEvent().getEventTarget() == panel.getHeader().getElement().cast())
event.cancel();
}
});
}
}
});
The obvious answer is review the javadoc here: https://google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/gwt/user/client/ui/DisclosurePanel.html
There is a setOpen() method that: Changes the visible state of this DisclosurePanel.
Set it to false from a click event to capture the user action.
The JavaDoc is right here: https://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/user/client/ui/DisclosurePanel.html
jamesDrinkard pointed the old 1.5 javadoc.
You can use the addCloseHandler(CloseHandler<DisclosurePanel> handler) method to add a handler so when the user tries to close it you can reopen it again with setOpen().
Maybe not the best way, but it worked for me (maybe just one of both will work too):
dPanel.setOpen(true);
dPanel.addOpenHandler(new OpenHandler<DisclosurePanel>() {
#Override
public void onOpen(OpenEvent<DisclosurePanel> event) {
dPanel.setOpen(true);
}
});
dPanel.addCloseHandler(new CloseHandler<DisclosurePanel>() {
#Override
public void onClose(CloseEvent<DisclosurePanel> event) {
dPanel.setOpen(true);
}
});

What is need History.fireCurrentHistoryState() in GWT History?

Hello I am working on a GWT sample history management application.
Here is my onModuleLoad Code.
public void onModuleLoad() {
ContentPanel panel = ContentPanel.getInstance();
if(History.getToken()!=null && History.getToken().length()==0)
{
History.newItem("first_page");
}
History.addValueChangeHandler(new HistoryHandler());
RootPanel.get().add(panel);
History.fireCurrentHistoryState();
}
In this I fired History.fireCurrentHistoryState(); to fire current state of history.
Now In my firstPanel class ther is button named Second Panel on which history token second_page is fired.
public FirstPanel() {
VerticalPanel panel = new VerticalPanel();
Button button2 = new Button("Second Panel");
button2.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
History.newItem("second_page");
}
});
panel.add(button2);
initWidget(panel);
}
But here no need to fire History.fireCurrentHistoryState() ; again.
simply histtory.newItem works fine.
Now I want to know that what the need of History.fireCurrentHistoryState() at module load time only?
Also why it is not required second time in the application.?
History.fireCurrentHistoryState() invokes your history handlers without actually inserting new history item in browser history stack, while History.newItem(token) does insert new history token into history stack.
Note: if your current token is the same as new token (i.e. same page is reloaded), then browsers do not insert this into history stack. In this case (current token == new token) History.fireCurrentHistoryState() has the same effect as History.newItem(currentToken).

Integration Widget (GWT) with DynamicForm (Smartgwt) - com.google.gwt.user.client.ui.AttachDetachException

I had this problem when I created a Window (Smartgwt) and put a DynamicForm (Smartgwt) in this Window, In this DynamicForm, I have a CanvasItem (Smartgwt) in which I put a RichTextArea (GWT). And when I press "ESC", I can quit the Window (Smartgwt) without probleme. But when I press "F5" to refresh my application, the browser pops up a exception saying "com.google.gwt.user.client.ui.AttachDetachException". To solve this problem, I do the following:
public class MailWindow extends Window {
public MailWindow(){
this.addCloseClickHandler(new CloseClickHandler() {
public void onCloseClick(CloseClientEvent event) {
form.getRichTextArea().removeFromParent();
MailWindow.this.destroy();
}
});
}
}
Which solved my problem! :)
Kewei
Thanks for posting this. We'll try to incorporate the logic in SmartGWT itself so that you don't need to explicitly call removeFromParent()