NEW - bug 300462: [modeling] [masterdetails] Remove button and remove toolbar action should be bound to the EmfActionBarContributor's Delete action
https://bugs.eclipse.org/bugs/show_bug.cgi?id=300462
diff --git a/modeling/plugins/org.eclipse.pde.emfforms/META-INF/MANIFEST.MF b/modeling/plugins/org.eclipse.pde.emfforms/META-INF/MANIFEST.MF
index 4a10c29..b6f8ec2 100644
--- a/modeling/plugins/org.eclipse.pde.emfforms/META-INF/MANIFEST.MF
+++ b/modeling/plugins/org.eclipse.pde.emfforms/META-INF/MANIFEST.MF
@@ -19,14 +19,14 @@
  org.eclipse.wst.xsd.core;bundle-version="1.1.300";resolution:=optional,
  org.eclipse.jface.text;bundle-version="3.4.0",
  org.eclipse.ui.editors;bundle-version="3.4.0",
- org.eclipse.emf.transaction;bundle-version="[1.2.0,2.0.0)"
+ org.eclipse.emf.transaction;bundle-version="[1.2.0,2.0.0)",
+ org.eclipse.core.databinding.property;bundle-version="1.2.0"
 Bundle-RequiredExecutionEnvironment: J2SE-1.5
 Bundle-Localization: plugin
 Bundle-ActivationPolicy: lazy
 Export-Package: org.eclipse.pde.emfforms.editor,
  org.eclipse.pde.emfforms.editor.actions,
  org.eclipse.pde.emfforms.internal;x-internal:=true,
- org.eclipse.pde.emfforms.internal.actions;x-internal:=true,
  org.eclipse.pde.emfforms.internal.editor;x-internal:=true,
  org.eclipse.pde.emfforms.internal.validation;x-internal:=true
 Import-Package: org.eclipse.core.resources
diff --git a/modeling/plugins/org.eclipse.pde.emfforms/src/org/eclipse/pde/emfforms/editor/EmfActionBarContributor.java b/modeling/plugins/org.eclipse.pde.emfforms/src/org/eclipse/pde/emfforms/editor/EmfActionBarContributor.java
index cc98058..d9c04c5 100644
--- a/modeling/plugins/org.eclipse.pde.emfforms/src/org/eclipse/pde/emfforms/editor/EmfActionBarContributor.java
+++ b/modeling/plugins/org.eclipse.pde.emfforms/src/org/eclipse/pde/emfforms/editor/EmfActionBarContributor.java
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2009 Anyware Technologies and others.
+ * Copyright (c) 2009, 2010 Anyware Technologies and others.
  * 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
@@ -8,7 +8,7 @@
  * Contributors:
  *     Anyware Technologies - initial API and implementation
  *
- * $Id: EmfActionBarContributor.java,v 1.6 2009/09/02 14:11:43 bcabe Exp $
+ * $Id: EmfActionBarContributor.java,v 1.7 2009/12/02 11:24:33 bcabe Exp $
  */
 package org.eclipse.pde.emfforms.editor;
 
@@ -71,6 +71,10 @@
 		actionBars.updateActionBars();
 	}
 
