Skip to content

Package: EditableEReferenceLabelControlSWTRenderer$1

EditableEReferenceLabelControlSWTRenderer$1

nameinstructionbranchcomplexitylinemethod
convert(Object)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
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.internal.editor.controls;
15:
16: import org.eclipse.core.databinding.UpdateValueStrategy;
17: import org.eclipse.core.databinding.observable.IObserving;
18: import org.eclipse.core.databinding.observable.value.IObservableValue;
19: import org.eclipse.emf.databinding.EMFDataBindingContext;
20: import org.eclipse.emf.ecore.EObject;
21: import org.eclipse.emf.ecore.EStructuralFeature;
22: import org.eclipse.emf.ecp.edit.spi.swt.util.ECPDialogExecutor;
23: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
24: import org.eclipse.emf.ecp.view.spi.model.VControl;
25: import org.eclipse.emf.ecp.view.spi.swt.reporting.RenderingFailedReport;
26: import org.eclipse.emfforms.spi.common.report.ReportService;
27: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
28: import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider;
29: import org.eclipse.emfforms.spi.core.services.label.NoLabelFoundException;
30: import org.eclipse.jface.databinding.swt.typed.WidgetProperties;
31: import org.eclipse.jface.dialogs.IDialogLabelKeys;
32: import org.eclipse.jface.dialogs.MessageDialog;
33: import org.eclipse.jface.layout.GridLayoutFactory;
34: import org.eclipse.jface.resource.JFaceResources;
35: import org.eclipse.swt.SWT;
36: import org.eclipse.swt.events.SelectionAdapter;
37: import org.eclipse.swt.events.SelectionEvent;
38: import org.eclipse.swt.widgets.Button;
39: import org.eclipse.swt.widgets.Composite;
40: import org.eclipse.swt.widgets.Control;
41: import org.eclipse.swt.widgets.Shell;
42:
43: /**
44: * @author Eugen Neufeld
45: *
46: */
47: public abstract class EditableEReferenceLabelControlSWTRenderer extends EReferenceLabelControlSWTRenderer {
48:
49:         private final EMFDataBindingContext viewModelDBC;
50:
51:         /**
52:          * @param vElement the view model element to be rendered
53:          * @param viewContext the view context
54:          * @param reportService the {@link ReportService}
55:          */
56:         public EditableEReferenceLabelControlSWTRenderer(VControl vElement, ViewModelContext viewContext,
57:                 ReportService reportService) {
58:                 super(vElement, viewContext, reportService);
59:                 viewModelDBC = new EMFDataBindingContext();
60:         }
61:
62:         /**
63:          * {@inheritDoc}
64:          *
65:          * @see org.eclipse.emf.ecp.view.internal.editor.controls.ControlRootEClassControl2SWTRenderer#createSWTControl(org.eclipse.swt.widgets.Composite)
66:          */
67:         @Override
68:         protected Control createSWTControl(final Composite parent2) throws DatabindingFailedException {
69:                 final Composite composite = (Composite) super.createSWTControl(parent2);
70:
71:                 GridLayoutFactory.fillDefaults().numColumns(3).spacing(0, 0).equalWidth(false).applyTo(composite);
72:
73:                 final EMFFormsLabelProvider labelProvider = getEMFFormsLabelProvider();
74:                 final Button selectClass = new Button(composite, SWT.PUSH);
75:                 try {
76:                         final IObservableValue labelText = labelProvider.getDisplayName(getVElement().getDomainModelReference(),
77:                                 getViewModelContext().getDomainModel());
78:                         final IObservableValue tooltip = labelProvider.getDescription(getVElement().getDomainModelReference(),
79:                                 getViewModelContext().getDomainModel());
80:
81:                         viewModelDBC.bindValue(WidgetProperties.text().observe(selectClass), labelText, null,
82:                                 new UpdateValueStrategy() {
83:
84:                                         /**
85:                                          * {@inheritDoc}
86:                                          *
87:                                          * @see org.eclipse.core.databinding.UpdateValueStrategy#convert(java.lang.Object)
88:                                          */
89:                                         @Override
90:                                         public Object convert(Object value) {
91:                                                 final String result = (String) super.convert(value);
92:                                                 return "Link " + result; //$NON-NLS-1$
93:                                         }
94:
95:                                 });
96:                         viewModelDBC.bindValue(WidgetProperties.tooltipText().observe(selectClass), tooltip, null,
97:                                 new UpdateValueStrategy() {
98:
99:                                         /**
100:                                          * {@inheritDoc}
101:                                          *
102:                                          * @see org.eclipse.core.databinding.UpdateValueStrategy#convert(java.lang.Object)
103:                                          */
104:                                         @Override
105:                                         public Object convert(Object value) {
106:                                                 final String result = (String) super.convert(value);
107:                                                 return "Link " + result; //$NON-NLS-1$
108:                                         }
109:
110:                                 });
111:                 } catch (final NoLabelFoundException e) {
112:                         // FIXME Expectation?
113:                         getReportService().report(new RenderingFailedReport(e));
114:                         selectClass.setText("Link "); //$NON-NLS-1$
115:                         selectClass.setToolTipText("Link "); //$NON-NLS-1$
116:                 }
117:                 selectClass.addSelectionListener(new SelectionAdapter() {
118:
119:                         /**
120:                          * {@inheritDoc}
121:                          *
122:                          * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
123:                          */
124:                         @Override
125:                         public void widgetSelected(SelectionEvent e) {
126:                                 super.widgetSelected(e);
127:                                 linkValue(composite.getShell());
128:                                 composite.layout(true, true);
129:                         }
130:
131:                 });
132:
133:                 final Button unset = new Button(composite, SWT.PUSH);
134:                 unset.setText("Unset"); //$NON-NLS-1$
135:                 unset.setToolTipText("Unset"); //$NON-NLS-1$
136:                 final IObservableValue observableValue = getEMFFormsDatabinding()
137:                         .getObservableValue(getVElement().getDomainModelReference(), getViewModelContext().getDomainModel());
138:                 final EObject eObject = (EObject) ((IObserving) observableValue).getObserved();
139:                 final EStructuralFeature structuralFeature = (EStructuralFeature) observableValue.getValueType();
140:                 observableValue.dispose();
141:
142:                 unset.addSelectionListener(new SelectionAdapter() {
143:
144:                         /**
145:                          * {@inheritDoc}
146:                          *
147:                          * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
148:                          */
149:                         @Override
150:                         public void widgetSelected(SelectionEvent e) {
151:                                 super.widgetSelected(e);
152:                                 eObject.eUnset(structuralFeature);
153:                                 composite.layout(true, true);
154:                         }
155:
156:                 });
157:
158:                 return composite;
159:         }
160:
161:         /**
162:          * This method should be overwritten to provide a correct selection mechanism.
163:          *
164:          * @param shell the Shell
165:          */
166:         protected abstract void linkValue(Shell shell);
167:
168:         /**
169:          * Shows an error message dialog indicating a failed value link due to an exception.
170:          *
171:          * @param shell The parent {@link Shell} of the message dialog
172:          * @param ex The {@link Exception} causing the failure
173:          */
174:         protected void showLinkValueFailedMessageDialog(Shell shell, final Exception ex) {
175:                 final MessageDialog dialog = new MessageDialog(
176:                         shell, "Link Value Failed", null, //$NON-NLS-1$
177:                         "The value could not be linked due to an exception: " + ex.getMessage(), MessageDialog.ERROR, new String[] { //$NON-NLS-1$
178:                                 JFaceResources.getString(IDialogLabelKeys.OK_LABEL_KEY) },
179:                         0);
180:
181:                 new ECPDialogExecutor(dialog) {
182:                         @Override
183:                         public void handleResult(int codeResult) {
184:                                 // no op
185:                         }
186:                 }.execute();
187:         }
188:
189:         /**
190:          * {@inheritDoc}
191:          *
192:          * @see org.eclipse.emf.ecp.view.internal.editor.controls.EReferenceLabelControlSWTRenderer#dispose()
193:          */
194:         @Override
195:         public void dispose() {
196:                 viewModelDBC.dispose();
197:                 super.dispose();
198:         }
199: }