| /******************************************************************************* |
| * Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Pierre Allard - initial API and implementation |
| * Regent L'Archeveque |
| * |
| * SPDX-License-Identifier: EPL-1.0 |
| *******************************************************************************/ |
| package org.eclipse.apogy.addons.sensors.imaging.camera.composites; |
| |
| import org.eclipse.apogy.addons.sensors.imaging.camera.ApogyAddonsSensorsImagingCameraPackage; |
| import org.eclipse.apogy.addons.sensors.imaging.camera.ImageSizePolicy; |
| import org.eclipse.apogy.addons.sensors.imaging.camera.URLImageOverlay; |
| import org.eclipse.apogy.common.emf.transaction.ApogyCommonTransactionFacade; |
| import org.eclipse.apogy.common.images.ApogyCommonImagesFactory; |
| import org.eclipse.apogy.common.images.EImagesUtilities; |
| import org.eclipse.apogy.common.images.URLEImage; |
| import org.eclipse.apogy.common.images.ui.composites.ImageDisplayComposite; |
| import org.eclipse.apogy.common.ui.composites.URLSelectionComposite; |
| import org.eclipse.core.databinding.DataBindingContext; |
| import org.eclipse.core.databinding.UpdateValueStrategy; |
| import org.eclipse.core.databinding.observable.value.IObservableValue; |
| import org.eclipse.emf.common.notify.AdapterFactory; |
| import org.eclipse.emf.databinding.FeaturePath; |
| import org.eclipse.emf.databinding.edit.EMFEditProperties; |
| import org.eclipse.emf.edit.provider.ComposedAdapterFactory; |
| import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; |
| import org.eclipse.jface.databinding.viewers.ViewerProperties; |
| import org.eclipse.jface.viewers.ArrayContentProvider; |
| import org.eclipse.jface.viewers.ComboViewer; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.events.DisposeEvent; |
| import org.eclipse.swt.events.DisposeListener; |
| import org.eclipse.swt.graphics.ImageData; |
| import org.eclipse.swt.layout.GridData; |
| import org.eclipse.swt.layout.GridLayout; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Event; |
| import org.eclipse.swt.widgets.Group; |
| import org.eclipse.swt.widgets.Label; |
| import org.eclipse.swt.widgets.Listener; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class URLImageOverlayComposite extends Composite { |
| private static final Logger Logger = LoggerFactory.getLogger(URLImageOverlayComposite.class); |
| |
| private URLImageOverlay urlImageOverlay; |
| |
| private final AdapterFactory adapterFactory = new ComposedAdapterFactory( |
| ComposedAdapterFactory.Descriptor.Registry.INSTANCE); |
| |
| private final ComboViewer imageSizePolicyComboViewer; |
| private final URLSelectionComposite urlSelectionComposite; |
| |
| private final Label imageWidthValue; |
| private final Label imageHeightValue; |
| private final ImageDisplayComposite imageDisplayComposite; |
| |
| private DataBindingContext m_bindingContext; |
| |
| public URLImageOverlayComposite(Composite parent, int style) { |
| super(parent, style); |
| setLayout(new GridLayout(2, false)); |
| |
| Label lblImageSizePolicy = new Label(this, SWT.NONE); |
| lblImageSizePolicy.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); |
| lblImageSizePolicy.setText("Image Resize Policy :"); |
| |
| this.imageSizePolicyComboViewer = new ComboViewer(this, SWT.NONE); |
| GridData gd_imageSizePolicyComboViewer = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1); |
| gd_imageSizePolicyComboViewer.minimumWidth = 200; |
| gd_imageSizePolicyComboViewer.widthHint = 200; |
| this.imageSizePolicyComboViewer.getCombo().setLayoutData(gd_imageSizePolicyComboViewer); |
| this.imageSizePolicyComboViewer.setContentProvider(new ArrayContentProvider()); |
| this.imageSizePolicyComboViewer.setLabelProvider(new AdapterFactoryLabelProvider(this.adapterFactory)); |
| this.imageSizePolicyComboViewer.setInput(ImageSizePolicy.values()); |
| |
| this.urlSelectionComposite = new URLSelectionComposite(this, SWT.None, |
| new String[] { "*.gif", "*.jpg", "*.jpeg" }, true, true, true) { |
| @Override |
| protected void urlStringSelected(String newURLString) { |
| if (URLImageOverlayComposite.this.urlImageOverlay != null) { |
| if (ApogyCommonTransactionFacade.INSTANCE |
| .getTransactionalEditingDomain(URLImageOverlayComposite.this.urlImageOverlay) == null) { |
| ApogyCommonTransactionFacade.INSTANCE |
| .addInTempTransactionalEditingDomain(URLImageOverlayComposite.this.urlImageOverlay); |
| } |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(URLImageOverlayComposite.this.urlImageOverlay, |
| ApogyAddonsSensorsImagingCameraPackage.Literals.URL_IMAGE_OVERLAY__URL, newURLString); |
| |
| URLImageOverlayComposite.this.updateImagePreview(newURLString); |
| |
| newURLSelected(newURLString); |
| } |
| } |
| }; |
| this.urlSelectionComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); |
| |
| Group grpImagePreview = new Group(this, SWT.BORDER); |
| grpImagePreview.setText("Image Preview"); |
| grpImagePreview.setLayout(new GridLayout(2, false)); |
| grpImagePreview.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1)); |
| |
| Composite imageSizeComposite = new Composite(grpImagePreview, SWT.NONE); |
| imageSizeComposite.setLayout(new GridLayout(2, false)); |
| imageSizeComposite.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1)); |
| |
| Label lblimageWidthInMeters = new Label(imageSizeComposite, SWT.NONE); |
| lblimageWidthInMeters.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); |
| lblimageWidthInMeters.setText("Image width (pixels):"); |
| |
| this.imageWidthValue = new Label(imageSizeComposite, SWT.BORDER); |
| GridData gd_imageWidthValue = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1); |
| gd_imageWidthValue.minimumWidth = 100; |
| gd_imageWidthValue.widthHint = 100; |
| this.imageWidthValue.setLayoutData(gd_imageWidthValue); |
| |
| Label lblimageHeightInMeters = new Label(imageSizeComposite, SWT.NONE); |
| lblimageHeightInMeters.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); |
| lblimageHeightInMeters.setText("Image height (pixels):"); |
| |
| this.imageHeightValue = new Label(imageSizeComposite, SWT.BORDER); |
| GridData gd_imageHeightValue = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1); |
| gd_imageHeightValue.minimumWidth = 100; |
| gd_imageHeightValue.widthHint = 100; |
| this.imageHeightValue.setLayoutData(gd_imageHeightValue); |
| new Label(grpImagePreview, SWT.NONE); |
| |
| this.imageDisplayComposite = new ImageDisplayComposite(grpImagePreview, SWT.BORDER); |
| GridData gd_imageDisplayComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1); |
| gd_imageDisplayComposite.heightHint = 200; |
| gd_imageDisplayComposite.minimumHeight = 200; |
| gd_imageDisplayComposite.widthHint = 200; |
| gd_imageDisplayComposite.minimumWidth = 200; |
| this.imageDisplayComposite.setLayoutData(gd_imageDisplayComposite); |
| this.imageDisplayComposite.addListener(SWT.Resize, new Listener() { |
| @Override |
| public void handleEvent(Event event) { |
| URLImageOverlayComposite.this.imageDisplayComposite.fitImage(); |
| } |
| }); |
| |
| // Dispose |
| addDisposeListener(new DisposeListener() { |
| @Override |
| public void widgetDisposed(DisposeEvent e) { |
| if (URLImageOverlayComposite.this.m_bindingContext != null) |
| URLImageOverlayComposite.this.m_bindingContext.dispose(); |
| |
| } |
| }); |
| } |
| |
| public URLImageOverlay getUrlImageOverlay() { |
| return this.urlImageOverlay; |
| } |
| |
| public void setUrlImageOverlay(URLImageOverlay urlImageOverlay) { |
| if (this.m_bindingContext != null) |
| this.m_bindingContext.dispose(); |
| |
| this.urlImageOverlay = urlImageOverlay; |
| |
| if (urlImageOverlay != null) { |
| if (ApogyCommonTransactionFacade.INSTANCE.getTransactionalEditingDomain(urlImageOverlay) == null) { |
| ApogyCommonTransactionFacade.INSTANCE.addInTempTransactionalEditingDomain(urlImageOverlay); |
| } |
| |
| this.m_bindingContext = customInitDataBindings(); |
| } |
| } |
| |
| protected void newURLSelected(String newURL) { |
| } |
| |
| protected void updateImagePreview(String urlString) { |
| try { |
| // Update preview image. |
| URLEImage urlEImage = ApogyCommonImagesFactory.eINSTANCE.createURLEImage(); |
| urlEImage.setUrl(urlString); |
| |
| this.imageWidthValue.setText(Integer.toString(urlEImage.getWidth())); |
| this.imageHeightValue.setText(Integer.toString(urlEImage.getHeight())); |
| |
| ImageData imageData = EImagesUtilities.INSTANCE.convertToImageData(urlEImage.asBufferedImage()); |
| |
| this.imageDisplayComposite.setImageData(imageData); |
| this.imageDisplayComposite.fitImage(); |
| } catch (Exception e) { |
| this.imageDisplayComposite.setImageData(null); |
| this.imageWidthValue.setText("0"); |
| this.imageHeightValue.setText("0"); |
| |
| Logger.error(e.getMessage(), e); |
| } |
| } |
| |
| @SuppressWarnings("unchecked") |
| protected DataBindingContext customInitDataBindings() { |
| DataBindingContext bindingContext = new DataBindingContext(); |
| |
| if (this.urlImageOverlay.getUrl() != null) { |
| this.urlSelectionComposite.setUrlString(this.urlImageOverlay.getUrl()); |
| } |
| |
| /* Image Resize Policy. */ |
| IObservableValue<Double> observeImageSizePolicy = EMFEditProperties |
| .value(ApogyCommonTransactionFacade.INSTANCE.getDefaultEditingDomain(), |
| FeaturePath.fromList( |
| ApogyAddonsSensorsImagingCameraPackage.Literals.URL_IMAGE_OVERLAY__IMAGE_SIZE_POLICY)) |
| .observe(this.urlImageOverlay); |
| |
| IObservableValue<?> observeImageSizePolicyCombViewer = ViewerProperties.singleSelection() |
| .observe(this.imageSizePolicyComboViewer); |
| bindingContext.bindValue(observeImageSizePolicyCombViewer, observeImageSizePolicy, new UpdateValueStrategy(), |
| new UpdateValueStrategy()); |
| |
| return bindingContext; |
| } |
| } |