diff options
author | Stéphane Bégaudeau | 2016-05-10 15:04:20 +0000 |
---|---|---|
committer | Stéphane Bégaudeau | 2016-05-10 15:04:20 +0000 |
commit | 332ecc3653851b81b1c36dfcb265caa1b2ad2ca6 (patch) | |
tree | 9adf77cd35dd7a9324218bd0f40c0f6f54dc9d0a | |
parent | 180172ee2da000ab508ba6a9a5e460e51d219bcc (diff) | |
download | org.eclipse.eef-332ecc3653851b81b1c36dfcb265caa1b2ad2ca6.tar.gz org.eclipse.eef-332ecc3653851b81b1c36dfcb265caa1b2ad2ca6.tar.xz org.eclipse.eef-332ecc3653851b81b1c36dfcb265caa1b2ad2ca6.zip |
Improve the reference lifecycle manager
1) The EEFReferenceLifecycleManager has been divided into two separated
classes, the EEFReferenceLifecycleManager for monovalued references and
the EEFMultipleReferencesLifecycleManager for multivalued references
2) The onClickExpression is now properly triggered when the user click
on the table for multivalued references
3) A new variable is available for the onClickExpression of a reference
widget. This variable named onClickEventKind will indicate if the user
has realized a single click or a double click
Change-Id: I7fddd2432c094de063390126c321fe7f74a7fd86
Signed-off-by: Stéphane Bégaudeau <stephane.begaudeau@obeo.fr>
9 files changed, 647 insertions, 199 deletions
diff --git a/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/api/EEFExpressionUtils.java b/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/api/EEFExpressionUtils.java index fe79c3c08..0a086b86a 100644 --- a/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/api/EEFExpressionUtils.java +++ b/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/api/EEFExpressionUtils.java @@ -108,6 +108,21 @@ public final class EEFExpressionUtils { public static final String SELECTION = "selection"; //$NON-NLS-1$ /** + * The name of the variable used to indicate the kind of click event. + */ + public static final String ON_CLICK_EVENT_KIND = "onClickEventKind"; //$NON-NLS-1$ + + /** + * The value used to indicate a single click. + */ + public static final String SINGLE_CLICK = "SINGLE_CLICK"; //$NON-NLS-1$ + + /** + * The value used to indicate a double click. + */ + public static final String DOUBLE_CLICK = "DOUBLE_CLICK"; //$NON-NLS-1$ + + /** * The constructor. */ private EEFReference() { diff --git a/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/api/controllers/IEEFReferenceController.java b/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/api/controllers/IEEFReferenceController.java index 5b8c5f1f2..30ddd5803 100644 --- a/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/api/controllers/IEEFReferenceController.java +++ b/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/api/controllers/IEEFReferenceController.java @@ -39,8 +39,10 @@ public interface IEEFReferenceController extends IEEFWidgetController { * * @param element * Semantic element + * @param onClickEventKind + * The kind of event (single click or double click) */ - void onClick(Object element); + void onClick(Object element, String onClickEventKind); /** * Invoked when the user clicks on an action button. diff --git a/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/controllers/EEFReferenceController.java b/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/controllers/EEFReferenceController.java index 2d66449ad..a1988700d 100644 --- a/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/controllers/EEFReferenceController.java +++ b/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/controllers/EEFReferenceController.java @@ -92,7 +92,7 @@ public class EEFReferenceController extends AbstractEEFWidgetController implemen } @Override - public void onClick(final Object element) { + public void onClick(final Object element, final String onClickEventKind) { contextAdapter.performModelChange(new Runnable() { @Override public void run() { @@ -102,6 +102,7 @@ public class EEFReferenceController extends AbstractEEFWidgetController implemen Map<String, Object> variables = new HashMap<String, Object>(); variables.putAll(EEFReferenceController.this.variableManager.getVariables()); variables.put(EEFExpressionUtils.EEFReference.SELECTION, element); + variables.put(EEFExpressionUtils.EEFReference.ON_CLICK_EVENT_KIND, onClickEventKind); EvalFactory.of(EEFReferenceController.this.interpreter, variables).logIfBlank(attr).call(expression); } diff --git a/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFControlSwitch.java b/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFControlSwitch.java index 55d2e7e13..f6ac3f641 100644 --- a/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFControlSwitch.java +++ b/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFControlSwitch.java @@ -198,8 +198,15 @@ public class EEFControlSwitch { } else if (widgetDescription instanceof EEFReferenceDescription) { EEFReferenceDescription eefReferenceDescription = (EEFReferenceDescription) widgetDescription; - EEFReferenceLifecycleManager eefReferenceLifecycleManager = new EEFReferenceLifecycleManager(eefReferenceDescription, - childVariableManager, interpreter, contextAdapter); + IEEFLifecycleManager eefReferenceLifecycleManager = null; + if (eefReferenceDescription.isMultiple()) { + eefReferenceLifecycleManager = new EEFMultipleReferencesLifecycleManager(eefReferenceDescription, childVariableManager, + interpreter, contextAdapter); + } else { + eefReferenceLifecycleManager = new EEFReferenceLifecycleManager(eefReferenceDescription, childVariableManager, interpreter, + contextAdapter); + } + eefReferenceLifecycleManager.createControl(parent, formContainer); lifecycleManagers.add(eefReferenceLifecycleManager); diff --git a/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFMultipleReferencesLifecycleManager.java b/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFMultipleReferencesLifecycleManager.java new file mode 100644 index 000000000..d93851d4e --- /dev/null +++ b/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFMultipleReferencesLifecycleManager.java @@ -0,0 +1,345 @@ +/******************************************************************************* + * Copyright (c) 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.ide.ui.internal.widgets; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.eef.EEFReferenceDescription; +import org.eclipse.eef.EEFWidgetAction; +import org.eclipse.eef.EEFWidgetDescription; +import org.eclipse.eef.common.ui.api.EEFWidgetFactory; +import org.eclipse.eef.common.ui.api.IEEFFormContainer; +import org.eclipse.eef.core.api.EditingContextAdapter; +import org.eclipse.eef.core.api.controllers.EEFControllersFactory; +import org.eclipse.eef.core.api.controllers.IConsumer; +import org.eclipse.eef.core.api.controllers.IEEFReferenceController; +import org.eclipse.eef.core.api.controllers.IEEFWidgetController; +import org.eclipse.eef.ide.ui.api.widgets.AbstractEEFWidgetLifecycleManager; +import org.eclipse.jface.viewers.ArrayContentProvider; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.sirius.common.interpreter.api.IInterpreter; +import org.eclipse.sirius.common.interpreter.api.IVariableManager; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.ScrolledComposite; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.layout.RowLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Table; + +/** + * This class will be used in order to manager the lifecycle of a multiple references widget. + * + * @author sbegaudeau + */ +public class EEFMultipleReferencesLifecycleManager extends AbstractEEFWidgetLifecycleManager { + /** + * Default height. + */ + private static final int DEFAULT_HEIGHT = 34; + + /** + * Minimal height of the table widget. + */ + private static final int TABLE_MINIMAL_HEIGHT = 100; + + /** + * The description. + */ + private EEFReferenceDescription description; + + /** + * The widget factory. + */ + private EEFWidgetFactory widgetFactory; + + /** + * The action buttons. + */ + private List<ActionButton> actionButtons = new ArrayList<ActionButton>(); + + /** + * The default background color of the text field. + */ + private Color defaultBackgroundColor; + + /** + * The controller. + */ + private IEEFReferenceController controller; + + /** + * The table viewer used to display the references. + */ + private TableViewer tableViewer; + + /** + * The listener used to run the onClick expression when the user will click on the table. + */ + private SelectionListener tableSelectionListener; + + /** + * The constructor. + * + * @param description + * The description + * @param variableManager + * The variable manager + * @param interpreter + * The interpreter + * @param contextAdapter + * The editing context adapter + */ + public EEFMultipleReferencesLifecycleManager(EEFReferenceDescription description, IVariableManager variableManager, IInterpreter interpreter, + EditingContextAdapter contextAdapter) { + super(variableManager, interpreter, contextAdapter); + this.description = description; + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.eef.ide.ui.api.widgets.AbstractEEFWidgetLifecycleManager#createMainControl(org.eclipse.swt.widgets.Composite, + * org.eclipse.eef.common.ui.api.IEEFFormContainer) + */ + @Override + protected void createMainControl(Composite parent, IEEFFormContainer formContainer) { + widgetFactory = formContainer.getWidgetFactory(); + defaultBackgroundColor = parent.getBackground(); + + // this is the parent composite + Composite reference = widgetFactory.createFlatFormComposite(parent); + GridLayout layout = new GridLayout(2, false); + reference.setLayout(layout); + + GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false); + reference.setLayoutData(gridData); + + this.createMultipleValuedReferenceWidget(reference); + this.createWidgetActionButtons(reference); + + widgetFactory.paintBordersFor(parent); + + this.controller = new EEFControllersFactory().createReferenceController(this.description, this.variableManager, this.interpreter, + this.contextAdapter); + } + + /** + * Create table widget. + * + * @param parent + * The parent composite + */ + private void createMultipleValuedReferenceWidget(Composite parent) { + ScrolledComposite scrolledComposite = widgetFactory.createScrolledComposite(parent, SWT.NONE); + GridData gridData = new GridData(); + gridData.grabExcessHorizontalSpace = true; + gridData.horizontalAlignment = SWT.FILL; + scrolledComposite.setLayoutData(gridData); + + Table table = widgetFactory.createTable(scrolledComposite, SWT.READ_ONLY | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER | SWT.MULTI); + this.tableViewer = new TableViewer(table); + + GridData tableGridData = new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1); + this.tableViewer.getTable().setLayoutData(tableGridData); + + this.tableViewer.setContentProvider(ArrayContentProvider.getInstance()); + this.tableViewer.setLabelProvider(new EEFMultipleReferencesTableLabelProvider(this.description, this.interpreter, this.variableManager)); + + scrolledComposite.setContent(table); + + int widgetHeight = DEFAULT_HEIGHT; + List<EEFWidgetAction> actions = description.getActions(); + if (actions != null && actions.size() > 0) { + widgetHeight = widgetHeight * (actions.size() + 1); + } + + final int clientWidth = scrolledComposite.getClientArea().width; + this.tableViewer.getTable().setSize(clientWidth, Math.max(TABLE_MINIMAL_HEIGHT, widgetHeight)); + + scrolledComposite.setExpandHorizontal(true); + scrolledComposite.setAlwaysShowScrollBars(true); + } + + /** + * Create widget action buttons. + * + * @param parent + * The parent composite + */ + private void createWidgetActionButtons(Composite parent) { + Composite buttons = widgetFactory.createFlatFormComposite(parent); + + GridData gridData = new GridData(); + gridData.grabExcessHorizontalSpace = false; + buttons.setLayoutData(gridData); + + // Buttons are visible only if an action is defined + for (EEFWidgetAction action : this.description.getActions()) { + ActionButton actionButton = new ActionButton(action, buttons, widgetFactory, this.interpreter, this.variableManager); + actionButtons.add(actionButton); + } + + buttons.setLayout(new RowLayout(SWT.VERTICAL)); + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.eef.ide.ui.api.widgets.AbstractEEFWidgetLifecycleManager#aboutToBeShown() + */ + @Override + public void aboutToBeShown() { + super.aboutToBeShown(); + + this.tableSelectionListener = new EEFMultipleReferencesSelectionListener(this.controller); + this.tableViewer.getTable().addSelectionListener(tableSelectionListener); + + this.controller.onNewValue(new IConsumer<Object>() { + @Override + public void apply(Object value) { + if (value == null) { + return; + } + setMultipleValuedReference(value); + } + }); + + for (final ActionButton actionButton : actionButtons) { + SelectionAdapter selectionListener = new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + List<Object> selections = new ArrayList<Object>(); + IStructuredSelection structuredSelection = (IStructuredSelection) tableViewer.getSelection(); + for (Object selection : structuredSelection.toList()) { + selections.add(selection); + } + controller.action(actionButton.getAction(), selections); + } + }; + + actionButton.addSelectionListener(selectionListener); + } + } + + /** + * Set multiple valued reference. + * + * @param value + * Value to select + */ + private void setMultipleValuedReference(Object value) { + if (!this.tableViewer.getTable().isDisposed()) { + final ISelection selection = new StructuredSelection(value); + tableViewer.setSelection(selection); + + List<Object> values = new ArrayList<Object>(); + if (value instanceof Iterable<?>) { + for (Object val : (Iterable<?>) value) { + values.add(val); + } + } else { + values.add(value); + } + tableViewer.setInput(values.toArray()); + if (!this.tableViewer.getTable().isEnabled()) { + this.tableViewer.getTable().setEnabled(true); + } + } + } + + /** + * Get the background color according to the current valid style. + * + * @return The background color to use in the text field. + */ + private Color getBackgroundColor() { + Color color = defaultBackgroundColor; + if (!isEnabled()) { + color = widgetFactory.getColors().getInactiveBackground(); + } + return color; + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.eef.ide.ui.api.widgets.AbstractEEFLifecycleManager#refresh() + */ + @Override + public void refresh() { + super.refresh(); + + if (this.tableViewer != null && this.tableViewer.getTable() != null) { + this.tableViewer.getTable().setEnabled(isEnabled()); + this.tableViewer.getTable().setBackground(getBackgroundColor()); + } + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.eef.ide.ui.api.widgets.AbstractEEFWidgetLifecycleManager#aboutToBeHidden() + */ + @Override + public void aboutToBeHidden() { + super.aboutToBeHidden(); + + for (ActionButton actionButton : this.actionButtons) { + actionButton.removeSelectionListener(); + } + + if (this.tableViewer != null && this.tableViewer.getTable() != null && !this.tableViewer.getTable().isDisposed()) { + this.tableViewer.getTable().removeSelectionListener(this.tableSelectionListener); + } + this.controller.removeNewValueConsumer(); + this.actionButtons.clear(); + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.eef.ide.ui.api.widgets.AbstractEEFWidgetLifecycleManager#getController() + */ + @Override + protected IEEFWidgetController getController() { + return this.controller; + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.eef.ide.ui.api.widgets.AbstractEEFWidgetLifecycleManager#getWidgetDescription() + */ + @Override + protected EEFWidgetDescription getWidgetDescription() { + return this.description; + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.eef.ide.ui.api.widgets.AbstractEEFLifecycleManager#getValidationControl() + */ + @Override + protected Control getValidationControl() { + return this.tableViewer.getTable(); + } +} diff --git a/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFMultipleReferencesSelectionListener.java b/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFMultipleReferencesSelectionListener.java new file mode 100644 index 000000000..e520662d7 --- /dev/null +++ b/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFMultipleReferencesSelectionListener.java @@ -0,0 +1,87 @@ +/******************************************************************************* + * Copyright (c) 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.ide.ui.internal.widgets; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.eef.core.api.EEFExpressionUtils; +import org.eclipse.eef.core.api.controllers.IEEFReferenceController; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.TableItem; + +/** + * This class is used to call the controller once the end user click on the table. + * + * @author sbegaudeau + */ +public class EEFMultipleReferencesSelectionListener implements SelectionListener { + + /** + * The controller. + */ + private IEEFReferenceController controller; + + /** + * The constructor. + * + * @param controller + * The controller + */ + public EEFMultipleReferencesSelectionListener(IEEFReferenceController controller) { + this.controller = controller; + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent) + */ + @Override + public void widgetSelected(SelectionEvent event) { + this.triggerOnClick(event, EEFExpressionUtils.EEFReference.SINGLE_CLICK); + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent) + */ + @Override + public void widgetDefaultSelected(SelectionEvent event) { + this.triggerOnClick(event, EEFExpressionUtils.EEFReference.DOUBLE_CLICK); + } + + /** + * Triggers the on click operation of the controller. + * + * @param event + * The event + * @param onClickEventKind + * The kind of click realized by the end user + */ + private void triggerOnClick(SelectionEvent event, String onClickEventKind) { + Object source = event.getSource(); + if (source instanceof Table) { + Table table = (Table) source; + TableItem[] selection = table.getSelection(); + if (selection != null) { + List<Object> data = new ArrayList<Object>(); + for (TableItem tableItem : selection) { + data.add(tableItem.getData()); + } + this.controller.onClick(data, onClickEventKind); + } + } + } +} diff --git a/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFMultipleReferencesTableLabelProvider.java b/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFMultipleReferencesTableLabelProvider.java new file mode 100644 index 000000000..a3ad1fe76 --- /dev/null +++ b/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFMultipleReferencesTableLabelProvider.java @@ -0,0 +1,82 @@ +/******************************************************************************* + * Copyright (c) 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.ide.ui.internal.widgets; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.eef.EEFReferenceDescription; +import org.eclipse.eef.EefPackage; +import org.eclipse.eef.core.api.EEFExpressionUtils; +import org.eclipse.eef.core.api.utils.EvalFactory; +import org.eclipse.emf.ecore.EAttribute; +import org.eclipse.jface.viewers.StyledCellLabelProvider; +import org.eclipse.jface.viewers.ViewerCell; +import org.eclipse.sirius.common.interpreter.api.IInterpreter; +import org.eclipse.sirius.common.interpreter.api.IVariableManager; + +/** + * Multiple references widget label provider. + * + * @author mbats + */ +final class EEFMultipleReferencesTableLabelProvider extends StyledCellLabelProvider { + + /** + * The description. + */ + private EEFReferenceDescription description; + + /** + * The interpreter. + */ + private IInterpreter interpreter; + + /** + * The variable manager. + */ + private IVariableManager variableManager; + + /** + * The constructor. + * + * @param description + * The description + * @param interpreter + * The interpreter + * @param variableManager + * The variable manager + */ + public EEFMultipleReferencesTableLabelProvider(EEFReferenceDescription description, IInterpreter interpreter, IVariableManager variableManager) { + this.description = description; + this.interpreter = interpreter; + this.variableManager = variableManager; + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.jface.viewers.StyledCellLabelProvider#update(org.eclipse.jface.viewers.ViewerCell) + */ + @Override + public void update(ViewerCell cell) { + Object element = cell.getElement(); + Map<String, Object> variables = new HashMap<String, Object>(); + variables.putAll(this.variableManager.getVariables()); + variables.put(EEFExpressionUtils.EEFReference.VALUE, element); + + String expression = this.description.getDisplayExpression(); + EAttribute eAttribute = EefPackage.Literals.EEF_REFERENCE_DESCRIPTION__DISPLAY_EXPRESSION; + String value = EvalFactory.of(this.interpreter, variables).logIfInvalidType(String.class).logIfBlank(eAttribute).evaluate(expression); + cell.setText(value); + super.update(cell); + } +}
\ No newline at end of file diff --git a/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFReferenceHyperlinkListener.java b/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFReferenceHyperlinkListener.java new file mode 100644 index 000000000..418f70c1a --- /dev/null +++ b/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFReferenceHyperlinkListener.java @@ -0,0 +1,75 @@ +/******************************************************************************* + * Copyright (c) 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.ide.ui.internal.widgets; + +import org.eclipse.eef.core.api.EEFExpressionUtils; +import org.eclipse.eef.core.api.controllers.IEEFReferenceController; +import org.eclipse.ui.forms.events.HyperlinkEvent; +import org.eclipse.ui.forms.events.IHyperlinkListener; +import org.eclipse.ui.forms.widgets.Hyperlink; + +/** + * This class will be used to run the onClick expression once the used click on the hyperlink. + * + * @author sbegaudeau + */ +public class EEFReferenceHyperlinkListener implements IHyperlinkListener { + + /** + * The controller. + */ + private IEEFReferenceController controller; + + /** + * The constructor. + * + * @param controller + * The controller + */ + public EEFReferenceHyperlinkListener(IEEFReferenceController controller) { + this.controller = controller; + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.ui.forms.events.IHyperlinkListener#linkEntered(org.eclipse.ui.forms.events.HyperlinkEvent) + */ + @Override + public void linkEntered(HyperlinkEvent event) { + // Do nothing + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.ui.forms.events.IHyperlinkListener#linkExited(org.eclipse.ui.forms.events.HyperlinkEvent) + */ + @Override + public void linkExited(HyperlinkEvent event) { + // Do nothing + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.ui.forms.events.IHyperlinkListener#linkActivated(org.eclipse.ui.forms.events.HyperlinkEvent) + */ + @Override + public void linkActivated(HyperlinkEvent event) { + Hyperlink link = (Hyperlink) event.getSource(); + if (link != null) { + Object element = link.getData(); + this.controller.onClick(element, EEFExpressionUtils.EEFReference.SINGLE_CLICK); + } + } + +} diff --git a/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFReferenceLifecycleManager.java b/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFReferenceLifecycleManager.java index 36408c80d..353824ed6 100644 --- a/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFReferenceLifecycleManager.java +++ b/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFReferenceLifecycleManager.java @@ -18,7 +18,6 @@ import java.util.Map; import org.eclipse.eef.EEFReferenceDescription; import org.eclipse.eef.EEFWidgetAction; import org.eclipse.eef.EEFWidgetDescription; -import org.eclipse.eef.EefPackage; import org.eclipse.eef.common.api.utils.Util; import org.eclipse.eef.common.ui.api.EEFWidgetFactory; import org.eclipse.eef.common.ui.api.IEEFFormContainer; @@ -30,18 +29,9 @@ import org.eclipse.eef.core.api.controllers.IEEFReferenceController; import org.eclipse.eef.core.api.controllers.IEEFWidgetController; import org.eclipse.eef.core.api.utils.EvalFactory; import org.eclipse.eef.ide.ui.api.widgets.AbstractEEFWidgetLifecycleManager; -import org.eclipse.emf.ecore.EAttribute; -import org.eclipse.jface.viewers.ArrayContentProvider; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.jface.viewers.StructuredSelection; -import org.eclipse.jface.viewers.StyledCellLabelProvider; -import org.eclipse.jface.viewers.TableViewer; -import org.eclipse.jface.viewers.ViewerCell; import org.eclipse.sirius.common.interpreter.api.IInterpreter; import org.eclipse.sirius.common.interpreter.api.IVariableManager; import org.eclipse.swt.SWT; -import org.eclipse.swt.custom.ScrolledComposite; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Color; @@ -51,7 +41,6 @@ import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Label; -import org.eclipse.ui.forms.events.HyperlinkEvent; import org.eclipse.ui.forms.events.IHyperlinkListener; import org.eclipse.ui.forms.widgets.Hyperlink; @@ -68,11 +57,6 @@ public class EEFReferenceLifecycleManager extends AbstractEEFWidgetLifecycleMana private static final int DEFAULT_HEIGHT = 34; /** - * Minimal height of the table widget. - */ - private static final int TABLE_MINIMAL_HEIGHT = 100; - - /** * The description. */ private EEFReferenceDescription description; @@ -93,26 +77,6 @@ public class EEFReferenceLifecycleManager extends AbstractEEFWidgetLifecycleMana private List<ActionButton> actionButtons = new ArrayList<ActionButton>(); /** - * The main parent. - */ - private Composite reference; - - /** - * The combo viewer. - */ - private TableViewer tableViewer; - - /** - * The list. - */ - private org.eclipse.swt.widgets.Table table; - - /** - * The buttons. - */ - private Composite buttons; - - /** * The controller. */ private IEEFReferenceController controller; @@ -162,21 +126,15 @@ public class EEFReferenceLifecycleManager extends AbstractEEFWidgetLifecycleMana defaultBackgroundColor = parent.getBackground(); // this is the parent composite - this.reference = widgetFactory.createFlatFormComposite(parent); + Composite reference = widgetFactory.createFlatFormComposite(parent); GridLayout layout = new GridLayout(2, false); reference.setLayout(layout); GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false); reference.setLayoutData(gridData); - if (description.isMultiple()) { - createMultipleValuedReferenceWidget(); - } else { - createSingleValuedReferenceWidget(); - } - - // Create widget action buttons - createWidgetActionButtons(); + this.createSingleValuedReferenceWidget(reference); + this.createWidgetActionButtons(reference); widgetFactory.paintBordersFor(parent); @@ -186,81 +144,51 @@ public class EEFReferenceLifecycleManager extends AbstractEEFWidgetLifecycleMana /** * Create widget action buttons. + * + * @param parent + * The parent composite */ - private void createWidgetActionButtons() { - this.buttons = widgetFactory.createFlatFormComposite(reference); + private void createWidgetActionButtons(Composite parent) { + Composite buttons = widgetFactory.createFlatFormComposite(parent); GridData gridData = new GridData(); gridData.grabExcessHorizontalSpace = false; - this.buttons.setLayoutData(gridData); + buttons.setLayoutData(gridData); // Buttons are visible only if an action is defined for (EEFWidgetAction action : this.description.getActions()) { - ActionButton actionButton = new ActionButton(action, this.buttons, widgetFactory, this.interpreter, this.variableManager); + ActionButton actionButton = new ActionButton(action, buttons, widgetFactory, this.interpreter, this.variableManager); actionButtons.add(actionButton); } - if (description.isMultiple()) { - this.buttons.setLayout(new RowLayout(SWT.VERTICAL)); - } else { - this.buttons.setLayout(new RowLayout(SWT.HORIZONTAL)); - } + buttons.setLayout(new RowLayout(SWT.HORIZONTAL)); } /** * Create a single valued reference widget : a text field or a label field if the onclick expression exists. + * + * @param parent + * The parent composite */ - private void createSingleValuedReferenceWidget() { + private void createSingleValuedReferenceWidget(Composite parent) { GridData gd = new GridData(); gd.grabExcessHorizontalSpace = true; gd.horizontalAlignment = SWT.FILL; // Use hyperlink if the onclick expression exists - final int clientWidth = reference.getClientArea().width; + final int clientWidth = parent.getClientArea().width; if (!Util.isBlank(this.description.getOnClickExpression())) { - this.hyperlink = widgetFactory.createHyperlink(this.reference, "", SWT.NONE); //$NON-NLS-1$ + this.hyperlink = widgetFactory.createHyperlink(parent, "", SWT.NONE); //$NON-NLS-1$ hyperlink.setLayoutData(gd); hyperlink.setSize(clientWidth, DEFAULT_HEIGHT); } else { - this.text = widgetFactory.createLabel(this.reference, "", SWT.NONE); //$NON-NLS-1$ + this.text = widgetFactory.createLabel(parent, "", SWT.NONE); //$NON-NLS-1$ text.setLayoutData(gd); text.setSize(clientWidth, DEFAULT_HEIGHT); } } /** - * Create table widget. - */ - private void createMultipleValuedReferenceWidget() { - ScrolledComposite scrolledComposite = widgetFactory.createScrolledComposite(reference, SWT.NONE); - GridData gridData = new GridData(); - gridData.grabExcessHorizontalSpace = true; - gridData.horizontalAlignment = SWT.FILL; - scrolledComposite.setLayoutData(gridData); - - this.table = widgetFactory.createTable(scrolledComposite, SWT.READ_ONLY | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER | SWT.MULTI); - this.tableViewer = new TableViewer(this.table); - GridData tableGridData = new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1); - this.table.setLayoutData(tableGridData); - this.tableViewer.setContentProvider(ArrayContentProvider.getInstance()); - this.tableViewer.setLabelProvider(new EEFTableReferencesLabelProvider()); - - scrolledComposite.setContent(table); - - int widgetHeight = DEFAULT_HEIGHT; - List<EEFWidgetAction> actions = description.getActions(); - if (actions != null && actions.size() > 0) { - widgetHeight = widgetHeight * (actions.size() + 1); - } - - final int clientWidth = scrolledComposite.getClientArea().width; - this.table.setSize(clientWidth, Math.max(TABLE_MINIMAL_HEIGHT, widgetHeight)); - - scrolledComposite.setExpandHorizontal(true); - scrolledComposite.setAlwaysShowScrollBars(true); - } - - /** * {@inheritDoc} * * @see org.eclipse.eef.ide.ui.internal.widgets.AbstractEEFWidgetLifecycleManager#getController() @@ -279,7 +207,7 @@ public class EEFReferenceLifecycleManager extends AbstractEEFWidgetLifecycleMana public void aboutToBeShown() { super.aboutToBeShown(); if (this.hyperlink != null) { - this.onClickListener = createOnClickListener(); + this.onClickListener = new EEFReferenceHyperlinkListener(this.controller); this.hyperlink.addHyperlinkListener(this.onClickListener); } @@ -289,11 +217,7 @@ public class EEFReferenceLifecycleManager extends AbstractEEFWidgetLifecycleMana if (value == null) { return; } - if (description.isMultiple()) { - setMultipleValuedReference(value); - } else { - setSingleValuedReference(value); - } + EEFReferenceLifecycleManager.this.setSingleValuedReference(value); } }); @@ -302,19 +226,10 @@ public class EEFReferenceLifecycleManager extends AbstractEEFWidgetLifecycleMana @Override public void widgetSelected(SelectionEvent e) { List<Object> selections = new ArrayList<Object>(); - if (description.isMultiple()) { - IStructuredSelection structuredSelection = (IStructuredSelection) EEFReferenceLifecycleManager.this.tableViewer - .getSelection(); - - for (Object selection : structuredSelection.toList()) { - selections.add(selection); - } + if (EEFReferenceLifecycleManager.this.hyperlink != null) { + selections.add(EEFReferenceLifecycleManager.this.hyperlink.getData()); } else { - if (EEFReferenceLifecycleManager.this.hyperlink != null) { - selections.add(EEFReferenceLifecycleManager.this.hyperlink.getData()); - } else { - selections.add(EEFReferenceLifecycleManager.this.text.getData()); - } + selections.add(EEFReferenceLifecycleManager.this.text.getData()); } controller.action(actionButton.getAction(), selections); } @@ -334,7 +249,8 @@ public class EEFReferenceLifecycleManager extends AbstractEEFWidgetLifecycleMana String expression = description.getDisplayExpression(); Map<String, Object> variables = new HashMap<String, Object>(); - variables.put(EEFExpressionUtils.SELF, value); + variables.putAll(this.variableManager.getVariables()); + variables.put(EEFExpressionUtils.EEFReference.VALUE, value); String display = EvalFactory.of(EEFReferenceLifecycleManager.this.interpreter, variables).logIfInvalidType(String.class).evaluate(expression); if (!Util.isBlank(display)) { @@ -355,67 +271,16 @@ public class EEFReferenceLifecycleManager extends AbstractEEFWidgetLifecycleMana } /** - * Set multiple valued reference. - * - * @param value - * Value to select - */ - private void setMultipleValuedReference(Object value) { - if (!table.isDisposed()) { - final ISelection selection = new StructuredSelection(value); - tableViewer.setSelection(selection); - List<Object> values = new ArrayList<Object>(); - if (value instanceof Iterable<?>) { - for (Object val : (Iterable<?>) value) { - values.add(val); - } - } else { - values.add(value); - } - tableViewer.setInput(values.toArray()); - if (!table.isEnabled()) { - table.setEnabled(true); - } - } - } - - /** - * Create onclick listener. - * - * @return Listener - */ - private IHyperlinkListener createOnClickListener() { - return new IHyperlinkListener() { - - @Override - public void linkExited(HyperlinkEvent e) { - // Nothing - } - - @Override - public void linkEntered(HyperlinkEvent e) { - // Nothing - } - - @Override - public void linkActivated(HyperlinkEvent e) { - Hyperlink link = (Hyperlink) e.getSource(); - if (link != null) { - Object element = link.getData(); - controller.onClick(element); - } - } - }; - } - - /** * {@inheritDoc} * * @see org.eclipse.eef.ide.ui.internal.widgets.AbstractEEFWidgetLifecycleManager#getValidationControl() */ @Override protected Control getValidationControl() { - return this.reference; + if (this.hyperlink != null) { + return this.hyperlink; + } + return this.text; } /** @@ -450,34 +315,6 @@ public class EEFReferenceLifecycleManager extends AbstractEEFWidgetLifecycleMana } /** - * Table references widget label provider. - * - * @author mbats - */ - private final class EEFTableReferencesLabelProvider extends StyledCellLabelProvider { - - /** - * {@inheritDoc} - * - * @see org.eclipse.jface.viewers.StyledCellLabelProvider#update(org.eclipse.jface.viewers.ViewerCell) - */ - @Override - public void update(ViewerCell cell) { - Object element = cell.getElement(); - Map<String, Object> variables = new HashMap<String, Object>(); - variables.putAll(EEFReferenceLifecycleManager.this.variableManager.getVariables()); - variables.put(EEFExpressionUtils.EEFReference.VALUE, element); - - String expression = description.getDisplayExpression(); - EAttribute eAttribute = EefPackage.Literals.EEF_REFERENCE_DESCRIPTION__DISPLAY_EXPRESSION; - String value = EvalFactory.of(EEFReferenceLifecycleManager.this.interpreter, variables).logIfInvalidType(String.class) - .logIfBlank(eAttribute).evaluate(expression); - cell.setText(value); - super.update(cell); - } - } - - /** * {@inheritDoc} * * @see org.eclipse.eef.ide.ui.api.widgets.AbstractEEFWidgetLifecycleManager#getLabelVerticalAlignment() @@ -504,9 +341,6 @@ public class EEFReferenceLifecycleManager extends AbstractEEFWidgetLifecycleMana } else if (this.hyperlink != null) { this.hyperlink.setEnabled(isEnabled()); this.hyperlink.setBackground(getBackgroundColor()); - } else if (this.table != null) { - this.table.setEnabled(isEnabled()); - this.table.setBackground(getBackgroundColor()); } } |