GWT handle back, refresh, close buttons - java

I'm trying to run some RPC calls when the user closes the window, refreshes it or clicks the back button but just for one single page. I found a post talking about handling but the solution is not working well, missing back button handler (not working) and always is for all page on the web, I can't find something for remove handler if you leave from page
Window.addWindowClosingHandler(new Window.ClosingHandler() {
#Override
public void onWindowClosing(ClosingEvent event) {
event.setMessage("You sure?");
}
});
Window.addCloseHandler(new CloseHandler<Window>() {
#Override
public void onClose(CloseEvent<Window> event) {
// Execute code when window closes!
System.out.println("ble ! ");
}
});
Framework: GWT 2.4 with mvp4g.
Browsers: FF and Chrome.

Because i use mvp4g framework i found a solution there , you need to extends your presenter with CyclePresenter and override onLoad and onUnload methods. These methods fire when view is load/unload from DOM, i tested and work for all cases, f5, back button, close browser/tab, go other web and call others events. Now i cant put some code there.

You need to remove the handler when you leave the page and then re-add it when you enter the page again. You have the "add" side covered with the above code, you are missing the "remove" part. You do that by using the HandlerRegistration object that is returned from the add*Handler methods. When you want to remove the registered handler, you just call the HandlerRegistration.removeHandler() method:
HandlerRegistration windowClosingHandler = Window.addWindowClosingHandler(new ClosingHandler() {
#Override
public void onWindowClosing(ClosingEvent event) {
// Handle window closing
}
});
// From now on the CloseHandler will be fired
// ...
// Somewhere else:
windowClosingHandler.removeHandler();
// From now on the CloseHandler won't be fired

Related

How to avoid triggering TableViewer when closing in Eclipse

I'm creating a TableViewer which has to respond to selections in other components.
I'm doing some actions in the implemented selectionChanged method.
#Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
}
I want to avoid triggering the operations in selectionChanged method when I close the view.
I tried checking the received part:
if(part.getSite().getId()!="myviewid")
{
}
But I always get the part of the previous selection (in my case ResourceNavigator).
How can I check if I selected my view and avoid doing some operations ?
You should remove your selection listener from the selection service when your view part is disposed
#Override
public void dispose()
{
ISelectionService service = (ISelectionService)getSite().getService(ISelectionService.class);
service.removeSelectionListener(myListener);
super.dispose();
}

valueChangeListener doesn't fire

I would like to set a CheckBox ReadOnly when my new window pop up.
Here is my code.
getForm().getField("foreign").addValueChangeListener(
new Property.ValueChangeListener(){
#Override
public void valueChange(Property.ValueChangeEvent event) {
changeValueForCurrency(event.getProperty().getValue());
}
}
);
Tried to let it happen by calling the changeValueForCurrency()
changeValueForCurrency(getForm().getField("foreign"));
Does not work.
My mate told me something about to fire it, but It doesn't has any methods like firing it.
Also, I tried to set this method below true;
setImmediate(true);
Still does not work
Did you try to do setImmediate() after adding the listener? i had this issue with a table.

org.eclipse.ui.startup in use with WindowListener - first activation not caught

Greetings fellow Stackoverflowians,
I am developing an Eclipse RCP application, and I want to add a listener to the ProjectExplorer Eclipse View, and this listener needs to be added before the user does anything, but after the GUI has been generated.
Right on startup, though, the PlatformUI.getWorkbench().getActiveWorkbenchWindow() returns null (d'oh, the window isn't activated) so therefore I add to the already created Workbench a WindowListener
PlatformUI.getWorkbench().addWindowListener(new IWindowListener() {
#Override
public void windowActivated(IWorkbenchWindow activatedWindow) {
//do stuff here
}
}
#Override
public void windowClosed(IWorkbenchWindow arg0) {
//remove stuff here
}
#Override
public void windowDeactivated(IWorkbenchWindow arg0) {
// stub
}
#Override
public void windowOpened(IWorkbenchWindow arg0) {
//stub
}
});
Now the problem that I've come across is that even though the ActiveWorkbenchWindow is populated, the windowActivated() method from the WindowListener is not called :(
Funnily enough, when I click on another window, then I click back on the application window, the windowActivated() method is called... therefore the listener was indeed registered.
Any help and suggestions are appreciated!
You could use overrides of the postWindowCreate or postWindowOpen methods of WorkbenchWindowAdvisor to set this up.
I've succeeded in not using the WindowListener anymore on the Workbench, so instead of adding it in the earlyStartup() method of my IStartup implementation, I've done this:
public class StartupHook implements IStartup {
#Override
public void earlyStartup() {
IWorkbenchWindow window = PlatformUI.getWorkbench().getWorkbenchWindows()[0];
ISelectionListener projectListener = new ProjectSelectionListener();
window.getSelectionService().addSelectionListener(projectListener);
}
}
The trick is that despite there being multiple windows opened on startup, only one is represented, that includes all the views, hence it is reasonable to access: PlatformUI.getWorkbench().getWorkbenchWindows()[0]
Presto, worked around the active window not being in the getActiveWindow() method of the Workbench

