Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Problem with IEditorInput(IEditorInput)
Problem with IEditorInput [message #525936] Thu, 08 April 2010 10:57 Go to next message
Darie Moldovan is currently offline Darie MoldovanFriend
Messages: 18
Registered: April 2010
Junior Member
Hello everyone,

I read several posts on dev.eclipse.org about how to use IEditorInput, but there still are some things which I cannot get to work in my project.

I'm in a similar situation as the person who wrote this topic.

I'm trying to create an XML Editor plugin for Eclipse which has 2 pages (it's a multi page editor) - page 1, where you can edit xml source code and page 2, where you can see the xml as a TreeViewer (and edit it, of course, by right click, add element, delete element ...).

What I want to do is to make any change from one of those pages visible in the other, without saving the actual file on the hard drive. So when I edit something in the source code and change the page, the tree should update (and vice-versa). I already implemented this part and it works fine. Now I tried to implement the other "direction", when editing the tree, the changes should be visible in the source code.

I should mention that I am working with DOM and when I make a change, I also edit the dom and pass it to the other view. So the org.w3c.dom.Document object always holds the actual content of the text editor / tree editor.

This part works only half, because I want to set the input of the text editor, but I need an object of the type IEditorInput, in order to say

textEditor.setInput(newInput);


Ok, so I said to myself, I'll write my own class, say "MyEditorInput", which implements IEditorInput and has a field of type org.w3c.dom.Document.

Ok, now the question: how should I use this class? How do I set the input of the text editor (which is a subclass of org.eclipse.ui.editors.text.TextEditor) if I have an object of type org.w3c.dom.Document?

Thank you for your time.
Re: Problem with IEditorInput [message #525967 is a reply to message #525936] Thu, 08 April 2010 12:15 Go to previous messageGo to next message
Jonas Helming is currently offline Jonas HelmingFriend
Messages: 699
Registered: July 2009
Senior Member
Hi,
first a question: did you have a look at XText? they might provide what
you are looking for, without having to write pure XML. Also the are
dealing with updates between text artifacts and models. At EclipseCon
the even presented a syncornization between GMF and XText. I ask this
because there are some problems in the synchronization between models
and text, especially if the model is not valid in the textual presentation.

Second to your question: I maybe do not really understand your problem.
I also use a subclas of EditorInput for my editor, which includes a new
field. I would simply implement IEditorInput and offer a parameter in
the constructor to pass the dom.document and save it to a field. The
implementation of the other interface methods is straight forward.
Inside your custom editor you handle the custom editorinput and get the
dom.document out of it. Sounds easy but maybe i missed your real issue :-)
Cheers
Jonas