+	public IAction getDeleteAction() {
+		return deleteAction;
+	}
+
 	/** The show properties view action. */
 	protected IAction showPropertiesViewAction = new Action("Open properties") {
 		@Override
diff --git a/modeling/plugins/org.eclipse.pde.emfforms/src/org/eclipse/pde/emfforms/editor/EmfMasterDetailBlock.java b/modeling/plugins/org.eclipse.pde.emfforms/src/org/eclipse/pde/emfforms/editor/EmfMasterDetailBlock.java
index 9d73f8a..f03cf6d 100644
--- a/modeling/plugins/org.eclipse.pde.emfforms/src/org/eclipse/pde/emfforms/editor/EmfMasterDetailBlock.java
+++ b/modeling/plugins/org.eclipse.pde.emfforms/src/org/eclipse/pde/emfforms/editor/EmfMasterDetailBlock.java
@@ -8,26 +8,22 @@
  * Contributors:
  *     Anyware Technologies - initial API and implementation
  *
- * $Id: EmfMasterDetailBlock.java,v 1.20 2009/10/31 16:30:44 bcabe Exp $
+ * $Id: EmfMasterDetailBlock.java,v 1.21 2010/01/05 15:45:45 bcabe Exp $
  */
 package org.eclipse.pde.emfforms.editor;
 
 import org.eclipse.core.databinding.DataBindingContext;
-import org.eclipse.core.databinding.observable.value.IValueChangeListener;
-import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
-import org.eclipse.emf.common.command.Command;
-import org.eclipse.emf.databinding.EMFUpdateValueStrategy;
-import org.eclipse.emf.edit.command.RemoveCommand;
+import org.eclipse.core.databinding.property.value.IValueProperty;
 import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
 import org.eclipse.emf.edit.ui.dnd.*;
 import org.eclipse.emf.edit.ui.provider.*;
 import org.eclipse.jface.action.*;
 import org.eclipse.jface.databinding.swt.SWTObservables;
-import org.eclipse.jface.databinding.viewers.ViewersObservables;
+import org.eclipse.jface.databinding.util.JFaceProperties;
 import org.eclipse.jface.layout.GridDataFactory;
 import org.eclipse.jface.layout.GridLayoutFactory;
 import org.eclipse.jface.viewers.*;
-import org.eclipse.pde.emfforms.internal.actions.RemoveAction;
+import org.eclipse.pde.emfforms.editor.actions.RemoveAction;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.dnd.DND;
 import org.eclipse.swt.dnd.Transfer;
@@ -76,7 +72,7 @@
 	private Button removeButton;
 
 	protected ToolBarManager toolBarManager;
-	private Action removeAction;
+	private IAction removeAction;
 
 	public EmfMasterDetailBlock(EmfFormEditor<?> editor, String title) {
 		this.title = title;
@@ -127,9 +123,6 @@
 
 		//SectionToolBar
 		removeAction = createCustomToolbarRemoveAction();
-		if (removeAction == null) {
-			removeAction = new RemoveAction(this);
-		}
 
 		if (showToolbarButtons()) {
 			toolBarManager = PDEFormToolkit.createSectionToolBarManager(section);
@@ -175,41 +168,17 @@
 
 			DataBindingContext bindingContext = new DataBindingContext();
 
-			//Enable button when the tree selection is not empty
-			bindingContext.bindValue(ViewersObservables.observeSingleSelection(getTreeViewer()), SWTObservables.observeEnabled(getRemoveButton()), new EMFUpdateValueStrategy() {
-				/**
-				 * @see org.eclipse.core.databinding.UpdateValueStrategy#convert(java.lang.Object)
-				 */
-				@Override
-				public Object convert(Object value) {
-					return !(getTreeViewer().getSelection().isEmpty());
-				}
-			}, null);
+			IValueProperty p = JFaceProperties.value(IAction.class, IAction.ENABLED, IAction.ENABLED);
+			bindingContext.bindValue(SWTObservables.observeEnabled(getRemoveButton()), p.observe(removeAction));
 
 			//Generic action for remove button
 			getRemoveButton().addSelectionListener(new SelectionAdapter() {
 				public void widgetSelected(SelectionEvent e) {
-					Object sel = ((IStructuredSelection) getTreeViewer().getSelection()).getFirstElement();
-					if (sel != null) {
-						Command c = RemoveCommand.create(getEditor().getEditingDomain(), sel);
-						getEditor().getEditingDomain().getCommandStack().execute(c);
-					}
+					removeAction.run();
 				}
 			});
 		}
 
-		if (showToolbarButtons()) {
-
-			//Enable action when the tree selection is not empty
-			ViewersObservables.observeSingleSelection(getTreeViewer()).addValueChangeListener(new IValueChangeListener() {
-				public void handleValueChange(ValueChangeEvent event) {
-					boolean bool = !(getTreeViewer().getSelection().isEmpty());
-					removeAction.setEnabled(bool);
-				}
-			});
-
-		}
-
 		createContextMenuFor(treeViewer);
 
 		//update Editor selection
@@ -261,9 +230,15 @@
 		return null;
 	}
 
+	/**
+	 * Create the Action to be performed when a deletion should be performed.
+	 * Default implementation create a {@link RemoveAction} that delegates the job to the DELETE action
+	 * registered through the {@link EmfActionBarContributor} of the current {@link EmfFormEditor} 
+	 * Subclasses may override this method in order to provide their own Action
+	 * @return Action the delete action to perform 
+	 */
 	protected Action createCustomToolbarRemoveAction() {
-		// Subclass may override this method
-		return null;
+		return new RemoveAction(this);
 	}
 
 	protected Button createButton(Composite parent, String btnText) {
diff --git a/modeling/plugins/org.eclipse.pde.emfforms/src/org/eclipse/pde/emfforms/editor/actions/AbstractRemoveAction.java b/modeling/plugins/org.eclipse.pde.emfforms/src/org/eclipse/pde/emfforms/editor/actions/AbstractRemoveAction.java
deleted file mode 100644
index 4483a88..0000000
--- a/modeling/plugins/org.eclipse.pde.emfforms/src/org/eclipse/pde/emfforms/editor/actions/AbstractRemoveAction.java
+++ /dev/null
@@ -1,82 +0,0 @@
-/**
- * Copyright (c) 2009 Anyware Technologies and others.
- * 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:
- *     Anyware Technologies - initial API and implementation
- *
- * $Id: EmfMasterDetailBlock.java,v 1.12 2009/08/07 16:25:33 bcabe Exp $
- */
-package org.eclipse.pde.emfforms.editor.actions;
-
-import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
-
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.edit.domain.EditingDomain;
-import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
-import org.eclipse.jface.action.Action;
-import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.jface.resource.ImageDescriptor;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.jface.viewers.TreeViewer;
-import org.eclipse.pde.emfforms.editor.EmfMasterDetailBlock;
-import org.eclipse.pde.emfforms.internal.Activator;
-import org.eclipse.pde.emfforms.internal.editor.IEmfFormsImages;
-import org.eclipse.ui.PlatformUI;
-
-public abstract class AbstractRemoveAction extends Action {
-
-	private EmfMasterDetailBlock masterDetail;
-
-	public AbstractRemoveAction(EmfMasterDetailBlock masterDetail) {
-		super();
-		this.masterDetail = masterDetail;
-		setText("Remove");
-		setToolTipText("Remove selected element");
-	}
-
-	public void run() {
-		TreeViewer treeViewer = masterDetail.getTreeViewer();
-
-		if (!treeViewer.getSelection().isEmpty()) {
-
-			int selIndex = treeViewer.getTree().indexOf(treeViewer.getTree().getSelection()[0]);
-
-			EObject sel = (EObject) AdapterFactoryEditingDomain.unwrap((((IStructuredSelection) treeViewer.getSelection()).getFirstElement()));
-			AdapterFactoryLabelProvider labelProvider = new AdapterFactoryLabelProvider(masterDetail.getEditor().getAdapterFactory());
-
-			// -- Confirmation Dialog
-			String title = "Delete the current selection"; //$NON-NLS-1$
-			String message = "Do you really want to delete '" + labelProvider.getText(sel) + "' ?"; //$NON-NLS-1$ //$NON-NLS-2$
-
-			if (MessageDialog.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), title, message)) {
-				EditingDomain editingDomain = masterDetail.getEditor().getEditingDomain();
-				// perform remove method
-				removeObject(sel, editingDomain);
-
-				treeViewer.refresh();
-				if (treeViewer.getTree().getItemCount() > 0) {
-
-					// if we delete the last line, select the new last
-					// line
-					if (selIndex >= treeViewer.getTree().getItemCount())
-						selIndex = selIndex - 1;
-
-					treeViewer.getTree().setSelection(treeViewer.getTree().getItem(selIndex));
-					treeViewer.getTree().setFocus();
-				}
-			}
-		}
-	}
-
-	protected abstract void removeObject(EObject sel, EditingDomain editingDomain);
-
-	@Override
-	public ImageDescriptor getImageDescriptor() {
-		return ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource(IEmfFormsImages.REMOVE_TOOLBAR_BUTTON));
-	}
-
-}
diff --git a/modeling/plugins/org.eclipse.pde.emfforms/src/org/eclipse/pde/emfforms/editor/actions/RemoveAction.java b/modeling/plugins/org.eclipse.pde.emfforms/src/org/eclipse/pde/emfforms/editor/actions/RemoveAction.java
new file mode 100644
index 0000000..8a903c4
--- /dev/null
+++ b/modeling/plugins/org.eclipse.pde.emfforms/src/org/eclipse/pde/emfforms/editor/actions/RemoveAction.java
@@ -0,0 +1,105 @@
+/**
+ * Copyright (c) 2010 Sierra Wireless and others.
+ * 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:
+ *     Jacques Lescot, Sierra Wireless - initial API and implementation (bug 300462)
+ *
+ * $Id: RemoveAction.java,v 1.1 2009/08/20 17:22:09 bcabe Exp $
+ */
+package org.eclipse.pde.emfforms.editor.actions;
+
+import org.eclipse.emf.edit.ui.action.EditingDomainActionBarContributor;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.util.IPropertyChangeListener;
+import org.eclipse.jface.viewers.TreeViewer;
+import org.eclipse.pde.emfforms.editor.EmfActionBarContributor;
+import org.eclipse.pde.emfforms.editor.EmfMasterDetailBlock;
+import org.eclipse.pde.emfforms.internal.Activator;
+import org.eclipse.pde.emfforms.internal.editor.IEmfFormsImages;
+
+public class RemoveAction extends Action {
+
+	public void addPropertyChangeListener(IPropertyChangeListener listener) {
+		getDeleteAction().addPropertyChangeListener(listener);
+	}
+
+	public int getAccelerator() {
+		return getDeleteAction().getAccelerator();
+	}
+
+	public String getActionDefinitionId() {
+		return getDeleteAction().getActionDefinitionId();
+	}
+
+	public String getDescription() {
+		return getDeleteAction().getDescription();
+	}
+
+	public String getToolTipText() {
+		return getDeleteAction().getToolTipText();
+	}
+
+	@Override
+	public void setEnabled(boolean enabled) {
+		getDeleteAction().setEnabled(enabled);
+	}
+
+	public boolean isEnabled() {
+		return getDeleteAction().isEnabled();
+	}
+
+	@Override
+	public String getText() {
+		return getDeleteAction().getText();
+	}
+
+	@Override
+	public void run() {
+		TreeViewer treeViewer = masterDetail.getTreeViewer();
+		int selIndex = treeViewer.getTree().indexOf(treeViewer.getTree().getSelection()[0]);
+
+		getDeleteAction().run();
+
+		treeViewer.refresh();
+		if (treeViewer.getTree().getItemCount() > 0) {
+			// if we delete the last line, select the new last line
+			if (selIndex >= treeViewer.getTree().getItemCount()) {
+				selIndex = selIndex - 1;
+			}
+			// treeViewer.getTree().indexOf(TreeItem) method is returning -1 when the provided item is not a root element ...
+			// In that case we select the first root element.
+			if (selIndex > -1)
+				treeViewer.getTree().setSelection(treeViewer.getTree().getItem(selIndex));
+			else
+				treeViewer.getTree().setSelection(treeViewer.getTree().getItem(0));
+			treeViewer.getTree().setFocus();
+		}
+	}
+
+	private EmfMasterDetailBlock masterDetail;
+
+	public RemoveAction(EmfMasterDetailBlock masterDetail) {
+		super();
+		this.masterDetail = masterDetail;
+		getDeleteAction();
+
+	}
+
+	private IAction getDeleteAction() {
+		EditingDomainActionBarContributor actionBarContributor = masterDetail.getEditor().getActionBarContributor();
+		if (actionBarContributor != null && actionBarContributor instanceof EmfActionBarContributor)
+			return ((EmfActionBarContributor) actionBarContributor).getDeleteAction();
+		return null;
+	}
+
+	@Override
+	public ImageDescriptor getImageDescriptor() {
+		return ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource(IEmfFormsImages.REMOVE_TOOLBAR_BUTTON));
+	}
+}
diff --git a/modeling/plugins/org.eclipse.pde.emfforms/src/org/eclipse/pde/emfforms/internal/actions/RemoveAction.java b/modeling/plugins/org.eclipse.pde.emfforms/src/org/eclipse/pde/emfforms/internal/actions/RemoveAction.java
deleted file mode 100644
index 69579ff..0000000
--- a/modeling/plugins/org.eclipse.pde.emfforms/src/org/eclipse/pde/emfforms/internal/actions/RemoveAction.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/**
- * Copyright (c) 2009 Anyware Technologies and others.
- * 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:
- *     Anyware Technologies - initial API and implementation
- *
- * $Id: EmfMasterDetailBlock.java,v 1.12 2009/08/07 16:25:33 bcabe Exp $
- */
-package org.eclipse.pde.emfforms.internal.actions;
-
-import org.eclipse.emf.common.command.Command;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.edit.command.RemoveCommand;
-import org.eclipse.emf.edit.domain.EditingDomain;
-import org.eclipse.pde.emfforms.editor.EmfMasterDetailBlock;
-import org.eclipse.pde.emfforms.editor.actions.AbstractRemoveAction;
-
-public class RemoveAction extends AbstractRemoveAction {
-
-	public RemoveAction(EmfMasterDetailBlock masterDetail) {
-		super(masterDetail);
-	}
-
-	protected void removeObject(EObject sel, EditingDomain editingDomain) {
-		Command c = RemoveCommand.create(editingDomain, sel);
-		editingDomain.getCommandStack().execute(c);
-	}
-
-}