]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableView.java
6c25251c344da677ea4459de21be02db14533979
[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.district.network.DistrictNetworkUtil;
25 import org.simantics.district.network.techtype.requests.PossibleTechTypeTable;
26 import org.simantics.district.network.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         public static final String ID = "org.simantics.district.network.ui.techtype.table.techtypeTableView";
35
36         @Inject ESelectionService selectionService;
37
38         public static TechTypeTable table;
39
40         @Inject
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                 toolBar.getChildren().add(createValidateTableToolItem(app));
46                 part.setToolbar(toolBar);
47         }
48
49         private MHandledToolItem createValidateTableToolItem(MApplication app) {
50                 MHandledToolItem createHandledToolItem = MMenuFactory.INSTANCE.createHandledToolItem();
51                 // Command is contributed via fragment
52                 MCommand command = app.getCommand("org.simantics.district.network.ui.command.validatetechtypetable");
53                 createHandledToolItem.setCommand(command); //$NON-NLS-1$
54                 createHandledToolItem.setLabel("Validate Tech Type Table");
55                 createHandledToolItem.setIconURI("platform:/plugin/com.famfamfam.silk/icons/accept.png"); //$NON-NLS-1$
56                 return createHandledToolItem;
57         }
58
59         private MHandledToolItem createImportCSVDataToolItem(MApplication app) {
60                 MHandledToolItem createHandledToolItem = MMenuFactory.INSTANCE.createHandledToolItem();
61                 // Command is contributed via fragment
62                 MCommand command = app.getCommand("org.simantics.district.network.ui.command.importtechtypecsv");
63                 createHandledToolItem.setCommand(command); //$NON-NLS-1$
64                 createHandledToolItem.setLabel("Import Tech Type");
65                 createHandledToolItem.setIconURI("platform:/plugin/com.famfamfam.silk/icons/table_edit.png"); //$NON-NLS-1$
66                 return createHandledToolItem;
67         }
68
69         @PostConstruct
70         public void postConstruct(Composite parent) {
71                 Resource pipe = null;
72                 try {
73                         List<NamedResource> componentTypes = DistrictNetworkUtil.getDistrictComponents();
74
75                         pipe = componentTypes.stream()
76                                         .filter(r -> r.getName().toLowerCase().contains("pipe"))
77                                         .map(r -> r.getResource())
78                                         .findFirst().orElse(null);
79                 } catch (DatabaseException e) {
80                         LOGGER.error("Failed to read district component types for active model", e);
81                 }
82
83                 LOGGER.debug("Pipe component type is {}", pipe);
84
85                 String data = null;
86                 Resource tableResource = null;
87                 if (pipe != null) {
88                         try {
89                                 Resource model = Simantics.getSession().syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));
90                                 if (model != null) {
91                                         tableResource = Simantics.getSession().syncRequest(new PossibleTechTypeTable(model, pipe), TransientCacheListener.instance());
92                                         data = Simantics.getSession().syncRequest(new PossibleTechTypeTableData(model, pipe), TransientCacheListener.instance());
93                                 }
94                         } catch (DatabaseException e) {
95                                 LOGGER.error("Failed to read tech type table data for {}", pipe, e);
96                         }
97                 }
98
99                 table = new TechTypeTable(parent, 0, pipe, tableResource, data);
100         }
101
102         @PreDestroy
103         public void dispose() {
104                 table.dispose();
105                 table = null;
106         }
107
108 }