From 18cf6d540ad4b8058b81b3725e934ca9a5bfd294 Mon Sep 17 00:00:00 2001 From: Alexander Fedorov Date: Sat, 1 May 2021 11:16:10 +0300 Subject: [PATCH 1/3] Bug 573296 - [Passage] rework EMF migration facilities Extract "ExtractResource" Signed-off-by: Alexander Fedorov --- .../lic/emf/ecore/ExtractResource.java | 41 +++++++++++++++++++ .../passage/lic/emf/ecore/LicensingEcore.java | 11 ----- .../workbench/emfforms/parts/DetailsView.java | 9 ++-- 3 files changed, 46 insertions(+), 15 deletions(-) create mode 100644 bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/ExtractResource.java diff --git a/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/ExtractResource.java b/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/ExtractResource.java new file mode 100644 index 000000000..377252023 --- /dev/null +++ b/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/ExtractResource.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 2021 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.lic.emf.ecore; + +import java.util.Optional; +import java.util.function.Function; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; + +/** + * Extracts {@link Resource} from the given object + * + * @since 2.0 + * + */ +public final class ExtractResource implements Function> { + + @Override + public Optional apply(Object object) { + if (object instanceof EObject) { + EObject eObject = (EObject) object; + return Optional.ofNullable(eObject.eResource()); + } + if (object instanceof Resource) { + return Optional.of((Resource) object); + } + return Optional.empty(); + } + +} diff --git a/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/LicensingEcore.java b/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/LicensingEcore.java index 5d5d4a116..a00cb1157 100644 --- a/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/LicensingEcore.java +++ b/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/LicensingEcore.java @@ -68,17 +68,6 @@ public static EObject extractEObject(Object object) { return null; } - public static Resource extractResource(Object object) { - if (object instanceof EObject) { - EObject eObject = (EObject) object; - return eObject.eResource(); - } - if (object instanceof Resource) { - return (Resource) object; - } - return null; - } - public static String extractValidationError(EObject eObject) { if (eObject == null) { return EmfMessages.LicensingEcore_input_invalid; diff --git a/bundles/org.eclipse.passage.loc.workbench.emfforms/src/org/eclipse/passage/loc/workbench/emfforms/parts/DetailsView.java b/bundles/org.eclipse.passage.loc.workbench.emfforms/src/org/eclipse/passage/loc/workbench/emfforms/parts/DetailsView.java index 8052d87f3..0a23088a5 100644 --- a/bundles/org.eclipse.passage.loc.workbench.emfforms/src/org/eclipse/passage/loc/workbench/emfforms/parts/DetailsView.java +++ b/bundles/org.eclipse.passage.loc.workbench.emfforms/src/org/eclipse/passage/loc/workbench/emfforms/parts/DetailsView.java @@ -52,6 +52,7 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.viewers.TreeViewer; +import org.eclipse.passage.lic.emf.ecore.ExtractResource; import org.eclipse.passage.lic.emf.ecore.LicensingEcore; import org.eclipse.passage.loc.internal.workbench.emfforms.i18n.WorkbenchEmfformsMessages; import org.eclipse.passage.loc.workbench.LocWokbench; @@ -118,7 +119,7 @@ protected void show(Notifier input, IEclipseContext context) { return; } this.root = LicensingEcore.extractEObject(input); - Resource resource = LicensingEcore.extractResource(input); + java.util.Optional resource = new ExtractResource().apply(input); configurePart(resource, context); Control[] children = content.getChildren(); for (Control control : children) { @@ -202,7 +203,7 @@ public Menu getMenu(TreeViewer treeViewer, EditingDomain editingDomain) { return menuProvider; } - protected void configurePart(Resource resource, IEclipseContext context) { + protected void configurePart(java.util.Optional resource, IEclipseContext context) { EditingDomain editingDomain = AdapterFactoryEditingDomain .getEditingDomainFor(LicensingEcore.extractEObject(resource)); context.set(EditingDomain.class, editingDomain); @@ -215,8 +216,8 @@ protected void configurePart(Resource resource, IEclipseContext context) { } commandStack.flush(); } - if (resource != null) { - URI uri = resource.getURI(); + if (!resource.isEmpty()) { + URI uri = resource.get().getURI(); if (uri != null) { part.setLabel(uri.lastSegment()); part.setTooltip(String.valueOf(uri)); From 79250362b6f5206809bb6cf99c63fc262a6d44aa Mon Sep 17 00:00:00 2001 From: Alexander Fedorov Date: Sat, 1 May 2021 11:33:42 +0300 Subject: [PATCH 2/3] Bug 573296 - [Passage] rework EMF migration facilities Extract "ExtractEObject" Signed-off-by: Alexander Fedorov --- .../passage/lic/emf/ecore/ExtractEObject.java | 45 ++++++++++++++++++ .../passage/lic/emf/ecore/LicensingEcore.java | 16 ------- .../workbench/emfforms/parts/DetailsView.java | 46 +++++++++++-------- 3 files changed, 72 insertions(+), 35 deletions(-) create mode 100644 bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/ExtractEObject.java diff --git a/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/ExtractEObject.java b/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/ExtractEObject.java new file mode 100644 index 000000000..8084e6fba --- /dev/null +++ b/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/ExtractEObject.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2021 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.lic.emf.ecore; + +import java.util.Optional; +import java.util.function.Function; + +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; + +/** + * Extracts {@link EObject} from the given object + * + * @since 2.0 + * + */ +public final class ExtractEObject implements Function> { + + @Override + public Optional apply(Object object) { + if (object instanceof EObject) { + return Optional.of((EObject) object); + } + if (object instanceof Resource) { + Resource resource = (Resource) object; + EList contents = resource.getContents(); + if (!contents.isEmpty()) { + return Optional.of(contents.get(0)); + } + } + return Optional.empty(); + } + +} diff --git a/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/LicensingEcore.java b/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/LicensingEcore.java index a00cb1157..9de436949 100644 --- a/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/LicensingEcore.java +++ b/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/LicensingEcore.java @@ -13,12 +13,10 @@ package org.eclipse.passage.lic.emf.ecore; import org.eclipse.emf.common.util.Diagnostic; -import org.eclipse.emf.common.util.EList; import org.eclipse.emf.ecore.EClass; import org.eclipse.emf.ecore.EDataType; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EPackage; -import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.util.Diagnostician; import org.eclipse.passage.lic.internal.emf.i18n.EmfMessages; @@ -54,20 +52,6 @@ public static String composeFullQualifiedName(EDataType eDataType) { return sb.toString(); } - public static EObject extractEObject(Object object) { - if (object instanceof EObject) { - return (EObject) object; - } - if (object instanceof Resource) { - Resource resource = (Resource) object; - EList contents = resource.getContents(); - if (!contents.isEmpty()) { - return contents.get(0); - } - } - return null; - } - public static String extractValidationError(EObject eObject) { if (eObject == null) { return EmfMessages.LicensingEcore_input_invalid; diff --git a/bundles/org.eclipse.passage.loc.workbench.emfforms/src/org/eclipse/passage/loc/workbench/emfforms/parts/DetailsView.java b/bundles/org.eclipse.passage.loc.workbench.emfforms/src/org/eclipse/passage/loc/workbench/emfforms/parts/DetailsView.java index 0a23088a5..2f7b2ebbb 100644 --- a/bundles/org.eclipse.passage.loc.workbench.emfforms/src/org/eclipse/passage/loc/workbench/emfforms/parts/DetailsView.java +++ b/bundles/org.eclipse.passage.loc.workbench.emfforms/src/org/eclipse/passage/loc/workbench/emfforms/parts/DetailsView.java @@ -12,6 +12,7 @@ *******************************************************************************/ package org.eclipse.passage.loc.workbench.emfforms.parts; +import java.util.ArrayList; import java.util.List; import javax.annotation.PostConstruct; @@ -52,8 +53,8 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.viewers.TreeViewer; +import org.eclipse.passage.lic.emf.ecore.ExtractEObject; import org.eclipse.passage.lic.emf.ecore.ExtractResource; -import org.eclipse.passage.lic.emf.ecore.LicensingEcore; import org.eclipse.passage.loc.internal.workbench.emfforms.i18n.WorkbenchEmfformsMessages; import org.eclipse.passage.loc.workbench.LocWokbench; import org.eclipse.passage.loc.workbench.viewers.DomainRegistryLabelProvider; @@ -72,7 +73,7 @@ public class DetailsView { private Composite content; // TreeMasterDetailComposite implies the Resource has root EObject - private EObject root; + private final List root; private final CommandStackListener dirtyStackListener; private final ISelectionChangedListener selectionChangedListener; @@ -80,6 +81,7 @@ public class DetailsView { @Inject public DetailsView(MPart part, ESelectionService selectionService) { + root = new ArrayList<>(); this.part = part; this.dirtyStackListener = e -> { Object source = e.getSource(); @@ -118,21 +120,22 @@ protected void show(Notifier input, IEclipseContext context) { if (input == null) { return; } - this.root = LicensingEcore.extractEObject(input); + root.clear(); + new ExtractEObject().apply(input).ifPresent(root::add); java.util.Optional resource = new ExtractResource().apply(input); configurePart(resource, context); Control[] children = content.getChildren(); for (Control control : children) { control.dispose(); } - if (this.root != null) { + if (!root.isEmpty()) { try { TreeMasterDetailComposite rootView = createRootView(content, resource, getCreateElementCallback(), context); TreeViewer selectionProvider = rootView.getSelectionProvider(); selectionProvider.addSelectionChangedListener(selectionChangedListener); selectionProvider.refresh(); - EObject objectToReveal = this.root; + EObject objectToReveal = root.get(0); while (objectToReveal != null) { selectionProvider.reveal(objectToReveal); if (selectionProvider.testFindItem(objectToReveal) != null) { @@ -204,18 +207,12 @@ public Menu getMenu(TreeViewer treeViewer, EditingDomain editingDomain) { } protected void configurePart(java.util.Optional resource, IEclipseContext context) { - EditingDomain editingDomain = AdapterFactoryEditingDomain - .getEditingDomainFor(LicensingEcore.extractEObject(resource)); - context.set(EditingDomain.class, editingDomain); - if (editingDomain instanceof AdapterFactoryEditingDomain) { - AdapterFactory adapterFactory = ((AdapterFactoryEditingDomain) editingDomain).getAdapterFactory(); - context.set(AdapterFactory.class, adapterFactory); - if (commandStack == null) { - commandStack = editingDomain.getCommandStack(); - commandStack.addCommandStackListener(dirtyStackListener); - } - commandStack.flush(); - } + resource.// + flatMap(new ExtractEObject())// + .map(AdapterFactoryEditingDomain::getEditingDomainFor)// + .filter(AdapterFactoryEditingDomain.class::isInstance)// + .map(AdapterFactoryEditingDomain.class::cast)// + .ifPresent(d -> configureCommandStack(d, context)); if (!resource.isEmpty()) { URI uri = resource.get().getURI(); if (uri != null) { @@ -227,6 +224,17 @@ protected void configurePart(java.util.Optional resource, IEclipseCont } } + protected void configureCommandStack(AdapterFactoryEditingDomain domain, IEclipseContext context) { + AdapterFactory adapters = domain.getAdapterFactory(); + context.set(EditingDomain.class, domain); + context.set(AdapterFactory.class, adapters); + if (commandStack == null) { + commandStack = domain.getCommandStack(); + commandStack.addCommandStackListener(dirtyStackListener); + } + commandStack.flush(); + } + @PreDestroy public void dispose() { if (commandStack != null) { @@ -236,11 +244,11 @@ public void dispose() { @Persist public void save() { - if (root == null) { + if (root.isEmpty()) { part.setDirty(false); return; } - Resource eResource = root.eResource(); + Resource eResource = root.get(0).eResource(); if (eResource != null) { IStatus status = LocWokbench.save(eResource); if (status.isOK()) { From 89b502bf84fbb6db7def86652a95304e0c232731 Mon Sep 17 00:00:00 2001 From: Alexander Fedorov Date: Sat, 1 May 2021 11:40:41 +0300 Subject: [PATCH 3/3] Bug 573296 - [Passage] rework EMF migration facilities extract "resource" package and switch to 2.0.0 Signed-off-by: Alexander Fedorov --- bundles/org.eclipse.passage.lic.emf/META-INF/MANIFEST.MF | 3 ++- .../passage/lic/emf/{ecore => resource}/ExtractEObject.java | 2 +- .../passage/lic/emf/{ecore => resource}/ExtractResource.java | 2 +- .../passage/loc/workbench/emfforms/parts/DetailsView.java | 4 ++-- features/org.eclipse.passage.lic.emf.feature/feature.xml | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) rename bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/{ecore => resource}/ExtractEObject.java (96%) rename bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/{ecore => resource}/ExtractResource.java (96%) diff --git a/bundles/org.eclipse.passage.lic.emf/META-INF/MANIFEST.MF b/bundles/org.eclipse.passage.lic.emf/META-INF/MANIFEST.MF index 68d5a00a3..17e75d0ec 100644 --- a/bundles/org.eclipse.passage.lic.emf/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.passage.lic.emf/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Automatic-Module-Name: org.eclipse.passage.lic.emf.edit Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.eclipse.passage.lic.emf -Bundle-Version: 1.0.201.qualifier +Bundle-Version: 2.0.0.qualifier Bundle-Name: %Bundle-Name Bundle-Vendor: %Bundle-Vendor Bundle-Copyright: %Bundle-Copyright @@ -28,6 +28,7 @@ Export-Package: org.eclipse.passage.lic.emf.ecore; org.eclipse.passage.lic.users.migration, org.eclipse.passage.lic.users.migration.tests", org.eclipse.passage.lic.emf.edit;x-friends:="org.eclipse.passage.loc.workbench", + org.eclipse.passage.lic.emf.resource, org.eclipse.passage.lic.internal.emf; x-friends:="org.eclipse.passage.lic.hc, org.eclipse.passage.lbc.base, diff --git a/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/ExtractEObject.java b/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/resource/ExtractEObject.java similarity index 96% rename from bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/ExtractEObject.java rename to bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/resource/ExtractEObject.java index 8084e6fba..84fdb49d4 100644 --- a/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/ExtractEObject.java +++ b/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/resource/ExtractEObject.java @@ -10,7 +10,7 @@ * Contributors: * ArSysOp - initial API and implementation *******************************************************************************/ -package org.eclipse.passage.lic.emf.ecore; +package org.eclipse.passage.lic.emf.resource; import java.util.Optional; import java.util.function.Function; diff --git a/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/ExtractResource.java b/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/resource/ExtractResource.java similarity index 96% rename from bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/ExtractResource.java rename to bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/resource/ExtractResource.java index 377252023..31be81f9f 100644 --- a/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/ExtractResource.java +++ b/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/resource/ExtractResource.java @@ -10,7 +10,7 @@ * Contributors: * ArSysOp - initial API and implementation *******************************************************************************/ -package org.eclipse.passage.lic.emf.ecore; +package org.eclipse.passage.lic.emf.resource; import java.util.Optional; import java.util.function.Function; diff --git a/bundles/org.eclipse.passage.loc.workbench.emfforms/src/org/eclipse/passage/loc/workbench/emfforms/parts/DetailsView.java b/bundles/org.eclipse.passage.loc.workbench.emfforms/src/org/eclipse/passage/loc/workbench/emfforms/parts/DetailsView.java index 2f7b2ebbb..1fd013ec8 100644 --- a/bundles/org.eclipse.passage.loc.workbench.emfforms/src/org/eclipse/passage/loc/workbench/emfforms/parts/DetailsView.java +++ b/bundles/org.eclipse.passage.loc.workbench.emfforms/src/org/eclipse/passage/loc/workbench/emfforms/parts/DetailsView.java @@ -53,8 +53,8 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.viewers.TreeViewer; -import org.eclipse.passage.lic.emf.ecore.ExtractEObject; -import org.eclipse.passage.lic.emf.ecore.ExtractResource; +import org.eclipse.passage.lic.emf.resource.ExtractEObject; +import org.eclipse.passage.lic.emf.resource.ExtractResource; import org.eclipse.passage.loc.internal.workbench.emfforms.i18n.WorkbenchEmfformsMessages; import org.eclipse.passage.loc.workbench.LocWokbench; import org.eclipse.passage.loc.workbench.viewers.DomainRegistryLabelProvider; diff --git a/features/org.eclipse.passage.lic.emf.feature/feature.xml b/features/org.eclipse.passage.lic.emf.feature/feature.xml index be58c55cf..496501b08 100644 --- a/features/org.eclipse.passage.lic.emf.feature/feature.xml +++ b/features/org.eclipse.passage.lic.emf.feature/feature.xml @@ -14,7 +14,7 @@