1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.profile.view;
14 import org.eclipse.swt.widgets.Display;
15 import org.eclipse.ui.IEditorPart;
16 import org.eclipse.ui.IEditorReference;
17 import org.eclipse.ui.IPartListener2;
18 import org.eclipse.ui.IPartService;
19 import org.eclipse.ui.IWorkbenchPage;
20 import org.eclipse.ui.IWorkbenchPart;
21 import org.eclipse.ui.IWorkbenchPartReference;
22 import org.eclipse.ui.IWorkbenchSite;
23 import org.eclipse.ui.IWorkbenchWindow;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.part.EditorPart;
26 import org.simantics.Simantics;
27 import org.simantics.browsing.ui.GraphExplorer;
28 import org.simantics.browsing.ui.common.ErrorLogger;
29 import org.simantics.browsing.ui.graph.impl.InputSourceListener;
30 import org.simantics.browsing.ui.graph.impl.ObservableInputSource;
31 import org.simantics.browsing.ui.graph.impl.WorkbenchSessionContextInputSource;
32 import org.simantics.db.Disposable;
33 import org.simantics.db.ReadGraph;
34 import org.simantics.db.Resource;
35 import org.simantics.db.Session;
36 import org.simantics.db.common.request.UniqueRead;
37 import org.simantics.db.exception.DatabaseException;
38 import org.simantics.db.management.ISessionContext;
39 import org.simantics.db.procedure.Procedure;
40 import org.simantics.diagram.stubs.DiagramResource;
41 import org.simantics.utils.ObjectUtils;
42 import org.simantics.utils.ui.SWTUtils;
45 * @author Tuukka Lehtonen
47 public class ActiveRuntimeDiagramInputSource implements WorkbenchSessionContextInputSource, ObservableInputSource, IPartListener2, Disposable {
49 protected Display display;
50 protected IWorkbenchPart ownerPart;
51 protected IPartService service;
52 protected IEditorPart activeEditor;
53 protected Resource activeRuntimeDiagram;
54 protected InputSourceListener listener;
55 protected boolean ownerIsVisible = true;
56 protected Resource lastInputResource;
59 public void init(IWorkbenchSite site, IWorkbenchPart part) {
60 this.display = site.getShell().getDisplay();
61 this.ownerPart = part;
68 protected void attachToWorkbench() {
69 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
72 service = window.getPartService();
73 service.addPartListener(this);
75 IWorkbenchPage activePage = window.getActivePage();
76 if (activePage == null)
79 IEditorPart activeEditor = activePage.getActiveEditor();
80 if (activeEditor != null)
81 editorActivated(activeEditor);
88 public void dispose() {
90 service.removePartListener(this);
93 activeRuntimeDiagram = null;
99 protected void fireIfInputChanged(Object oldSelection, Object newSelection) {
100 InputSourceListener l = listener;
102 if (!ObjectUtils.objectEquals(oldSelection, newSelection))
103 l.inputChanged(this);
107 public Object get(ISessionContext ctx) {
108 return activeRuntimeDiagram != null ? activeRuntimeDiagram : GraphExplorer.EMPTY_INPUT;
112 public IWorkbenchPart getProvider() {
117 public void setListener(InputSourceListener listener) {
118 this.listener = listener;
121 private static class InputTransformation extends UniqueRead<Resource> {
123 private Resource resource;
124 private Resource defaultResult;
126 public InputTransformation(Resource input, Resource defaultResult) {
127 this.resource = input;
128 this.defaultResult = defaultResult;
132 public Resource perform(ReadGraph graph) throws DatabaseException {
133 if (graph.isInstanceOf(resource, DiagramResource.getInstance(graph).RuntimeDiagram))
135 if (!graph.hasStatement(resource))
138 // if (defaultResult != null && !graph.hasStatement(defaultResult))
141 return defaultResult;
146 class InputProcedure implements Procedure<Resource> {
148 public void execute(Resource result) {
151 SWTUtils.asyncExec(d, () -> changeInput(result));
154 public void exception(Throwable t) {
155 ErrorLogger.defaultLogError(t);
159 private InputProcedure inputProcedure = new InputProcedure();
161 protected void testAndChangeInput(Resource resource) {
162 Session session = Simantics.peekSession();
163 if (session != null && resource != null) {
164 session.asyncRequest(
165 new InputTransformation(resource, activeRuntimeDiagram),
172 protected void editorActivated(IEditorPart part) {
173 Resource resource = part.getAdapter(Resource.class);
174 lastInputResource = resource;
175 if (ownerIsVisible) {
176 testAndChangeInput(resource);
180 private boolean allEditorsClosed(IEditorPart forWindowOfPart) {
181 for (IWorkbenchPage page : forWindowOfPart.getEditorSite().getWorkbenchWindow().getPages()) {
182 IEditorReference[] editors = page.getEditorReferences();
183 if (editors.length > 0)
189 protected void activeEditorClosed() {
190 if (allEditorsClosed(activeEditor))
194 private void changeInput(Resource newInput) {
195 Resource oldInput = activeRuntimeDiagram;
196 this.activeRuntimeDiagram = newInput;
197 fireIfInputChanged(oldInput, newInput != null ? newInput : GraphExplorer.EMPTY_INPUT);
201 public void partActivated(IWorkbenchPartReference partRef) {
202 //System.out.println("partActivated1: " + partRef);
203 if (partRef instanceof IEditorReference) {
204 IEditorPart part = ((IEditorReference) partRef).getEditor(false);
205 //System.out.println("partActivated2: " + part);
206 if (part instanceof EditorPart) {
207 editorActivated((IEditorPart) part);
213 public void partBroughtToTop(IWorkbenchPartReference partRef) {
214 //System.out.println("partBroughtToTop: " + partRef);
218 public void partClosed(IWorkbenchPartReference partRef) {
219 //System.out.println("partClosed1: " + partRef);
220 if (partRef instanceof IEditorReference) {
221 IEditorPart part = ((IEditorReference) partRef).getEditor(false);
222 //System.out.println("partClosed2: " + part);
223 if (part == activeEditor) {
224 activeEditorClosed();
230 public void partDeactivated(IWorkbenchPartReference partRef) {
231 //System.out.println("partDeactivated: " + partRef);
235 public void partOpened(IWorkbenchPartReference partRef) {
236 //System.out.println("partOpened: " + partRef);
240 public void partInputChanged(IWorkbenchPartReference partRef) {
241 //System.out.println("partInputChanged: " + partRef);
245 public void partHidden(IWorkbenchPartReference partRef) {
246 //System.out.println("partHidden: " + partRef);
247 IWorkbenchPart part = partRef.getPart(false);
248 if (ownerPart != null && ownerPart.equals(part)) {
249 ownerIsVisible = false;
254 public void partVisible(IWorkbenchPartReference partRef) {
255 //System.out.println("partVisible: " + partRef);
256 IWorkbenchPart part = partRef.getPart(false);
257 if (ownerPart != null && ownerPart.equals(part)) {
258 ownerIsVisible = true;
259 testAndChangeInput(lastInputResource);