]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableView.java
Add enable/disable feature for tech type tables
[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.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.db.Resource;
20 import org.simantics.db.common.NamedResource;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.district.network.DistrictNetworkUtil;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class TechTypeTableView {
27
28         private final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTableView.class);
29
30         public static final String ID = "org.simantics.district.network.ui.techtype.table.techtypeTableView";
31
32         @Inject ESelectionService selectionService;
33
34         public static TechTypeTable table;
35
36         @Inject
37         public void init(MPart part, MApplication app) {
38                 MToolBar toolBar = MMenuFactory.INSTANCE.createToolBar();
39                 toolBar.setToBeRendered(true);
40                 toolBar.getChildren().add(createImportCSVDataToolItem(app));
41                 toolBar.getChildren().add(createValidateTableToolItem(app));
42                 toolBar.getChildren().add(createResetComponentsItem(app));
43                 part.setToolbar(toolBar);
44         }
45
46         private MHandledToolItem createValidateTableToolItem(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.validatetechtypetable");
50                 createHandledToolItem.setCommand(command); //$NON-NLS-1$
51                 createHandledToolItem.setLabel("Validate Tech Type Table");
52                 createHandledToolItem.setIconURI("platform:/plugin/com.famfamfam.silk/icons/accept.png"); //$NON-NLS-1$
53                 return createHandledToolItem;
54         }
55
56         private MHandledToolItem createImportCSVDataToolItem(MApplication app) {
57                 MHandledToolItem createHandledToolItem = MMenuFactory.INSTANCE.createHandledToolItem();
58                 // Command is contributed via fragment
59                 MCommand command = app.getCommand("org.simantics.district.network.ui.command.importtechtypecsv");
60                 createHandledToolItem.setCommand(command); //$NON-NLS-1$
61                 createHandledToolItem.setLabel("Import Tech Type");
62                 createHandledToolItem.setIconURI("platform:/plugin/com.famfamfam.silk/icons/table_edit.png"); //$NON-NLS-1$
63                 return createHandledToolItem;
64         }
65
66         private MToolBarElement createResetComponentsItem(MApplication app) {
67                 MHandledToolItem createHandledToolItem = MMenuFactory.INSTANCE.createHandledToolItem();
68                 // Command is contributed via fragment
69                 MCommand command = app.getCommand("org.simantics.district.network.ui.command.resetcomponentproperties");
70                 createHandledToolItem.setCommand(command); //$NON-NLS-1$
71                 createHandledToolItem.setLabel("Reset Component Properties");
72                 createHandledToolItem.setIconURI("platform:/plugin/com.famfamfam.silk/icons/database_go.png"); //$NON-NLS-1$
73                 return createHandledToolItem;
74         }
75
76         @PostConstruct
77         public void postConstruct(Composite parent) {
78                 Resource pipe = null;
79                 try {
80                         List<NamedResource> componentTypes = DistrictNetworkUtil.getDistrictComponents();
81
82                         pipe = componentTypes.stream()
83                                         .filter(r -> r.getName().toLowerCase().contains("pipe"))
84                                         .map(r -> r.getResource())
85                                         .findFirst().orElse(null);
86                 } catch (DatabaseException e) {
87                         LOGGER.error("Failed to read district component types for active model", e);
88                 }
89
90                 LOGGER.debug("Pipe component type is {}", pipe);
91
92                 table = new TechTypeTable(parent, 0, pipe);
93         }
94
95         @PreDestroy
96         public void dispose() {
97                 table.dispose();
98                 table = null;
99         }
100
101 }