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()) {