/******************************************************************************* * Copyright (c) 2016, 2017 Obeo. * 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: * Obeo - initial API and implementation *******************************************************************************/ package org.eclipse.eef.core.internal.controllers; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.function.Consumer; import org.eclipse.core.runtime.IStatus; import org.eclipse.eef.EEFSelectDescription; import org.eclipse.eef.EEFWidgetDescription; import org.eclipse.eef.EefPackage; import org.eclipse.eef.core.api.EEFExpressionUtils; import org.eclipse.eef.core.api.EditingContextAdapter; import org.eclipse.eef.core.api.controllers.AbstractEEFWidgetController; import org.eclipse.eef.core.api.controllers.IEEFSelectController; import org.eclipse.eef.core.api.utils.EvalFactory; import org.eclipse.emf.ecore.EAttribute; import org.eclipse.sirius.common.interpreter.api.IInterpreter; import org.eclipse.sirius.common.interpreter.api.IVariableManager; /** * This class will be used in order to manage the behavior of the combo. * * @author mbats */ public class EEFSelectController extends AbstractEEFWidgetController implements IEEFSelectController { /** * The description. */ private final EEFSelectDescription description; /** * The consumer of a new value of the combo. */ private Consumer<Object> newValueConsumer; /** * The consumer of a new candidates of the combo. */ private Consumer<List<Object>> newCandidatesConsumer; /** * The constructor. * * @param description * The description * @param variableManager * The variable manager * @param interpreter * The interpreter * @param editingContextAdapter * The editing context adapter */ public EEFSelectController(EEFSelectDescription description, IVariableManager variableManager, IInterpreter interpreter, EditingContextAdapter editingContextAdapter) { super(variableManager, interpreter, editingContextAdapter); this.description = description; } @Override public IStatus updateValue(final Object text) { return this.editingContextAdapter.performModelChange(() -> { String editExpression = this.description.getEditExpression(); EAttribute eAttribute = EefPackage.Literals.EEF_SELECT_DESCRIPTION__EDIT_EXPRESSION; Map<String, Object> variables = new HashMap<String, Object>(); variables.putAll(this.variableManager.getVariables()); variables.put(EEFExpressionUtils.EEFText.NEW_VALUE, text); EvalFactory.of(this.interpreter, variables).logIfBlank(eAttribute).call(editExpression); }); } /** * {@inheritDoc} * * @see org.eclipse.eef.core.api.controllers.AbstractEEFWidgetController#refresh() */ @Override public void refresh() { super.refresh(); String candidatesExpression = this.description.getCandidatesExpression(); EAttribute candidatesExpressionEAttribute = EefPackage.Literals.EEF_SELECT_DESCRIPTION__CANDIDATES_EXPRESSION; this.newEval().logIfBlank(candidatesExpressionEAttribute).call(candidatesExpression, (value) -> { if (value instanceof Iterable<?>) { List<Object> candidates = new ArrayList<Object>(); ((Iterable<?>) value).forEach(candidates::add); Optional.ofNullable(this.newCandidatesConsumer).ifPresent(consumer -> { consumer.accept(candidates); }); } }); String valueExpression = this.description.getValueExpression(); Optional.ofNullable(this.newValueConsumer).ifPresent(consumer -> { this.newEval().call(valueExpression, consumer); }); } /** * {@inheritDoc} * * @see org.eclipse.eef.core.api.controllers.IEEFTextController#onNewValue(java.util.function.Consumer) */ @Override public void onNewValue(Consumer<Object> consumer) { this.newValueConsumer = consumer; } /** * {@inheritDoc} * * @see org.eclipse.eef.core.api.controllers.IEEFTextController#onNewValue(java.util.function.Consumer) */ @Override public void onNewCandidates(Consumer<List<Object>> consumer) { this.newCandidatesConsumer = consumer; } /** * {@inheritDoc} * * @see org.eclipse.eef.core.api.controllers.IEEFSelectController#removeNewValueConsumer() */ @Override public void removeNewValueConsumer() { this.newValueConsumer = null; } /** * {@inheritDoc} * * @see org.eclipse.eef.core.api.controllers.IEEFSelectController#removeNewCandidatesConsumer() */ @Override public void removeNewCandidatesConsumer() { this.newCandidatesConsumer = null; } /** * {@inheritDoc} * * @see org.eclipse.eef.core.api.controllers.AbstractEEFWidgetController#getDescription() */ @Override protected EEFWidgetDescription getDescription() { return this.description; } }