Bug 566073 - Fix "WithDialog" editing for table controls

* add action to conform to current Table API

Change-Id: I356bea6a2f4ddcd6d021ae603df06cb388b17807
diff --git a/bundles/org.eclipse.emf.ecp.view.table.ui.swt/OSGI-INF/l10n/bundle.properties b/bundles/org.eclipse.emf.ecp.view.table.ui.swt/OSGI-INF/l10n/bundle.properties
index 66b5466..dc39868 100644
--- a/bundles/org.eclipse.emf.ecp.view.table.ui.swt/OSGI-INF/l10n/bundle.properties
+++ b/bundles/org.eclipse.emf.ecp.view.table.ui.swt/OSGI-INF/l10n/bundle.properties
@@ -6,4 +6,5 @@
 TableControl_MoveUp=Move up the selected %1$s
 TableControl_MoveDown=Move down the selected %1$s
 TableControl_Duplicate=Duplicate the selected %1$s
-TableControl_defaultReferenceDisplayName=Element
\ No newline at end of file
+TableControl_defaultReferenceDisplayName=Element
+TableControl_DetailEdit=Edit selected element in dialog
\ No newline at end of file
diff --git a/bundles/org.eclipse.emf.ecp.view.table.ui.swt/src/org/eclipse/emf/ecp/view/internal/table/swt/MessageKeys.java b/bundles/org.eclipse.emf.ecp.view.table.ui.swt/src/org/eclipse/emf/ecp/view/internal/table/swt/MessageKeys.java
index 775cee4..f2971ea 100644
--- a/bundles/org.eclipse.emf.ecp.view.table.ui.swt/src/org/eclipse/emf/ecp/view/internal/table/swt/MessageKeys.java
+++ b/bundles/org.eclipse.emf.ecp.view.table.ui.swt/src/org/eclipse/emf/ecp/view/internal/table/swt/MessageKeys.java
@@ -28,6 +28,7 @@
 	String TableControl_MoveUp = "TableControl_MoveUp"; //$NON-NLS-1$
 	String TableControl_MoveDown = "TableControl_MoveDown"; //$NON-NLS-1$
 	String TableControl_Duplicate = "TableControl_Duplicate"; //$NON-NLS-1$
+	String TableControl_DetailEdit = "TableControl_DetailEdit"; //$NON-NLS-1$
 	/**
 	 * Default display name for a reference, in case the model's edit support is not available.
 	 *
diff --git a/bundles/org.eclipse.emf.ecp.view.table.ui.swt/src/org/eclipse/emf/ecp/view/spi/table/swt/TableControlDetailDialogSWTRenderer.java b/bundles/org.eclipse.emf.ecp.view.table.ui.swt/src/org/eclipse/emf/ecp/view/spi/table/swt/TableControlDetailDialogSWTRenderer.java
index 66a9a9d..8baa8b4 100644
--- a/bundles/org.eclipse.emf.ecp.view.table.ui.swt/src/org/eclipse/emf/ecp/view/spi/table/swt/TableControlDetailDialogSWTRenderer.java
+++ b/bundles/org.eclipse.emf.ecp.view.table.ui.swt/src/org/eclipse/emf/ecp/view/spi/table/swt/TableControlDetailDialogSWTRenderer.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011-2014 EclipseSource Muenchen GmbH and others.
+ * Copyright (c) 2011-2020 EclipseSource Muenchen GmbH and others.
  *
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -9,17 +9,20 @@
  * SPDX-License-Identifier: EPL-2.0
  *
  * Contributors:
- * Johannes Faltermeier - initial API and implementation
+ * Johannes Faltermeier - initial API and implementation, Bug 566073
  ******************************************************************************/
 package org.eclipse.emf.ecp.view.spi.table.swt;
 
 import javax.inject.Inject;
 
 import org.eclipse.core.databinding.property.value.IValueProperty;
+import org.eclipse.emf.ecore.EClass;
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.emf.ecore.EReference;
+import org.eclipse.emf.ecore.EStructuralFeature.Setting;
 import org.eclipse.emf.ecore.util.EcoreUtil;
 import org.eclipse.emf.ecp.edit.spi.swt.util.ECPDialogExecutor;
+import org.eclipse.emf.ecp.view.internal.table.swt.MessageKeys;
 import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
 import org.eclipse.emf.ecp.view.spi.model.VElement;
 import org.eclipse.emf.ecp.view.spi.model.VView;
