Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCamille Letavernier2013-10-28 09:24:37 -0400
committerCamille Letavernier2013-10-28 09:24:37 -0400
commit463c216e3df017b4d893bb3886642497aeff94ba (patch)
tree513862569f96487895e0c96ec272ace18bb8dc6c /plugins
parent99c85a43de4733d08796a99c5892eaf26ce50bdd (diff)
downloadorg.eclipse.papyrus-463c216e3df017b4d893bb3886642497aeff94ba.tar.gz
org.eclipse.papyrus-463c216e3df017b4d893bb3886642497aeff94ba.tar.xz
org.eclipse.papyrus-463c216e3df017b4d893bb3886642497aeff94ba.zip
420199: [All diagrams] Create Diagram Toolbar is flickering
https://bugs.eclipse.org/bugs/show_bug.cgi?id=420199 Fix regression & implement the "enabled" state
Diffstat (limited to 'plugins')
-rw-r--r--plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.navigation/src/org/eclipse/papyrus/infra/gmfdiag/navigation/CreateDiagramWithNavigationHandler.java82
-rw-r--r--plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/plugin.xml61
-rw-r--r--plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/util/UMLModelTester.java120
3 files changed, 192 insertions, 71 deletions
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.navigation/src/org/eclipse/papyrus/infra/gmfdiag/navigation/CreateDiagramWithNavigationHandler.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.navigation/src/org/eclipse/papyrus/infra/gmfdiag/navigation/CreateDiagramWithNavigationHandler.java
index 3f06d81d76b..3bc66d3ed60 100644
--- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.navigation/src/org/eclipse/papyrus/infra/gmfdiag/navigation/CreateDiagramWithNavigationHandler.java
+++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.navigation/src/org/eclipse/papyrus/infra/gmfdiag/navigation/CreateDiagramWithNavigationHandler.java
@@ -13,12 +13,14 @@
*****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.navigation;
+import java.util.Collection;
import java.util.Collections;
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.expressions.IEvaluationContext;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.jface.dialogs.IDialogConstants;
@@ -33,11 +35,12 @@ import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
import org.eclipse.papyrus.infra.emf.utils.BusinessModelResolver;
+import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForHandlers;
import org.eclipse.papyrus.infra.widgets.toolbox.dialog.InformationDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
-import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.handlers.HandlerUtil;
/**
* This command handler will try to create a diagram on the currently selected
@@ -60,13 +63,8 @@ public abstract class CreateDiagramWithNavigationHandler extends AbstractHandler
this.creationCondition = creationCondition;
}
- @Override
- public boolean isEnabled() {
- return true;
- }
-
public Object execute(ExecutionEvent event) throws ExecutionException {
- NavigableElement navElement = getNavigableElementWhereToCreateDiagram();
+ NavigableElement navElement = getNavigableElementWhereToCreateDiagram(event);
ServicesRegistry registry;
try {
registry = ServiceUtilsForHandlers.getInstance().getServiceRegistry(event);
@@ -84,27 +82,35 @@ public abstract class CreateDiagramWithNavigationHandler extends AbstractHandler
return null;
}
- private NavigableElement getNavigableElementWhereToCreateDiagram() {
- EObject selectedElement = getSelectedElement();
-
- if(selectedElement != null) {
- // First check if the current element can host the requested diagram
- if(creationCondition.create(selectedElement)) {
- return new ExistingNavigableElement(selectedElement, null);
- } else {
- List<NavigableElement> navElements = NavigationHelper.getInstance().getAllNavigableElements(selectedElement);
- // this will sort elements by navigation depth
- Collections.sort(navElements);
-
- for(NavigableElement navElement : navElements) {
- // ignore existing elements because we want a hierarchy to
- // be created if it is not on the current element
- if(navElement instanceof CreatedNavigableElement && creationCondition.create(navElement.getElement())) {
- return navElement;
+ private NavigableElement getNavigableElementWhereToCreateDiagram(ExecutionEvent event) {
+ ISelection selection = HandlerUtil.getCurrentSelection(event);
+ if(selection.isEmpty()) {
+ return null;
+ }
+
+ if(selection instanceof IStructuredSelection) {
+ EObject selectedElement = EMFHelper.getEObject(((IStructuredSelection)selection).getFirstElement());
+
+ if(selectedElement != null) {
+ // First check if the current element can host the requested diagram
+ if(creationCondition.create(selectedElement)) {
+ return new ExistingNavigableElement(selectedElement, null);
+ } else {
+ List<NavigableElement> navElements = NavigationHelper.getInstance().getAllNavigableElements(selectedElement);
+ // this will sort elements by navigation depth
+ Collections.sort(navElements);
+
+ for(NavigableElement navElement : navElements) {
+ // ignore existing elements because we want a hierarchy to
+ // be created if it is not on the current element
+ if(navElement instanceof CreatedNavigableElement && creationCondition.create(navElement.getElement())) {
+ return navElement;
+ }
}
}
}
}
+
return null;
}
@@ -126,15 +132,6 @@ public abstract class CreateDiagramWithNavigationHandler extends AbstractHandler
}
}
- private EObject getSelectedElement() {
- ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
- if(selection instanceof IStructuredSelection) {
- Object obj = ((IStructuredSelection)selection).getFirstElement();
- return resolveSemanticObject(obj);
- }
- return null;
- }
-
/**
* Resolve semantic element
*
@@ -154,4 +151,23 @@ public abstract class CreateDiagramWithNavigationHandler extends AbstractHandler
return null;
}
+ @Override
+ public void setEnabled(Object evaluationContext) {
+ if(evaluationContext instanceof IEvaluationContext) {
+ Object selectionVariable = ((IEvaluationContext)evaluationContext).getDefaultVariable();
+
+ if(selectionVariable instanceof Collection<?>) {
+ List<?> selection = (selectionVariable instanceof List<?>) ? (List<?>)selectionVariable : new java.util.ArrayList<Object>((Collection<?>)selectionVariable);
+ if(selection.size() != 1) {
+ setBaseEnabled(false);
+ return;
+ }
+
+ EObject target = EMFHelper.getEObject(selection.get(0));
+ setBaseEnabled(creationCondition.create(target));
+ }
+ }
+ super.setEnabled(evaluationContext);
+ }
+
}
diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/plugin.xml b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/plugin.xml
index 27d6abc5373..ada1123b5b3 100644
--- a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/plugin.xml
+++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/plugin.xml
@@ -249,6 +249,13 @@
properties="isUMLModel, isUMLProfile"
type="org.eclipse.jface.viewers.ISelection">
</propertyTester>
+ <propertyTester
+ class="org.eclipse.papyrus.uml.diagram.common.util.UMLModelTester"
+ id="org.eclipse.papyrus.UMLModelTester"
+ namespace="org.eclipse.papyrus.UMLModelTester"
+ properties="isUMLModel, isUMLProfile"
+ type="org.eclipse.ui.IWorkbenchPart">
+ </propertyTester>
</extension>
<!-- Reusable test definitions verifying:
@@ -259,47 +266,25 @@
point="org.eclipse.core.expressions.definitions">
<definition id="org.eclipse.papyrus.uml.diagram.common.IsPapyrusActiveWithUMLModel">
- <and>
-
- <with variable="activeEditorId">
- <equals value="org.eclipse.papyrus.infra.core.papyrusEditor"/>
- </with>
- <with
- variable="activeEditor">
- <adapt
- type="org.eclipse.jface.viewers.ISelection">
- <test
- forcePluginActivation="true"
- property="org.eclipse.papyrus.UMLSelectionTester.isUMLModel"
- value="true">
- </test>
- </adapt>
- </with>
-
-
- </and>
+ <with
+ variable="activePart">
+ <test
+ forcePluginActivation="true"
+ property="org.eclipse.papyrus.UMLModelTester.isUMLModel"
+ value="true">
+ </test>
+ </with>
</definition>
<definition id="org.eclipse.papyrus.uml.diagram.common.IsPapyrusActiveWithUMLProfile">
- <and>
-
- <with variable="activeEditorId">
- <equals value="org.eclipse.papyrus.infra.core.papyrusEditor"/>
- </with>
-
- <with
- variable="activeEditor">
- <adapt
- type="org.eclipse.jface.viewers.ISelection">
- <test
- forcePluginActivation="true"
- property="org.eclipse.papyrus.UMLSelectionTester.isUMLProfile"
- value="true">
- </test>
- </adapt>
- </with>
-
- </and>
+ <with
+ variable="activePart">
+ <test
+ forcePluginActivation="true"
+ property="org.eclipse.papyrus.UMLModelTester.isUMLProfile"
+ value="true">
+ </test>
+ </with>
</definition>
</extension>
diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/util/UMLModelTester.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/util/UMLModelTester.java
new file mode 100644
index 00000000000..c539f1c4d93
--- /dev/null
+++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/util/UMLModelTester.java
@@ -0,0 +1,120 @@
+/*****************************************************************************
+ * 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:
+ * Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
+ *****************************************************************************/
+package org.eclipse.papyrus.uml.diagram.common.util;
+
+import org.eclipse.core.expressions.PropertyTester;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.papyrus.infra.core.resource.ModelSet;
+import org.eclipse.papyrus.infra.core.resource.NotFoundException;
+import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
+import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
+import org.eclipse.papyrus.uml.tools.model.UmlModel;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.uml2.uml.Package;
+import org.eclipse.uml2.uml.Profile;
+
+/**
+ *
+ * @author Camille Letavernier
+ *
+ */
+public class UMLModelTester extends PropertyTester {
+
+ /** Tester ID for UML Model nature */
+ public final static String IS_UML_MODEL = "isUMLModel";
+
+ /** Tester ID for UML Profile nature */
+ public final static String IS_UML_PROFILE = "isUMLProfile";
+
+ /** Test the receiver against the selected property */
+ public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
+ Object currentValue = null;
+ if(IS_UML_MODEL.equals(property)) {
+ currentValue = testUMLModelNature(receiver);
+ return (currentValue == expectedValue);
+ } else if(IS_UML_PROFILE.equals(property)) {
+ currentValue = testUMLProfileNature(receiver);
+ return (currentValue == expectedValue);
+ }
+
+ return false;
+ }
+
+ /** True if root object is a UML Model */
+ protected boolean testUMLModelNature(Object receiver) {
+ EObject root = getRoot(receiver);
+
+ /*
+ * For controlled resources, it is very important to consider root of UML model can be a Package. Of course, we
+ * can still exclude Profile, which should be dedicated to profile diagrams.
+ */
+ return root instanceof Package && !(root instanceof Profile);
+ }
+
+ /** True if root object is a UML Profile */
+ protected boolean testUMLProfileNature(Object receiver) {
+ return (getRoot(receiver) instanceof Profile);
+ }
+
+ private EObject getRoot(Object receiver) {
+ ModelSet modelSet = getModelSet(receiver);
+ return getRoot(modelSet);
+ }
+
+ private ModelSet getModelSet(Object receiver) {
+ if(receiver instanceof IWorkbenchPart) {
+ try {
+
+ /* First, retrieve the ModelSet from the Active Part (e.g. ActiveEditor, ModelExplorer, ...) */
+ IWorkbenchPart currentPart = (IWorkbenchPart)receiver;
+ ServicesRegistry servicesRegistry = (ServicesRegistry)currentPart.getAdapter(ServicesRegistry.class);
+
+ if(servicesRegistry != null) {
+ return ServiceUtils.getInstance().getModelSet(servicesRegistry);
+ }
+
+ /* If not found, retrieve the ModelSet from the Active Editor */
+
+ IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
+ servicesRegistry = (ServicesRegistry)activeEditor.getAdapter(ServicesRegistry.class);
+
+ if(servicesRegistry != null) {
+ return ServiceUtils.getInstance().getModelSet(servicesRegistry);
+ }
+
+ } catch (Exception ex) {
+ return null; //NPE (getActiveEditor) or ServiceException (Service registry cannot be found). In both cases, we just don't handle the event. Fail silently.
+ }
+ }
+
+ return null;
+ }
+
+ /** Returns the root EObject of currently opened model */
+ private EObject getRoot(ModelSet modelSet) {
+ UmlModel openedModel = (UmlModel)modelSet.getModel(UmlModel.MODEL_ID);
+ if(openedModel != null) {
+ EObject root;
+ try {
+ root = openedModel.lookupRoot();
+ } catch (NotFoundException e) {
+ return null;
+ }
+ return root;
+ }
+
+ return null;
+ }
+
+}

Back to the top