Skip to content

Package: DMREditingSupport$EditingState

DMREditingSupport$EditingState

nameinstructionbranchcomplexitylinemethod
DMREditingSupport.EditingState(DMREditingSupport, EMFDataBindingContext, Binding, IObservableValue, IObservableValue)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
dispose()
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 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: * Johannes Faltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.swt.table.util;
15:
16: import org.eclipse.core.databinding.Binding;
17: import org.eclipse.core.databinding.observable.value.IObservableValue;
18: import org.eclipse.core.databinding.property.value.IValueProperty;
19: import org.eclipse.emf.databinding.EMFDataBindingContext;
20: import org.eclipse.emf.ecore.EObject;
21: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
22: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
23: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
24: import org.eclipse.jface.databinding.swt.typed.WidgetProperties;
25: import org.eclipse.jface.viewers.CellEditor;
26: import org.eclipse.jface.viewers.ColumnViewer;
27: import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
28: import org.eclipse.jface.viewers.ColumnViewerEditorActivationListener;
29: import org.eclipse.jface.viewers.ColumnViewerEditorDeactivationEvent;
30: import org.eclipse.jface.viewers.EditingSupport;
31: import org.eclipse.jface.viewers.ViewerCell;
32: import org.eclipse.swt.SWT;
33: import org.osgi.framework.Bundle;
34: import org.osgi.framework.BundleContext;
35: import org.osgi.framework.FrameworkUtil;
36: import org.osgi.framework.ServiceReference;
37:
38: /**
39: * {@link EditingSupport} for a value described by a {@link org.eclipse.emf.ecp.view.spi.model.VDomainModelReference
40: * VDomainModelReference}.
41: *
42: * @author Johannes Faltermeier
43: *
44: */
45: public class DMREditingSupport extends EditingSupport {
46:
47:         private final CellEditor cellEditor;
48:
49:         private final IValueProperty valueProperty;
50:
51:         private EditingState editingState;
52:
53:         private final ColumnViewerEditorActivationListenerHelper activationListener = new ColumnViewerEditorActivationListenerHelper();
54:
55:         /**
56:          * Constructs a new {@link DMREditingSupport}.
57:          *
58:          * @param viewer the {@link ColumnViewer}
59:          * @param cellEditor the {@link CellEditor}
60:          * @param valueProperty the {@link IValueProperty} for the value of the cell
61:          */
62:         public DMREditingSupport(ColumnViewer viewer, CellEditor cellEditor, IValueProperty valueProperty) {
63:                 super(viewer);
64:                 this.cellEditor = cellEditor;
65:                 this.valueProperty = valueProperty;
66:                 if (valueProperty == null) {
67:                         throw new IllegalArgumentException("ValueProperty may not be null"); //$NON-NLS-1$
68:                 }
69:         }
70:
71:         /**
72:          * Constructs a new {@link DMREditingSupport}.
73:          *
74:          * @param columnViewer the {@link ColumnViewer}
75:          * @param cellEditor the {@link CellEditor}
76:          * @param domainModelReference the domain model reference of the column value
77:          * @param parent the domain object of the column value
78:          */
79:         public DMREditingSupport(ColumnViewer columnViewer, CellEditor cellEditor,
80:                 VDomainModelReference domainModelReference, EObject parent) {
81:                 this(columnViewer, cellEditor,
82:                         getValueProperty(domainModelReference, parent));
83:         }
84:
85:         private static IValueProperty getValueProperty(VDomainModelReference domainModelReference, EObject parent) {
86:                 try {
87:                         return getService(EMFFormsDatabinding.class).getValueProperty(domainModelReference, parent);
88:                 } catch (final DatabindingFailedException ex) {
89:                         return null;
90:                 }
91:         }
92:
93:         @Override
94:         protected Object getValue(Object element) {
95:                 // no op
96:                 return null;
97:         }
98:
99:         @Override
100:         protected void setValue(Object element, Object value) {
101:                 // no op
102:         }
103:
104:         @Override
105:         protected void initializeCellEditorValue(CellEditor cellEditor, ViewerCell cell) {
106:                 final EMFDataBindingContext bindingContext = new EMFDataBindingContext();
107:                 final IObservableValue target = doCreateCellEditorObservable(cellEditor);
108:                 final IObservableValue model = valueProperty.observe(cell.getElement());
109:                 final Binding binding = bindingContext.bindValue(target, model);
110:                 editingState = new EditingState(bindingContext, binding, target, model);
111:                 getViewer().getColumnViewerEditor().addEditorActivationListener(activationListener);
112:         }
113:
114:         @Override
115:         protected CellEditor getCellEditor(Object element) {
116:                 return cellEditor;
117:         }
118:
119:         private IObservableValue doCreateCellEditorObservable(CellEditor cellEditor) {
120:                 return WidgetProperties.text(SWT.FocusOut).observe(cellEditor.getControl());
121:         }
122:
123:         @Override
124:         protected final void saveCellEditorValue(CellEditor cellEditor, ViewerCell cell) {
125:                 editingState.binding.updateTargetToModel();
126:         }
127:
128:         @Override
129:         protected boolean canEdit(Object element) {
130:                 return true;
131:         }
132:
133:         private static <T> T getService(Class<T> clazz) {
134:                 final Bundle bundle = FrameworkUtil.getBundle(DMRCellLabelProvider.class);
135:                 final BundleContext bundleContext = bundle.getBundleContext();
136:                 final ServiceReference<T> serviceReference = bundleContext.getServiceReference(clazz);
137:                 final T service = bundleContext.getService(serviceReference);
138:                 bundleContext.ungetService(serviceReference);
139:                 return service;
140:         }
141:
142:         /**
143:          * A ColumnViewerEditorActivationListener to reset the cells after focus lost.
144:          *
145:          * @author Eugen Neufeld
146:          *
147:          */
148:         private class ColumnViewerEditorActivationListenerHelper extends ColumnViewerEditorActivationListener {
149:
150:                 @Override
151:                 public void afterEditorActivated(ColumnViewerEditorActivationEvent event) {
152:                         // no op
153:                 }
154:
155:                 @Override
156:                 public void afterEditorDeactivated(ColumnViewerEditorDeactivationEvent event) {
157:                         editingState.dispose();
158:                         editingState = null;
159:
160:                         getViewer().getColumnViewerEditor().removeEditorActivationListener(this);
161:                         final ViewerCell focusCell = getViewer().getColumnViewerEditor().getFocusCell();
162:                         if (focusCell != null) {
163:                                 getViewer().update(focusCell.getElement(), null);
164:                         }
165:                 }
166:
167:                 @Override
168:                 public void beforeEditorActivated(ColumnViewerEditorActivationEvent event) {
169:                         // do nothing
170:                 }
171:
172:                 @Override
173:                 public void beforeEditorDeactivated(ColumnViewerEditorDeactivationEvent event) {
174:                         // do nothing
175:                 }
176:         }
177:
178:         /** @author Johannes Faltermeier */
179:         private class EditingState {
180:                 private final EMFDataBindingContext bindingContext;
181:
182:                 private final IObservableValue target;
183:
184:                 private final IObservableValue model;
185:
186:                 private final Binding binding;
187:
188:                 EditingState(EMFDataBindingContext bindingContext, Binding binding, IObservableValue target,
189:                         IObservableValue model) {
190:                         this.binding = binding;
191:                         this.target = target;
192:                         this.model = model;
193:                         this.bindingContext = bindingContext;
194:                 }
195:
196:                 void dispose() {
197:                         binding.dispose();
198:                         target.dispose();
199:                         model.dispose();
200:                         bindingContext.dispose();
201:                 }
202:         }
203: }