How do I stop an Eclipse Editor from closing in an RCP - eclipse

I am working on an Eclipse based RCP. We have a need to prevent one of the opened editors from being closed by the user.
The desired behavior is:
the user clicks the X in the editor window or "CTRL+W"
a dialog pops up saying: "If you close this editor, your activity will stop. Do you want to?"
if they click yes, it closes, if no, it stays open.
Oh yeah, and is this even possible?
Thanks,
gk

You could use a org.eclipse.ui.ISaveablePart2, more specifically the method promptToSaveOnClose().
However, as said in this thread,
it will only be shown if the editor is dirty at the time it is closed.
See an example in this SaveableHelper.java source file.
See also the article Prevent that a RCP Editor is closed, which explains how this method works:

You can also cancel the saving in
#Override
public void doSave(IProgressMonitor monitor) {
by calling
monitor.setCanceled(true);
In the EditorPart implementation

Not directly related but I was looking for a way to prevent an Editor to be closed and found this little hack, hope it could help.
page.addPartListener(new IPartListener2() {
// [...]
#Override
public void partClosed(IWorkbenchPartReference partRef) {
try {
page.openEditor(input, id);
} catch (PartInitException e) {
e.printStackTrace();
}
}
});

Related

Eclipse RAP multi-window/tab

I would like to have a multi-tab/windowed Eclipse RAP application.
I am able to open a second window using
UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
launcher.openURL("/gasf?foo=other_perspective");
Where I use the foo paramter to select the perspetive I want. However using this method will create a speparate http session, thus the various listeners and so on won't communicate with my first window.
I also tried opening a second window/page using
PlatformUI.getWorkbench().getActiveWorkbenchWindow().openPage("other_perspective" , null);
But this merely changes the current window perspective but does not open a second window or tab in my browser.
Has anyone achieved a multi-tab RAP application with working selectionlisteners between the tabs?
Thanks for any help you can provide
EDIT:
THANKS a lot ralfstx, as you pointed out, I can share the listeners or anything using the shared HTTP session, so far so good. Now the next step is to be able to update a tab based on an external event.
To try my idea of refresh from another tab, I did a dummy timer that does something 2 seconds later (i.e. simulate something triggered from another tab) with:
final ServerPushSession pushSession = new ServerPushSession();
pushSession.start();
Display display = Display.getDefault();
NavigationView navigationView = ((NavigationView) window.getActivePage().findView(NavigationView.ID));
timer.schedule(new TimerTask() {
#Override
public void run() {
display.asyncExec(new Runnable() {
public void run() {
navigationView.doSomething();
}
});
}
}, 2000);
This works! The pushSession.start() forces the UI to refresh without any user interaction. So now the action doSomething() is executed on the navigationView as soon as the 2 seconds are reached.
My only remaining concern is how much load this puts on the server, but its a reasonable solution at least. I validated your answer.
EDIT2:
Just to be complete, to make sure not bump in an invalid Thread access error since we are updating a display from another display, in the doSomething() method we must execute actions using display.asyncExec:
Display display = Display.getCurrent();
public void doSomething() {
display.asyncExec(new Runnable() {
public void run() {
treeViewer.refresh();
}
});
}
With the current architecture of RAP, you can't spread workbench windows over different browser tabs. Every new browser starts a new UISession which implies another Display (see Scopes in RAP).
However, the HttpSession should be the same (unless you have cookies turned off), so you could use this as a means of communicating between different browser tabs.

Click Event handler

when i click on a button the click event handler executes a code . If by mistake(if browser hangs) i click on the button twice the code gets executed twice.i dont want that to happen.
Any suggestions to stop that?
i suppose i should use a schedular or timer but i am not sure
below is the code:
public void onSendButtonClicked() {
disableButtons();
eventBus.fireEvent(new SendEmcsDeclarationEvent(getDeclaration(), getMsgType()));
}
You can - as Abdullah mentioned - disable/enable every widget in GWT with
widget.setEnable(false)
and
widget.setEnable(true).
If you want to lock the whole screen, create a modal popup, show it, after the button is pressed and hide it, after the code has finished.
public void onSendButtonClicked() {
myProgessBar.show();
eventBus.fireEvent(new SendEmcsDeclarationEvent(getDeclaration(), getMsgType()));
myProgressBar.hide();
}
If you are using a async call, you have to hide the progessbar in the callbacks. In this case the finally command might be executed before the callback is executed. In your case it might be a good idea to create a ShowProgressBarEvent and HideProgressbarEvent, so that you can use the progressbar in your whole application.
If your are using a widget library f.e.: GXT, you will find a ProgressBar ready to use.
Hope that helps.
The best way I can think of is to enable/disable the button itself so as to make sure that the code in handler is not called again until before the previous call finishes up.
public void onSendButtonClicked()
{
try
{
disableButtons();
eventBus.fireEvent(new SendEmcsDeclarationEvent(getDeclaration(), getMsgType()));
}
catch (Exception ex)
{
throw ex;
}
finally
{
enableButtons();
}
}
When I create a button, I always also add an animating gif (ajaxloader).
When the button is clicked I make the button invisble, the ajaxloader visible.
When the action is done, I make the ajaxloader invisible, and the button visible.
This way the user has some visual feedback that something is happening (what you don't get when disabling the button), and not the entire application gets blocked (as a modal does) which is one of the plus points using ajax.

How to close a dialog in code?

So I'm making a eclipse plugin and I have a made my own dialog by extending the dialog class.
My dialog basically populates a treeview with data from a server. Sometimes the data cannot be populated (because the server is down) so my treeview is empty.
I have made another dialog appear reporting the error if I am unable to connect to the server.
My problem is that I would like to close the initial dialog when I press ok in the error dialog.
I have not been able to find a good way to do this.
I have tried setting setBlockOnOpen to false.
I have tried calling cancelPressed.
Neither of them have worked.
I called them in the createDialogArea function.
Any Ideas on how I could get this to work?
It is basically user cancelling dialog. you need to invoke cancelPressed() so it will be consistent handling if you have any code that depends on returnCode
if(noDataLoaded){
Display.getDefault().asyncExec(new Runnable() {
public void run() {
cancelPressed():
}
});
}
You need to do the close call after the dialog creation has finished. You can do this by using this code:
parent.getDisplay().asyncExec(new Runnable()
{
#Override
public void run()
{
close();
}
});
in your createDialogArea method. However the dialog may appear briefly. It would be better to do your check before creating the dialog.

customize window closing event message in IE?

i am using GWT. on window close we get browser provided message" Are you sure you want to navigate away from this page?". i want to replace the message with my own message. please help me. below is my code.
Window.addWindowClosingHandler(new Window.ClosingHandler() {
#Override
public void onWindowClosing(final Window.ClosingEvent closingEvent) {
closingEvent.setMessage("some message.");
}
});
you can not modify the dialog that is opened if you provide a string in the closing event.
The dialog is handled by the browser and can not be customized.

what is method to call the eclipse Save/SaveAs option programatically in plugin [duplicate]

I am developing a plug-in.
On clicking a button, I'd like to call the save method of Eclipse or call the save button on Eclipse toolbar.
What is the way to do it?
org.eclipse.ui.PlatformUI.getWorkbench().saveAll(..)
should do the trick.
If you want to save the active editor, please try
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart editor = page.getActiveEditor();
page.saveEditor(editor, true /* confirm */);
Note that the elements in the navigation path may be null.
I use this to save dirty editors for one or more projects:
//Save all changes
Display.getDefault().syncExec(new Runnable() { // save all editors needs to be called by the ui thread!
#Override
public void run() {
IDE.saveAllEditors(new IResource[]{prj}, true);
}
});
where prj is an IProject object.
hope this helps
bye
I used -
IDEWorkbenchPlugin.getDefault().getWorkbench().saveAllEditors(true);