Skip to content

Commit

Permalink
Merge pull request #758 from eclipse-passage/573296
Browse files Browse the repository at this point in the history
Bug 573296 - [Passage] rework EMF migration facilities
  • Loading branch information
ruspl-afed authored May 1, 2021
2 parents e63ec08 + 89b502b commit 44b1c7d
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 52 deletions.
3 changes: 2 additions & 1 deletion bundles/org.eclipse.passage.lic.emf/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
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,31 +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 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;
Expand Down
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.resource;

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
@@ -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.resource;

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<Object, Optional<Resource>> {

@Override
public Optional<Resource> 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();
}

}
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,7 +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.LicensingEcore;
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;
Expand All @@ -71,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 @@ -117,21 +120,22 @@ protected void show(Notifier input, IEclipseContext context) {
if (input == null) {
return;
}
this.root = LicensingEcore.extractEObject(input);
Resource resource = LicensingEcore.extractResource(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 @@ -202,21 +206,15 @@ public Menu getMenu(TreeViewer treeViewer, EditingDomain editingDomain) {
return menuProvider;
}

protected void configurePart(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();
}
if (resource != null) {
URI uri = resource.getURI();
protected void configurePart(java.util.Optional<Resource> resource, IEclipseContext context) {
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) {
part.setLabel(uri.lastSegment());
part.setTooltip(String.valueOf(uri));
Expand All @@ -226,6 +224,17 @@ protected void configurePart(Resource resource, IEclipseContext context) {
}
}

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 @@ -235,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
2 changes: 1 addition & 1 deletion features/org.eclipse.passage.lic.emf.feature/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<feature
id="org.eclipse.passage.lic.emf.feature"
label="%featureName"
version="1.0.101.qualifier"
version="2.0.0.qualifier"
provider-name="%providerName"
plugin="org.eclipse.passage.lic.emf"
license-feature="org.eclipse.license"
Expand Down

0 comments on commit 44b1c7d

Please sign in to comment.