Skip to content

Package: HexColorSelectionControlSWTRenderer

HexColorSelectionControlSWTRenderer

nameinstructionbranchcomplexitylinemethod
HexColorSelectionControlSWTRenderer(VControl, ViewModelContext, ReportService)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
createBindings(Control)
M: 53 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
createSWTControl(Composite)
M: 82 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
getString(RGB)
M: 39 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getUnsetText()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setValue(String)
M: 47 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 32 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%

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.model.VTViewTemplateProvider;
28: import org.eclipse.emf.edit.command.SetCommand;
29: import org.eclipse.emf.edit.domain.EditingDomain;
30: import org.eclipse.emfforms.spi.common.report.ReportService;
31: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
32: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedReport;
33: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
34: import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider;
35: import org.eclipse.jface.databinding.swt.typed.WidgetProperties;
36: import org.eclipse.jface.layout.GridDataFactory;
37: import org.eclipse.jface.layout.GridLayoutFactory;
38: import org.eclipse.swt.SWT;
39: import org.eclipse.swt.events.SelectionAdapter;
40: import org.eclipse.swt.events.SelectionEvent;
41: import org.eclipse.swt.graphics.Color;
42: import org.eclipse.swt.graphics.RGB;
43: import org.eclipse.swt.widgets.Button;
44: import org.eclipse.swt.widgets.ColorDialog;
45: import org.eclipse.swt.widgets.Composite;
46: import org.eclipse.swt.widgets.Control;
47: import org.eclipse.swt.widgets.Display;
48: import org.eclipse.swt.widgets.Label;
49: import org.osgi.framework.BundleContext;
50: import org.osgi.framework.FrameworkUtil;
51: import org.osgi.framework.ServiceReference;
52:
53: /**
54: * @author Eugen
55: *
56: */
57: public class HexColorSelectionControlSWTRenderer extends SimpleControlSWTControlSWTRenderer {
58:
59:         private static final EMFFormsDatabinding EMFFORMS_DATABINDING;
60:         private static final EMFFormsLabelProvider EMFFORMS_LABELPROVIDER;
61:         private static final VTViewTemplateProvider VT_VIEW_TEMPLATEPROVIDER;
62:
63:         static {
64:                 final BundleContext bundleContext = FrameworkUtil.getBundle(HexColorSelectionControlSWTRenderer.class)
65:                         .getBundleContext();
66:                 final ServiceReference<EMFFormsDatabinding> emfFormsDatabindingServiceReference = bundleContext
67:                         .getServiceReference(EMFFormsDatabinding.class);
68:                 EMFFORMS_DATABINDING = bundleContext.getService(emfFormsDatabindingServiceReference);
69:                 final ServiceReference<EMFFormsLabelProvider> emfFormsLabelProviderServiceReference = bundleContext
70:                         .getServiceReference(EMFFormsLabelProvider.class);
71:                 EMFFORMS_LABELPROVIDER = bundleContext.getService(emfFormsLabelProviderServiceReference);
72:                 final ServiceReference<VTViewTemplateProvider> vtViewTemplateProviderServiceReference = bundleContext
73:                         .getServiceReference(VTViewTemplateProvider.class);
74:                 VT_VIEW_TEMPLATEPROVIDER = bundleContext.getService(vtViewTemplateProviderServiceReference);
75:         }
76:
77:         /**
78:          * Default constructor.
79:          *
80:          * @param vElement the view model element to be rendered
81:          * @param viewContext the view context
82:          * @param reportService The {@link ReportService}
83:          */
84:         public HexColorSelectionControlSWTRenderer(VControl vElement, ViewModelContext viewContext,
85:                 ReportService reportService) {
86:                 super(vElement, viewContext, reportService, EMFFORMS_DATABINDING, EMFFORMS_LABELPROVIDER,
87:                         VT_VIEW_TEMPLATEPROVIDER);
88:         }
89:
90:         /**
91:          * {@inheritDoc}
92:          *
93:          * @see org.eclipse.emf.ecp.view.spi.core.swt.SimpleControlSWTControlSWTRenderer#createBindings(org.eclipse.swt.widgets.Control,
94:          * org.eclipse.emf.ecore.EStructuralFeature.Setting)
95:          */
96:         @Override
97:         protected Binding[] createBindings(Control control) throws DatabindingFailedException {
98:                 final Composite composite = Composite.class.cast(control);
99:                 final Control childControl = composite.getChildren()[0];
100:                 final IObservableValue value = WidgetProperties.background().observe(childControl);
101:                 final Binding binding = getDataBindingContext().bindValue(value, getModelValue(),
102:                         withPreSetValidation(new UpdateValueStrategy() {
103:                                 @Override
104:                                 public Object convert(Object value) {
105:                                         if (value == null) {
106:                                                 return null;
107:                                         }
108:                                         return getString(Color.class.cast(value).getRGB());
109:                                 }
110:                         }), new UpdateValueStrategy() {
111:                                 @Override
112:                                 public Object convert(Object value) {
113:                                         final String hexString = (String) value;
114:                                         if (hexString == null) {
115:                                                 return null;
116:                                         }
117:                                         final int red = Integer.parseInt(hexString.substring(0, 2), 16);
118:                                         final int green = Integer.parseInt(hexString.substring(2, 4), 16);
119:                                         final int blue = Integer.parseInt(hexString.substring(4, 6), 16);
120:                                         final Color color = new Color(Display.getCurrent(), red, green, blue);
121:                                         return color;
122:                                 }
123:
124:                         });
125:                 final IObservableValue textValue = WidgetProperties.tooltipText().observe(childControl);
126:                 final Binding textBinding = getDataBindingContext().bindValue(textValue, getModelValue());
127:                 return new Binding[] { binding, textBinding };
128:         }
129:
130:         /**
131:          * {@inheritDoc}
132:          *
133:          * @see org.eclipse.emf.ecp.view.spi.core.swt.SimpleControlSWTControlSWTRenderer#createSWTControl(org.eclipse.swt.widgets.Composite)
134:          */
135:         @Override
136:         protected Control createSWTControl(final Composite parent) {
137:                 final Composite composite = new Composite(parent, SWT.NONE);
138:                 GridLayoutFactory.fillDefaults().numColumns(3).equalWidth(false).applyTo(composite);
139:                 final Label colorExample = new Label(composite, SWT.BORDER);
140:                 GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.FILL).grab(false, false).hint(40, SWT.DEFAULT)
141:                         .applyTo(colorExample);
142:                 final Button selectColorBtn = new Button(composite, SWT.PUSH);
143:                 GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.FILL).grab(false, false).applyTo(selectColorBtn);
144:                 selectColorBtn.setText(Messages.HexColorSelectionControlSWTRenderer_SelectColorBtn);
145:                 selectColorBtn.addSelectionListener(new SelectionAdapter() {
146:
147:                         /**
148:                          * {@inheritDoc}
149:                          *
150:                          * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
151:                          */
152:                         @Override
153:                         public void widgetSelected(SelectionEvent e) {
154:                                 super.widgetSelected(e);
155:                                 final ColorDialog cd = new ColorDialog(parent.getShell());
156:                                 final RGB rgb = cd.open();
157:                                 if (rgb != null) {
158:                                         setValue(getString(rgb));
159:                                 }
160:                         }
161:
162:                 });
163:                 final Button unsetColorBtn = new Button(composite, SWT.PUSH);
164:                 GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.FILL).grab(false, false).applyTo(unsetColorBtn);
165:                 unsetColorBtn.setText(Messages.HexColorSelectionControlSWTRenderer_UnsetColorBtn);
166:                 unsetColorBtn.addSelectionListener(new SelectionAdapter() {
167:                         @Override
168:                         public void widgetSelected(SelectionEvent e) {
169:                                 setValue(null);
170:                         }
171:                 });
172:
173:                 return composite;
174:         }
175:
176:         private String getString(RGB rgb) {
177:•                if (rgb == null) {
178:                         return null;
179:                 }
180:                 final String red = Integer.toHexString(0x100 | rgb.red).substring(1);
181:                 final String green = Integer.toHexString(0x100 | rgb.green).substring(1);
182:                 final String blue = Integer.toHexString(0x100 | rgb.blue).substring(1);
183:                 return red + green + blue;
184:         }
185:
186:         private void setValue(String hexColor) {
187:                 IObservableValue observableValue;
188:                 try {
189:                         observableValue = getEMFFormsDatabinding()
190:                                 .getObservableValue(getVElement().getDomainModelReference(), getViewModelContext().getDomainModel());
191:                 } catch (final DatabindingFailedException ex) {
192:                         getReportService().report(new DatabindingFailedReport(ex));
193:                         return;
194:                 }
195:                 final EStructuralFeature structuralFeature = (EStructuralFeature) observableValue.getValueType();
196:                 final EObject eObject = (EObject) ((IObserving) observableValue).getObserved();
197:                 observableValue.dispose();
198:
199:                 final EditingDomain editingDomain = getEditingDomain(eObject);
200:                 final Command command = SetCommand.create(editingDomain, eObject, structuralFeature, hexColor);
201:                 editingDomain.getCommandStack().execute(command);
202:         }
203:
204:         /**
205:          * {@inheritDoc}
206:          *
207:          * @see org.eclipse.emf.ecp.view.spi.core.swt.SimpleControlSWTRenderer#getUnsetText()
208:          */
209:         @Override
210:         protected String getUnsetText() {
211:                 // TODO Auto-generated method stub
212:                 return null;
213:         }
214:
215: }