views:

206

answers:

2

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:

  1. the user clicks the X in the editor window or "CTRL+W"
  2. a dialog pops up saying: "If you close this editor, your activity will stop. Do you want to?"
  3. if they click yes, it closes, if no, it stays open.

Oh yeah, and is this even possible?

Thanks, gk

+4  A: 

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:

alt text

VonC
I thought about the "dirty" approach, but don't want users to have the dirty flag shown. Maybe I could have two dirty flags, one the gets used only for making sure it doesn't close and the other one for actual dirtiness.
GreenKiwi
@GreenKiwi: two "dirty" states could work, but will require some specialization on your part in order to use your own ISaveablePart implementation.
VonC
I think that I'm going to end up finding another way to do this w/o trying to keep the editor open. Trouble always seems to creep up when one tries to get a framework to do something it doesn't ordinarily like to do. Thanks.
GreenKiwi
@GreenKiwi: right, in Eclipse-land, jobs run independently of editors. It would seem reasonable to me that an editor could show you details about a running job, provide a way to set up and launch a job, etc. But the editor "is" not the job; the job has its own life and could show in a view such as Eclipse's "Progress" view. (If an editor "is" a job, ask what should happen if someone right-clicks the editor tab and gets a "New Editor".) If the "Progress" view is hard for your users to find, add it to a default perspective, make it show when a job is launched, etc.
Woody Zenfell III
A: 

You can also cancel the saving in

@Override
public void doSave(IProgressMonitor monitor) {

by calling

monitor.setCanceled(true);

In the EditorPart implementation