| /******************************************************************************* |
| * Copyright (c) 2015, 2016 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.HashMap; |
| import java.util.Map; |
| |
| import org.eclipse.eef.EEFTextDescription; |
| 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.IConsumer; |
| import org.eclipse.eef.core.api.controllers.IEEFTextController; |
| 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 text. |
| * |
| * @author sbegaudeau |
| */ |
| public class EEFTextController extends AbstractEEFWidgetController implements IEEFTextController { |
| /** |
| * The description. |
| */ |
| private EEFTextDescription description; |
| |
| /** |
| * The editing context adapter. |
| */ |
| private EditingContextAdapter contextAdapter; |
| |
| /** |
| * The consumer of a new value of the text. |
| */ |
| private IConsumer<Object> newValueConsumer; |
| |
| /** |
| * The constructor. |
| * |
| * @param description |
| * The description |
| * @param variableManager |
| * The variable manager |
| * @param interpreter |
| * The interpreter |
| * @param contextAdapter |
| * The editing context adapter |
| */ |
| public EEFTextController(EEFTextDescription description, IVariableManager variableManager, IInterpreter interpreter, |
| EditingContextAdapter contextAdapter) { |
| super(variableManager, interpreter); |
| this.description = description; |
| this.contextAdapter = contextAdapter; |
| } |
| |
| @Override |
| public void updateValue(final String text) { |
| this.contextAdapter.performModelChange(new Runnable() { |
| @Override |
| public void run() { |
| String editExpression = EEFTextController.this.description.getEditExpression(); |
| EAttribute eAttribute = EefPackage.Literals.EEF_TEXT_DESCRIPTION__EDIT_EXPRESSION; |
| |
| Map<String, Object> variables = new HashMap<String, Object>(); |
| variables.putAll(EEFTextController.this.variableManager.getVariables()); |
| variables.put(EEFExpressionUtils.EEFText.NEW_VALUE, text); |
| |
| EvalFactory.of(EEFTextController.this.interpreter, variables).logIfBlank(eAttribute).call(editExpression); |
| } |
| }); |
| } |
| |
| /** |
| * {@inheritDoc} |
| * |
| * @see org.eclipse.eef.core.api.controllers.AbstractEEFWidgetController#refresh() |
| */ |
| @Override |
| public void refresh() { |
| super.refresh(); |
| |
| String valueExpression = this.description.getValueExpression(); |
| this.newEval().call(valueExpression, this.newValueConsumer); |
| } |
| |
| /** |
| * {@inheritDoc} |
| * |
| * @see org.eclipse.eef.core.api.controllers.IEEFTextController#onNewValue(org.eclipse.eef.core.api.controllers.IConsumer) |
| */ |
| @Override |
| public void onNewValue(IConsumer<Object> consumer) { |
| this.newValueConsumer = consumer; |
| } |
| |
| /** |
| * {@inheritDoc} |
| * |
| * @see org.eclipse.eef.core.api.controllers.IEEFTextController#removeNewValueConsumer() |
| */ |
| @Override |
| public void removeNewValueConsumer() { |
| this.newValueConsumer = null; |
| } |
| |
| /** |
| * {@inheritDoc} |
| * |
| * @see org.eclipse.eef.core.api.controllers.AbstractEEFWidgetController#getDescription() |
| */ |
| @Override |
| protected EEFWidgetDescription getDescription() { |
| return this.description; |
| } |
| |
| } |