]> gerrit.simantics Code Review - simantics/district.git/blob
6ab176ede84979537a7b1dc303cbcfcf8984afd7
[simantics/district.git] /
1 package org.simantics.district.network.ui.techtype.adapters;
2
3 import java.io.IOException;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6
7 import org.eclipse.core.runtime.FileLocator;
8 import org.eclipse.e4.core.contexts.IEclipseContext;
9 import org.eclipse.e4.ui.workbench.modeling.EPartService;
10 import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
11 import org.eclipse.jface.resource.ImageDescriptor;
12 import org.eclipse.ui.PlatformUI;
13 import org.simantics.Simantics;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.request.ReadRequest;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.district.network.ontology.DistrictNetworkResource;
19 import org.simantics.district.network.ui.techtype.table.TechTypeTableView;
20 import org.simantics.ui.utils.ResourceAdaptionUtils;
21 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
22 import org.simantics.ui.workbench.editor.EditorAdapter;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * @author Reino Ruusu
28  */
29 public class TechTypeEditorAdapter extends AbstractResourceEditorAdapter implements EditorAdapter {
30
31         private static final Logger LOGGER = LoggerFactory.getLogger(TechTypeEditorAdapter.class);
32
33         public TechTypeEditorAdapter() throws MalformedURLException, IOException {
34                 super("Tech Type Table View");
35                 setIcon(ImageDescriptor.createFromURL(FileLocator.resolve(new URL("platform:/plugin/com.famfamfam.silk/icons/table.png")))); //$NON-NLS-1$
36         }
37
38         @Override
39         public boolean canHandle(ReadGraph graph, Object input) throws DatabaseException {
40                 Resource r = ResourceAdaptionUtils.toSingleResource(input);
41                 if (r == null)
42                         return false;
43                 return graph.isInstanceOf(r, DistrictNetworkResource.getInstance(graph).TechType_TechTypeTable);
44         }
45
46         @Override
47         public void openEditor(Resource table) throws Exception {
48                 IEclipseContext eclipseContext = PlatformUI.getWorkbench().getService(IEclipseContext.class);
49                 if (eclipseContext == null) {
50                         LOGGER.error("No Eclipse context available");
51                         return;
52                 }
53
54                 eclipseContext = eclipseContext.getActiveLeaf();
55                 EPartService partService = eclipseContext.get(EPartService.class);
56                 if (partService == null)
57                         return;
58
59                 partService.showPart(TechTypeTableView.ID, PartState.ACTIVATE);
60
61                 try {
62                         Simantics.getSession().syncRequest(new ReadRequest() {
63                                 @Override
64                                 public void run(ReadGraph graph) throws DatabaseException {
65                                         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
66                                         Resource componentType = graph.getPossibleObject(table, DN.TechType_TechTypeTable_HasComponentType);
67
68                                         TechTypeTableView.table.getDisplay().asyncExec(() -> {
69                                                 TechTypeTableView.table.setComponentType(componentType);
70                                         });
71                                 }
72                         });
73                 } catch (DatabaseException e) {
74                         LOGGER.error("Failed to read tech type table {}", table, e);
75                 }
76         }
77 }