Skip to main content
summaryrefslogtreecommitdiffstats
blob: 98fc8e37d4150cb30c528930f0655c067f10a404 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*******************************************************************************
 * Copyright (c) 2016-2017, Thales Global Services S.A.S.
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Abel Hegedus, Tamas Borbas, Balazs Grill, Peter Lunk, Daniel Segesdi (IncQuery Labs Ltd.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.diffmerge.patch.runtime.modelaccess

import org.eclipse.emf.common.util.EList
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EReference
import org.eclipse.emf.ecore.EStructuralFeature
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.viatra.transformation.runtime.emf.modelmanipulation.IModelManipulations

class ViatraModelAccess extends AbstractEMFModleAccess {
  val IModelManipulations viatraModelManipulator
  val Resource temporaryResource

  new(IModelManipulations viatraModelManipulator, Resource temporaryResource) {
    this.viatraModelManipulator = viatraModelManipulator
    this.temporaryResource = temporaryResource
  }

  override protected doCreate(EClass type) {
    viatraModelManipulator.create(temporaryResource, type)
  }

  override protected doRemove(EObject element) {
    viatraModelManipulator.remove(element)
  }

  override protected doAdd(EObject container, EStructuralFeature feature, Object element) {
    if(feature.isMany) {
      if(feature instanceof EReference  && (feature as EReference).isContainment) {
        if(element instanceof EObject) {
          viatraModelManipulator.moveTo(element, container, feature as EReference)
        } else {
          throw new IllegalArgumentException("Only EObjects can be added to EReferences.")
        }
      } else {
        viatraModelManipulator.add(container, feature, element)
      }
    } else {
      viatraModelManipulator.set(container, feature, element)
    }
  }

  override protected doAdd(EObject container, EStructuralFeature feature, Object element, int index) {
    if(feature instanceof EReference  && (feature as EReference).isContainment) {
      if(element instanceof EObject) {
        viatraModelManipulator.moveTo(element as EObject, container, feature as EReference, index)
      } else {
        throw new IllegalArgumentException("Only EObjects can be added to EReferences.")
      }
    } else {
      viatraModelManipulator.add(container, feature, element, index)
    }
  }

  override protected doRemove(EObject container, EStructuralFeature feature, Object element) {
    if(feature.isMany) {
      viatraModelManipulator.remove(container, feature, element)
    } else {
      viatraModelManipulator.set(container, feature, feature.defaultValue)
    }
  }

  override protected doRemove(EObject container, EStructuralFeature feature, int index) {
    viatraModelManipulator.remove(container, feature, index)
  }

  override protected doChangeIndex(EObject container, EStructuralFeature feature, Object element, int index) {
    val oldIndex = (container.eGet(feature) as EList).indexOf(element)
    viatraModelManipulator.changeIndex(container, feature, oldIndex, index)
  }

}

Back to the top