Skip to content

Commit

Permalink
[#1292] Operator: contribute editing domain for a registry
Browse files Browse the repository at this point in the history
  • Loading branch information
eparovyshnaya committed Feb 22, 2024
1 parent fb1d074 commit 71ac0dc
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.eclipse.passage.loc.internal.api;

import org.eclipse.emf.edit.domain.EditingDomain;

public interface EditingDomainSource {

EditingDomain create();

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ public interface OperatorGear extends Gear {

OperatorWorkspace workspace();

EditingDomainSource editingDomainSource();

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.command.BasicCommandStack;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EContentAdapter;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.osgi.util.NLS;
import org.eclipse.passage.lic.api.ServiceInvocationResult;
import org.eclipse.passage.lic.api.diagnostic.Diagnostic;
Expand All @@ -38,6 +36,7 @@
import org.eclipse.passage.lic.base.diagnostic.DiagnosticExplained;
import org.eclipse.passage.lic.emf.resource.ResourceLoadFailed;
import org.eclipse.passage.lic.internal.emf.i18n.EmfMessages;
import org.eclipse.passage.loc.internal.api.EditingDomainSource;
import org.eclipse.passage.loc.internal.api.OperatorGear;
import org.eclipse.passage.loc.internal.api.OperatorGearSupplier;
import org.eclipse.passage.loc.internal.api.workspace.KnownResources;
Expand All @@ -49,35 +48,35 @@ public abstract class BaseDomainRegistry<I> implements EditingDomainRegistry<I>,

protected String domainName;

private final AdapterFactoryEditingDomain editingDomain;
private Optional<EditingDomain> editingDomain = Optional.empty();

private final List<URI> sources;

private final EContentAdapter contentAdapter;

private final List<OperatorGearSupplier> gear;
private Optional<OperatorGearSupplier> gear = Optional.empty();

public BaseDomainRegistry() {
sources = new ArrayList<>();
gear = new ArrayList<>(1);
BasicCommandStack commandStack = new BasicCommandStack();
editingDomain = new AdapterFactoryEditingDomain(
new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE), commandStack,
new HashMap<Resource, Boolean>());
contentAdapter = createContentAdapter();
}

public void bindGear(OperatorGearSupplier supplier) {
gear.add(supplier);
gear = Optional.of(supplier);
}

public void unbindGear(OperatorGearSupplier supplier) {
gear.remove(supplier);
if (gear.isEmpty()) {
return;
}
if (gear.get().equals(supplier)) {
gear = Optional.empty();
}
}

protected void activate(Map<String, Object> properties) {
domainName = String.valueOf(properties.get(EditingDomainRegistryAccess.PROPERTY_DOMAIN_NAME));
editingDomain.getResourceSet().eAdapters().add(contentAdapter);
getEditingDomain().getResourceSet().eAdapters().add(contentAdapter);
try {
gear.stream()//
.findFirst()//
Expand Down Expand Up @@ -107,7 +106,7 @@ protected void logDiagnostic(Diagnostic diagnostic) {

protected abstract DomainContentAdapter<I, ? extends EditingDomainRegistry<I>> createContentAdapter();

protected void deactivate(Map<String, Object> properties) {
protected void deactivate(@SuppressWarnings("unused") Map<String, Object> properties) {
try {
gear.stream()//
.findFirst()//
Expand All @@ -117,7 +116,7 @@ protected void deactivate(Map<String, Object> properties) {
} catch (Exception e) {
Platform.getLog(getClass()).error(e.getMessage(), e);
}
editingDomain.getResourceSet().eAdapters().remove(contentAdapter);
getEditingDomain().getResourceSet().eAdapters().remove(contentAdapter);
}

private void store(OperatorWorkspace workspace) {
Expand All @@ -129,7 +128,12 @@ private void store(OperatorWorkspace workspace) {

@Override
public EditingDomain getEditingDomain() {
return editingDomain;
if (editingDomain.isEmpty()) {
editingDomain = gear.map(OperatorGearSupplier::gear)//
.map(OperatorGear::editingDomainSource)//
.map(EditingDomainSource::create);
}
return editingDomain.orElse(null);
}

protected Map<?, ?> getLoadOptions() {
Expand All @@ -141,7 +145,7 @@ public EditingDomain getEditingDomain() {
}

public ServiceInvocationResult<Boolean> loadSource(URI uri) {
ResourceSet set = editingDomain.getResourceSet();
ResourceSet set = getEditingDomain().getResourceSet();
Resource resource = createResource(uri);
set.getResources().add(resource);
try {
Expand All @@ -157,7 +161,7 @@ public ServiceInvocationResult<Boolean> loadSource(URI uri) {
protected abstract Resource createResource(URI uri);

public void unloadSource(URI uri) {
ResourceSet resourceSet = editingDomain.getResourceSet();
ResourceSet resourceSet = getEditingDomain().getResourceSet();
Resource resource = resourceSet.getResource(uri, false);
resource.unload();
resourceSet.getResources().remove(resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
import org.eclipse.passage.lic.base.registry.ReadOnlyRegistry;
import org.eclipse.passage.lic.bc.BcStreamCodec;
import org.eclipse.passage.lic.oshi.HardwareEnvironment;
import org.eclipse.passage.loc.internal.api.EditingDomainSource;
import org.eclipse.passage.loc.internal.api.OperatorGear;
import org.eclipse.passage.loc.internal.api.workspace.OperatorWorkspace;
import org.eclipse.passage.loc.operator.internal.gear.services.PassageEditingDomainSource;
import org.eclipse.passage.loc.workspace.CollectiveWorkspace;

@SuppressWarnings("restriction")
Expand All @@ -41,13 +43,15 @@ final class DefaultGear implements OperatorGear {
private final Registry<EvaluationType, RuntimeEnvironment> environments;
private final Registry<StringServiceId, Hashes> hashes;
private final OperatorWorkspace workspace;
private final EditingDomainSource editing;

private DefaultGear() {
this.environments = new ReadOnlyRegistry<>(Arrays.asList(//
new HardwareEnvironment() //
));
this.hashes = new ReadOnlyRegistry<>(new MD5Hashes());
this.workspace = new CollectiveWorkspace();
this.editing = new PassageEditingDomainSource();
}

@Override
Expand All @@ -71,4 +75,9 @@ public HashesRegistry hashes() {
return () -> hashes;
}

@Override
public EditingDomainSource editingDomainSource() {
return editing;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.eclipse.passage.loc.operator.internal.gear.services;

import java.util.HashMap;

import org.eclipse.emf.common.command.BasicCommandStack;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.passage.loc.internal.api.EditingDomainSource;

public final class PassageEditingDomainSource implements EditingDomainSource {

@Override
public EditingDomain create() {
return new AdapterFactoryEditingDomain(
new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE), //
new BasicCommandStack(), //
new HashMap<>()//
);
}

}

0 comments on commit 71ac0dc

Please sign in to comment.