From: Reino Ruusu Date: Mon, 31 Aug 2020 14:57:33 +0000 (+0300) Subject: Editor adapter for tech type table entities X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=9c0a0447dccfa9e28c585236d78336ee7985b6d0;p=simantics%2Fdistrict.git Editor adapter for tech type table entities gitlab #93 Change-Id: I7f7dd9aae0d90b00250ecf42e01ce566e57544d0 --- diff --git a/org.simantics.district.network.ui/plugin.xml b/org.simantics.district.network.ui/plugin.xml index cee0a77f..3b8bf45c 100644 --- a/org.simantics.district.network.ui/plugin.xml +++ b/org.simantics.district.network.ui/plugin.xml @@ -80,6 +80,10 @@ priority="300" class="org.simantics.district.network.ui.OpenDiagramFromNetworkElementAdapter"> + + diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/adapters/TechTypeEditorAdapter.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/adapters/TechTypeEditorAdapter.java new file mode 100644 index 00000000..5ebe6ed4 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/adapters/TechTypeEditorAdapter.java @@ -0,0 +1,79 @@ +package org.simantics.district.network.ui.techtype.adapters; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; + +import org.eclipse.core.runtime.FileLocator; +import org.eclipse.e4.core.contexts.IEclipseContext; +import org.eclipse.e4.ui.workbench.modeling.EPartService; +import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.ui.PlatformUI; +import org.simantics.Simantics; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.district.network.ontology.DistrictNetworkResource; +import org.simantics.district.network.ui.techtype.table.TechTypeTableView; +import org.simantics.ui.utils.ResourceAdaptionUtils; +import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter; +import org.simantics.ui.workbench.editor.EditorAdapter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Reino Ruusu + */ +public class TechTypeEditorAdapter extends AbstractResourceEditorAdapter implements EditorAdapter { + + private static final Logger LOGGER = LoggerFactory.getLogger(TechTypeEditorAdapter.class); + + public TechTypeEditorAdapter() throws MalformedURLException, IOException { + super("Tech Type Table View"); + setIcon(ImageDescriptor.createFromURL(FileLocator.resolve(new URL("platform:/plugin/com.famfamfam.silk/icons/table.png")))); //$NON-NLS-1$ + } + + @Override + public boolean canHandle(ReadGraph graph, Object input) throws DatabaseException { + Resource r = ResourceAdaptionUtils.toSingleResource(input); + if (r == null) + return false; + return graph.isInstanceOf(r, DistrictNetworkResource.getInstance(graph).TechType_TechTypeTable); + } + + @Override + public void openEditor(Resource table) throws Exception { + IEclipseContext eclipseContext = PlatformUI.getWorkbench().getService(IEclipseContext.class); + if (eclipseContext == null) { + LOGGER.error("No Eclipse context available"); + return; + } + + eclipseContext = eclipseContext.getActiveLeaf(); + EPartService partService = eclipseContext.get(EPartService.class); + if (partService == null) + return; + + partService.showPart(TechTypeTableView.ID, PartState.ACTIVATE); + + try { + Simantics.getSession().syncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + Resource componentType = graph.getPossibleObject(table, DN.TechType_TechTypeTable_HasComponentType); + String data = graph.getPossibleRelatedValue2(table, DN.TechType_TechTypeTable_HasData); + + TechTypeTableView.table.getDisplay().asyncExec(() -> { + TechTypeTableView.table.setComponentType(componentType); + TechTypeTableView.table.setTechTypeData(data); + }); + } + }); + } catch (DatabaseException e) { + LOGGER.error("Failed to read tech type table {}", table, e); + } + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTable.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTable.java index 3b3d4a70..296d9af3 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTable.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTable.java @@ -208,8 +208,17 @@ public class TechTypeTable extends Composite { LOGGER.error("Failed to write tech type table data to model", e); } + setTechTypeData(data); + } + + public void setTechTypeData(String data) { bodyDataProvider.setData(data); table.refresh(true); } + public void setComponentType(Resource componentType) { + this.componentType = componentType; + } + } + diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableView.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableView.java index 42b88207..50368d2c 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableView.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableView.java @@ -31,6 +31,8 @@ public class TechTypeTableView { private final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTableView.class); + public static final String ID = "org.simantics.district.network.ui.techtype.table.techtypeTableView"; + @Inject ESelectionService selectionService; public static TechTypeTable table;