Synchronizing the form editor with an XML source editor [message #425940] |
Fri, 12 December 2008 11:15  |
Eclipse User |
|
|
|
Hello everybody,
this mail is a follow-up to the following thread:
http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg2 7934.html
Actually, I am trying to synchronize a form editor with an XML source
editor like in the above thread. I succeeded to synchronize one way, from
the source editor to the form using the solution provided by Marie (
http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg2 8148.html ):
On a page change in the multipage editor from source to form I just call
the method:
protected void handleDocumentChange() {
try {
IDocument document = editor.getDocumentProvider().
getDocument(editor.getEditorInput());
XMLResource xmlResource = (XMLResource) editingDomain
.getResourceSet().getResources().get(0);
// unload resource
if (xmlResource.isLoaded()) {
xmlResource.unload();
}
// construct new DOM
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document newDom = builder.parse(new InputSource(new StringReader(
document.get())));
// load resource
xmlResource.load(newDom, Collections.EMPTY_MAP);
} catch (Exception e) {}
}
Actually, this works fine. The other way, synchronizing from the form to
the source is done by calling the method:
protected void handleStructuredModelChange() {
IDocument doc = editor.getDocumentProvider().getDocument(
editor.getEditorInput());
DocumentRootImpl root = (DocumentRootImpl) editingDomain
.getResourceSet().getResources().get(0).getContents().get(0) ;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
root.eResource().save(out, null);
String newContent = out.toString();
String oldContent = doc.get();
int startIndex = 0;
while (startIndex < newContent.length()
&& startIndex < oldContent.length()
&& newContent.charAt(startIndex) == oldContent
.charAt(startIndex)) {
++startIndex;
}
int newEndIndex = newContent.length() - 1;
int oldEndIndex = oldContent.length() - 1;
while (newEndIndex >= startIndex
&& oldEndIndex >= startIndex
&& newContent.charAt(newEndIndex) == oldContent
.charAt(oldEndIndex)) {
--newEndIndex;
--oldEndIndex;
}
String replacement = newContent.substring(startIndex,newEndIndex + 1);
int length = oldEndIndex - startIndex + 1;
doc.replace(startIndex, length, replacement);
} catch (Exception exception) {}
}
Actually, this synchronization is also working, but only one time: If I
change anything in the form editor, and change to the XML source editor, I
can see the changes. But if I return to the form, and change something
again - the changes will not be propagated to the XML editor again. I
could track down the problem a little bit. The issue is, that something
weird happens on the change form the source editor to the form editor
(method handleDocumentChange above) -- somehow the form editor is
disconnected from the model so that future changes are not synchronized
anymore.
Can you help me with this topic? Any suggestions are highly appreciated!
Thank you very much,
Stephan Seufert
|
|
|
Re: Synchronizing the form editor with an XML source editor [message #425946 is a reply to message #425940] |
Fri, 12 December 2008 16:57  |
Eclipse User |
|
|
|
Stephan,
Comments below/
Stephan Seufert wrote:
> Hello everybody,
>
> this mail is a follow-up to the following thread:
> http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg2 7934.html
>
> Actually, I am trying to synchronize a form editor with an XML source
> editor like in the above thread. I succeeded to synchronize one way,
> from the source editor to the form using the solution provided by
> Marie (
> http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg2 8148.html ):
>
> On a page change in the multipage editor from source to form I just
> call the method:
>
> protected void handleDocumentChange() {
> try {
> IDocument document = editor.getDocumentProvider().
> getDocument(editor.getEditorInput());
>
> XMLResource xmlResource = (XMLResource) editingDomain
> .getResourceSet().getResources().get(0);
>
> // unload resource
> if (xmlResource.isLoaded()) {
> xmlResource.unload();
> }
>
> // construct new DOM
> DocumentBuilderFactory factory = DocumentBuilderFactory
> .newInstance();
>
> DocumentBuilder builder = factory.newDocumentBuilder();
> Document newDom = builder.parse(new InputSource(new StringReader(
> document.get())));
>
> // load resource
> xmlResource.load(newDom, Collections.EMPTY_MAP);
> } catch (Exception e) {}
> }
>
> Actually, this works fine. The other way, synchronizing from the form
> to the source is done by calling the method:
>
> protected void handleStructuredModelChange() {
> IDocument doc = editor.getDocumentProvider().getDocument(
> editor.getEditorInput());
> DocumentRootImpl root = (DocumentRootImpl) editingDomain
Casting to DocumentRoot not the Impl would seem better.
> .getResourceSet().getResources().get(0).getContents().get(0) ;
>
> ByteArrayOutputStream out = new ByteArrayOutputStream();
>
> try {
> root.eResource().save(out, null);
> String newContent = out.toString();
You'd not taking encoding into account properly. Note that XML resource
can save to a writer and can even save directly to a DOM.
> String oldContent = doc.get();
>
> int startIndex = 0;
> while (startIndex < newContent.length()
> && startIndex < oldContent.length()
> && newContent.charAt(startIndex) == oldContent
> .charAt(startIndex)) {
> ++startIndex;
> }
>
> int newEndIndex = newContent.length() - 1;
> int oldEndIndex = oldContent.length() - 1;
>
> while (newEndIndex >= startIndex
> && oldEndIndex >= startIndex
> && newContent.charAt(newEndIndex) == oldContent
> .charAt(oldEndIndex)) {
> --newEndIndex;
> --oldEndIndex;
> }
>
> String replacement = newContent.substring(startIndex,newEndIndex +
> 1);
> int length = oldEndIndex - startIndex + 1;
> doc.replace(startIndex, length, replacement);
> } catch (Exception exception) {}
> }
>
> Actually, this synchronization is also working, but only one time: If
> I change anything in the form editor, and change to the XML source
> editor, I can see the changes. But if I return to the form, and change
> something again - the changes will not be propagated to the XML editor
> again. I could track down the problem a little bit. The issue is, that
> something weird happens on the change form the source editor to the
> form editor (method handleDocumentChange above) -- somehow the form
> editor is disconnected from the model so that future changes are not
> synchronized anymore.
> Can you help me with this topic? Any suggestions are highly appreciated!
Maybe someone can help, but it doesn't sound specific to EMF...
>
> Thank you very much,
> Stephan Seufert
>
|
|
|
Powered by
FUDForum. Page generated in 0.03441 seconds