summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenoit Maggi2014-06-12 04:15:21 (EDT)
committerCamille Letavernier2014-08-08 05:29:56 (EDT)
commit6c9c17e521259fc5243604e126cc5dc75403762c (patch)
tree0fa51747ac1428e17681802e7ea1439fb26b4184
parentd9a46275fa68885cef100fee66d3a12640605d78 (diff)
downloadorg.eclipse.papyrus-6c9c17e521259fc5243604e126cc5dc75403762c.zip
org.eclipse.papyrus-6c9c17e521259fc5243604e126cc5dc75403762c.tar.gz
org.eclipse.papyrus-6c9c17e521259fc5243604e126cc5dc75403762c.tar.bz2
Bug 431298 - NPE in ShowHideRelatedContentsHandlerrefs/changes/00/28400/2
- remove some NPE on PlatformUI.getWorkbench(). getActiveWorkbenchWindow().getSelectionService() Change-Id: I8fdd6f594a866c9ee558b398c3f1aab273255664 Signed-off-by: Benoit Maggi <benoit.maggi@cea.fr>
-rw-r--r--extraplugins/marte/textedit/org.eclipse.papyrus.marte.textedit.stereotypeapplicationwithvsl.xtext.ui/src/org/eclipse/papyrus/marte/textedit/stereotypeapplicationwithvsl/xtext/ui/contributions/OpenTextualEditorForStereotypeApplications.java288
-rw-r--r--plugins/infra/nattable/org.eclipse.papyrus.infra.nattable.modelexplorer/src/org/eclipse/papyrus/infra/nattable/modelexplorer/handlers/AbstractTableCommandHandler.java121
-rw-r--r--plugins/infra/services/org.eclipse.papyrus.infra.services.validation/src/org/eclipse/papyrus/infra/services/validation/handler/AbstractCommandHandler.java339
3 files changed, 380 insertions, 368 deletions
diff --git a/extraplugins/marte/textedit/org.eclipse.papyrus.marte.textedit.stereotypeapplicationwithvsl.xtext.ui/src/org/eclipse/papyrus/marte/textedit/stereotypeapplicationwithvsl/xtext/ui/contributions/OpenTextualEditorForStereotypeApplications.java b/extraplugins/marte/textedit/org.eclipse.papyrus.marte.textedit.stereotypeapplicationwithvsl.xtext.ui/src/org/eclipse/papyrus/marte/textedit/stereotypeapplicationwithvsl/xtext/ui/contributions/OpenTextualEditorForStereotypeApplications.java
index 100cf00..3ac3267 100644
--- a/extraplugins/marte/textedit/org.eclipse.papyrus.marte.textedit.stereotypeapplicationwithvsl.xtext.ui/src/org/eclipse/papyrus/marte/textedit/stereotypeapplicationwithvsl/xtext/ui/contributions/OpenTextualEditorForStereotypeApplications.java
+++ b/extraplugins/marte/textedit/org.eclipse.papyrus.marte.textedit.stereotypeapplicationwithvsl.xtext.ui/src/org/eclipse/papyrus/marte/textedit/stereotypeapplicationwithvsl/xtext/ui/contributions/OpenTextualEditorForStereotypeApplications.java
@@ -1,143 +1,145 @@
-/*****************************************************************************
- * Copyright (c) 2010 CEA LIST.
- *
- *
- * 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:
- * CEA LIST - Initial API and implementation
- *
- *****************************************************************************/
-package org.eclipse.papyrus.marte.textedit.stereotypeapplicationwithvsl.xtext.ui.contributions;
-
-import java.util.Iterator;
-import java.util.List;
-import java.util.Vector;
-
-import org.eclipse.core.commands.AbstractHandler;
-import org.eclipse.core.commands.ExecutionEvent;
-import org.eclipse.core.runtime.IAdaptable;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.gef.GraphicalEditPart;
-import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Shell;
-import org.eclipse.ui.IObjectActionDelegate;
-import org.eclipse.ui.IWorkbenchPart;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.uml2.uml.Element;
-
-public class OpenTextualEditorForStereotypeApplications extends AbstractHandler implements IObjectActionDelegate {
-
- public OpenTextualEditorForStereotypeApplications() {
- // TODO Auto-generated constructor stub
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Object execute(ExecutionEvent event) {
- Display current = Display.getCurrent();
-
- if(current == null) {
- current = Display.getDefault();
- }
- Shell shell = current.getActiveShell();
-
- if(shell != null) {
- updateSelectedEObject();
- } else {
- // Activator.log.error("impossible to find a shell to open the message dialog", null);
- }
-
- return null;
- }
-
- /**
- * Convert each selected elements from the explorator menu, or modeling view
- * to an EObject and add it to a list
- *
- * @return list of EObject
- */
- public void updateSelectedEObject() {
- Vector<EObject> currentSelectedEObjects = new Vector<EObject>();
-
- // Retrieve selected elements
- IStructuredSelection selection = (IStructuredSelection)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
-
-
- Iterator<?> eltIt = selection.iterator();
-
- while(eltIt.hasNext()) {
- Object currentObject = eltIt.next();
-
- // If the object is an edit part, try to get semantic bridge
- if(currentObject instanceof GraphicalEditPart) {
- GraphicalEditPart editPart = (GraphicalEditPart)currentObject;
- EObject semantic = EMFHelper.getEObject(editPart);
- if(semantic instanceof Element) {
- //FIXME: The new XText integration doesn't support direct dialog creation on an edit part (The TextAwareEditPart is supposed to handle this case itself)
- //DirectEditManager manager = new StereotypeApplicationPopupEditorConfigurationContribution().createDirectEditManager(editPart);
- }
- }
-
- // check whether part of model explorer
- if(currentObject instanceof IAdaptable) {
- // modisco ModelElementItem supports IAdaptable (cleaner than cast / dependency with modisco)
- currentObject = ((IAdaptable)currentObject).getAdapter(EObject.class);
- }
-
- // If element is a UML Element
- if(currentObject instanceof Element) {
- currentSelectedEObjects.add((EObject)currentObject);
- }
-
- }
- selectedEObjects = currentSelectedEObjects;
- }
-
- /**
- * Convert each selected elements from the explorator menu, or modeling view
- * to an EObject and add it to a list
- *
- * @return list of EObject
- */
- public EObject getSelectedEObject() {
- if(selectedEObjects.size() > 0) {
- return selectedEObjects.get(0);
- }
- return null;
- }
-
- public List<EObject> getSelectedEObjects() {
- return selectedEObjects;
- }
-
- private List<EObject> selectedEObjects;
-
-
- @Override
- public void run(IAction action) {
- // TODO Auto-generated method stub
- this.execute(null);
- }
-
-
- @Override
- public void selectionChanged(IAction action, ISelection selection) {
- }
-
-
- @Override
- public void setActivePart(IAction action, IWorkbenchPart targetPart) {
- }
-
-}
+/*****************************************************************************
+ * Copyright (c) 2010 CEA LIST.
+ *
+ *
+ * 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:
+ * CEA LIST - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.marte.textedit.stereotypeapplicationwithvsl.xtext.ui.contributions;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Vector;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.gef.GraphicalEditPart;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IObjectActionDelegate;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.uml2.uml.Element;
+
+public class OpenTextualEditorForStereotypeApplications extends AbstractHandler implements IObjectActionDelegate {
+
+ public OpenTextualEditorForStereotypeApplications() {
+ // TODO Auto-generated constructor stub
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Object execute(ExecutionEvent event) {
+ Display current = Display.getCurrent();
+
+ if(current == null) {
+ current = Display.getDefault();
+ }
+ Shell shell = current.getActiveShell();
+
+ if(shell != null) {
+ updateSelectedEObject();
+ } else {
+ // Activator.log.error("impossible to find a shell to open the message dialog", null);
+ }
+
+ return null;
+ }
+
+ /**
+ * Convert each selected elements from the explorator menu, or modeling view
+ * to an EObject and add it to a list
+ *
+ * @return list of EObject
+ */
+ public void updateSelectedEObject() {
+ Vector<EObject> currentSelectedEObjects = new Vector<EObject>();
+
+ // Retrieve selected elements
+ IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+ if (activeWorkbenchWindow != null){
+ IStructuredSelection selection = (IStructuredSelection)activeWorkbenchWindow.getSelectionService().getSelection();
+ Iterator<?> eltIt = selection.iterator();
+
+ while(eltIt.hasNext()) {
+ Object currentObject = eltIt.next();
+
+ // If the object is an edit part, try to get semantic bridge
+ if(currentObject instanceof GraphicalEditPart) {
+ GraphicalEditPart editPart = (GraphicalEditPart)currentObject;
+ EObject semantic = EMFHelper.getEObject(editPart);
+ if(semantic instanceof Element) {
+ //FIXME: The new XText integration doesn't support direct dialog creation on an edit part (The TextAwareEditPart is supposed to handle this case itself)
+ //DirectEditManager manager = new StereotypeApplicationPopupEditorConfigurationContribution().createDirectEditManager(editPart);
+ }
+ }
+
+ // check whether part of model explorer
+ if(currentObject instanceof IAdaptable) {
+ // modisco ModelElementItem supports IAdaptable (cleaner than cast / dependency with modisco)
+ currentObject = ((IAdaptable)currentObject).getAdapter(EObject.class);
+ }
+
+ // If element is a UML Element
+ if(currentObject instanceof Element) {
+ currentSelectedEObjects.add((EObject)currentObject);
+ }
+ }
+ }
+
+ selectedEObjects = currentSelectedEObjects;
+ }
+
+ /**
+ * Convert each selected elements from the explorator menu, or modeling view
+ * to an EObject and add it to a list
+ *
+ * @return list of EObject
+ */
+ public EObject getSelectedEObject() {
+ if(selectedEObjects.size() > 0) {
+ return selectedEObjects.get(0);
+ }
+ return null;
+ }
+
+ public List<EObject> getSelectedEObjects() {
+ return selectedEObjects;
+ }
+
+ private List<EObject> selectedEObjects;
+
+
+ @Override
+ public void run(IAction action) {
+ // TODO Auto-generated method stub
+ this.execute(null);
+ }
+
+
+ @Override
+ public void selectionChanged(IAction action, ISelection selection) {
+ }
+
+
+ @Override
+ public void setActivePart(IAction action, IWorkbenchPart targetPart) {
+ }
+
+}
diff --git a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable.modelexplorer/src/org/eclipse/papyrus/infra/nattable/modelexplorer/handlers/AbstractTableCommandHandler.java b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable.modelexplorer/src/org/eclipse/papyrus/infra/nattable/modelexplorer/handlers/AbstractTableCommandHandler.java
index 62f0c61..f94401b 100644
--- a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable.modelexplorer/src/org/eclipse/papyrus/infra/nattable/modelexplorer/handlers/AbstractTableCommandHandler.java
+++ b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable.modelexplorer/src/org/eclipse/papyrus/infra/nattable/modelexplorer/handlers/AbstractTableCommandHandler.java
@@ -1,59 +1,62 @@
-/*****************************************************************************
- * Copyright (c) 2013 CEA LIST.
- *
- * 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:
- * Juan Cadavid (CEA LIST) juan.cadavid@cea.fr - Initial API and implementation
- *****************************************************************************/
-package org.eclipse.papyrus.infra.nattable.modelexplorer.handlers;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.papyrus.infra.nattable.model.nattable.Table;
-import org.eclipse.papyrus.views.modelexplorer.NavigatorUtils;
-import org.eclipse.papyrus.views.modelexplorer.handler.AbstractCommandHandler;
-import org.eclipse.ui.PlatformUI;
-
-public abstract class AbstractTableCommandHandler extends AbstractCommandHandler {
-
- /**
- * Returns the list of selected tables
- *
- * @return the list of selected tables
- */
- protected List<Table> getSelectedTables() {
- List<Table> tables = new ArrayList<Table>();
- ISelection selection = null;
-
- // Get current selection
- selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
-
- // Get first element if the selection is an IStructuredSelection
- if(selection instanceof IStructuredSelection) {
- IStructuredSelection structuredSelection = (IStructuredSelection)selection;
- Iterator<?> iter = structuredSelection.iterator();
- while(iter.hasNext()) {
- Object current = iter.next();
- /**
- * Get the table object. This getElement is used in order to
- * use IAdaptable mechanisme For example for Facet Elements
- */
- EObject table = NavigatorUtils.getElement(current, EObject.class);
- if(table instanceof Table) {
- tables.add((Table)table);
- }
- }
- }
-
- return tables;
- }
-}
+/*****************************************************************************
+ * Copyright (c) 2013 CEA LIST.
+ *
+ * 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:
+ * Juan Cadavid (CEA LIST) juan.cadavid@cea.fr - Initial API and implementation
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.nattable.modelexplorer.handlers;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.papyrus.infra.nattable.model.nattable.Table;
+import org.eclipse.papyrus.views.modelexplorer.NavigatorUtils;
+import org.eclipse.papyrus.views.modelexplorer.handler.AbstractCommandHandler;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+
+public abstract class AbstractTableCommandHandler extends AbstractCommandHandler {
+
+ /**
+ * Returns the list of selected tables
+ *
+ * @return the list of selected tables
+ */
+ protected List<Table> getSelectedTables() {
+ List<Table> tables = new ArrayList<Table>();
+ ISelection selection = null;
+
+ // Get current selection
+ IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+ if (activeWorkbenchWindow != null) {
+ selection = activeWorkbenchWindow.getSelectionService().getSelection();
+
+ // Get first element if the selection is an IStructuredSelection
+ if(selection instanceof IStructuredSelection) {
+ IStructuredSelection structuredSelection = (IStructuredSelection)selection;
+ Iterator<?> iter = structuredSelection.iterator();
+ while(iter.hasNext()) {
+ Object current = iter.next();
+ /**
+ * Get the table object. This getElement is used in order to
+ * use IAdaptable mechanisme For example for Facet Elements
+ */
+ EObject table = NavigatorUtils.getElement(current, EObject.class);
+ if(table instanceof Table) {
+ tables.add((Table)table);
+ }
+ }
+ }
+ }
+ return tables;
+ }
+}
diff --git a/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/src/org/eclipse/papyrus/infra/services/validation/handler/AbstractCommandHandler.java b/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/src/org/eclipse/papyrus/infra/services/validation/handler/AbstractCommandHandler.java
index 61a73d7..1ba02c6 100644
--- a/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/src/org/eclipse/papyrus/infra/services/validation/handler/AbstractCommandHandler.java
+++ b/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/src/org/eclipse/papyrus/infra/services/validation/handler/AbstractCommandHandler.java
@@ -1,166 +1,173 @@
-/*****************************************************************************
- * Copyright (c) 2010 CEA LIST.
- *
- *
- * 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:
- *
- * Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
- * Vincent Lorenzo (CEA-LIST) vincent.lorenzo@cea.fr
- *****************************************************************************/
-package org.eclipse.papyrus.infra.services.validation.handler;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.eclipse.core.commands.AbstractHandler;
-import org.eclipse.core.commands.ExecutionEvent;
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.runtime.IAdaptable;
-import org.eclipse.emf.common.command.Command;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.papyrus.infra.emf.utils.BusinessModelResolver;
-import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
-import org.eclipse.ui.PlatformUI;
-
-/**
- * <pre>
- *
- * This abstract command handler manages:
- * - current selection in order to build a list of the selected {@link EObject}
- * - execute the command (returned by children)
- * - calculate the command enablement and visibility regarding the command executability
- * (the command is now shown in menu if not executable).
- *
- * </pre>
- */
-public abstract class AbstractCommandHandler extends AbstractHandler {
-
- /**
- * <pre>
- *
- * Returns the command to execute (to be implemented
- * in children implementing this class)
- *
- * @return the command to execute
- *
- * </pre>
- */
- protected abstract Command getCommand();
-
- /**
- * <pre>
- * Get the selected element, the first selected element if several are selected or null
- * if no selection or the selection is not an {@link EObject}.
- *
- * @return selected {@link EObject} or null
- * </pre>
- *
- */
- protected EObject getSelectedElement() {
- EObject eObject = null;
- Object selection = null;
-
- // Get current selection
- selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
-
- // Get first element if the selection is an IStructuredSelection
- if(selection instanceof IStructuredSelection) {
- IStructuredSelection structuredSelection = (IStructuredSelection)selection;
- selection = structuredSelection.getFirstElement();
- }
-
- // Treat non-null selected object (try to adapt and return EObject)
- if(selection != null) {
- if(selection instanceof IAdaptable) {
- selection = EMFHelper.getEObject(selection);
- }
-
- Object businessObject = BusinessModelResolver.getInstance().getBusinessModel(selection);
- if(businessObject instanceof EObject) {
- eObject = (EObject)businessObject;
- }
- }
- return eObject;
- }
-
- /**
- * <pre>
- * Parse current selection and extract the list of {@link EObject} from
- * this selection.
- *
- * This also tries to adapt selected element into {@link EObject}
- * (for example to get the {@link EObject} from a selection in the ModelExplorer).
- *
- * @return a list of currently selected {@link EObject}
- * </pre>
- *
- */
- protected List<EObject> getSelectedElements() {
-
- List<EObject> selectedEObjects = new ArrayList<EObject>();
-
- // Parse current selection
- ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
- if(selection instanceof IStructuredSelection) {
- IStructuredSelection structuredSelection = (IStructuredSelection)selection;
- for(Object current : structuredSelection.toArray()) {
- // Adapt current selection to EObject
- if(current instanceof IAdaptable) {
- selectedEObjects.add(EMFHelper.getEObject(current));
- }
- }
- } else { // Not a IStructuredSelection
- if(selection != null) {
- // Adapt current selection to EObject
- if(selection instanceof IAdaptable) {
- selectedEObjects.add(EMFHelper.getEObject(selection));
- }
- }
- }
-
- return selectedEObjects;
- }
-
- /**
- *
- * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
- *
- * @param event
- * @return null
- * @throws ExecutionException
- */
- public Object execute(ExecutionEvent event) throws ExecutionException {
- // try {
- // ServiceUtilsForActionHandlers util = new ServiceUtilsForActionHandlers();
- // util.getTransactionalEditingDomain().getCommandStack().execute(getCommand());
- //
- // } catch (ServiceException e) {
- // Activator.log.error("Unexpected error while executing command.", e); //$NON-NLS-1$
- // }
-
- getCommand().execute(); //Validation commands should not be executed in the stack trace
-
- return null;
- }
- @Override
- public void setEnabled(Object evaluationContext) {
- setBaseEnabled(getCommand().canExecute());
- }
-
- /**
- *
- * @return true (visible) when the command can be executed.
- */
- public boolean isVisible() {
- return getCommand().canExecute();
- }
-
-
-}
+/*****************************************************************************
+ * Copyright (c) 2010 CEA LIST.
+ *
+ *
+ * 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:
+ *
+ * Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
+ * Vincent Lorenzo (CEA-LIST) vincent.lorenzo@cea.fr
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.services.validation.handler;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.emf.common.command.Command;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.papyrus.infra.emf.utils.BusinessModelResolver;
+import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+
+/**
+ * <pre>
+ *
+ * This abstract command handler manages:
+ * - current selection in order to build a list of the selected {@link EObject}
+ * - execute the command (returned by children)
+ * - calculate the command enablement and visibility regarding the command executability
+ * (the command is now shown in menu if not executable).
+ *
+ * </pre>
+ */
+public abstract class AbstractCommandHandler extends AbstractHandler {
+
+ /**
+ * <pre>
+ *
+ * Returns the command to execute (to be implemented
+ * in children implementing this class)
+ *
+ * @return the command to execute
+ *
+ * </pre>
+ */
+ protected abstract Command getCommand();
+
+ /**
+ * <pre>
+ * Get the selected element, the first selected element if several are selected or null
+ * if no selection or the selection is not an {@link EObject}.
+ *
+ * @return selected {@link EObject} or null
+ * </pre>
+ *
+ */
+ protected EObject getSelectedElement() {
+ EObject eObject = null;
+ Object selection = null;
+
+
+ IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+ if (activeWorkbenchWindow != null){
+ // Get current selection
+ selection = activeWorkbenchWindow.getSelectionService().getSelection();
+
+ // Get first element if the selection is an IStructuredSelection
+ if(selection instanceof IStructuredSelection) {
+ IStructuredSelection structuredSelection = (IStructuredSelection)selection;
+ selection = structuredSelection.getFirstElement();
+ }
+
+ // Treat non-null selected object (try to adapt and return EObject)
+ if(selection != null) {
+ if(selection instanceof IAdaptable) {
+ selection = EMFHelper.getEObject(selection);
+ }
+
+ Object businessObject = BusinessModelResolver.getInstance().getBusinessModel(selection);
+ if(businessObject instanceof EObject) {
+ eObject = (EObject)businessObject;
+ }
+ }
+ }
+ return eObject;
+ }
+
+ /**
+ * <pre>
+ * Parse current selection and extract the list of {@link EObject} from
+ * this selection.
+ *
+ * This also tries to adapt selected element into {@link EObject}
+ * (for example to get the {@link EObject} from a selection in the ModelExplorer).
+ *
+ * @return a list of currently selected {@link EObject}
+ * </pre>
+ *
+ */
+ protected List<EObject> getSelectedElements() {
+
+ List<EObject> selectedEObjects = new ArrayList<EObject>();
+
+ // Parse current selection
+ IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+ if (activeWorkbenchWindow != null){
+ ISelection selection = activeWorkbenchWindow.getSelectionService().getSelection();
+ if(selection instanceof IStructuredSelection) {
+ IStructuredSelection structuredSelection = (IStructuredSelection)selection;
+ for(Object current : structuredSelection.toArray()) {
+ // Adapt current selection to EObject
+ if(current instanceof IAdaptable) {
+ selectedEObjects.add(EMFHelper.getEObject(current));
+ }
+ }
+ } else { // Not a IStructuredSelection
+ if(selection != null) {
+ // Adapt current selection to EObject
+ if(selection instanceof IAdaptable) {
+ selectedEObjects.add(EMFHelper.getEObject(selection));
+ }
+ }
+ }
+ }
+ return selectedEObjects;
+ }
+
+ /**
+ *
+ * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
+ *
+ * @param event
+ * @return null
+ * @throws ExecutionException
+ */
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ // try {
+ // ServiceUtilsForActionHandlers util = new ServiceUtilsForActionHandlers();
+ // util.getTransactionalEditingDomain().getCommandStack().execute(getCommand());
+ //
+ // } catch (ServiceException e) {
+ // Activator.log.error("Unexpected error while executing command.", e); //$NON-NLS-1$
+ // }
+
+ getCommand().execute(); //Validation commands should not be executed in the stack trace
+
+ return null;
+ }
+ @Override
+ public void setEnabled(Object evaluationContext) {
+ setBaseEnabled(getCommand().canExecute());
+ }
+
+ /**
+ *
+ * @return true (visible) when the command can be executed.
+ */
+ public boolean isVisible() {
+ return getCommand().canExecute();
+ }
+
+
+}