/** * This method creates a model that reflects the state that would be obtained if * this proposal was accepted. This model can differ from the current model, * because different proposals can result in different models. The code that is * passed as argument is executed once the (changed) model was created. After * executing the given code, all changes are reverted. */ public void materialize(Runnable code) { EObject root = EcoreUtil.getRootContainer(getContainer()); if (root == null) { code.run(); return; } ChangeRecorder recorder = new ChangeRecorder(); recorder.beginRecording(Collections.singleton(root)); // attach proposal model fragment to main model Runnable attachmentCode = getAttachmentCode(); if (attachmentCode != null) { // Applying attachment code attachmentCode.run(); } ChangeDescription changes = recorder.endRecording(); code.run(); // revert changes changes.apply(); }
@Override public void applyChange(Deltas deltas, IAcceptor<IEmfResourceChange> changeAcceptor) { XtextResource res = (XtextResource) lifecycleManager.openAndApplyReferences(getResourceSet(), getResource()); if (!referenceUpdater.isAffected(deltas, getResource())) { return; } ITextRegionAccess base = textRegionBuilderProvider.get().forNodeModel(res).create(); ITextRegionDiffBuilder rewriter = new StringBasedTextRegionAccessDiffBuilder(base); ReferenceUpdaterContext context = new ReferenceUpdaterContext(deltas, rewriter); referenceUpdater.update(context); if (!context.getModifications().isEmpty()) { ChangeRecorder rec = new ChangeRecorder(res); for (Runnable run : context.getModifications()) { run.run(); } ChangeDescription recording = rec.endRecording(); ResourceSet rs = res.getResourceSet(); ResourceSetRecording tree = changeTreeProvider.createChangeTree(rs, Collections.emptyList(), recording); ResourceRecording recordedResource = tree.getRecordedResource(res); serializer.serializeChanges(recordedResource, rewriter); } for (IUpdatableReference upd : context.getUpdatableReferences()) { referenceUpdater.updateReference(rewriter, upd); } ITextRegionAccessDiff rewritten = rewriter.create(); List<ITextReplacement> rep = formatter.format(rewritten); TextDocumentChange change = new TextDocumentChange(rewritten, getResource().getUri(), rep); changeAcceptor.accept(change); }
public ITextRegionDiffBuilder beginRecording(IChangeSerializer serializer, XtextResource resource) { this.serializer = serializer; this.snapshot = snapshotProvider.createResourceSnapshot(resource); this.document = new StringBasedTextRegionAccessDiffBuilder(this.snapshot.getRegions()); EcoreUtil.resolveAll(resource); this.recorder = new ChangeRecorder(resource); return this.document; }
/** * rollbackResourceSet * * @param recorder void */ public static void rollbackResourceSet(final ChangeRecorder recorder) { if (recorder == null || !recorder.isRecording()) { return; } DomainRegistry.getEditingDomain().getCommandStack().execute(new CompoundCommand() { /** * @see org.eclipse.emf.common.command.CompoundCommand#execute() */ @Override public void execute() { try { ChangeDescription endRecording = recorder.endRecording(); if (endRecording != null) { endRecording.apply(); } } catch (Exception e) { e.printStackTrace(); } } /** * @see org.eclipse.emf.common.command.AbstractCommand#canExecute() */ @Override public boolean canExecute() { return true; } }); }
/** * @see org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette#initializeGraphicalViewer() */ @Override protected void initializeGraphicalViewer() { super.initializeGraphicalViewer(); GraphicalViewer viewer = getGraphicalViewer(); viewer.setContents(diagramEditDomain.getDiagram()); viewer.addDropTargetListener(createDropTagertListener()); this.recorder = new ChangeRecorder(getDiagram()); }
/** * start * void */ public void start() { resourceSetinitialize(); saveResource = new ArrayList<Resource>(); recorder = new ChangeRecorder(DomainRegistry.getUMLDomain().getResourceSet()); // skipOn(); rsListener = ContentProviderRegistry.getContentProvider(); if (rsListener != null) { rsListener.stopResourceListening(); } }
/** * @see nexcore.tool.uml.ui.core.editor.AbstractMultiPageFormEditor#createModel(org.eclipse.ui.IEditorInput) */ @Override protected void createModel(IEditorInput input) throws PartInitException { if (input instanceof UMLEditorInput) { this.model = (Model) ((UMLEditorInput) getEditorInput()).getElement(); this.recorder = new ChangeRecorder(model); registerResourceSetListener(); } }
void doit() throws Exception { ResourceSet originalResourceSet = new ResourceSetImpl(); loadResources(originalResourceSet); @SuppressWarnings("unused") Map<EObject, URI> eObjectToProxyURIMap = new HashMap<EObject, URI>(); ChangeRecorder changeRecorder = new ChangeRecorder(); /* Uncomment this in 3.7.x for the test to work/pass changeRecorder.setRecordingTransientFeatures(false); changeRecorder.setEObjectToProxyURIMap(eObjectToProxyURIMap); */ changeRecorder.beginRecording(Collections.singleton(originalResourceSet)); makeChanges(); ChangeDescription changeDescription = changeRecorder.endRecording(); /* Uncomment this in 3.7.x for the test to work/pass changeDescription.copyAndReverse(eObjectToProxyURIMap); */ ResourceSet finalResourceSet = new ResourceSetImpl(); Resource changeDescriptionResource = finalResourceSet.createResource(URI.createURI("changes.change")); changeDescriptionResource.getContents().add(changeDescription); ByteArrayOutputStream out = new ByteArrayOutputStream(); changeDescriptionResource.save(System.out, null); changeDescriptionResource.save(out, null); changeDescriptionResource.getContents().clear(); changeDescriptionResource.unload(); changeDescriptionResource.load(new ByteArrayInputStream(out.toByteArray()), null); ChangeDescription finalChangeDescription = (ChangeDescription) changeDescriptionResource.getContents().get(0); finalChangeDescription.apply(); finalResourceSet.getResources().remove(0); assertEquals(originalResourceSet, finalResourceSet); }
/** * 모델 rollback 후 모델 저장 * * * @param recorder * @param eObject * void */ public static void rollbackWithSave(final ChangeRecorder recorder, final EObject eObject) { if (recorder == null || !recorder.isRecording() || eObject == null) { return; } DomainRegistry.getEditingDomain().getCommandStack().execute(new CompoundCommand() { /** * @see org.eclipse.emf.common.command.CompoundCommand#execute() */ @Override public void execute() { try { if (recorder.isRecording()) { ChangeDescription endRecording = recorder.endRecording(); if (endRecording != null) { endRecording.apply(); } } } catch (Exception e) { e.printStackTrace(); } finally { DomainModelHandlerUtil.save(eObject); if( eObject instanceof Package) { TreeMap<String, Package> fragmentedPackageList = ProjectUtil.getFragmentedPackageList((Package) eObject); for (Iterator<Package> iterator = fragmentedPackageList.values().iterator(); iterator.hasNext();) { Package pkg = (Package) iterator.next(); DomainModelHandlerUtil.save(pkg); } } } } /** * @see org.eclipse.emf.common.command.AbstractCommand#canExecute() */ @Override public boolean canExecute() { return true; } }); }
/** * 모델 rollback 처리 * * * @param recorder * @param eObject * void */ public static void beginRecording(final ChangeRecorder recorder, final EObject eObject) { if (recorder == null || !recorder.isRecording() || eObject == null) { return; } DomainRegistry.getEditingDomain().getCommandStack().execute(new CompoundCommand() { /** * @see org.eclipse.emf.common.command.CompoundCommand#execute() */ @Override public void execute() { try { if (recorder.isRecording()) { recorder.endRecording(); } } catch (Exception e) { e.printStackTrace(); } finally { if (eObject instanceof Package) { List<EObject> list = new ArrayList<EObject>(); TreeMap<String, Package> fragmentedPackageList = ProjectUtil.getFragmentedPackageList((Package) eObject); list.add(eObject); list.addAll(fragmentedPackageList.values()); recorder.beginRecording(list); } else { recorder.beginRecording(Collections.singleton(eObject)); } } } /** * @see org.eclipse.emf.common.command.AbstractCommand#canExecute() */ @Override public boolean canExecute() { return true; } }); }
/** * * @param elements * @param target * @return String 이동되지 않은 다이어그램의 이름(Comma로 구분됨) */ public static String moveDiagram(Collection<Object> elements, EObject target) { String notMovedDiagrams = UICoreConstant.EMPTY_STRING; final ChangeRecorder recorder = new ChangeRecorder(DomainRegistry.getUMLDomain().getResourceSet()); try { List<Diagram> activityDiagramList = new ArrayList<Diagram>(); List<Diagram> classDiagramList = new ArrayList<Diagram>(); List<Diagram> sequenceDiagramList = new ArrayList<Diagram>(); List<Diagram> usecaseDiagramList = new ArrayList<Diagram>(); List<Diagram> componentDiagramList = new ArrayList<Diagram>(); for (Iterator<Object> iterator = elements.iterator(); iterator.hasNext();) { Object object = iterator.next(); if (object instanceof Diagram) { Diagram diagram = (Diagram) object; if (diagram.getType() == DiagramType.ACTIVITY_DIAGRAM) { if (target instanceof Model || target instanceof Package || target instanceof Activity) { activityDiagramList.add(diagram); } else { if (UICoreConstant.EMPTY_STRING.equals(notMovedDiagrams)) { notMovedDiagrams = diagram.getName(); } else { notMovedDiagrams += UICoreConstant.PROJECT_CONSTANTS__COMMA + diagram.getName(); } } } else if (diagram.getType() == DiagramType.CLASS_DIAGRAM) { classDiagramList.add(diagram); } else if (diagram.getType() == DiagramType.SEQUENCE_DIAGRAM) { sequenceDiagramList.add(diagram); } else if (diagram.getType() == DiagramType.USE_CASE_DIAGRAM) { usecaseDiagramList.add(diagram); } else if (diagram.getType() == DiagramType.COMPONENT_DIAGRAM) { componentDiagramList.add(diagram); } } } if (activityDiagramList.size() > 0) { moveActivityDiagram(activityDiagramList, target); } if (classDiagramList.size() > 0) { moveClassDiagram(classDiagramList, target); } if (sequenceDiagramList.size() > 0) { moveSequenceDiagram(sequenceDiagramList, target); } if (usecaseDiagramList.size() > 0) { moveUsecaseDiagram(usecaseDiagramList, target); } if (componentDiagramList.size() > 0) { moveComponentDiagram(componentDiagramList, target); } } catch (Exception e) { e.printStackTrace(); ProjectUtil.rollbackResourceSet(recorder); // return new Status(Status.ERROR, IRuntimeConstants.PI_RUNTIME, Status.ERROR, e.getMessage(), null); } return notMovedDiagrams; // return Status.OK_STATUS; }