Skip to content

Commit

Permalink
Bug 573296 - [Passage] rework EMF migration facilities
Browse files Browse the repository at this point in the history
Extract "ExtractEObject"

Signed-off-by: Alexander Fedorov <[email protected]>
  • Loading branch information
ruspl-afed committed May 1, 2021
1 parent 18cf6d5 commit 7925036
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -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<Object, Optional<EObject>> {

@Override
public Optional<EObject> apply(Object object) {
if (object instanceof EObject) {
return Optional.of((EObject) object);
}
if (object instanceof Resource) {
Resource resource = (Resource) object;
EList<EObject> contents = resource.getContents();
if (!contents.isEmpty()) {
return Optional.of(contents.get(0));
}
}
return Optional.empty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<EObject> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.passage.loc.workbench.emfforms.parts;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
Expand Down Expand Up @@ -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;
Expand All @@ -72,14 +73,15 @@ public class DetailsView {
private Composite content;

// TreeMasterDetailComposite implies the Resource has root EObject
private EObject root;
private final List<EObject> root;

private final CommandStackListener dirtyStackListener;
private final ISelectionChangedListener selectionChangedListener;
private CommandStack commandStack;

@Inject
public DetailsView(MPart part, ESelectionService selectionService) {
root = new ArrayList<>();
this.part = part;
this.dirtyStackListener = e -> {
Object source = e.getSource();
Expand Down Expand Up @@ -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> 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) {
Expand Down Expand Up @@ -204,18 +207,12 @@ public Menu getMenu(TreeViewer treeViewer, EditingDomain editingDomain) {
}

protected void configurePart(java.util.Optional<Resource> 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) {
Expand All @@ -227,6 +224,17 @@ protected void configurePart(java.util.Optional<Resource> 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) {
Expand All @@ -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()) {
Expand Down

0 comments on commit 7925036

Please sign in to comment.