How to capture chrome browser close event in GWT? - gwt

Trying to capture browser close event in Chrome. tried below code for Chrome and event doesn't get fired when closing browser but works in Internet explorer.
Window.addCloseHandler(new CloseHandler() {
#Override
public void onClose(CloseEvent event) {
SC.say("on window close event");
}
});
Window.addWindowClosingHandler(new ClosingHandler() {
#Override
public void onWindowClosing(ClosingEvent event) {
SC.say("on window closing event");
}
});
how to capture the browser close event for Chrome in GWT?

Related

Android studio Same activity back press

Android Studio Same page visible for same activity onbackpressed
public void onBackPressed() {
// do something on back.
startActivity(new Intent(MainActivity.this, MainActivity.class));
return;
}
}

Eclipse plugin - add mouse listener to an editor everytime its opened

I am developing an Eclipse plugin and I need to store the click location in X and Y axis coordinates.
So far, I am doing this by using the org.eclipse.ui.IStartup interface and using this in the class implementing it:
#Override
public void earlyStartup() {
IWorkbench wb = PlatformUI.getWorkbench();
wb.addWindowListener(generateWindowListener());
}
And the in the given method I do this:
private IWindowListener generateWindowListener()
{
return new IWindowListener() {
private MouseListener clickMouseListener = new ClickMouseListener();
#Override
public void windowOpened(IWorkbenchWindow window) {
//Nothing
}
#Override
public void windowDeactivated(IWorkbenchWindow window) {
System.out.println("deactivaed");
IWorkbenchPage activePage = window.getActivePage();
activePage.getActiveEditor().getAdapter(org.eclipse.swt.widgets.Control.class).addMouseListener(clickMouseListener);
}
#Override
public void windowClosed(IWorkbenchWindow window) {
//Nothing
}
#Override
public void windowActivated(IWorkbenchWindow window) {
System.out.println("activated");
IWorkbenchPage activePage = window.getActivePage();
activePage.getActiveEditor().getAdapter(org.eclipse.swt.widgets.Control.class).addMouseListener(clickMouseListener);
}
};
}
So I basically register a listener to the general workbench with the intent to register a new MouseListener - where I save the coordinates - on newly opened editors.
This, however, works only if the entire eclipse window is minimized/maximized, as this listener is bound to the main window. If a new editor is opened, nothing happens (as it probably should work).
I would like to register new MouseListeners everytime a new editor is opened/activated/clicked on - so I can register them to the editor right when a new editor is opened. How can I subscribe/listen to any events related to editors (mostly activated/open new editor) so I can then subscribe a MouseListener to the editor and get current X and Y axis of the mouse-clicks?
Thank for any suggestions.
Use an IPartListener to listen to part events in a workbench window:
IPartService partService = window.getPartService();
partService.addPartListener(listener);
This will tell you about all editors (and views) being opened, closed, activiated, ....
In the early startup method the workbench window is not yet available. Use Display.asyncExec to delay running your code until the UI initialization is complete:
Display.getDefault().asyncExec(new Runnable() {
#Override
public void run() {
IPartService service = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService();
......
}
});

How to prevent gwt app to go to login page after page refresh?

I have a gwt app with a login page and a main page. After login app goes to main page. What i want is if i refresh the page to stay in main page and not going to login page. I have read many things and i tried History Mechanish but no result. Here is my code:
#Override
public void onSuccess(Login result) {
if (result.getLoginCount() == 1) {
final VerticalPanel userPanel = new VerticalPanel();
Anchor logout = new Anchor("logout");
logout.addStyleName("user");
logout.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
loginPanel.setVisible(true);
tablePanel.setVisible(false);
addPanel.setVisible(false);
userPanel.setVisible(false);
}
});
Label user = new Label("Hi " + usernameBox.getText());
userPanel.add(user);
user.addStyleName("user");
userPanel.add(logout);
userPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
userPanel.setVisible(true);
usernameBox.setText("");
passwordBox.setText("");
RootPanel.get("user").add(userPanel);
loginPanel.setVisible(false);
tablePanel.setVisible(true);
addPanel.setVisible(true);
History.newItem("main");
History.addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
if(History.getToken().equals("main")){
loginPanel.setVisible(false);
tablePanel.setVisible(true);
addPanel.setVisible(true);
}
}
});
}
i also tried:
String historyToken = event.getValue();
if(historyToken.substring(0 , 4).equals("main")){
loginPanel.setVisible(false);
tablePanel.setVisible(true);
addPanel.setVisible(true);
} else {
loginPanel.setVisible(true);
tablePanel.setVisible(false);
addPanel.setVisible(false);
}
Is this the right way to handle page refresh with History.addValueChangeHandler? I would appreciate any help.
GWT application is a single page application. It means that if your reload page, the state of your application will be lost. What you can do, is to use local storage to store same state data, but that is not a good idea for an authentication data.
I recommend you to refactor your code in a way that the authentication is done against the back end and your GWT client will recover it's state from back end data when user refreshes the page.

Back Button in webView

When the user clicks on back button, I've implemented the following code that works very well for links inside of my webview:
#Override
public void onBackPressed() {
if (this.webViewFragment != null && this.webViewFragment.canGoBack()) {
this.webViewFragment.goBack();
} else {
super.onBackPressed();
}
}
My problem is that I have native menu in Android, and when the user clicks on the menu Profile for example and then clicks on the menu Dashboard, the back button doesn't work. Nothing happens. As I said before, just works for links clicked inside of the webview.
Anyone knows a solution for that?
or try this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}

GWT capturing keyboard events on Composite

I am trying to capture keyboard events when there isn't a widget with the focus. I tried adding this to a composite widget :
addDomHandler(new KeyPressHandler(){
public void onKeyPress(KeyPressEvent event) {
logger.info("onKeyPress: "+event);
}
}, KeyPressEvent.getType());
but I am not trapping the keyboard events. I'd like to be able to determine when the Ctrl key is press whilst a mousedown event is occuring. How can I do that?
Use MouseDownHandler:
MouseDownHandler mouseDownHandler = new MouseDownHandler() {
#Override
public void onMouseDown(MouseDownEvent event) {
if (event.isControlKeyDown()) {
// do something
}
}
};
myCompositeWidget.addDomHandler(mouseDownHandler, MouseDownEvent.getType());