@@ -27,14 +30,21 @@
 import org.eclipse.emf.ecp.view.spi.model.util.ViewModelPropertiesHelper;
 import org.eclipse.emf.ecp.view.spi.provider.ViewProviderHelper;
 import org.eclipse.emf.ecp.view.spi.table.model.VTableControl;
+import org.eclipse.emf.ecp.view.spi.table.swt.action.TableActionIconButton;
+import org.eclipse.emf.ecp.view.spi.table.swt.action.TableRendererAction;
+import org.eclipse.emf.ecp.view.spi.table.swt.action.TableRendererViewerActionContext;
 import org.eclipse.emf.ecp.view.spi.util.swt.ImageRegistryService;
 import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider;
 import org.eclipse.emfforms.spi.common.report.ReportService;
 import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
 import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedReport;
+import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
 import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
 import org.eclipse.emfforms.spi.core.services.editsupport.EMFFormsEditSupport;
 import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider;
+import org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService;
+import org.eclipse.emfforms.spi.swt.table.action.ActionConfiguration;
+import org.eclipse.emfforms.spi.swt.table.action.ActionConfigurationBuilder;
 import org.eclipse.jface.dialogs.Dialog;
 import org.eclipse.jface.dialogs.IDialogLabelKeys;
 import org.eclipse.jface.dialogs.MessageDialog;
@@ -70,8 +80,8 @@
 	 * @param emfFormsEditSupport The {@link EMFFormsEditSupport}
 	 * @since 1.8
 	 */
+	@Deprecated
 	// BEGIN COMPLEX CODE
-	@Inject
 	public TableControlDetailDialogSWTRenderer(
 		VTableControl vElement,
 		ViewModelContext viewContext,
@@ -93,14 +103,47 @@
 			emfFormsEditSupport);
 	}
 
+	/**
+	 * Default constructor.
+	 *
+	 * @param vElement the view model element to be rendered
+	 * @param viewContext the view context
+	 * @param emfFormsDatabinding The {@link EMFFormsDatabinding}
+	 * @param emfFormsLabelProvider The {@link EMFFormsLabelProvider}
+	 * @param reportService The {@link ReportService}
+	 * @param vtViewTemplateProvider The {@link VTViewTemplateProvider}
+	 * @param imageRegistryService The {@link ImageRegistryService}
+	 * @param emfFormsEditSupport The {@link EMFFormsEditSupport}
+	 * @param localizationService The {@link EMFFormsLocalizationService}
+	 * @since 1.26
+	 */
+	@Inject
+	// BEGIN COMPLEX CODE
+	public TableControlDetailDialogSWTRenderer(
+		VTableControl vElement,
+		ViewModelContext viewContext,
+		ReportService reportService,
+		EMFFormsDatabindingEMF emfFormsDatabinding,
+		EMFFormsLabelProvider emfFormsLabelProvider,
+		VTViewTemplateProvider vtViewTemplateProvider,
+		ImageRegistryService imageRegistryService,
+		EMFFormsEditSupport emfFormsEditSupport,
+		EMFFormsLocalizationService localizationService) {
+		// END COMPLEX CODE
+		super(vElement,
+			viewContext,
+			reportService,
+			emfFormsDatabinding,
+			emfFormsLabelProvider,
+			vtViewTemplateProvider,
+			imageRegistryService,
+			emfFormsEditSupport,
+			localizationService);
+	}
+
 	private Button detailEditButton;
 	private VView view;
 
