--- /dev/null
+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);
+ }
+ }
+}