Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Modify text in XtextEditor
Modify text in XtextEditor [message #917378] Thu, 20 September 2012 03:24 Go to next message
ken liu is currently offline ken liuFriend
Messages: 3
Registered: September 2012
Junior Member
In my use case, I set two editor to share one resource, one is XtextEditor, the other is a editor with a table viewer.
Now, my problem is after I modify resource in table viewer, how can I update the text which displayed on XtextEditor? Is there any method which can generate the text according to the xtext grammar and resource automatically? 0r need I write this kind of method by myself.

the only way I know to modify text in an editor is to use editor.getDocumentProvider().getDocument(editor.getEditorInput()).set();
but I don't know how to get the text according to xtext grammer from resource

I'll appreciate if someone can provide helps Smile

[Updated on: Thu, 20 September 2012 03:25]

Report message to a moderator

Re: Modify text in XtextEditor [message #917465 is a reply to message #917378] Thu, 20 September 2012 05:37 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

why dont you simply call xtextdocument.modify and manipulate the ast directly?

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Modify text in XtextEditor [message #917482 is a reply to message #917465] Thu, 20 September 2012 05:59 Go to previous messageGo to next message
ken liu is currently offline ken liuFriend
Messages: 3
Registered: September 2012
Junior Member
Hi!
Thanks for your help

In fact, I saw this method before and try to use it but end of no effect. Maybe I didn't use it properly, could you post some simple example code please, cause I'm a new beginner to Xtext and even Java. Thank you~

[Updated on: Thu, 20 September 2012 06:00]

Report message to a moderator

Re: Modify text in XtextEditor [message #917490 is a reply to message #917482] Thu, 20 September 2012 06:11 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

since i dont know your usecase it is hard to say.

xtextDocument.modify(new IUnitOfWork<Void, XtextResource>() {

			@Override
			public java.lang.Void exec(XtextResource res) throws Exception {
				//do something with res
// e.g.
//Model m = (Model) resource.getContents().get(0);
//or
// res.getContents().set(0, MyDslFactory.eINSTANCE.createModel());
				return null;
			}
		}
			);


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Modify text in XtextEditor [message #917512 is a reply to message #917490] Thu, 20 September 2012 06:39 Go to previous messageGo to next message
ken liu is currently offline ken liuFriend
Messages: 3
Registered: September 2012
Junior Member
Hi
I think you misunderstood my problem.
I don't want to do any operation on the model & resource. My problem is how to display these stuff on the xtextEditor.

In my use case, I put two editor into a multipage editor as two separate pages, but they share the same resource. And I override the pageChange(int newPageIndex), this will called when I click on the tab bar of multipage editor.
Here is some of these code
@Override
protected void pageChange(int newPageIndex) {
	IEditorInput input = getEditorInput();
	if (input instanceof FileEditorInput) {
		FileEditorInput fInput = (FileEditorInput)input;
		Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
		XtextResourceSet resourceSet = (injector.getInstance(XtextResourceSet.class));
		resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
		Resource resource = resourceSet.getResource(URI.createURI(fInput.getURI().toString()), true);
		
		//this method will get the content from resource and display on editor
		resourceTableEditor.refreshContentsWithResource(resource);
		
		//don't know how to deal with it, what i wanna do is to display the content of resource on the editor
		XtextDocument document = (XtextDocument) editor.getDocument();
		document.modify(new IUnitOfWork<Void, XtextResource>() {

			@Override
			public java.lang.Void exec(XtextResource state)
					throws Exception {
				// TODO Auto-generated method stub
				return null;
			}
			
		});
		
		//the only way i found to change text on the editor(XtextEditor)
		editor.getDocumentProvider().getDocument(editor.getEditorInput()).set("add stuff!!");
	}
}


editor.getDocumentProvider().getDocument(editor.getEditorInput()).set("add stuff!!") Although this method can modify the text, I don't know how to generate the string text from resource which need correspond to the dsl grammar. Do I need to write the transform method by myself? Or there exist another method such as the modify() you said.


[Updated on: Thu, 20 September 2012 06:59]

Report message to a moderator

Re: Modify text in XtextEditor [message #917693 is a reply to message #917512] Thu, 20 September 2012 10:35 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi sorry I have no idea on your usecase

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Modify text in XtextEditor [message #918590 is a reply to message #917693] Fri, 21 September 2012 06:44 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Btw calling MyDslStandaloneSetup is a NONONONONOGGGGOOOOO (from within eclipse)

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Fri, 21 September 2012 06:44]

Report message to a moderator

Previous Topic:Formatting ML_Comment inside a indentation block ?
Next Topic:Large Files and Excluding Directories
Goto Forum:
  


Current Time: Fri Apr 19 03:26:31 GMT 2024

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

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

Back to the top