darie17 wrote:
> Hello everyone,
>
> I read several posts on dev.eclipse.org about how to use IEditorInput,
> but there still are some things which I cannot get to work in my project.
>
> I'm in a similar situation as the person who wrote
> http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg2 7996.html
> topic.
>
> I'm trying to create an XML Editor plugin for Eclipse which has 2 pages
> (it's a multi page editor) - page 1, where you can edit xml source code
> and page 2, where you can see the xml as a TreeViewer (and edit it, of
> course, by right click, add element, delete element ...).
> What I want to do is to make any change from one of those pages visible
> in the other, without saving the actual file on the hard drive. So when
> I edit something in the source code and change the page, the tree should
> update (and vice-versa). I already implemented this part and it works
> fine. Now I tried to implement the other "direction", when editing the
> tree, the changes should be visible in the source code.
> I should mention that I am working with DOM and when I make a change, I
> also edit the dom and pass it to the other view. So the
> org.w3c.dom.Document object always holds the actual content of the text
> editor / tree editor.
>
> This part works only half, because I want to set the input of the text
> editor, but I need an object of the type IEditorInput, in order to say
> textEditor.setInput(newInput);
>
> Ok, so I said to myself, I'll write my own class, say "MyEditorInput",
> which implements IEditorInput and has a field of type org.w3c.dom.Document.
> Ok, now the question: how should I use this class? How do I set the
> input of the text editor (which is a subclass of
> org.eclipse.ui.editors.text.TextEditor) if I have an object of type
> org.w3c.dom.Document?
>
> Thank you for your time.
Re: Problem with IEditorInput [message #525980 is a reply to message #525967] Thu, 08 April 2010 12:53 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
XSDEditor, the XSD models' sample editor, does something crudely
similar. In general you can't expect an existing editor to deal with
your specialized IEditorInput. You'd need to convert the model to text
and update the text being displayed by your editor instance; you'd not
change the editor input itself. The Web Tools project has a structured
source editing framework that might better suit your needs.


Jonas wrote:
> Hi,
> first a question: did you have a look at XText? they might provide
> what you are looking for, without having to write pure XML. Also the
> are dealing with updates between text artifacts and models. At
> EclipseCon the even presented a syncornization between GMF and XText.
> I ask this because there are some problems in the synchronization
> between models and text, especially if the model is not valid in the
> textual presentation.
>
> Second to your question: I maybe do not really understand your
> problem. I also use a subclas of EditorInput for my editor, which
> includes a new field. I would simply implement IEditorInput and offer
> a parameter in the constructor to pass the dom.document and save it to
> a field. The implementation of the other interface methods is straight
> forward. Inside your custom editor you handle the custom editorinput
> and get the dom.document out of it. Sounds easy but maybe i missed
> your real issue :-)
> Cheers
> Jonas
>
> darie17 wrote:
>> Hello everyone,
>>
>> I read several posts on dev.eclipse.org about how to use
>> IEditorInput, but there still are some things which I cannot get to
>> work in my project.
>>
>> I'm in a similar situation as the person who wrote
>> http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg2 7996.html
>> topic.
>>
>> I'm trying to create an XML Editor plugin for Eclipse which has 2
>> pages (it's a multi page editor) - page 1, where you can edit xml
>> source code and page 2, where you can see the xml as a TreeViewer
>> (and edit it, of course, by right click, add element, delete element
>> ...).
>> What I want to do is to make any change from one of those pages
>> visible in the other, without saving the actual file on the hard
>> drive. So when I edit something in the source code and change the
>> page, the tree should update (and vice-versa). I already implemented
>> this part and it works fine. Now I tried to implement the other
>> "direction", when editing the tree, the changes should be visible in
>> the source code.
>> I should mention that I am working with DOM and when I make a change,
>> I also edit the dom and pass it to the other view. So the
>> org.w3c.dom.Document object always holds the actual content of the
>> text editor / tree editor.
>>
>> This part works only half, because I want to set the input of the
>> text editor, but I need an object of the type IEditorInput, in order
>> to say
>> textEditor.setInput(newInput);
>>
>> Ok, so I said to myself, I'll write my own class, say
>> "MyEditorInput", which implements IEditorInput and has a field of
>> type org.w3c.dom.Document.
>> Ok, now the question: how should I use this class? How do I set the
>> input of the text editor (which is a subclass of
>> org.eclipse.ui.editors.text.TextEditor) if I have an object of type
>> org.w3c.dom.Document?
>>
>> Thank you for your time.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Problem with IEditorInput [message #525982 is a reply to message #525967] Thu, 08 April 2010 13:15 Go to previous messageGo to next message
Darie Moldovan is currently offline Darie MoldovanFriend
Messages: 18
Registered: April 2010
Junior Member
Hi Jonas,

first of all thank you for your answer.

Yes, I first tried to work with xtext, but due to some problems (long history...), I decided to do some things by myself.

Ok, suppose I wrote my class as follows:

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPersistableElement;
import org.w3c.dom.Document;

public class XMLEditorInput implements IEditorInput {
	
	private Document domDocument;
	
	public XMLEditorInput(Document domDocument)
	{
		this.domDocument = domDocument;
	}

	@Override
	public boolean exists() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public ImageDescriptor getImageDescriptor() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IPersistableElement getPersistable() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getToolTipText() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object getAdapter(Class adapter) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (super.equals(obj)) {
			return true;
		}
		if (obj instanceof XMLEditorInput) {
			return domDocument.equals(((XMLEditorInput) obj).getDomDocument());
		}
		return false;
	}

	@Override
	public int hashCode() {
		return domDocument.hashCode();
	}


	/**
	 * @return the domDocument
	 */
	public Document getDomDocument() {
		return domDocument;
	}

	/**
	 * @param domDocument the domDocument to set
	 */
	public void setDomDocument(Document domDocument) {
		this.domDocument = domDocument;
	}

}


Then, in the MultiPageEditor class I tried to change the input of the text editor as follows:

XMLEditorInput myInput = new XMLEditorInput(treeViewer.getDOM());
// treeViewer.getDOM() returns the org.w3c.dom.Document Object, which holds the actual content
textEditor.setInput(myInput);


I got no exception, but the tab where the source code should appear looks something like this.

What should I understand from this error? I checked that the dom file, which I give as a parameter is not null and has the right contents. So not the dom file is the problem.

And, what I did not understand is how the text editor should know that it should read the content of the dom file in the XMLEditorInput class. Or did I forget to explicitly call a method?

Thanks again.

[Updated on: Thu, 08 April 2010 13:17]

Report message to a moderator

Re: Problem with IEditorInput [message #526026 is a reply to message #525982] Thu, 08 April 2010 14:42 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Use the debugger to step through the call. No doubt the editor checks
for subclasses such as IStorageEditorInput and IFileEditorInput.
Failing to find anything it recognizes what do do with, it decides: "I
have no clue how to pull text out of what's been handed to me."


darie17 wrote:
> Hi Jonas,
>
> first of all thank you for your answer.
>
> Yes, I first tried to work with xtext, but due to some problems (long
> history...), I decided to do some things by myself.
>
> Ok, suppose I wrote my class as follows:
>
> import org.eclipse.jface.resource.ImageDescriptor;
> import org.eclipse.ui.IEditorInput;
> import org.eclipse.ui.IPersistableElement;
> import org.w3c.dom.Document;
>
> public class XMLEditorInput implements IEditorInput {
>
> private Document domDocument;
>
> public XMLEditorInput(Document domDocument)
> {
> this.domDocument = domDocument;
> }
>
> @Override
> public boolean exists() {
> // TODO Auto-generated method stub
> return false;
> }
>
> @Override
> public ImageDescriptor getImageDescriptor() {
> // TODO Auto-generated method stub
> return null;
> }
>
> @Override
> public String getName() {
> // TODO Auto-generated method stub
> return null;
> }
>
> @Override
> public IPersistableElement getPersistable() {
> // TODO Auto-generated method stub
> return null;
> }
>
> @Override
> public String getToolTipText() {
> // TODO Auto-generated method stub
> return null;
> }
>
> @Override
> public Object getAdapter(Class adapter) {
> // TODO Auto-generated method stub
> return null;
> }
>
> @Override
> public boolean equals(Object obj) {
> if (super.equals(obj)) {
> return true;
> }
> if (obj instanceof XMLEditorInput) {
> return domDocument.equals(((XMLEditorInput)
> obj).getDomDocument());
> }
> return false;
> }
>
> @Override
> public int hashCode() {
> return domDocument.hashCode();
> }
>
>
> /**
> * @return the domDocument
> */
> public Document getDomDocument() {
> return domDocument;
> }
>
> /**
> * @param domDocument the domDocument to set
> */
> public void setDomDocument(Document domDocument) {
> this.domDocument = domDocument;
> }
>
> }
>
>
> Then, in the MultiPageEditor class I tried to change the input of the
> text editor as follows:
>
> XMLEditorInput myInput = new XMLEditorInput(treeViewer.getDOM());
> textEditor.setInput(myInput);
>
> I got no exception, but the tab where the source code should appear
> looks something like
> http://img176.imageshack.us/img176/6967/17211477.jpg
> What should I understand from this error? I checked that the dom file,
> which I give as a parameter is not null and has the right contents. So
> not the dom file is the problem.
>
> And, what I did not understand is how the text editor should know that
> it should read the content of the dom file in the XMLEditorInput
> class. Or did I forget to explicitly call a method?
>
> Thanks again.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Problem with IEditorInput [message #526039 is a reply to message #526026] Thu, 08 April 2010 15:07 Go to previous messageGo to next message
Darie Moldovan is currently offline Darie MoldovanFriend
Messages: 18
Registered: April 2010
Junior Member
Hello Ed,

I already tried to debug the program, I see no error / exception. I also tried to surround the code with try-catch blocks to catch any possible exceptions. Unfortunately that didn't work either.

Thanks anyway for your help!

If you or any other member here has other ideas, I would be very grateful!

Thanks in advance.
Re: Problem with IEditorInput [message #526053 is a reply to message #526039] Thu, 08 April 2010 15:14 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Have you stepped through the call with the debugger to see how the text
editor implementation deals with the setInput call? The debugger can
answer many questions faster than newsgroups. It seems clear to me that
it's impossible for the text editor to know what to do with your
specialized input.


darie17 wrote:
> Hello Ed,
>
> I already tried to debug the program, I see no error / exception. I
> also tried to surround the code with try-catch blocks to catch any
> possible exceptions. Unfortunately that didn't work either.
>
> Thanks anyway for your help!
>
> If you or any other member here has other ideas, I would be very
> grateful!
>
> Thanks in advance.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Problem with IEditorInput [message #526065 is a reply to message #526053] Thu, 08 April 2010 15:38 Go to previous messageGo to next message
Darie Moldovan is currently offline Darie MoldovanFriend
Messages: 18
Registered: April 2010
Junior Member
Ed Merks wrote on Thu, 08 April 2010 11:14
It seems clear to me that it's impossible for the text editor to know what to do with your specialized input.


You are right, the question is why? What do I do wrong? I tried debugging for the last several hours and didn't learn anything new. I simply cannot find the source of the error, which is, in most cases, worst than if you know where the mistake is, but don't know how to fix it...
Re: Problem with IEditorInput [message #526077 is a reply to message #526065] Thu, 08 April 2010 15:56 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------060406050806090908010104
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Comments below.

darie17 wrote:
> Ed Merks wrote on Thu, 08 April 2010 11:14
>> It seems clear to me that it's impossible for the text editor to know
>> what to do with your specialized input.
>
>
> You are right, the question is why?
I don't understand why you would expect this to work?
> What do I do wrong?
I've already told you that changing the editor's input isn't the right
thing to do. The editor's input not only determines what the editor
will display, it determines where/how the results are stored.
> I tried debugging for the last several hours and didn't learn anything
> new.
Really? You've used the debugger to get to code that looked like this?

public IDocumentProvider getDocumentProvider(IEditorInput
editorInput) {

IDocumentProvider provider= null;

IFile file= (IFile) editorInput.getAdapter(IFile.class);
if (file != null)
provider= getDocumentProvider(file.getFileExtension());

if (provider == null) {
Set set= (Set) findInputTypeMapping(editorInput.getClass());
if (set != null) {
IConfigurationElement entry=
selectConfigurationElement(set);
provider= getDocumentProvider(entry);
}
}

return provider;
}

> I simply cannot find the source of the error, which is, in most cases,
> worst than if you know where the mistake is, but don't know how to fix
> it...
I think you need to update the text shown by the editor, not change its
input.

--------------060406050806090908010104
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Comments below.<br>
<br>
darie17 wrote:
<blockquote cite="mid:hpktad$me7$1@build.eclipse.org" type="cite">Ed
Merks wrote on Thu, 08 April 2010 11:14
<br>
<blockquote type="cite">It seems clear to me that it's impossible for
the text editor to know what to do with your specialized input.
<br>
</blockquote>
<br>
<br>
You are right, the question is why?</blockquote>
I don't understand why you would expect this to work?<br>
<blockquote cite="mid:hpktad$me7$1@build.eclipse.org" type="cite"> What
do I do wrong?</blockquote>
I've already told you that changing the editor's input isn't the right
thing to do.  The editor's input not only determines what the editor
will display, it determines where/how the results are stored.<br>
<blockquote cite="mid:hpktad$me7$1@build.eclipse.org" type="cite"> I
tried debugging for the last several hours and didn't learn anything
new. </blockquote>
Really?  You've used the debugger to get to code that looked like this?<small><br>
</small>
<blockquote><small>    public IDocumentProvider
getDocumentProvider(IEditorInput editorInput) {</small><br>
<br>
<small>        IDocumentProvider provider= null;</small><br>
<br>
<small>        IFile file= (IFile)
editorInput.getAdapter(IFile.class);</small><br>
<small>        if (file != null)</small><br>
<small>            provider=
getDocumentProvider(file.getFileExtension());</small><br>
<br>
<small>        if (provider == null) {</small><br>
<small>            Set set= (Set)
findInputTypeMapping(editorInput.getClass());</small><br>
<small>            if (set != null) {</small><br>
<small>                IConfigurationElement entry=
selectConfigurationElement(set);</small><br>
<small>                provider= getDocumentProvider(entry);</small><br>
<small>            }</small><br>
<small>        }</small><br>
<br>
<small>        return provider;</small><br>
<small>    }</small><br>
</blockquote>
<blockquote cite="mid:hpktad$me7$1@build.eclipse.org" type="cite">I
simply cannot find the source of the error, which is, in most cases,
worst than if you know where the mistake is, but don't know how to fix
it...
<br>
</blockquote>
I think you need to update the text shown by the editor, not change its
input.<br>
</body>
</html>

--------------060406050806090908010104--


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Problem with IEditorInput [message #526182 is a reply to message #525936] Fri, 09 April 2010 07:23 Go to previous message
Hallvard Traetteberg is currently offline Hallvard TraettebergFriend
Messages: 673
Registered: July 2009
Location: Trondheim, Norway
Senior Member
I had a similar problem when trying to reuse the WTP's Javascript editor
for scripts stored in EMF models. It was fairly difficult to understand
which IEditorInput subclass to implement, to satisfy the editor, but I
somehow managed to make it work (not a general solution, but OK for a
proof of concept). I've included the code below. I'm afraid I cannot
answer too many questions about this at the moment, since it was written
some time ago.

Ed: You are right (as always), using the debugger is absolutely
necessary, but often difficult when the code becomes complex.

Hallvard

StringEditorInput.java

/**
*
*/
package org.eclipse.e4.emf.javascript.ui;

import java.io.InputStream;
import java.io.StringBufferInputStream;

import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.IStorageEditorInput;

public class StringEditorInput implements IStorageEditorInput {

private String id;

protected final String scriptSource;
protected final IPath storagePath;

public StringEditorInput(String id, IPath storagePath, String source) {
this.id = id;
this.storagePath = storagePath;
this.scriptSource = source;
}

public boolean equals(Object other) {
return other instanceof StringEditorInput &&
id.equals(((StringEditorInput)other).id);
}

public Object getAdapter(Class adapter) {
return null;
}

public String getToolTipText() {
return id;
}

public IPersistableElement getPersistable() {
return null; // new Element();
}

public String getName() {
int pos = id.lastIndexOf('/');
return (pos > 0 ? id.substring(pos + 1) : id);
}

public ImageDescriptor getImageDescriptor() {
return null;
}

public boolean exists() {
return true;
}

public IStorage getStorage() {
return new IStorage() {
public Object getAdapter(Class adapter) {
return null;
}
public boolean isReadOnly() {
return false;
}
public String getName() {
return StringEditorInput.this.getName();
}
public IPath getFullPath() {
return storagePath;
}

public InputStream getContents() throws CoreException {
return new StringBufferInputStream(scriptSource);
}
};
}
}

//

Here's how the class was used:

final IEditorInput editorInput = new StringEditorInput(resUri + "#" +
fragment, storagePath, scriptSource);

final IEditorPart editor =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().openEditor(editorInput,
editorId);
editor.addPropertyListener(new IPropertyListener() {
public void propertyChanged(Object source, int propId) {

// check for changes of PROP_DIRTY from true to false, meaning the
editor saved the String
if (IEditorPart.PROP_DIRTY == propId) {
if (editor instanceof AbstractDecoratedTextEditor && (!
editor.isDirty())) {
IDocumentProvider documentProvider =
((AbstractDecoratedTextEditor)editor).getDocumentProvider();
String newSource =
documentProvider.getDocument(editorInput).get();
try {
ScriptSourceView.commitScriptText(eObject, newSource,
editingDomainProvider);
} catch (Exception e) {
log.log(Level.WARNING, "Exception setting script text to " +
newSource + ": " + e, e);
}
}
}
}
});

StringEditorInputDocumentProvider.java

/*********************************************************** ********************
* Copyright (c) 2009 Hallvard Traetteberg.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Hallvard Traetteberg - initial API and implementation

************************************************************ ******************/
package org.eclipse.e4.emf.javascript.ui;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.AnnotationModel;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.ui.editors.text.StorageDocumentProvider;

public class StringEditorInputDocumentProvider extends
StorageDocumentProvider {

public StringEditorInputDocumentProvider() {
super();
}

protected IAnnotationModel createAnnotationModel(Object element) throws
CoreException {
return new AnnotationModel();
}

protected void doSaveDocument(IProgressMonitor monitor, Object element,
IDocument document, boolean overwrite) throws CoreException {
// ((StringEditorInput)element).doSave(document.get());
}
}
Previous Topic:[CDO Databinding] Attempting to data bind to derived attribute
Next Topic:EMF Compare merger bug
Goto Forum:
  


Current Time: Thu Mar 28 10:05:50 GMT 2024

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

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

Back to the top