1

I'm making an editor that manipulates the information of a file through tags.

I made some logic to detect editing these tags, but after a lot of research, I still haven't found a way to manually handle the dirty state of my editor.

With this I can get the reference of my editor:

IEditorReference[] ed = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();

for (IEditorReference iEditorReference : ed) {
   if (iEditorReference.getId().equals(*my editor's id*)) {
     ??? what to do here? ???
   }
}
  • 1
    "Handle" it, meaning do what? Toggle it as dirty (which you can do whenever an edit is made)? Clean? – nitind Sep 07 '22 at 23:05
  • set as dirty and at other times set as not dirty... I want to have this control and not leave it to the IDE – Johnliveira Sep 08 '22 at 00:16
  • 1
    I don't understand the question. It's your editor, it's *always* under your control. – nitind Sep 08 '22 at 01:30

1 Answers1

1

The dirty state of an editor is set by the

public boolean isDirty()

method implemented by the editor part (defined in the org.eclipse.ui.ISaveablePart interface)

Whenever the dirty state changes the editor part must call

firePropertyChange(IEditorPart.PROP_DIRTY);

If you have an editor reference as shown in your question you would have to call a method that you write in your editor to change the dirty state:

MyEditor myEditor = (MyEditor)iEditorReference.getEditor();

myEditor.setDirty();

where setDirty is a method you write. That method will have to set a flag that isDirty can use and call firePropertyChange

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • how can i call this method in the action of a button (example of study)? – Johnliveira Sep 08 '22 at 10:41
  • I added an outline of what you might do to the answer – greg-449 Sep 08 '22 at 10:48
  • sorry for resurrecting this issue, i called `firePropertyChange` many times but i still couldn't get my editor dirty – Johnliveira Sep 09 '22 at 14:37
  • Did you override `isDirty` with suitable code? Is Eclipse calling isDirty after you fire the propery change? – greg-449 Sep 09 '22 at 15:51
  • from the debug I could see that the only time it goes through a `setDirty()` is in the `CompatibilityPart` class but it doesn't set it to true – Johnliveira Sep 09 '22 at 17:05
  • I don't follow your last comment at all. Did you override `isDirty` in your editor? Are you returning `true` from that when necessary? Is it being called? Show us your code ([edit] the question and add details) – greg-449 Sep 09 '22 at 17:09
  • I found out what seems to be my problem, my editor extends MultiPageEditorPart, the page that does the editing is another class that extends Composite. I do created my own style, now my question is how do I bind the text fields to dirty the editor when they are modified. – Johnliveira Sep 12 '22 at 14:25
  • You will have to use something a modify listener on the text fields and hook them up to your `isDirty` method. – greg-449 Sep 12 '22 at 16:05