| /******************************************************************************* |
| * 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.ui.composites; |
| |
| import org.eclipse.apogy.addons.ApogyAddonsPackage; |
| import org.eclipse.apogy.addons.SimpleTool; |
| import org.eclipse.apogy.common.emf.transaction.ApogyCommonTransactionFacade; |
| import org.eclipse.apogy.common.emf.ui.emfforms.ApogyCommonEMFUiEMFFormsFacade; |
| import org.eclipse.core.databinding.DataBindingContext; |
| import org.eclipse.core.databinding.UpdateValueStrategy; |
| import org.eclipse.core.databinding.observable.value.IObservableValue; |
| import org.eclipse.emf.databinding.EMFProperties; |
| import org.eclipse.emf.databinding.FeaturePath; |
| import org.eclipse.jface.databinding.swt.WidgetProperties; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.events.DisposeEvent; |
| import org.eclipse.swt.events.DisposeListener; |
| import org.eclipse.swt.events.SelectionEvent; |
| import org.eclipse.swt.events.SelectionListener; |
| import org.eclipse.swt.layout.GridData; |
| import org.eclipse.swt.layout.GridLayout; |
| import org.eclipse.swt.widgets.Button; |
| import org.eclipse.swt.widgets.Composite; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class SimpleToolsDetailsComposite extends Composite { |
| private static final Logger Logger = LoggerFactory.getLogger(SimpleToolsDetailsComposite.class); |
| |
| private final Button btnActivate; |
| private final Button btnDeactivate; |
| private final Composite compositeDetails; |
| |
| private DataBindingContext m_bindingContext; |
| |
| private SimpleTool simpleTool; |
| |
| public SimpleToolsDetailsComposite(Composite parent, int style) { |
| super(parent, style); |
| setLayout(new GridLayout(2, false)); |
| |
| this.btnActivate = new Button(this, SWT.NONE); |
| this.btnActivate.setText("Activate"); |
| this.btnActivate.setEnabled(false); |
| this.btnActivate.addSelectionListener(new SelectionListener() { |
| |
| @Override |
| public void widgetSelected(SelectionEvent e) { |
| if (getSimpleTool() != null) { |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(getSimpleTool(), |
| ApogyAddonsPackage.Literals.SIMPLE_TOOL__ACTIVE, true); |
| } |
| } |
| |
| @Override |
| public void widgetDefaultSelected(SelectionEvent e) { |
| } |
| }); |
| |
| this.btnDeactivate = new Button(this, SWT.NONE); |
| this.btnDeactivate.setText("De-Activate"); |
| this.btnDeactivate.setEnabled(false); |
| this.btnDeactivate.addSelectionListener(new SelectionListener() { |
| |
| @Override |
| public void widgetSelected(SelectionEvent e) { |
| if (getSimpleTool() != null) { |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(getSimpleTool(), |
| ApogyAddonsPackage.Literals.SIMPLE_TOOL__ACTIVE, false); |
| } |
| } |
| |
| @Override |
| public void widgetDefaultSelected(SelectionEvent e) { |
| } |
| }); |
| |
| this.compositeDetails = new Composite(this, SWT.BORDER); |
| this.compositeDetails.setLayout(new GridLayout()); |
| GridData gd_compositeDetails = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1); |
| gd_compositeDetails.minimumWidth = 400; |
| gd_compositeDetails.widthHint = 400; |
| this.compositeDetails.setLayoutData(gd_compositeDetails); |
| this.compositeDetails.layout(); |
| |
| this.m_bindingContext = customInitDataBindings(); |
| |
| // Dispose |
| addDisposeListener(new DisposeListener() { |
| @Override |
| public void widgetDisposed(DisposeEvent e) { |
| if (SimpleToolsDetailsComposite.this.m_bindingContext != null) |
| SimpleToolsDetailsComposite.this.m_bindingContext.dispose(); |
| |
| } |
| }); |
| } |
| |
| public SimpleTool getSimpleTool() { |
| return this.simpleTool; |
| } |
| |
| public void setSimpleTool(SimpleTool simpleTool) { |
| // Dispose of current bindings if applicable. |
| if (this.m_bindingContext != null) { |
| this.m_bindingContext.dispose(); |
| } |
| |
| this.simpleTool = simpleTool; |
| |
| if (simpleTool != null) { |
| // Updates bindings |
| this.m_bindingContext = customInitDataBindings(); |
| |
| try { |
| ApogyCommonEMFUiEMFFormsFacade.INSTANCE.createEMFForms(this.compositeDetails, simpleTool); |
| } catch (Throwable t) { |
| Logger.error(t.getMessage(), t); |
| } |
| } |
| } |
| |
| @SuppressWarnings("unchecked") |
| protected DataBindingContext customInitDataBindings() { |
| DataBindingContext bindingContext = new DataBindingContext(); |
| |
| IObservableValue<?> simpleToolObservable = EMFProperties |
| .value(FeaturePath.fromList(ApogyAddonsPackage.Literals.SIMPLE_TOOL__ACTIVE)).observe(getSimpleTool()); |
| |
| /* Activate Button Enabled Binding. */ |
| IObservableValue<?> observeEnabledBtnActivateWidget = WidgetProperties.enabled().observe(this.btnActivate); |
| UpdateValueStrategy btnActivateEnableStrategy = new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE) { |
| @Override |
| public Object convert(Object value) { |
| return !((Boolean) value).booleanValue(); |
| } |
| }; |
| |
| bindingContext.bindValue(observeEnabledBtnActivateWidget, simpleToolObservable, |
| new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), btnActivateEnableStrategy); |
| |
| /* Activate Button Enabled Binding. */ |
| IObservableValue<?> observeEnabledBtnDectivateWidget = WidgetProperties.enabled().observe(this.btnDeactivate); |
| UpdateValueStrategy btnDeactivateEnableStrategy = new UpdateValueStrategy(); |
| bindingContext.bindValue(observeEnabledBtnDectivateWidget, simpleToolObservable, |
| new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), btnDeactivateEnableStrategy); |
| |
| return bindingContext; |
| } |
| } |