Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Access Model tree in a IEditorActionDelegate
Access Model tree in a IEditorActionDelegate [message #558303] Fri, 10 September 2010 22:25 Go to next message
No real name is currently offline No real nameFriend
Messages: 101
Registered: August 2010
Senior Member
Hi,

I have an action in my menubar that should convert teh parsed model of a .hlm (my DSL) file to some other format. I define my convert class as follows:


package org.xtext.example.hlm.ui;

import org.eclipse.jface.action.IAction;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.actions.ActionDelegate;

/*
 * Perform static checking on the HLM model.
 */
public class MyHLMStaticChecker extends ActionDelegate implements IEditorActionDelegate {
	
	public void run(IAction action) {
		System.out.println("Executing command static checker!!!!");
	}
	
	public void setActiveEditor(IAction action, IEditorPart targetEditor) {
		
	}
}


However, I need the root of the parse tree (i.e., HLMModel object) for process while the run() function only sends in an IAction.

How do I get access to the root of the parse tree for this model which is sitting in foo.hlm? I tried,

org.xtext.example.hlm.myHLM.MyHLMPackage.eINSTANCE but that returns an EClass() object while I need a HMLModel() object.

Thank you for any ideas.
Re: Access Model tree in a IEditorActionDelegate [message #558327 is a reply to message #558303] Sat, 11 September 2010 08:40 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

is the setActiveEditor method called before the run? In this case you
IEditorPart should be an XtextEditor which you can ask for the
XtextDocument, using a readonly-/modifyUnitOfWork in order to perform
the action.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Access Model tree in a IEditorActionDelegate [message #558380 is a reply to message #558327] Sun, 12 September 2010 05:13 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 101
Registered: August 2010
Senior Member
Hi
Thanks for the response. I've been able to get the xtext editor/document. However, how do I use the IUnitofWork tin the readOnly()? So far I have the following code but I'm not sure how to call the readOnly() function.

In my DSL, the root-level object is of type HLMModel.



package org.xtext.example.hlm.ui;

import org.eclipse.jface.action.IAction;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.actions.ActionDelegate;
import org.eclipse.xtext.ui.editor.XtextEditor;
import org.eclipse.xtext.ui.editor.model.IXtextDocument;
import org.eclipse.xtext.ui.editor.model.XtextDocumentUtil;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;
import org.xtext.example.hlm.myHLM.HLMModel;

import java.lang.RuntimeException;;

/*
 * Perform static checking on the HLM model.
 */
public class MyHLMStaticChecker extends ActionDelegate implements IEditorActionDelegate {
	
	//private XtextEditor myxtext;
	
	public void run(IAction action) {
		System.out.println("Executing command static checker!!!!");

		// Get Xtext editor.
		XtextEditor myxtext = org.eclipse.xtext.ui.editor.utils.EditorUtils.getActiveXtextEditor();
		if (myxtext == null)
			throw new RuntimeException("XtextEditor is null. Serious problem.");

		// Get the document.
		IXtextDocument hlmdoc = XtextDocumentUtil.get(myxtext);
//		HLMModel model = hlmdoc.readOnly(new IUnitOfWork<HLMModel, XtextResource>());
		
//System.out.println("Startup state is " + p.getStartup().getName().getName());
//System.out.println("No of states " + p.getStates().size());
		}
	
	public void setActiveEditor(IAction action, IEditorPart targetEditor) {
		
	//	if (targetEditor != null)
		//	myxtext = (XtextEditor) targetEditor.getAdapter(XtextEditor.class);
	}
}


The DSL looks like this:



HLMModel : {HLMModel}
	(startup=Startup)
	(states+=State)+;
	
State :
	'state' name=ID '{'
		(actions+=Action)?
		(conditions+=Condition)*
		(defaults=Default)
	'};';
	
Action :
	'actions' '{' (acts+=Act)+ '},';

Act:
	name=ID;

Condition :
	'cond' '(' (conds=Lconds) ')' '{' name=[State] '},';

Lconds:
	name=ID;
	
Startup :
	'startup' '{' name=[State] '};';
	
Default :
	'default' '{' name=[State] '}';
Re: Access Model tree in a IEditorActionDelegate [message #558382 is a reply to message #558303] Sun, 12 September 2010 06:20 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 101
Registered: August 2010
Senior Member
Essentially what I want is this:

ResourceSet rs = new ResourceSetImpl();
Resource resource = rs.getResource(URI.createURI("./mymodel.dmodel"), true);
EObject eobject = resource.getContents().get(0);
HLMModel hlm = (HLMModel) eobject;

But I'm wondering how to get this from the XtextEditor without having to do the rs.getResource(URI.createURI(..)) inside the run() method.

[Updated on: Sun, 12 September 2010 06:25]

Report message to a moderator

Re: Access Model tree in a IEditorActionDelegate [message #558384 is a reply to message #558303] Sun, 12 September 2010 06:38 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 101
Registered: August 2010
Senior Member
Finally got it to work!



	public void run(IAction action) {
		System.out.println("Executing command static checker!!!!");

		// Get Xtext editor.
		XtextEditor myxtext = org.eclipse.xtext.ui.editor.utils.EditorUtils.getActiveXtextEditor();
		if (myxtext == null)
			throw new RuntimeException("XtextEditor is null. Serious problem.");

		// Get the document.
		IXtextDocument hlmdoc = XtextDocumentUtil.get(myxtext);
		
		// Safely make a copy of the HLM model to process further.
		// See http://koehnlein.blogspot.com/2010/06/semantic-model-access-in-xtext.html
		HLMModel hlmmodel = hlmdoc.readOnly(new IUnitOfWork<HLMModel, XtextResource>() {
			public HLMModel exec(XtextResource resource) throws Exception {
				return EcoreUtil.copy((HLMModel) resource.getContents().get(0));
			}
		});
		if (hlmmodel == null)
			System.out.println("HLM model is null!");
		else {
			System.out.println("Startup state is " + hlmmodel.getStartup().getName().getName());
			System.out.println("No of state is " + hlmmodel.getStates().size());
		}
	}
Re: Access Model tree in a IEditorActionDelegate [message #558404 is a reply to message #558384] Sun, 12 September 2010 12:34 Go to previous message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

it is not recommended (discouraged) to return the model or parts of it from a unit of work. You may be on the safe side as you create a copy (but I am not expert enough to judge that).
Usually you would perform the action within the unit of work.

Alex
Previous Topic:Where to do type checking?
Next Topic:The Story of cq-markup-xtext (pt. 1)
Goto Forum:
  


Current Time: Thu Sep 26 19:50:07 GMT 2024

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

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

Back to the top