1 package org.simantics.district.network.ui.techtype.table;
6 import javax.annotation.PostConstruct;
7 import javax.annotation.PreDestroy;
8 import javax.inject.Inject;
10 import org.eclipse.e4.ui.model.application.MApplication;
11 import org.eclipse.e4.ui.model.application.commands.MCommand;
12 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
13 import org.eclipse.e4.ui.model.application.ui.menu.MHandledToolItem;
14 import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
15 import org.eclipse.e4.ui.model.application.ui.menu.MToolBar;
16 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
17 import org.eclipse.swt.widgets.Composite;
18 import org.simantics.Simantics;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.NamedResource;
21 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.request.PossibleActiveModel;
24 import org.simantics.db.layer0.request.PossibleResource;
25 import org.simantics.district.network.DistrictNetworkUtil;
26 import org.simantics.district.network.ui.techtype.requests.PossibleTechTypeTableData;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class TechTypeTableView {
32 private final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTableView.class);
34 public static final String ID = "org.simantics.district.network.ui.techtype.table.techtypeTableView";
36 @Inject ESelectionService selectionService;
38 public static TechTypeTable table;
41 public void init(MPart part, MApplication app) {
42 MToolBar toolBar = MMenuFactory.INSTANCE.createToolBar();
43 toolBar.setToBeRendered(true);
44 toolBar.getChildren().add(createImportCSVDataToolItem(app));
45 part.setToolbar(toolBar);
48 private MHandledToolItem createImportCSVDataToolItem(MApplication app) {
49 MHandledToolItem createHandledToolItem = MMenuFactory.INSTANCE.createHandledToolItem();
50 // Command is contributed via fragment
51 MCommand command = app.getCommand("org.simantics.district.network.ui.command.importtechtypecsv");
52 createHandledToolItem.setCommand(command); //$NON-NLS-1$
53 createHandledToolItem.setLabel("Import Tech Type");
54 createHandledToolItem.setIconURI("platform:/plugin/com.famfamfam.silk/icons/table_edit.png"); //$NON-NLS-1$
55 return createHandledToolItem;
59 public void postConstruct(Composite parent) {
62 List<NamedResource> componentTypes = DistrictNetworkUtil.getDistrictComponents();
64 pipe = componentTypes.stream()
65 .filter(r -> r.getName().toLowerCase().contains("pipe"))
66 .map(r -> r.getResource())
67 .findFirst().orElse(null);
70 pipe = Simantics.getSession().syncRequest(new PossibleResource("http://DistrictComponents@C/dh_pipe@1"));
72 } catch (DatabaseException e) {
73 LOGGER.error("Failed to read district component types for active model", e);
76 LOGGER.debug("Pipe component type is {}", pipe);
80 Resource model = Simantics.getSession().syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));
82 data = Simantics.getSession().syncRequest(new PossibleTechTypeTableData(model, pipe), TransientCacheListener.instance());
83 } catch (DatabaseException e) {
84 LOGGER.error("Failed to read tech type table data for {}", pipe, e);
87 table = new TechTypeTable(parent, 0, pipe, data);
91 public void dispose() {