]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableView.java
42b88207c1881fe0b8b1735f7a10e843675a915c
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / techtype / table / TechTypeTableView.java
1 package org.simantics.district.network.ui.techtype.table;
2
3
4 import java.util.List;
5
6 import javax.annotation.PostConstruct;
7 import javax.annotation.PreDestroy;
8 import javax.inject.Inject;
9
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;
29
30 public class TechTypeTableView {
31
32         private final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTableView.class);
33
34         @Inject ESelectionService selectionService;
35
36         public static TechTypeTable table;
37
38         @Inject
39         public void init(MPart part, MApplication app) {
40                 MToolBar toolBar = MMenuFactory.INSTANCE.createToolBar();
41                 toolBar.setToBeRendered(true);
42                 toolBar.getChildren().add(createImportCSVDataToolItem(app));
43                 part.setToolbar(toolBar);
44         }
45
46         private MHandledToolItem createImportCSVDataToolItem(MApplication app) {
47                 MHandledToolItem createHandledToolItem = MMenuFactory.INSTANCE.createHandledToolItem();
48                 // Command is contributed via fragment
49                 MCommand command = app.getCommand("org.simantics.district.network.ui.command.importtechtypecsv");
50                 createHandledToolItem.setCommand(command); //$NON-NLS-1$
51                 createHandledToolItem.setLabel("Import Tech Type");
52                 createHandledToolItem.setIconURI("platform:/plugin/com.famfamfam.silk/icons/table_edit.png"); //$NON-NLS-1$
53                 return createHandledToolItem;
54         }
55
56         @PostConstruct
57         public void postConstruct(Composite parent) {
58                 Resource pipe = null;
59                 try {
60                         List<NamedResource> componentTypes = DistrictNetworkUtil.getDistrictComponents();
61
62                         pipe = componentTypes.stream()
63                                         .filter(r -> r.getName().toLowerCase().contains("pipe"))
64                                         .map(r -> r.getResource())
65                                         .findFirst().orElse(null);
66
67                         if (pipe == null) {
68                                 pipe = Simantics.getSession().syncRequest(new PossibleResource("http://DistrictComponents@C/dh_pipe@1"));
69                         }
70                 } catch (DatabaseException e) {
71                         LOGGER.error("Failed to read district component types for active model", e);
72                 }
73
74                 LOGGER.debug("Pipe component type is {}", pipe);
75
76                 String data = null;
77                 try {
78                         Resource model = Simantics.getSession().syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));
79                         if (model != null)
80                                 data = Simantics.getSession().syncRequest(new PossibleTechTypeTableData(model, pipe), TransientCacheListener.instance());
81                 } catch (DatabaseException e) {
82                         LOGGER.error("Failed to read tech type table data for {}", pipe, e);
83                 }
84
85                 table = new TechTypeTable(parent, 0, pipe, data);
86         }
87
88         @PreDestroy
89         public void dispose() {
90                 table.dispose();
91                 table = null;
92         }
93
94 }