Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Call rename element
Call rename element [message #1727059] Fri, 18 March 2016 13:56 Go to next message
Benoit Ries is currently offline Benoit RiesFriend
Messages: 86
Registered: May 2013
Member
Hi,

I wish to rename programmatically an element in my model. For now I use a code similar to the one below.

The good thing is that it works, i.e. it renames the actor.

BUT unfortunately, all cross-references pointing to this element are NOT renamed.

- Maybe a first solution could be to perform a call to the "Rename Element" feature on my given EObject. But I have no clue on how to do that ?

- or any other solution that would notify the model that the EObject has been modified and forces the update of all its cross-references.

Any idea how I can call it directly do that ?

best regards,
Benoît

here is my current code:

IXtextDocument myDocument = XtextUtils.getXTextDocumentFromEObject(act);
myDocument.modify(new IUnitOfWork.Void<XtextResource>() {
@Override
public void process(XtextResource xtextResource) throws Exception {
PackageDeclaration pkg = (PackageDeclaration) xtextResource.getContents().get(0);
for (Actor actTmp: pkg.getEnvironmentM().getActors()) {
actTmp.setName(newName);
}}});

[Updated on: Mon, 21 March 2016 08:26]

Report message to a moderator

Re: Call rename element [message #1727191 is a reply to message #1727059] Mon, 21 March 2016 08:09 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
This may look bulky, but it should do what you expected. Of course you may use real monitors or provide better user feedback on errors.

@Inject private Provider<RenameElementProcessor> processorProvider;
...
RenameElementProcessor processor = processorProvider.get();
processor.initialize(new IRenameElementContext.Impl(targetElementURI, RefactoringPackage.Literals.ELEMENT);
processor.setNewName(newName);
RefactoringStatus initialStatus = processor.checkInitialConditions(new NullProgressMonitor());
if(initialStatus.isOK()) {
  RefactoringStatus finalStatus = processor.checkFinalConditions(new NullProgressMonitor(), null);
  if(finalStatus.isOK()) {
    Change change = processor.createChange(new NullProgressMonitor());
    new WorkspaceModifyOperation() {
      @Override
      protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
        change.perform(monitor);
      }
    }.run(null);
  } 
}


---
Get professional support from the Xtext committers at www.typefox.io
Re: Call rename element [message #1727204 is a reply to message #1727191] Mon, 21 March 2016 10:11 Go to previous messageGo to next message
Benoit Ries is currently offline Benoit RiesFriend
Messages: 86
Registered: May 2013
Member

=> Can't find the required plugin dependency for RefactoringPackage.Literals.ELEMENT, which one is it please ?

this is pretty clear thanks Jan
Re: Call rename element [message #1727206 is a reply to message #1727191] Mon, 21 March 2016 10:16 Go to previous message
Benoit Ries is currently offline Benoit RiesFriend
Messages: 86
Registered: May 2013
Member
no need to reply to the previous comment. I understood now: it should be replaced with the EClass related to my case.
Previous Topic:Workspace Builds repeatedly when running as eclipse application
Next Topic:Using generated Xtext web editor as a RAP widget
Goto Forum:
  


Current Time: Fri Apr 19 05:31:39 GMT 2024

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

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

Back to the top