]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram.profile/src/org/simantics/diagram/profile/view/ActiveRuntimeDiagramInputSource.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram.profile / src / org / simantics / diagram / profile / view / ActiveRuntimeDiagramInputSource.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.profile.view;\r
13 \r
14 import org.eclipse.swt.widgets.Display;\r
15 import org.eclipse.ui.IEditorPart;\r
16 import org.eclipse.ui.IEditorReference;\r
17 import org.eclipse.ui.IPartListener2;\r
18 import org.eclipse.ui.IPartService;\r
19 import org.eclipse.ui.IWorkbenchPage;\r
20 import org.eclipse.ui.IWorkbenchPart;\r
21 import org.eclipse.ui.IWorkbenchPartReference;\r
22 import org.eclipse.ui.IWorkbenchSite;\r
23 import org.eclipse.ui.IWorkbenchWindow;\r
24 import org.eclipse.ui.PlatformUI;\r
25 import org.eclipse.ui.part.EditorPart;\r
26 import org.simantics.Simantics;\r
27 import org.simantics.browsing.ui.GraphExplorer;\r
28 import org.simantics.browsing.ui.common.ErrorLogger;\r
29 import org.simantics.browsing.ui.graph.impl.InputSourceListener;\r
30 import org.simantics.browsing.ui.graph.impl.ObservableInputSource;\r
31 import org.simantics.browsing.ui.graph.impl.WorkbenchSessionContextInputSource;\r
32 import org.simantics.db.Disposable;\r
33 import org.simantics.db.ReadGraph;\r
34 import org.simantics.db.Resource;\r
35 import org.simantics.db.Session;\r
36 import org.simantics.db.common.request.UniqueRead;\r
37 import org.simantics.db.exception.DatabaseException;\r
38 import org.simantics.db.management.ISessionContext;\r
39 import org.simantics.db.procedure.Procedure;\r
40 import org.simantics.diagram.stubs.DiagramResource;\r
41 import org.simantics.utils.ObjectUtils;\r
42 import org.simantics.utils.ui.SWTUtils;\r
43 \r
44 /**\r
45  * @author Tuukka Lehtonen\r
46  */\r
47 public class ActiveRuntimeDiagramInputSource implements WorkbenchSessionContextInputSource, ObservableInputSource, IPartListener2, Disposable {\r
48 \r
49     protected Display             display;\r
50     protected IWorkbenchPart      ownerPart;\r
51     protected IPartService        service;\r
52     protected IEditorPart         activeEditor;\r
53     protected Resource            activeRuntimeDiagram;\r
54     protected InputSourceListener listener;\r
55     protected boolean             ownerIsVisible = true;\r
56     protected Resource            lastInputResource;\r
57 \r
58     @Override\r
59     public void init(IWorkbenchSite site, IWorkbenchPart part) {\r
60         this.display = site.getShell().getDisplay();\r
61         this.ownerPart = part;\r
62         attachToWorkbench();\r
63     }\r
64 \r
65     /**\r
66      * @thread SWT\r
67      */\r
68     protected void attachToWorkbench() {\r
69         IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();\r
70         if (window == null)\r
71             return;\r
72         service = window.getPartService();\r
73         service.addPartListener(this);\r
74 \r
75         IWorkbenchPage activePage = window.getActivePage();\r
76         if (activePage == null)\r
77             return;\r
78         \r
79         IEditorPart activeEditor = activePage.getActiveEditor();\r
80         if (activeEditor != null)\r
81             editorActivated(activeEditor);\r
82     }\r
83 \r
84     /**\r
85      * @thread SWT\r
86      */\r
87     @Override\r
88     public void dispose() {\r
89         if (service != null)\r
90             service.removePartListener(this);\r
91         service = null;\r
92         activeEditor = null;\r
93         activeRuntimeDiagram = null;\r
94         listener = null;\r
95         ownerPart = null;\r
96         display = null;\r
97     }\r
98 \r
99     protected void fireIfInputChanged(Object oldSelection, Object newSelection) {\r
100         InputSourceListener l = listener;\r
101         if (l != null)\r
102             if (!ObjectUtils.objectEquals(oldSelection, newSelection))\r
103                 l.inputChanged(this);\r
104     }\r
105 \r
106     @Override\r
107     public Object get(ISessionContext ctx) {\r
108         return activeRuntimeDiagram != null ? activeRuntimeDiagram : GraphExplorer.EMPTY_INPUT;\r
109     }\r
110     \r
111     @Override\r
112     public IWorkbenchPart getProvider() {\r
113         return activeEditor;\r
114     }\r
115 \r
116     @Override\r
117     public void setListener(InputSourceListener listener) {\r
118         this.listener = listener;\r
119     }\r
120 \r
121     private static class InputTransformation extends UniqueRead<Resource> {\r
122 \r
123         private Resource resource;\r
124         private Resource defaultResult;\r
125 \r
126         public InputTransformation(Resource input, Resource defaultResult) {\r
127             this.resource = input;\r
128             this.defaultResult = defaultResult;\r
129         }\r
130 \r
131         @Override\r
132         public Resource perform(ReadGraph graph) throws DatabaseException {\r
133             if (graph.isInstanceOf(resource, DiagramResource.getInstance(graph).RuntimeDiagram))\r
134                 return resource;\r
135             if (!graph.hasStatement(resource))\r
136                 return null;\r
137 \r
138 //            if (defaultResult != null && !graph.hasStatement(defaultResult))\r
139 //                return null;\r
140 \r
141             return defaultResult;\r
142         }\r
143 \r
144     }\r
145 \r
146     class InputProcedure implements Procedure<Resource> {\r
147         @Override\r
148         public void execute(Resource result) {\r
149             Display d = display;\r
150             if (d != null)\r
151                 SWTUtils.asyncExec(d, () -> changeInput(result));\r
152         }\r
153         @Override\r
154         public void exception(Throwable t) {\r
155             ErrorLogger.defaultLogError(t);\r
156         }\r
157     }\r
158 \r
159     private InputProcedure inputProcedure = new InputProcedure();\r
160 \r
161     protected void testAndChangeInput(Resource resource) {\r
162         Session session = Simantics.peekSession();\r
163         if (session != null && resource != null) {\r
164             session.asyncRequest(\r
165                     new InputTransformation(resource, activeRuntimeDiagram),\r
166                     inputProcedure);\r
167         } else {\r
168             changeInput(null);\r
169         }\r
170     }\r
171 \r
172     protected void editorActivated(IEditorPart part) {\r
173         Resource resource = part.getAdapter(Resource.class); \r
174         lastInputResource = resource;\r
175         if (ownerIsVisible) {\r
176             testAndChangeInput(resource);\r
177         }\r
178     }\r
179 \r
180     private boolean allEditorsClosed(IEditorPart forWindowOfPart) {\r
181         for (IWorkbenchPage page : forWindowOfPart.getEditorSite().getWorkbenchWindow().getPages()) {\r
182             IEditorReference[] editors = page.getEditorReferences();\r
183             if (editors.length > 0)\r
184                 return false;\r
185         }\r
186         return true;\r
187     }\r
188 \r
189     protected void activeEditorClosed() {\r
190         if (allEditorsClosed(activeEditor))\r
191             changeInput(null);\r
192     }\r
193 \r
194     private void changeInput(Resource newInput) {\r
195         Resource oldInput = activeRuntimeDiagram;\r
196         this.activeRuntimeDiagram = newInput;\r
197         fireIfInputChanged(oldInput, newInput != null ? newInput : GraphExplorer.EMPTY_INPUT);\r
198     }\r
199 \r
200     @Override\r
201     public void partActivated(IWorkbenchPartReference partRef) {\r
202         //System.out.println("partActivated1: " + partRef);\r
203         if (partRef instanceof IEditorReference) {\r
204             IEditorPart part = ((IEditorReference) partRef).getEditor(false);\r
205             //System.out.println("partActivated2: " + part);\r
206             if (part instanceof EditorPart) {\r
207                 editorActivated((IEditorPart) part);\r
208             }\r
209         }\r
210     }\r
211 \r
212     @Override\r
213     public void partBroughtToTop(IWorkbenchPartReference partRef) {\r
214         //System.out.println("partBroughtToTop: " + partRef);\r
215     }\r
216 \r
217     @Override\r
218     public void partClosed(IWorkbenchPartReference partRef) {\r
219         //System.out.println("partClosed1: " + partRef);\r
220         if (partRef instanceof IEditorReference) {\r
221             IEditorPart part = ((IEditorReference) partRef).getEditor(false);\r
222             //System.out.println("partClosed2: " + part);\r
223             if (part == activeEditor) {\r
224                 activeEditorClosed();\r
225             }\r
226         }\r
227     }\r
228 \r
229     @Override\r
230     public void partDeactivated(IWorkbenchPartReference partRef) {\r
231         //System.out.println("partDeactivated: " + partRef);\r
232     }\r
233 \r
234     @Override\r
235     public void partOpened(IWorkbenchPartReference partRef) {\r
236         //System.out.println("partOpened: " + partRef);\r
237     }\r
238 \r
239     @Override\r
240     public void partInputChanged(IWorkbenchPartReference partRef) {\r
241         //System.out.println("partInputChanged: " + partRef);\r
242     }\r
243 \r
244     @Override\r
245     public void partHidden(IWorkbenchPartReference partRef) {\r
246         //System.out.println("partHidden: " + partRef);\r
247         IWorkbenchPart part = partRef.getPart(false);\r
248         if (ownerPart != null && ownerPart.equals(part)) {\r
249             ownerIsVisible = false;\r
250         }\r
251     }\r
252 \r
253     @Override\r
254     public void partVisible(IWorkbenchPartReference partRef) {\r
255         //System.out.println("partVisible: " + partRef);\r
256         IWorkbenchPart part = partRef.getPart(false);\r
257         if (ownerPart != null && ownerPart.equals(part)) {\r
258             ownerIsVisible = true;\r
259             testAndChangeInput(lastInputResource);\r
260         }\r
261     }\r
262 \r
263 }\r