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.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;
31 public class TechTypeTableView {
33 private final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTableView.class);
35 public static final String ID = "org.simantics.district.network.ui.techtype.table.techtypeTableView";
37 @Inject ESelectionService selectionService;
39 public static TechTypeTable table;
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);
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;
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;
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;
82 public void postConstruct(Composite parent) {
85 List<NamedResource> componentTypes = DistrictNetworkUtil.getDistrictComponents();
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);
95 LOGGER.debug("Pipe component type is {}", pipe);
98 Resource tableResource = null;
101 Resource model = Simantics.getSession().syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));
103 tableResource = Simantics.getSession().syncRequest(new PossibleTechTypeTable(model, pipe), TransientCacheListener.instance());
104 data = Simantics.getSession().syncRequest(new PossibleTechTypeTableData(model, pipe), TransientCacheListener.instance());
106 } catch (DatabaseException e) {
107 LOGGER.error("Failed to read tech type table data for {}", pipe, e);
111 table = new TechTypeTable(parent, 0, pipe, tableResource, data);
115 public void dispose() {