While developing with the Eclipse Graphical Modeling Framework, I’ve come across this one error that made zero sense to me:

java.lang.IllegalStateException: Cannot modify resource set without a write transaction

This error means that you’re not allowed to execute a write transaction in a read-only domain, in other words you can’t edit your models from here. There is apparently two solutions for this, if you don’t want the command to have undo and redo options then take a look @ this. I myself was never able to get this to work. So there is an alternate solution through specifying your own command. The custom command class would look something like this:

    public static class MyCommand extends AbstractTransactionalCommand
    {
	public MyCommand(TransactionalEditingDomain editingDomain)
	{
	    super(editingDomain, "a message", null);
	}

	@Override
	protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException
	{
            // You can add here the line of code that is giving you the error!
            // Below is an example line.
	    getEditingDomain().getCommandStack().execute(SetCommand.create(getEditingDomain(), myElement, XXXPackage.eINSTANCE.getElemenet_name(), 'New name'));
	    return CommandResult.newOKCommandResult();
	}
    }

So after defining your new class in order to execute that command in your read-only domain, you must wrap it in an ICommandProxy and then execute/return.

ICommandProxy a = new ICommandProxy(new MyCommand(getEditingDomain()));
a.execute();

And now you can execute write transactions in read-only domains!

VN:F [1.9.17_1161]
Rating: +5 (from 5 votes)
How To: Overcome "Cannot modify resource set without a write transaction" in Eclipse GMF, 100% based on 5 ratings

Leave Comment

13