Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » How to restore edit location of recently closed editor?
How to restore edit location of recently closed editor? [message #636972] Wed, 03 November 2010 15:44 Go to next message
Max Mising name is currently offline Max Mising name
Messages: 54
Registered: September 2010
Member
I have an editor input which is of type IURIEditorInput which I use to open different types of packaged editors (java,python,xml,text,etc) or system editor. I need the ability to save and restore the edit position of the files opened in these editors and am having a bit of trouble figuring out how to. I am thinking that IMarker is the way to go by getting a marker before they are closed, store it in my editor input, then after they are opened, use IDE.gotoMarker(part,marker) to restore my state. The problem I'm having is creating the marker before they editors are actually closed. Any ideas how to do this? I want to restore the editor as if it was never actually closed.

Thanks,

Max
Re: How to restore edit location of recently closed editor? [message #636981 is a reply to message #636972] Wed, 03 November 2010 16:21 Go to previous messageGo to next message
Max Mising name is currently offline Max Mising name
Messages: 54
Registered: September 2010
Member
I'm not entirely sure I should be using IMarker in this case as the file's I'm opening to edit are not contained in the "Workspace" and it seems IMarker can only be used for workspace resources. Can anyone verify that is the case? Does anyone know of any other way to get and restore edit position on a generic text editor?

Thanks

Max
Re: How to restore edit location of recently closed editor? [message #637003 is a reply to message #636972] Wed, 03 November 2010 17:50 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Are these editors that you have written? Or are you trying to apply
this more generally to eclipse editors in your application?

If you write the editor, then you can save state information in the
dispose() method. You don't have to use markers, you can save
information in
org.eclipse.ui.plugin.AbstractUIPlugin.getDialogSettings() for your
plugin (if you are a UI plugin) or just save it (in any format you like)
in org.eclipse.core.runtime.Plugin.getStateLocation()

If you are talking about other editors, it is harder because
org.eclipse.ui.IPartListener2.partClosed(IWorkbenchPartRefer ence) is a
post-close event. Some of these (like the text based editors) will try
and save their cursor location if the editors are open on shutdown (so
they are re-opened when the app is restarted), but they won't do it on
simple open/close.

PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: How to restore edit location of recently closed editor? [message #637007 is a reply to message #637003] Wed, 03 November 2010 18:11 Go to previous messageGo to next message
Max Mising name is currently offline Max Mising name
Messages: 54
Registered: September 2010
Member
Thanks for the reply!

These are eclipse editors I'm trying to retain the state of. The problem isn't when to do it, I have that all set up, since this is for a special case that I control. The problem is how to get at the data I need to save (it will all be stored in memory, no need to preserve after shutdown).

Here is my code for when I'm going to save the state:

        for (IEditorReference ref : page.getEditorReferences()) {
            IEditorPart part = ref.getEditor(false);
            IEditorInput input = part.getEditorInput();
            if (input instanceof LocalizationEditorInput) { // my custom editor input
                LocalizationEditorInput lInput = (LocalizationEditorInput) input;
                // TODO: Figure out how to store edit position
            }
        }


This is what I have access to and this is where I need to get something from the editor that I can use to restore the position

Thanks

Max
Re: How to restore edit location of recently closed editor? [message #637016 is a reply to message #637007] Wed, 03 November 2010 18:48 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Max wrote:
> This is what I have access to and this is where I need to get something
> from the editor that I can use to restore the position

For Parts that are org.eclipse.ui.texteditor.ITextEditor you can get
that information, at least some of it. I noticed that interface has
methods that can highlight and move the cursor.

PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: How to restore edit location of recently closed editor? [message #637021 is a reply to message #637016] Wed, 03 November 2010 19:29 Go to previous messageGo to next message
Max Mising name is currently offline Max Mising name
Messages: 54
Registered: September 2010
Member
Hmm, I gave it a shot and it is always returning null...

Looking into the code, it goes into AbstractTextEditor.getHighlightRange() which ends up calling SourceViewer.getRangeIndication() which attempts to get the Position from the AnnotationModel passing in fRangeIndicator and that always returns null. Is there anything I have to do to get this to work that you know of? I'm not familiar with AnnotationModels or Attachments

Thanks

Max
Re: How to restore edit location of recently closed editor? [message #637023 is a reply to message #637016] Wed, 03 November 2010 19:29 Go to previous messageGo to next message
Devi Vara Prasad Bandaru is currently offline Devi Vara Prasad Bandaru
Messages: 98
Registered: March 2010
Location: Hyderabad
Member

You can restore the cursor position for text editor instances with the help of a part listener. Here is the code which might help you



	private Map<Object, Point> restoreLocations = new HashMap<Object, Point>();
	@Override
	public void partClosed(IWorkbenchPart part) {
        if (part instanceof ITextEditor && part instanceof  IEditorPart) {
            IEditorInput editorInput = ((IEditorPart)part).getEditorInput();
            if (editorInput instanceof IURIEditorInput) {
            	ITextSelection selection = (ITextSelection) ((ITextEditor)part).getSelectionProvider().getSelection();
            	restoreLocations.put(((IURIEditorInput) editorInput).getURI(), new Point(selection.getOffset(), selection.getLength()));
            }
        }
	}

	@Override
	public void partOpened(IWorkbenchPart part) {
        if (part instanceof ITextEditor && part instanceof  IEditorPart) {
            IEditorInput editorInput = ((IEditorPart)part).getEditorInput();
            if (editorInput instanceof IURIEditorInput) {
            	ITextEditor textEditor = (ITextEditor)part;
            	Point point = restoreLocations.get(((IURIEditorInput) editorInput).getURI());
            	if (point != null) {
            		textEditor.selectAndReveal(point.x, point.y);
            	}
            }
        }
	}





Re: How to restore edit location of recently closed editor? [message #637025 is a reply to message #637023] Wed, 03 November 2010 19:44 Go to previous message
Max Mising name is currently offline Max Mising name
Messages: 54
Registered: September 2010
Member
Worked like a charm, thanks for all the replies everyone!

Max
Previous Topic:Eclipse editor latin2 characters
Next Topic:Building RCP application
Goto Forum:
  


Current Time: Tue Sep 16 01:00:00 GMT 2014

Powered by FUDForum. Page generated in 0.01893 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software