]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/adapters/TechTypeEditorAdapter.java
TechTypeTable features
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / techtype / adapters / TechTypeEditorAdapter.java
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 (file)
index 0000000..5ebe6ed
--- /dev/null
@@ -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);
+               }
+       }
+}