Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to replace XtextDocument in the editor with a projection of it
How to replace XtextDocument in the editor with a projection of it [message #1007535] Wed, 06 February 2013 10:55 Go to next message
Marco Naddeo is currently offline Marco NaddeoFriend
Messages: 62
Registered: November 2012
Member
Hi Smile

Given an editor opened for my DSL language, I can generate a ProjectionDocument which is a projection of the master document (that opened in the editor), and where in particular I remove some portions of text.

Now, I'd like to "substitute" the master document in the editor with its projection, making it editable for the user; in other words, I want to give the user an action which limits his possible editing of the document to the projected text.

I tried implementing a UI action with this code:

public class MyHandler extends AbstractHandler implements IHandler {

  @Override
  public Object execute(ExecutionEvent event) throws ExecutionException {
    XtextEditor xtextEditor = (XtextEditor) HandlerUtil.getActiveEditor(event);
    IXtextDocument masterDocument = xtextEditor.getDocument();
    ProjectionDocumentManager manager = new ProjectionDocumentManager();
    ProjectionDocument projectedDocument = (ProjectionDocument) manager.createSlaveDocument(masterDocument);
		
    try {
      projectedDocument.addMasterDocumentRange(0, masterDocument.getLength());
    } catch (BadLocationException e1) {
      e1.printStackTrace();
    }
		
    //...remove some portions of text from projected document using removeMasterDocumentRange...

    ISourceViewer sourceViewer = xtextEditor.getInternalSourceViewer();
    sourceViewer.setDocument(projectedDocument);

    return null;
  }

}


This code generates a ClassCastException:

org.eclipse.e4.core.di.InjectionException: java.lang.ClassCastException: org.eclipse.jface.text.projection.ProjectionDocument cannot be cast to org.eclipse.xtext.ui.editor.model.XtextDocument


Taking a look to the API, I saw that in fact XtextDocument and ProjectionDocument are independent subclasses of AbstractDocument. So, a XtextDocument is not a ProjectionDocument and vice versa, but surely a XtextEditor supports folding and projection in general: I see that XtextSourceViewer extends ProjectionViewer.

I also tried to use the static method "get" in XtextDocumentUtil to obtain a XtextDocument given a ProjectionDocument, but in this way I still see the complete document in the editor!

Please help me! Have you any code example or suggestions? Rolling Eyes

Bye,
Marco
Re: How to replace XtextDocument in the editor with a projection of it [message #1007976 is a reply to message #1007535] Fri, 08 February 2013 07:22 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 2961
Registered: July 2009
Senior Member
Hi Marco,

you may want to hook into
org.eclipse.xtext.ui.editor.model.XtextDocumentProvider.createDocument(Object)
Your projection document will have to implement IXtextDocument. Your
handler could then use the projection-enabled document to trigger
whatever you try to do there.

Regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 06.02.13 11:55, schrieb Marco Naddeo:
> Hi :)
>
> Given an editor opened for my DSL language, I can generate a
> ProjectionDocument which is a projection of the master document (that
> opened in the editor), and where in particular I remove some portions of
> text.
>
> Now, I'd like to "substitute" the master document in the editor with its
> projection, making it editable for the user; in other words, I want to
> give the user an action which limits his possible editing of the
> document to the projected text.
>
> I tried implementing a UI action with this code:
>
>
> public class MyHandler extends AbstractHandler implements IHandler {
>
> @Override
> public Object execute(ExecutionEvent event) throws ExecutionException {
> XtextEditor xtextEditor = (XtextEditor)
> HandlerUtil.getActiveEditor(event);
> IXtextDocument masterDocument = xtextEditor.getDocument();
> ProjectionDocumentManager manager = new ProjectionDocumentManager();
> ProjectionDocument projectedDocument = (ProjectionDocument)
> manager.createSlaveDocument(masterDocument);
>
> try {
> projectedDocument.addMasterDocumentRange(0,
> masterDocument.getLength());
> } catch (BadLocationException e1) {
> e1.printStackTrace();
> }
>
> //...remove some portions of text from projected document using
> removeMasterDocumentRange...
>
> ISourceViewer sourceViewer = xtextEditor.getInternalSourceViewer();
> sourceViewer.setDocument(projectedDocument);
>
> return null;
> }
>
> }
>
> This code generates a ClassCastException:
>
> org.eclipse.e4.core.di.InjectionException: java.lang.ClassCastException:
> org.eclipse.jface.text.projection.ProjectionDocument cannot be cast to
> org.eclipse.xtext.ui.editor.model.XtextDocument
>
> Taking a look to the API, I saw that in fact XtextDocument and
> ProjectionDocument are independent subclasses of AbstractDocument. So, a
> XtextDocument is not a ProjectionDocument and vice versa, but surely a
> XtextEditor supports folding and projection in general: I see that
> XtextSourceViewer extends ProjectionViewer.
>
> I also tried to use the static method "get" in XtextDocumentUtil to
> obtain a XtextDocument given a ProjectionDocument, but in this way I
> still see the complete document in the editor!
>
> Please help me! Have you any code example or suggestions? :roll:
> Bye,
> Marco
Re: How to replace XtextDocument in the editor with a projection of it [message #1008605 is a reply to message #1007976] Wed, 13 February 2013 09:25 Go to previous messageGo to next message
Marco Naddeo is currently offline Marco NaddeoFriend
Messages: 62
Registered: November 2012
Member
Sebastian Zarnekow wrote on Fri, 08 February 2013 02:22
Hi Marco,

you may want to hook into
org.eclipse.xtext.ui.editor.model.XtextDocumentProvider.createDocument(Object)
Your projection document will have to implement IXtextDocument. Your
handler could then use the projection-enabled document to trigger
whatever you try to do there.

Regards,
Sebastian


Sorry I don't understand how I can hook into that protected method... by subclassing the class XtextDocumentProvider? Sad
Re: How to replace XtextDocument in the editor with a projection of it [message #1008781 is a reply to message #1008605] Wed, 13 February 2013 20:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6552
Registered: July 2009
Senior Member
Hi,

a simple subclass and a straight forward binding should do the trick

	
public Class<? extends XtextDocumentProvider> bindXtextDocumentProvider() {
		return MyXDP.class;
}


~Christian
Re: How to replace XtextDocument in the editor with a projection of it [message #1009074 is a reply to message #1008781] Thu, 14 February 2013 11:33 Go to previous message
Marco Naddeo is currently offline Marco NaddeoFriend
Messages: 62
Registered: November 2012
Member
Thank you, Christian! Smile

The problem now is that the XtextDocumentProvider code contains some casts to XtextDocument (class, not interface), so I'm worry that implementing the interface IXtextDocument for my projection document does not solve the ClassCastException problem, I say right? Rolling Eyes

But I also have another doubt: how a XtextEditor supports code folding without relying on a ProjectionDocument? In other words, is there perhaps another way to hook my projection code without directly instantiate a ProjectionDocument?

Which is the easiest way to obtain what I want (hide some portions of text in a XtextEditor on request and so limit the user interaction to the displayed text)?
Previous Topic:Strange Exception thrown in JUNIT test
Next Topic:Import And Scoping
Goto Forum:
  


Current Time: Mon Dec 22 22:58:34 GMT 2014

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

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