]> gerrit.simantics Code Review - simantics/district.git/blob
d98a37872651daef0c361948eda3ebad0060352f
[simantics/district.git] /
1 package org.simantics.district.network.ui.visualisations;
2
3 import javax.annotation.PostConstruct;
4 import javax.annotation.PreDestroy;
5 import javax.inject.Inject;
6
7 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
8 import org.eclipse.e4.ui.workbench.modeling.EPartService;
9 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
10 import org.eclipse.e4.ui.workbench.modeling.IPartListener;
11 import org.eclipse.swt.widgets.Composite;
12 import org.eclipse.ui.IEditorInput;
13 import org.eclipse.ui.IEditorPart;
14 import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
15 import org.simantics.district.network.ui.DistrictDiagramEditor;
16 import org.simantics.ui.workbench.IResourceEditorInput;
17 import org.simantics.utils.ui.workbench.WorkbenchUtils;
18
19 public class DynamicVisualisationsView {
20
21     @Inject
22     ESelectionService selectionService;
23     
24     @Inject
25     EPartService partService;
26
27     private DynamicVisualisationsUI ui;
28
29     @PostConstruct
30     public void postConstruct(Composite parent) {
31         ui = new DynamicVisualisationsUI(parent, 0);
32         
33         IEditorPart editor = WorkbenchUtils.getActiveEditor();
34         if (editor instanceof DistrictDiagramEditor)
35             setDiagramResource(editor.getEditorInput());
36         
37         partService.addPartListener(partListener);
38     }
39
40     @PreDestroy
41     public void dispose() {
42         partService.removePartListener(partListener);
43         ui.dispose();
44         ui = null;
45     }
46
47     private void setDiagramResourceFromCompatibilityEditor(MPart part) {
48         if (part.getObject() instanceof CompatibilityEditor) {
49             CompatibilityEditor editor = (CompatibilityEditor) part.getObject();
50             IEditorPart editorPart = editor.getEditor();
51             setDiagramResource(editorPart.getEditorInput());
52         }
53     }
54     
55     private void setDiagramResource(IEditorInput input) {
56         if (input instanceof IResourceEditorInput) {
57             ui.setDiagramResource(((IResourceEditorInput) input).getResource());
58         }
59     }
60     
61     private IPartListener partListener = new IPartListener() {
62         
63         @Override
64         public void partVisible(MPart part) {
65             setDiagramResourceFromCompatibilityEditor(part);
66         }
67         
68         @Override
69         public void partHidden(MPart part) {
70         }
71         
72         @Override
73         public void partDeactivated(MPart part) {
74         }
75         
76         @Override
77         public void partBroughtToTop(MPart part) {
78             setDiagramResourceFromCompatibilityEditor(part);
79         }
80         
81         @Override
82         public void partActivated(MPart part) {
83             setDiagramResourceFromCompatibilityEditor(part);
84         }
85     };
86 }