The following snippet illustrates the creation of a workbench editing domain:
// can use any operation history instance IOperationHistory myHistory = OperationHistoryFactory.getOperationHistory(); TransactionalEditingDomain domain = WorkspaceEditingDomainFactory.INSTANCE.createEditingDomain(myHistory); ResourceSet rset = domain.getResourceSet(); // could also just let the editing domain get the default history from the history factory TransactionalEditingDomain domain = WorkspaceEditingDomainFactory.INSTANCE.createEditingDomain();
The same editing domain registry and extension point used for sharing
TransactionalEditingDomain
s can also be used to share workbench editing
domains. Just register an editing domain ID and a factory implementation on the
org.eclipse.emf.transaction.editingDomains
extension point and
use the {@link org.eclipse.emf.transaction.TransactionalEditingDomain.Registry} to access
your domain.
The normal procedure for modifying resources in a workbench editing domain is to use undoable operations:
IUndoableOperation operation = new AbstractEMFOperation( domain, "Create Library") { protected IStatus doExecute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { Resource res = rset.getResource( URI.createFileURI("/tmp/my.xmi"), true); Library library = LibraryFactory.eINSTANCE.createLibrary(); // these modifications require a write transaction in this // editing domain. The operation provides this transaction res.getContents().add(library); library.setName("Main Branch"); } }; try { myHistory.execute(operation, new NullProgressMonitor(), null); } catch (ExecutionException ee) { getLog().log(ee); }
Of course, it is just as easy to re-use existing EMF Command
s:
IUndoableOperation operation = new EMFCommandOperation( domain, new CreateLibraryCommand()); try { myHistory.execute(operation, new NullProgressMonitor(), null); } catch (ExecutionException ee) { getLog().log(ee); } // alternatively, the command stack of our editing domain will automatically // wrap the command in an operation and execute it on the operation history domain.getCommandStack().execute(new CreateLibraryCommand());
In either case, undoing and redoing operations is as simple as the operation history API makes it:
// undo try { myHistory.undo(myEditorContext, new NullProgressMonitor(), null); } catch (ExecutionException ee) { getLog().log(ee); } // redo try { myHistory.redo(myEditorContext, new NullProgressMonitor(), null); } catch (ExecutionException ee) { getLog().log(ee); }@see org.eclipse.emf.workspace.WorkspaceEditingDomainFactory @see org.eclipse.emf.workspace.AbstractEMFOperation @see org.eclipse.emf.workspace.CompositeEMFOperation