Skip to content

Package: EMFKeyAttributeValueProperty

EMFKeyAttributeValueProperty

nameinstructionbranchcomplexitylinemethod
EMFKeyAttributeValueProperty(EditingDomain, EMFFormsDatabindingEMF, VDomainModelReference, Object, EStructuralFeature)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
doGetValue(Object)
M: 4 C: 41
91%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 2 C: 10
83%
M: 0 C: 1
100%
doSetValue(Object, Object)
M: 1 C: 96
99%
M: 4 C: 8
67%
M: 4 C: 3
43%
M: 0 C: 20
100%
M: 0 C: 1
100%
toString()
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2016 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.core.services.databinding.keyattribute;
15:
16: import org.eclipse.emf.common.command.Command;
17: import org.eclipse.emf.common.util.EList;
18: import org.eclipse.emf.databinding.internal.EMFValueProperty;
19: import org.eclipse.emf.ecore.EObject;
20: import org.eclipse.emf.ecore.EStructuralFeature;
21: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
22: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
23: import org.eclipse.emf.edit.command.AddCommand;
24: import org.eclipse.emf.edit.command.SetCommand;
25: import org.eclipse.emf.edit.domain.EditingDomain;
26: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
27: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
28:
29: /**
30: * @author Lucas Koehler
31: *
32: */
33: @SuppressWarnings("restriction")
34: public class EMFKeyAttributeValueProperty extends EMFValueProperty {
35:
36:         private final EditingDomain editingDomain;
37:         private final EMFFormsDatabindingEMF databinding;
38:         private final VDomainModelReference keyDMR;
39:         private final Object key;
40:
41:         /**
42:          * @param editingDomain The {@link EditingDomain}
43:          * @param databinding The {@link EMFFormsDatabindingEMF emf databinding service}
44:          * @param keyDMR The reference to the key field
45:          * @param key The key
46:          * @param eStructuralFeature The {@link EStructuralFeature} of the key attribute feature
47:          */
48:         public EMFKeyAttributeValueProperty(EditingDomain editingDomain, EMFFormsDatabindingEMF databinding,
49:                 VDomainModelReference keyDMR, Object key, EStructuralFeature eStructuralFeature) {
50:                 super(eStructuralFeature);
51:                 this.editingDomain = editingDomain;
52:                 this.databinding = databinding;
53:                 this.keyDMR = keyDMR;
54:                 this.key = key;
55:         }
56:
57:         /**
58:          * {@inheritDoc}
59:          *
60:          * @see org.eclipse.emf.databinding.internal.EMFValueProperty#doGetValue(java.lang.Object)
61:          */
62:         @SuppressWarnings("unchecked")
63:         @Override
64:         protected Object doGetValue(Object source) {
65:                 final Object object = super.doGetValue(source);
66:                 final EList<Object> list = (EList<Object>) object;
67:•                if (list == null) {
68:                         return null;
69:                 }
70:
71:                 // If it exists, find the EObject that contains the key.
72:•                for (int i = 0; i < list.size(); i++) {
73:                         final EObject eObject = (EObject) list.get(i);
74:                         Setting setting;
75:                         try {
76:                                 setting = databinding.getSetting(keyDMR, eObject);
77:                         } catch (final DatabindingFailedException ex) {
78:                                 // TODO report needed?
79:                                 continue;
80:                         }
81:•                        if (key.equals(setting.get(true))) {
82:                                 // key was present in the current EObject
83:                                 return eObject;
84:                         }
85:                 }
86:
87:                 // return null if the key was not present in any EObject
88:                 return null;
89:         }
90:
91:         /**
92:          * {@inheritDoc}
93:          *
94:          * @see org.eclipse.emf.databinding.internal.EMFValueProperty#doSetValue(java.lang.Object, java.lang.Object)
95:          */
96:         @SuppressWarnings("unchecked")
97:         @Override
98:         protected void doSetValue(Object source, Object value) {
99:                 final EObject newValue = (EObject) value;
100:                 Setting setting;
101:                 try {
102:                         setting = databinding.getSetting(keyDMR, newValue);
103:                 } catch (final DatabindingFailedException ex) {
104:                         throw new IllegalArgumentException("The new value's key could not be resolved.", ex); //$NON-NLS-1$
105:                 }
106:•                if (setting == null || !key.equals(setting.get(true))) {
107:                         throw new IllegalArgumentException("The new value must contain the key '" + key + "'."); //$NON-NLS-1$//$NON-NLS-2$
108:                 }
109:
110:                 final Object oldValue = doGetValue(source);
111:                 Command command = null;
112:•                if (oldValue == null) {
113:                         // no EObject in the list contains the key => add new EObject
114:                         command = AddCommand.create(editingDomain, source, getFeature(), newValue);
115:                 } else {
116:                         // An EObject in the list contains the key => Replace old EObject
117:                         final Object object = super.doGetValue(source);
118:                         final EList<Object> list = (EList<Object>) object;
119:•                        for (int i = 0; i < list.size(); i++) {
120:•                                if (list.get(i).equals(oldValue)) {
121:                                         command = SetCommand.create(editingDomain, source, getFeature(), newValue, i);
122:                                         break;
123:                                 }
124:                         }
125:                 }
126:
127:•                if (command != null) {
128:                         editingDomain.getCommandStack().execute(command);
129:                 }
130:         }
131:
132:         /**
133:          * {@inheritDoc}
134:          *
135:          * @see org.eclipse.emf.databinding.internal.EMFValueProperty#toString()
136:          */
137:         @Override
138:         public String toString() {
139:                 final String s = super.toString();
140:                 return s + " key '" + key + "'"; //$NON-NLS-1$ //$NON-NLS-2$
141:         }
142:
143: }