]> gerrit.simantics Code Review - simantics/district.git/blob
8f4b57c98b88384bbfaa9f46594406ab7976c7eb
[simantics/district.git] /
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.model.application.ui.menu.MToolBarElement;
17 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
18 import org.eclipse.swt.widgets.Composite;
19 import org.simantics.Simantics;
20 import org.simantics.db.Resource;
21 import org.simantics.db.common.NamedResource;
22 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.layer0.request.PossibleActiveModel;
25 import org.simantics.district.network.DistrictNetworkUtil;
26 import org.simantics.district.network.techtype.requests.PossibleTechTypeTable;
27 import org.simantics.district.network.techtype.requests.PossibleTechTypeTableData;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class TechTypeTableView {
32
33         private final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTableView.class);
34
35         public static final String ID = "org.simantics.district.network.ui.techtype.table.techtypeTableView";
36
37         @Inject ESelectionService selectionService;
38
39         public static TechTypeTable table;
40
41         @Inject
42         public void init(MPart part, MApplication app) {
43                 MToolBar toolBar = MMenuFactory.INSTANCE.createToolBar();
44                 toolBar.setToBeRendered(true);
45                 toolBar.getChildren().add(createImportCSVDataToolItem(app));
46                 toolBar.getChildren().add(createValidateTableToolItem(app));
47                 toolBar.getChildren().add(createResetComponentsItem(app));
48                 part.setToolbar(toolBar);
49         }
50
51         private MHandledToolItem createValidateTableToolItem(MApplication app) {
52                 MHandledToolItem createHandledToolItem = MMenuFactory.INSTANCE.createHandledToolItem();
53                 // Command is contributed via fragment
54                 MCommand command = app.getCommand("org.simantics.district.network.ui.command.validatetechtypetable");
55                 createHandledToolItem.setCommand(command); //$NON-NLS-1$
56                 createHandledToolItem.setLabel("Validate Tech Type Table");
57                 createHandledToolItem.setIconURI("platform:/plugin/com.famfamfam.silk/icons/accept.png"); //$NON-NLS-1$
58                 return createHandledToolItem;
59         }
60
61         private MHandledToolItem createImportCSVDataToolItem(MApplication app) {
62                 MHandledToolItem createHandledToolItem = MMenuFactory.INSTANCE.createHandledToolItem();
63                 // Command is contributed via fragment
64                 MCommand command = app.getCommand("org.simantics.district.network.ui.command.importtechtypecsv");
65                 createHandledToolItem.setCommand(command); //$NON-NLS-1$
66                 createHandledToolItem.setLabel("Import Tech Type");
67                 createHandledToolItem.setIconURI("platform:/plugin/com.famfamfam.silk/icons/table_edit.png"); //$NON-NLS-1$
68                 return createHandledToolItem;
69         }
70
71         private MToolBarElement createResetComponentsItem(MApplication app) {
72                 MHandledToolItem createHandledToolItem = MMenuFactory.INSTANCE.createHandledToolItem();
73                 // Command is contributed via fragment
74                 MCommand command = app.getCommand("org.simantics.district.network.ui.command.resetcomponentproperties");
75                 createHandledToolItem.setCommand(command); //$NON-NLS-1$
76                 createHandledToolItem.setLabel("Reset Component Properties");
77                 createHandledToolItem.setIconURI("platform:/plugin/com.famfamfam.silk/icons/database_go.png"); //$NON-NLS-1$
78                 return createHandledToolItem;
79         }
80
81         @PostConstruct
82         public void postConstruct(Composite parent) {
83                 Resource pipe = null;
84                 try {
85                         List<NamedResource> componentTypes = DistrictNetworkUtil.getDistrictComponents();
86
87                         pipe = componentTypes.stream()
88                                         .filter(r -> r.getName().toLowerCase().contains("pipe"))
89                                         .map(r -> r.getResource())
90                                         .findFirst().orElse(null);
91                 } catch (DatabaseException e) {
92                         LOGGER.error("Failed to read district component types for active model", e);
93                 }
94
95                 LOGGER.debug("Pipe component type is {}", pipe);
96
97                 String data = null;
98                 Resource tableResource = null;
99                 if (pipe != null) {
100                         try {
101                                 Resource model = Simantics.getSession().syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));
102                                 if (model != null) {
103                                         tableResource = Simantics.getSession().syncRequest(new PossibleTechTypeTable(model, pipe), TransientCacheListener.instance());
104                                         data = Simantics.getSession().syncRequest(new PossibleTechTypeTableData(model, pipe), TransientCacheListener.instance());
105                                 }
106                         } catch (DatabaseException e) {
107                                 LOGGER.error("Failed to read tech type table data for {}", pipe, e);
108                         }
109                 }
110
111                 table = new TechTypeTable(parent, 0, pipe, tableResource, data);
112         }
113
114         @PreDestroy
115         public void dispose() {
116                 table.dispose();
117                 table = null;
118         }
119
120 }