X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2Ftechtype%2Ftable%2FTechTypeTableView.java;fp=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2Ftechtype%2Ftable%2FTechTypeTableView.java;h=15b31784fae96db7579e4f99a65521a68f2e301a;hb=96d2122ccc3a7d3503d0bf2ab121eb8fd2186323;hp=0000000000000000000000000000000000000000;hpb=40d019be6ff00ecec1c3250a03bb5c046ad98182;p=simantics%2Fdistrict.git 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 new file mode 100644 index 00000000..15b31784 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableView.java @@ -0,0 +1,96 @@ +package org.simantics.district.network.ui.techtype.table; + + +import java.util.List; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; +import javax.inject.Inject; + +import org.eclipse.e4.ui.model.application.MApplication; +import org.eclipse.e4.ui.model.application.commands.MCommand; +import org.eclipse.e4.ui.model.application.ui.basic.MPart; +import org.eclipse.e4.ui.model.application.ui.menu.MHandledToolItem; +import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory; +import org.eclipse.e4.ui.model.application.ui.menu.MToolBar; +import org.eclipse.e4.ui.workbench.modeling.ESelectionService; +import org.eclipse.swt.widgets.Composite; +import org.simantics.Simantics; +import org.simantics.db.Resource; +import org.simantics.db.common.NamedResource; +import org.simantics.db.common.procedure.adapter.TransientCacheListener; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.request.PossibleActiveModel; +import org.simantics.db.layer0.request.PossibleResource; +import org.simantics.district.network.DistrictNetworkUtil; +import org.simantics.district.network.techtype.requests.PossibleTechTypeTableData; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +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; + + @Inject + public void init(MPart part, MApplication app) { + MToolBar toolBar = MMenuFactory.INSTANCE.createToolBar(); + toolBar.setToBeRendered(true); + toolBar.getChildren().add(createImportCSVDataToolItem(app)); + part.setToolbar(toolBar); + } + + private MHandledToolItem createImportCSVDataToolItem(MApplication app) { + MHandledToolItem createHandledToolItem = MMenuFactory.INSTANCE.createHandledToolItem(); + // Command is contributed via fragment + MCommand command = app.getCommand("org.simantics.district.network.ui.command.importtechtypecsv"); + createHandledToolItem.setCommand(command); //$NON-NLS-1$ + createHandledToolItem.setLabel("Import Tech Type"); + createHandledToolItem.setIconURI("platform:/plugin/com.famfamfam.silk/icons/table_edit.png"); //$NON-NLS-1$ + return createHandledToolItem; + } + + @PostConstruct + public void postConstruct(Composite parent) { + Resource pipe = null; + try { + List componentTypes = DistrictNetworkUtil.getDistrictComponents(); + + pipe = componentTypes.stream() + .filter(r -> r.getName().toLowerCase().contains("pipe")) + .map(r -> r.getResource()) + .findFirst().orElse(null); + + if (pipe == null) { + pipe = Simantics.getSession().syncRequest(new PossibleResource("http://DistrictComponents@C/dh_pipe@1")); + } + } catch (DatabaseException e) { + LOGGER.error("Failed to read district component types for active model", e); + } + + LOGGER.debug("Pipe component type is {}", pipe); + + String data = null; + try { + Resource model = Simantics.getSession().syncRequest(new PossibleActiveModel(Simantics.getProjectResource())); + if (model != null) + data = Simantics.getSession().syncRequest(new PossibleTechTypeTableData(model, pipe), TransientCacheListener.instance()); + } catch (DatabaseException e) { + LOGGER.error("Failed to read tech type table data for {}", pipe, e); + } + + table = new TechTypeTable(parent, 0, pipe, data); + } + + @PreDestroy + public void dispose() { + table.dispose(); + table = null; + } + +}