-	/**
-	 * {@inheritDoc}
-	 *
-	 * @see org.eclipse.emf.ecp.view.spi.table.swt.TableControlSWTRenderer#addButtonsToButtonBar(org.eclipse.swt.widgets.Composite)
-	 */
 	@Override
 	protected int addButtonsToButtonBar(Composite buttonComposite) {
 		createDetailEditButton(buttonComposite);
@@ -119,7 +162,7 @@
 		if (view == null) {
 			VView detailView = getVElement().getDetailView();
 			if (detailView == null) {
-				IValueProperty valueProperty;
+				IValueProperty<?, ?> valueProperty;
 				try {
 					valueProperty = getEMFFormsDatabinding()
 						.getValueProperty(getVElement().getDomainModelReference(),
@@ -143,11 +186,6 @@
 		return copy;
 	}
 
-	/**
-	 * {@inheritDoc}
-	 *
-	 * @see org.eclipse.emf.ecp.view.spi.table.swt.TableControlSWTRenderer#viewerSelectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
-	 */
 	@Override
 	protected void viewerSelectionChanged(SelectionChangedEvent event) {
 		if (event.getSelection().isEmpty()) {
@@ -162,41 +200,75 @@
 		super.viewerSelectionChanged(event);
 	}
 
-	/**
-	 * {@inheritDoc}
-	 *
-	 * @see org.eclipse.emf.ecp.view.spi.table.swt.TableControlSWTRenderer#dispose()
-	 */
+	@Override
+	protected ActionConfiguration configureActions(TableRendererViewerActionContext actionContext) {
+		final ActionConfigurationBuilder actionConfigBuilder = ActionConfigurationBuilder
+			.usingConfiguration(super.configureActions(actionContext));
+
+		final DetailEditAction detailEditAction = new DetailEditAction(actionContext);
+		final Setting setting = actionContext.getSetting();
+		final EClass eClass = ((EReference) setting.getEStructuralFeature()).getEReferenceType();
+
+		actionConfigBuilder
+			.addAction(detailEditAction)
+			.addControlFor(detailEditAction, new TableActionIconButton(
+				formatTooltipText(eClass, MessageKeys.TableControl_DetailEdit), getImage("icons/detailEdit.png"))); //$NON-NLS-1$
+
+		return actionConfigBuilder.build();
+	}
+
 	@Override
 	protected void dispose() {
 		detailEditButton = null;
 		super.dispose();
 	}
 
-	/**
-	 * {@link SelectionAdapter} used for the detail edit button.
-	 *
-	 * @author jfaltermeier
-	 *
-	 */
-	private class DetailEditButtonSelectionAdapter extends SelectionAdapter {
+	private Dialog createDialog(Shell shell) {
+		Dialog dialog;
+		if (getTableViewer().getSelection().isEmpty()) {
+			dialog = new MessageDialog(shell, "No Table Selection", null, //$NON-NLS-1$
+				"You must select one element in order to edit it.", MessageDialog.WARNING, new String[] { //$NON-NLS-1$
+					JFaceResources.getString(IDialogLabelKeys.OK_LABEL_KEY) },
+				0);
 
-		private final Shell shell;
+		} else if (getView() == null) {
+			dialog = new MessageDialog(
+				shell,
+				"No View Model", null, //$NON-NLS-1$
+				"Detail editing is not possible since there is no UI description for the selection.", //$NON-NLS-1$
+				MessageDialog.ERROR, new String[] { JFaceResources.getString(IDialogLabelKeys.OK_LABEL_KEY) },
+				0);
+		} else {
+			dialog = new DetailDialog(shell, (EObject) IStructuredSelection.class.cast(
+				getTableViewer().getSelection()).getFirstElement(), getVElement(), getView());
+		}
+		return dialog;
+	}
 
-		DetailEditButtonSelectionAdapter(Shell shell) {
-			this.shell = shell;
+	private class DetailEditAction extends TableRendererAction {
+
+		/**
+		 * Default constructor.
+		 *
+		 * @param actionContext the {@link TableRendererViewerActionContext}.
+		 */
+		DetailEditAction(TableRendererViewerActionContext actionContext) {
+			super(actionContext);
 		}
 
 		/**
-		 * {@inheritDoc}
-		 *
-		 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
+		 * The ID of this action.
 		 */
-		@Override
-		public void widgetSelected(SelectionEvent e) {
-			super.widgetSelected(e);
+		public static final String ACTION_ID = PREFIX + "tablecontrol.dialog_edit"; //$NON-NLS-1$
 
-			final Dialog dialog = createDialog();
+		@Override
+		public String getId() {
+			return ACTION_ID;
+		}
+
+		@Override
+		public void execute() {
+			final Dialog dialog = createDialog(getTableViewer().getControl().getShell());
 
 			new ECPDialogExecutor(dialog) {
 				@Override
@@ -206,31 +278,42 @@
 			}.execute();
 		}
 
-		/**
-		 * @param buttonComposite
-		 * @return
-		 */
-		private Dialog createDialog() {
-			Dialog dialog;
-			if (getTableViewer().getSelection().isEmpty()) {
-				dialog = new MessageDialog(shell, "No Table Selection", null, //$NON-NLS-1$
-					"You must select one element in order to edit it.", MessageDialog.WARNING, new String[] { //$NON-NLS-1$
-						JFaceResources.getString(IDialogLabelKeys.OK_LABEL_KEY) },
-					0);
-
-			} else if (getView() == null) {
-				dialog = new MessageDialog(
-					shell,
-					"No View Model", null, //$NON-NLS-1$
-					"Detail editing is not possible since there is no UI description for the selection.", //$NON-NLS-1$
-					MessageDialog.ERROR, new String[] { JFaceResources.getString(IDialogLabelKeys.OK_LABEL_KEY) },
-					0);
-			} else {
-				dialog = new DetailDialog(shell, (EObject) IStructuredSelection.class.cast(
-					getTableViewer().getSelection()).getFirstElement(), getVElement(), getView());
-			}
-			return dialog;
+		@Override
+		public boolean canExecute() {
+			return !getTableViewer().getSelection().isEmpty();
 		}
+
+	}
+
+	/**
+	 * {@link SelectionAdapter} used for the detail edit button.
+	 *
+	 * @author jfaltermeier
+	 *
+	 */
+	@Deprecated
+	private class DetailEditButtonSelectionAdapter extends SelectionAdapter {
+
+		private final Shell shell;
+
+		DetailEditButtonSelectionAdapter(Shell shell) {
+			this.shell = shell;
+		}
+
+		@Override
+		public void widgetSelected(SelectionEvent e) {
+			super.widgetSelected(e);
+
+			final Dialog dialog = createDialog(shell);
+
+			new ECPDialogExecutor(dialog) {
+				@Override
+				public void handleResult(int codeResult) {
+					// no op
+				}
+			}.execute();
+		}
+
 	}
 
 }
diff --git a/bundles/org.eclipse.emf.ecp.view.table.ui.swt/src/org/eclipse/emf/ecp/view/spi/table/swt/TableControlSWTRenderer.java b/bundles/org.eclipse.emf.ecp.view.table.ui.swt/src/org/eclipse/emf/ecp/view/spi/table/swt/TableControlSWTRenderer.java
index f3e966e..63c4a14 100644
--- a/bundles/org.eclipse.emf.ecp.view.table.ui.swt/src/org/eclipse/emf/ecp/view/spi/table/swt/TableControlSWTRenderer.java
+++ b/bundles/org.eclipse.emf.ecp.view.table.ui.swt/src/org/eclipse/emf/ecp/view/spi/table/swt/TableControlSWTRenderer.java
@@ -3175,7 +3175,15 @@
 		}
 	}
 
-	private String formatTooltipText(EClass eClass, String messageKey) {
+	/**
+	 * Localize the message.
+	 *
+	 * @param eClass The EClass of the table setting.
+	 * @param messageKey key of the string
+	 * @return tooltip.
+	 * @since 1.26
+	 */
+	protected String formatTooltipText(EClass eClass, String messageKey) {
 		final String instanceName = getReferenceDisplayName(eClass);
 		return String.format(LocalizationServiceHelper.getString(
 			TableControlSWTRenderer.class, messageKey),
diff --git a/i18n/org.eclipse.emf.ecp.view.table.ui.swt.nl_de/OSGI-INF/l10n/bundle_de.properties b/i18n/org.eclipse.emf.ecp.view.table.ui.swt.nl_de/OSGI-INF/l10n/bundle_de.properties
index ac97caf..90b1029 100644
--- a/i18n/org.eclipse.emf.ecp.view.table.ui.swt.nl_de/OSGI-INF/l10n/bundle_de.properties
+++ b/i18n/org.eclipse.emf.ecp.view.table.ui.swt.nl_de/OSGI-INF/l10n/bundle_de.properties
@@ -5,4 +5,5 @@
 TableControl_DeleteAreYouSure=Wollen Sie die ausgew\u00e4hlten Elemente wirklich l\u00f6schen?
 TableControl_MoveUp=Auswahl nach oben bewegen
 TableControl_MoveDown=Auswahl nach unten bewegen
-TableControl_Duplicate=Auswahl duplizieren
\ No newline at end of file
+TableControl_Duplicate=Auswahl duplizieren
+TableControl_DetailEdit=Auswahl in Dialog bearbeiten
\ No newline at end of file