Differentiate browser tab close and refresh (to detect multiple instances of application)

How can Differentiating browser tab close and refresh functionality.
As of now window refresh and close event doesn't have different events.
My requirement is to checking weather user already logged in or not in any of tabs,So that I wont allow him to load my app in any other tabs.
In GWT (java)
private void registerWindowCloseEvent() {
Window.addCloseHandler(new CloseHandler<Window>() {
#Override
public void onClose(CloseEvent<Window> event) {
// do something on close
}
});
}
in JavaScript/Jquery:
window.onbeforeunload = function(e) {
// do something on close
};
The above events are firing for both the events refresh and close..is there any way to differentiate.
Differentiating browser tab close and refresh functionality is really a pain because we don't have two events to know which event being fired.
But there are always some requirements :)
What I'm doing is setting one cookie in on-load and making a flag true if found that cookie and removing the cookie on browser close event.
So until unless the he closed the active tab(logged in tab), that cookie still there and if he tries to open in another tab, then the already active dialog comes.
Note:Solution provided with help of Cookies.
In
Here is the onModule() for GWT / same as onload/document.ready() for java script/Jquery.
#Override
public void onModuleLoad() {
if("already_in_browser".
equalsIgnoreCase(Cookies.getCookie("already_in_browser"))){
showAlreadyTabActiveDialog();
return;
}else{
setLoggedincookie();
}
private void setLoggedincookie() {
isLoggedintab = true; //this is a global variable
registerWindowCloseEvent();
com.google.gwt.user.client.Cookies.
setCookie("already_in_browser","already_in_browser");
}
private void showAlreadyTabActiveDialog() {
alert("You are already active in another tab");
registerWindowCloseEvent();
}
/** This event is onbeforeunload in javascript
private void registerWindowCloseEvent() {
Window.addCloseHandler(new CloseHandler<Window>() {
#Override
public void onClose(CloseEvent<Window> event) {
if(isLoggedintab ){
Cookies.removeCookie("already_in_browser");
}
}
});
}
Let me know If you found any bugs or loop holes in this.So that I'l look in to them.
I would be very happy,If some one provide a solution,without using cookies.

How to wait for a mouse click

class GameFrameClass extends javax.swing.JFrame {
public void MyFunc()
{
UserButton.setText(Str);
UserButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
UserButtonActionPerformed(evt);
}
});
getContentPane().add(UserButton);
}
private void UserButtonActionPerformed(java.awt.event.ActionEvent evt) {
//print some stuff after mouse click
}
}
In someother class i define this function
void functionAdd()
{
GameFrameClass gfc = new GameFrameClass()
gfc.MyFunc()
System.out.println("PRINT THIS AFTER MOUSE CLICK")
}
If someone can look into this code. I want to wait on the mouse click . Is there a way i can print the line System.out.println("PRINT THIS AFTER MOUSE CLICK") after the mouse is being clicked . For now this happens immediately and i am not able to wait for the mouse click . Is there a way of doing it ? Apart from doing it inside the function UserButtonActionPerformed() . Please let me know .
This is a really "bad" way to do it...
private void UserButtonActionPerformed(java.awt.event.ActionEvent evt) {
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
System.out.println("PRINT THIS AFTER MOUSE CLICK");
removeMouseListener(this);
}
});
}
A better way would be to have a flag in the actionPerformed method that would "enable" a mouse listener (which you added earlier). This listener would check the flag on each click and when set to true it would flip the flag (to false) and process the event...
It's hard to tell from the wording, but I assume he or she simply wants to execute code after the button is triggered (and not actually wait). For that, you need to add the code inside the method being invoked inside the actionlistener (in this case UserButtonActionPerformed).
So:
private void UserButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println(...);
}
Also, following the Java coding conventions will help people answering your questions in the future.
You can always wait in UserButtonActionPerformed by defining it in the same class. If that is the case then you should not have the problem you are facing
Events are managed on a different thread which is the event dispatching thread, they are not managed by the thread that is executing your code (which presumably is the main thread).
This means that you can attach listeners to GUI elements but the only thing you can do to "wait" for the click is to execute the code inside the actionPerformed callback.
There is no way to pause the execution since the addActionListener doesn't do anything to effectively catch the event, it just adds the listener. Theoretically you could lock the main thread waiting to be notified by the event dispatch one but that would just be bad design.

Categories