Skip to content

Package: AttributeValueControlSWTRenderer$1

AttributeValueControlSWTRenderer$1

nameinstructionbranchcomplexitylinemethod
convert(String)
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2014 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: * Eugen - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.template.internal.tooling.controls;
15:
16: import org.eclipse.core.databinding.Binding;
17: import org.eclipse.core.databinding.UpdateValueStrategy;
18: import org.eclipse.core.databinding.observable.IObserving;
19: import org.eclipse.core.databinding.observable.value.IObservableValue;
20: import org.eclipse.emf.common.command.Command;
21: import org.eclipse.emf.ecore.EObject;
22: import org.eclipse.emf.ecore.EStructuralFeature;
23: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
24: import org.eclipse.emf.ecp.view.spi.core.swt.SimpleControlSWTControlSWTRenderer;
25: import org.eclipse.emf.ecp.view.spi.model.VControl;
26: import org.eclipse.emf.ecp.view.template.internal.tooling.Messages;
27: import org.eclipse.emf.ecp.view.template.internal.tooling.util.ValueSelectionHelper;
28: import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider;
29: import org.eclipse.emf.ecp.view.template.selector.viewModelElement.model.VTViewModelElementSelector;
30: import org.eclipse.emf.edit.command.SetCommand;
31: import org.eclipse.emf.edit.domain.EditingDomain;
32: import org.eclipse.emfforms.spi.common.report.ReportService;
33: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
34: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedReport;
35: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
36: import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider;
37: import org.eclipse.jface.databinding.swt.typed.WidgetProperties;
38: import org.eclipse.jface.layout.GridDataFactory;
39: import org.eclipse.jface.layout.GridLayoutFactory;
40: import org.eclipse.swt.SWT;
41: import org.eclipse.swt.events.SelectionAdapter;
42: import org.eclipse.swt.events.SelectionEvent;
43: import org.eclipse.swt.widgets.Button;
44: import org.eclipse.swt.widgets.Composite;
45: import org.eclipse.swt.widgets.Control;
46: import org.eclipse.swt.widgets.Label;
47: import org.osgi.framework.BundleContext;
48: import org.osgi.framework.FrameworkUtil;
49: import org.osgi.framework.ServiceReference;
50:
51: /**
52: * Renderer for the
53: * {@link org.eclipse.emf.ecp.view.template.selector.viewModelElement.model.VTViewModelElementSelector#setAttributeValue(Object)
54: * VTViewModelElementSelector#setAttributeValue(Object)}.
55: *
56: * @author Eugen Neufeld
57: *
58: */
59: public class AttributeValueControlSWTRenderer extends SimpleControlSWTControlSWTRenderer {
60:
61:         private static final EMFFormsDatabinding EMFFORMS_DATABINDING;
62:         private static final EMFFormsLabelProvider EMFFORMS_LABELPROVIDER;
63:         private static final VTViewTemplateProvider VT_VIEW_TEMPLATEPROVIDER;
64:
65:         static {
66:                 final BundleContext bundleContext = FrameworkUtil.getBundle(AttributeValueControlSWTRenderer.class)
67:                         .getBundleContext();
68:                 final ServiceReference<EMFFormsDatabinding> emfFormsDatabindingServiceReference = bundleContext
69:                         .getServiceReference(EMFFormsDatabinding.class);
70:                 EMFFORMS_DATABINDING = bundleContext.getService(emfFormsDatabindingServiceReference);
71:                 final ServiceReference<EMFFormsLabelProvider> emfFormsLabelProviderServiceReference = bundleContext
72:                         .getServiceReference(EMFFormsLabelProvider.class);
73:                 EMFFORMS_LABELPROVIDER = bundleContext.getService(emfFormsLabelProviderServiceReference);
74:                 final ServiceReference<VTViewTemplateProvider> vtViewTemplateProviderServiceReference = bundleContext
75:                         .getServiceReference(VTViewTemplateProvider.class);
76:                 VT_VIEW_TEMPLATEPROVIDER = bundleContext.getService(vtViewTemplateProviderServiceReference);
77:         }
78:
79:         /**
80:          * Default constructor.
81:          *
82:          * @param vElement the view model element to be rendered
83:          * @param viewContext the view context
84:          * @param reportService The {@link ReportService}
85:          */
86:         public AttributeValueControlSWTRenderer(VControl vElement, ViewModelContext viewContext,
87:                 ReportService reportService) {
88:                 super(vElement, viewContext, reportService, EMFFORMS_DATABINDING, EMFFORMS_LABELPROVIDER,
89:                         VT_VIEW_TEMPLATEPROVIDER);
90:         }
91:
92:         @SuppressWarnings("unchecked")
93:         @Override
94:         protected Binding[] createBindings(Control control) throws DatabindingFailedException {
95:                 final Label label = (Label) Composite.class.cast(control).getChildren()[0];
96:                 final Binding binding = getDataBindingContext().bindValue(WidgetProperties.text().observe(label),
97:                         getModelValue(),
98:                         withPreSetValidation(new UpdateValueStrategy<String, String>(UpdateValueStrategy.POLICY_NEVER)),
99:                         new UpdateValueStrategy<String, String>() {
100:
101:                                 @Override
102:                                 public String convert(String value) {
103:                                         return value;
104:                                 }
105:
106:                         });
107:                 return new Binding[] { binding };
108:         }
109:
110:         @Override
111:         protected Control createSWTControl(final Composite parent) {
112:                 final Composite composite = new Composite(parent, SWT.NONE);
113:                 composite.setBackgroundMode(SWT.INHERIT_FORCE);
114:                 GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).applyTo(composite);
115:                 final Label label = new Label(composite, SWT.NONE);
116:                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).applyTo(label);
117:
118:                 final Button button = new Button(composite, SWT.PUSH);
119:                 button.setText(Messages.AttributeValueControlSWTRenderer_SelectAttributeValue);
120:                 GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.BEGINNING).grab(false, false).applyTo(button);
121:
122:                 button.addSelectionListener(new SelectionAdapter() {
123:                         @Override
124:                         public void widgetSelected(SelectionEvent e) {
125:                                 super.widgetSelected(e);
126:                                 IObservableValue observableValue;
127:                                 try {
128:                                         observableValue = getEMFFormsDatabinding()
129:                                                 .getObservableValue(getVElement().getDomainModelReference(),
130:                                                         getViewModelContext().getDomainModel());
131:                                 } catch (final DatabindingFailedException ex) {
132:                                         getReportService().report(new DatabindingFailedReport(ex));
133:                                         return;
134:                                 }
135:                                 final EObject eObject = (EObject) ((IObserving) observableValue).getObserved();
136:                                 final EStructuralFeature structuralFeature = (EStructuralFeature) observableValue.getValueType();
137:                                 observableValue.dispose();
138:
139:                                 final Object result = ValueSelectionHelper.openValueSelectionDialog(parent.getShell(),
140:                                         VTViewModelElementSelector.class.cast(eObject).getAttribute());
141:                                 final EditingDomain editingDomain = getEditingDomain(eObject);
142:                                 final Command command = SetCommand.create(editingDomain, eObject, structuralFeature, result);
143:                                 editingDomain.getCommandStack().execute(command);
144:                         }
145:                 });
146:
147:                 return composite;
148:         }
149:
150:         @Override
151:         protected String getUnsetText() {
152:                 return Messages.AttributeValueControlSWTRenderer_UnsetAttributeMessage;
153:         }
154:
155: }