| /******************************************************************************* |
| * Copyright (c) 2011-2014 EclipseSource Muenchen GmbH and others. |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Eugen - initial API and implementation |
| ******************************************************************************/ |
| package org.eclipse.emf.ecp.view.template.internal.tooling.controls; |
| |
| import org.eclipse.core.databinding.Binding; |
| import org.eclipse.core.databinding.UpdateValueStrategy; |
| import org.eclipse.core.databinding.observable.IObserving; |
| import org.eclipse.core.databinding.observable.value.IObservableValue; |
| import org.eclipse.emf.common.command.Command; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.EStructuralFeature; |
| import org.eclipse.emf.ecp.view.spi.context.ViewModelContext; |
| import org.eclipse.emf.ecp.view.spi.core.swt.SimpleControlSWTControlSWTRenderer; |
| import org.eclipse.emf.ecp.view.spi.model.VControl; |
| import org.eclipse.emf.ecp.view.template.internal.tooling.Messages; |
| import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider; |
| import org.eclipse.emf.edit.command.SetCommand; |
| import org.eclipse.emf.edit.domain.EditingDomain; |
| import org.eclipse.emfforms.spi.common.report.ReportService; |
| import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException; |
| import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedReport; |
| import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding; |
| import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider; |
| import org.eclipse.jface.databinding.swt.typed.WidgetProperties; |
| import org.eclipse.jface.layout.GridDataFactory; |
| import org.eclipse.jface.layout.GridLayoutFactory; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.events.SelectionAdapter; |
| import org.eclipse.swt.events.SelectionEvent; |
| import org.eclipse.swt.graphics.Color; |
| import org.eclipse.swt.graphics.RGB; |
| import org.eclipse.swt.widgets.Button; |
| import org.eclipse.swt.widgets.ColorDialog; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Control; |
| import org.eclipse.swt.widgets.Display; |
| import org.eclipse.swt.widgets.Label; |
| import org.osgi.framework.BundleContext; |
| import org.osgi.framework.FrameworkUtil; |
| import org.osgi.framework.ServiceReference; |
| |
| /** |
| * @author Eugen |
| * |
| */ |
| public class HexColorSelectionControlSWTRenderer extends SimpleControlSWTControlSWTRenderer { |
| |
| private static final EMFFormsDatabinding EMFFORMS_DATABINDING; |
| private static final EMFFormsLabelProvider EMFFORMS_LABELPROVIDER; |
| private static final VTViewTemplateProvider VT_VIEW_TEMPLATEPROVIDER; |
| |
| static { |
| final BundleContext bundleContext = FrameworkUtil.getBundle(HexColorSelectionControlSWTRenderer.class) |
| .getBundleContext(); |
| final ServiceReference<EMFFormsDatabinding> emfFormsDatabindingServiceReference = bundleContext |
| .getServiceReference(EMFFormsDatabinding.class); |
| EMFFORMS_DATABINDING = bundleContext.getService(emfFormsDatabindingServiceReference); |
| final ServiceReference<EMFFormsLabelProvider> emfFormsLabelProviderServiceReference = bundleContext |
| .getServiceReference(EMFFormsLabelProvider.class); |
| EMFFORMS_LABELPROVIDER = bundleContext.getService(emfFormsLabelProviderServiceReference); |
| final ServiceReference<VTViewTemplateProvider> vtViewTemplateProviderServiceReference = bundleContext |
| .getServiceReference(VTViewTemplateProvider.class); |
| VT_VIEW_TEMPLATEPROVIDER = bundleContext.getService(vtViewTemplateProviderServiceReference); |
| } |
| |
| /** |
| * Default constructor. |
| * |
| * @param vElement the view model element to be rendered |
| * @param viewContext the view context |
| * @param reportService The {@link ReportService} |
| */ |
| public HexColorSelectionControlSWTRenderer(VControl vElement, ViewModelContext viewContext, |
| ReportService reportService) { |
| super(vElement, viewContext, reportService, EMFFORMS_DATABINDING, EMFFORMS_LABELPROVIDER, |
| VT_VIEW_TEMPLATEPROVIDER); |
| } |
| |
| @Override |
| protected Binding[] createBindings(Control control) throws DatabindingFailedException { |
| final Composite composite = Composite.class.cast(control); |
| final Control childControl = composite.getChildren()[0]; |
| final IObservableValue<Color> value = WidgetProperties.background().observe(childControl); |
| final Binding binding = getDataBindingContext().bindValue(value, getModelValue(), |
| withPreSetValidation(new UpdateValueStrategy<Color, String>() { |
| @Override |
| public String convert(Color value) { |
| if (value == null) { |
| return null; |
| } |
| return getString(value.getRGB()); |
| } |
| }), new UpdateValueStrategy<String, Color>() { |
| @Override |
| public Color convert(String value) { |
| final String hexString = value; |
| if (hexString == null) { |
| return null; |
| } |
| final int red = Integer.parseInt(hexString.substring(0, 2), 16); |
| final int green = Integer.parseInt(hexString.substring(2, 4), 16); |
| final int blue = Integer.parseInt(hexString.substring(4, 6), 16); |
| final Color color = new Color(Display.getCurrent(), red, green, blue); |
| return color; |
| } |
| |
| }); |
| final IObservableValue textValue = WidgetProperties.tooltipText().observe(childControl); |
| final Binding textBinding = getDataBindingContext().bindValue(textValue, getModelValue()); |
| return new Binding[] { binding, textBinding }; |
| } |
| |
| @Override |
| protected Control createSWTControl(final Composite parent) { |
| final Composite composite = new Composite(parent, SWT.NONE); |
| GridLayoutFactory.fillDefaults().numColumns(3).equalWidth(false).applyTo(composite); |
| final Label colorExample = new Label(composite, SWT.BORDER); |
| GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.FILL).grab(false, false).hint(40, SWT.DEFAULT) |
| .applyTo(colorExample); |
| final Button selectColorBtn = new Button(composite, SWT.PUSH); |
| GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.FILL).grab(false, false).applyTo(selectColorBtn); |
| selectColorBtn.setText(Messages.HexColorSelectionControlSWTRenderer_SelectColorBtn); |
| selectColorBtn.addSelectionListener(new SelectionAdapter() { |
| |
| @Override |
| public void widgetSelected(SelectionEvent e) { |
| super.widgetSelected(e); |
| final ColorDialog cd = new ColorDialog(parent.getShell()); |
| final RGB rgb = cd.open(); |
| if (rgb != null) { |
| setValue(getString(rgb)); |
| } |
| } |
| |
| }); |
| final Button unsetColorBtn = new Button(composite, SWT.PUSH); |
| GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.FILL).grab(false, false).applyTo(unsetColorBtn); |
| unsetColorBtn.setText(Messages.HexColorSelectionControlSWTRenderer_UnsetColorBtn); |
| unsetColorBtn.addSelectionListener(new SelectionAdapter() { |
| @Override |
| public void widgetSelected(SelectionEvent e) { |
| setValue(null); |
| } |
| }); |
| |
| return composite; |
| } |
| |
| private String getString(RGB rgb) { |
| if (rgb == null) { |
| return null; |
| } |
| final String red = Integer.toHexString(0x100 | rgb.red).substring(1); |
| final String green = Integer.toHexString(0x100 | rgb.green).substring(1); |
| final String blue = Integer.toHexString(0x100 | rgb.blue).substring(1); |
| return red + green + blue; |
| } |
| |
| private void setValue(String hexColor) { |
| IObservableValue observableValue; |
| try { |
| observableValue = getEMFFormsDatabinding() |
| .getObservableValue(getVElement().getDomainModelReference(), getViewModelContext().getDomainModel()); |
| } catch (final DatabindingFailedException ex) { |
| getReportService().report(new DatabindingFailedReport(ex)); |
| return; |
| } |
| final EStructuralFeature structuralFeature = (EStructuralFeature) observableValue.getValueType(); |
| final EObject eObject = (EObject) ((IObserving) observableValue).getObserved(); |
| observableValue.dispose(); |
| |
| final EditingDomain editingDomain = getEditingDomain(eObject); |
| final Command command = SetCommand.create(editingDomain, eObject, structuralFeature, hexColor); |
| editingDomain.getCommandStack().execute(command); |
| } |
| |
| @Override |
| protected String getUnsetText() { |
| return null; |
| } |
| |
| } |