diff options
author | Ed Merks | 2018-02-09 12:13:59 +0000 |
---|---|---|
committer | Ed Merks | 2018-02-09 12:13:59 +0000 |
commit | 91b7a7c722fe9bb774f654378436aa7260d13c1b (patch) | |
tree | c29dd6cbb73d3f03a976114fadfb521ae308411f | |
parent | dc8c14e4fa3936a82efeb3e50041e02f24329887 (diff) | |
download | org.eclipse.emf-91b7a7c722fe9bb774f654378436aa7260d13c1b.tar.gz org.eclipse.emf-91b7a7c722fe9bb774f654378436aa7260d13c1b.tar.xz org.eclipse.emf-91b7a7c722fe9bb774f654378436aa7260d13c1b.zip |
[530939] Directly create annotations with the valid available sources
2 files changed, 224 insertions, 10 deletions
diff --git a/plugins/org.eclipse.emf.ecore.edit/src/org/eclipse/emf/ecore/provider/EModelElementItemProvider.java b/plugins/org.eclipse.emf.ecore.edit/src/org/eclipse/emf/ecore/provider/EModelElementItemProvider.java index 16cee74c9..70cf9df57 100644 --- a/plugins/org.eclipse.emf.ecore.edit/src/org/eclipse/emf/ecore/provider/EModelElementItemProvider.java +++ b/plugins/org.eclipse.emf.ecore.edit/src/org/eclipse/emf/ecore/provider/EModelElementItemProvider.java @@ -11,6 +11,7 @@ package org.eclipse.emf.ecore.provider; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -21,8 +22,10 @@ import java.util.Set; import org.eclipse.emf.common.notify.AdapterFactory; import org.eclipse.emf.common.notify.Notification; +import org.eclipse.emf.common.util.EList; import org.eclipse.emf.common.util.ResourceLocator; import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EAnnotation; import org.eclipse.emf.ecore.EModelElement; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EReference; @@ -33,6 +36,7 @@ import org.eclipse.emf.ecore.EPackage.Registry; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator; import org.eclipse.emf.edit.provider.IEditingDomainItemProvider; import org.eclipse.emf.edit.provider.IItemLabelProvider; import org.eclipse.emf.edit.provider.IItemPropertyDescriptor; @@ -154,17 +158,54 @@ public class EModelElementItemProvider * that can be created under this object. * <!-- begin-user-doc --> * <!-- end-user-doc --> - * @generated + * @generated NOT */ @Override protected void collectNewChildDescriptors(Collection<Object> newChildDescriptors, Object object) { super.collectNewChildDescriptors(newChildDescriptors, object); - newChildDescriptors.add - (createChildParameter - (EcorePackage.Literals.EMODEL_ELEMENT__EANNOTATIONS, - EcoreFactory.eINSTANCE.createEAnnotation())); + EModelElement eModelElement = (EModelElement)object; + + // Collect the sources we will use for creating annotations. + // It will always include null which will always be first. + // + Collection<String> sources = new ArrayList<String>(); + sources.add(null); + + // Create an annotation that we will temporarily add as a child. + // We will disable notifications because no adapter should see this happen. + // + EList<EAnnotation> eAnnotations = eModelElement.getEAnnotations(); + EAnnotation eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation(); + eModelElement.eSetDeliver(false); + try + { + // Add the annotation + eAnnotations.add(eAnnotation); + IItemPropertyDescriptor propertyDescriptor = new AdapterFactoryItemDelegator(getRootAdapterFactory()).getPropertyDescriptor( + eAnnotation, + EcorePackage.Literals.EANNOTATION__SOURCE); + @SuppressWarnings("unchecked") + Collection<String> choiceOfValues = (Collection<String>)propertyDescriptor.getChoiceOfValues(eAnnotation); + sources.addAll(choiceOfValues); + } + finally + { + // No matter what might go wrong, we will remove the annotation, re-enable notification, and clear any adapters added to the annotation. + eAnnotations.remove(eAnnotation); + eModelElement.eSetDeliver(true); + eAnnotation.eAdapters().clear(); + } + + // Create a child descriptor for each source. + // + for (String source : sources) + { + eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation(); + eAnnotation.setSource(source); + newChildDescriptors.add(createChildParameter(EcorePackage.Literals.EMODEL_ELEMENT__EANNOTATIONS, eAnnotation)); + } } /** diff --git a/plugins/org.eclipse.emf.ecore.editor/src/org/eclipse/emf/ecore/presentation/EcoreActionBarContributor.java b/plugins/org.eclipse.emf.ecore.editor/src/org/eclipse/emf/ecore/presentation/EcoreActionBarContributor.java index d8ce4fa08..a3f471087 100644 --- a/plugins/org.eclipse.emf.ecore.editor/src/org/eclipse/emf/ecore/presentation/EcoreActionBarContributor.java +++ b/plugins/org.eclipse.emf.ecore.editor/src/org/eclipse/emf/ecore/presentation/EcoreActionBarContributor.java @@ -15,6 +15,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; @@ -50,8 +51,10 @@ import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.dialogs.ElementListSelectionDialog; @@ -64,6 +67,7 @@ import org.eclipse.emf.common.util.Diagnostic; import org.eclipse.emf.common.util.EList; import org.eclipse.emf.common.util.TreeIterator; import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EAnnotation; import org.eclipse.emf.ecore.EGenericType; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EPackage; @@ -823,7 +827,7 @@ public class EcoreActionBarContributor continue; } } - actions.add(new CreateChildAction(activeEditorPart, selection, descriptor)); + actions.add(new EcoreCreateChildAction(activeEditorPart, selection, descriptor)); } } return actions; @@ -851,7 +855,7 @@ public class EcoreActionBarContributor continue; } } - actions.add(new CreateSiblingAction(activeEditorPart, selection, descriptor)); + actions.add(new EcoreCreateSiblingAction(activeEditorPart, selection, descriptor)); } } return actions; @@ -873,21 +877,83 @@ public class EcoreActionBarContributor * If <code>contributionID</code> is <code>null</code>, they are simply added. * <!-- begin-user-doc --> * <!-- end-user-doc --> - * @generated + * @generated NOT */ protected void populateManager(IContributionManager manager, Collection<? extends IAction> actions, String contributionID) { if (actions != null) { + // Look for actions that create EAnnotations. + // + Set<IAction> ignoredActions = new HashSet<IAction>(); + Set<IAction> annotationActions = new LinkedHashSet<IAction>(); for (IAction action : actions) { + if (action instanceof EObjectProvider) + { + EObjectProvider eObjectProvider = (EObjectProvider)action; + EObject eObject = eObjectProvider.getEObject(); + if (eObject instanceof EAnnotation) + { + annotationActions.add(action); + ignoredActions.add(action); + } + } + } + + // If there is more than one action that creates an annotation... + // + if (annotationActions.size() > 1) + { + // Create a menu manager to group them. + // This assumes the first action is one for the null source. + // + IAction action = annotationActions.iterator().next(); + String actionText = action.getText(); + MenuManager annotationMenuManager = new MenuManager(actionText); + annotationMenuManager.setImageDescriptor(action.getImageDescriptor()); + + // Add that menu manager instead of the individual actions. if (contributionID != null) { - manager.insertBefore(contributionID, action); + manager.insertBefore(contributionID, annotationMenuManager); } else { - manager.add(action); + manager.add(annotationMenuManager); + } + + // Add an item for each annotation action. + // + for (IAction annotationAction : annotationActions) + { + annotationMenuManager.add(annotationAction); + String source = ((EAnnotation)((EObjectProvider)annotationAction).getEObject()).getSource(); + if (source != null) + { + // Set the label to include the source. + // + annotationAction.setText(actionText + " - " + source); + } + } + } + else + { + ignoredActions.clear(); + } + + for (IAction action : actions) + { + if (!ignoredActions.contains(action)) + { + if (contributionID != null) + { + manager.insertBefore(contributionID, action); + } + else + { + manager.add(action); + } } } } @@ -980,4 +1046,111 @@ public class EcoreActionBarContributor return true; } + /** + * An interface implemented by {@link EcoreCreateChildAction} and {@link EcoreCreateSiblingAction} to provide access to the data in the descriptor. + * @since 2.14 + */ + public interface EObjectProvider + { + public EObject getEObject(); + } + + /** + * A create child action subclass that provides access to the {@link #descriptor} and specializes {@link #run()} to show the properties view. + * @since 2.14 + */ + public class EcoreCreateChildAction extends CreateChildAction implements EObjectProvider + { + public EcoreCreateChildAction(IWorkbenchPart workbenchPart, ISelection selection, Object descriptor) + { + super(workbenchPart, selection, descriptor); + } + + public EObject getEObject() + { + if (descriptor instanceof CommandParameter) + { + CommandParameter commandParameter = (CommandParameter)descriptor; + return commandParameter.getEValue(); + } + else + { + return null; + } + } + + @Override + public void run() + { + super.run(); + + // This is dispatched twice because the command stack listener dispatches once and then the viewer selection is also dispatches once, + // and we need to delay until the selection is established. + // + final Display display = EcoreActionBarContributor.this.getPage().getWorkbenchWindow().getShell().getDisplay(); + display.asyncExec(new Runnable() + { + public void run() + { + display.asyncExec(new Runnable() + { + public void run() + { + showPropertiesViewAction.run(); + } + }); + } + }); + } + } + + /** + * A create sibling action subclass that provides access to the {@link #descriptor} and specializes {@link #run()} to show the properties view. + * + * @since 2.14 + */ + public class EcoreCreateSiblingAction extends CreateSiblingAction implements EObjectProvider + { + public EcoreCreateSiblingAction(IWorkbenchPart workbenchPart, ISelection selection, Object descriptor) + { + super(workbenchPart, selection, descriptor); + } + + public EObject getEObject() + { + if (descriptor instanceof CommandParameter) + { + CommandParameter commandParameter = (CommandParameter)descriptor; + return commandParameter.getEValue(); + } + else + { + return null; + } + } + + @Override + public void run() + { + super.run(); + + // This is dispatched twice because the command stack listener dispatches once and then the viewer selection is also dispatches once, + // and we need to delay until the selection is established. + // + final Display display = EcoreActionBarContributor.this.getPage().getWorkbenchWindow().getShell().getDisplay(); + display.asyncExec(new Runnable() + { + public void run() + { + display.asyncExec(new Runnable() + { + public void run() + { + showPropertiesViewAction.run(); + } + }); + } + }); + } + } } |