1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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.views.swt;
14 import org.eclipse.core.runtime.IConfigurationElement;
15 import org.eclipse.jface.layout.GridDataFactory;
16 import org.eclipse.jface.layout.GridLayoutFactory;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.Point;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.swt.widgets.Event;
23 import org.eclipse.swt.widgets.Listener;
24 import org.eclipse.ui.IPartListener2;
25 import org.eclipse.ui.IWorkbenchPage;
26 import org.eclipse.ui.IWorkbenchPart;
27 import org.eclipse.ui.IWorkbenchPartReference;
28 import org.eclipse.ui.IWorkbenchSite;
29 import org.simantics.Simantics;
30 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
31 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;
32 import org.simantics.db.ReadGraph;
33 import org.simantics.db.Resource;
34 import org.simantics.db.VirtualGraph;
35 import org.simantics.db.WriteGraph;
36 import org.simantics.db.common.request.WriteRequest;
37 import org.simantics.db.common.request.WriteResultRequest;
38 import org.simantics.db.exception.DatabaseException;
39 import org.simantics.db.exception.ServiceNotFoundException;
40 import org.simantics.db.layer0.util.RemoverUtil;
41 import org.simantics.db.layer0.variable.Variable;
42 import org.simantics.db.management.ISessionContext;
43 import org.simantics.db.request.Read;
44 import org.simantics.layer0.Layer0;
45 import org.simantics.scenegraph.ontology.ScenegraphResources;
46 import org.simantics.scl.runtime.function.Function1;
47 import org.simantics.ui.workbench.IPropertyPage;
48 import org.simantics.utils.ui.ErrorLogger;
49 import org.simantics.utils.ui.jface.BasePostSelectionProvider;
50 import org.simantics.views.swt.client.base.SWTRoot;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * To use this class, first model your view contents in .pgraph files according
56 * to the Browsing.pgraph ontology. After that there are two ways to put your
57 * configuration to use by defining a new view extension:
59 * <li>Set view extension class to
60 * <code>org.simantics.browsing.ui.swt.ModelledView:configurationURI=ConfigURI</code>
61 * , where ConfigURI is the URI of your view configuration.</li>
62 * <li>Extend this class and override at least {@link #configurationURI()} to
63 * define the URI from which the configuration for the view is found. Set view
64 * extension class to the created class.</li>
67 * @author Antti Villberg
69 public class ModelledView extends SimanticsView implements IPartListener2 {
71 private static final Logger LOGGER = LoggerFactory.getLogger(ModelledView.class);
73 public static final int TIME_BEFORE_DISPOSE_WHEN_HIDDEN = 30000; // ms
75 private static final boolean DEBUG = false;
77 protected Resource runtime;
78 protected String configurationURI;
80 protected SWTRoot root;
82 protected Variable viewVariable;
84 protected Function1<Variable, Boolean> onInputChanged = null;
86 protected SWTViewLoaderProcess loader;
88 protected Composite body;
90 protected Composite container;
92 protected ModelledSupport support;
94 private BasePostSelectionProvider selectionProvider = new BasePostSelectionProvider() {
96 public void setSelection(ISelection selection) {
97 super.setAndFireNonEqualSelection(selection);
98 super.firePostSelection(selection);
102 protected String configurationURI() {
103 return configurationURI;
107 protected WidgetSupportImpl createSupport() {
109 runtime = Simantics.getSession().sync(
110 new WriteResultRequest<Resource>(Simantics.getSession().getService(VirtualGraph.class)) {
112 public Resource perform(WriteGraph graph) throws DatabaseException {
113 Layer0 L0 = Layer0.getInstance(graph);
114 ScenegraphResources SG = ScenegraphResources.getInstance(graph);
115 Resource runtime = graph.newResource();
116 graph.claim(runtime, L0.InstanceOf, null, SG.Runtime);
120 } catch (ServiceNotFoundException | DatabaseException e) {
121 LOGGER.error("Failed to initialize modelled view database runtime support structures", e);
124 support = new ModelledSupport(this);
129 public void fireInput() {
130 if (onInputChanged != null)
131 onInputChanged.apply(viewVariable);
135 public void setInitializationData(IConfigurationElement cfig, String propertyName, Object data) {
136 super.setInitializationData(cfig, propertyName, data);
137 if (data instanceof String) {
138 String[] parameters = ((String) data).split(";");
140 for (String parameter : parameters) {
141 String[] keyValue = parameter.split("=");
142 if (keyValue.length > 2) {
143 ErrorLogger.defaultLogWarning("Invalid parameter '" + parameter + ". Complete view argument: "
147 String key = keyValue[0];
148 String value = keyValue.length > 1 ? keyValue[1] : "";
150 if ("configurationURI".equals(key)) {
151 configurationURI = value;
157 protected void doCreateControls(boolean load) {
159 System.out.println(this + " doCreateControls(" + load + ")");
161 if (container == null) {
162 GridLayoutFactory.fillDefaults().applyTo(body);
163 container = new Composite(body, SWT.NONE);
164 GridDataFactory.fillDefaults().grab(true, true).applyTo(container);
165 GridLayoutFactory.fillDefaults().applyTo(container);
170 loader = new SWTViewLoaderProcess(this, getSite(), getClass().getSimpleName());
172 viewVariable = loader.getVariable(Simantics.getSession(), configurationURI(), runtime);
174 onInputChanged = Simantics.getSession().syncRequest(new Read<Function1<Variable, Boolean>>() {
177 public Function1<Variable, Boolean> perform(ReadGraph graph) throws DatabaseException {
178 return viewVariable.getPossiblePropertyValue(graph, "onInputChanged");
183 root = loader.load(Simantics.getSession(), viewVariable);
184 root.createControls(container);
185 root.getControl().addListener(SWT.Dispose, new Listener() {
187 final SWTViewLoaderProcess oldLoader = ModelledView.this.loader;
190 public void handleEvent(Event event) {
192 if (oldLoader != null && !oldLoader.isDisposed())
201 } catch (DatabaseException e) {
202 LOGGER.error("Failed to create modelled SWT view controls", e);
208 protected void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {
211 // Only create controls if the part is TRULY visible.
212 // Fast view parts seem to cause calls to createPartControl even
213 // when the part is hidden in reality
215 System.out.println(this + ": createControls( visible=" + site.getPage().isPartVisible(this) + " )");
216 doCreateControls(true);
218 getSite().setSelectionProvider(selectionProvider);
219 getSite().getPage().addPartListener(this);
222 protected void inputChanged(IWorkbenchPart provider, Object input) {
223 // Do not accept selections from self
224 if (provider == this)
226 applySessionContext(getSessionContext());
230 public void setFocus() {
231 if (root != null && !root.isNodeDisposed())
235 public void setVisible(boolean value) {
236 if (root != null && !root.isNodeDisposed())
237 root.setVisible(value);
241 public void dispose() {
243 disposeRuntime(runtime);
245 IWorkbenchSite site = getSite();
247 IWorkbenchPage page = site.getPage();
249 page.removePartListener(this);
257 if (loader != null) {
261 if (support != null) {
270 protected void disposeRuntime(Resource runtime) {
271 final Resource rt = this.runtime;
277 Simantics.getSession().sync(new WriteRequest(Simantics.getSession().getService(VirtualGraph.class)) {
279 public void perform(WriteGraph graph) throws DatabaseException {
280 RemoverUtil.remove(graph, rt);
283 } catch (ServiceNotFoundException | DatabaseException e) {
284 LOGGER.error("Failed to dispose of modelled view database runtime support structures", e);
289 public void partActivated(IWorkbenchPartReference partRef) {
291 IWorkbenchPart part = partRef.getPart(false);
292 if (this.equals(part)) {
293 System.out.println(this + ": ACTIVATED ( loader=" + loader + ", visible="
294 + getSite().getPage().isPartVisible(part) + " )");
300 public void partBroughtToTop(IWorkbenchPartReference partRef) {
302 IWorkbenchPart part = partRef.getPart(false);
303 if (this.equals(part)) {
304 System.out.println(this + ": BROUGHT TO TOP ( loader=" + loader + ", visible="
305 + getSite().getPage().isPartVisible(part) + " )");
311 public void partClosed(IWorkbenchPartReference partRef) {
313 IWorkbenchPart part = partRef.getPart(false);
314 if (this.equals(part)) {
315 System.out.println(this + ": CLOSED ( loader=" + loader + ", visible="
316 + getSite().getPage().isPartVisible(part) + " )");
322 public void partDeactivated(IWorkbenchPartReference partRef) {
323 IWorkbenchPart part = partRef.getPart(false);
324 if (this.equals(part)) {
326 System.out.println(this + ": DEACTIVATED ( loader=" + loader + ", visible="
327 + getSite().getPage().isPartVisible(part) + " )");
333 public void partOpened(IWorkbenchPartReference partRef) {
335 IWorkbenchPart part = partRef.getPart(false);
336 if (this.equals(part)) {
337 System.out.println(this + ": OPENED ( loader=" + loader + ", visible="
338 + getSite().getPage().isPartVisible(part) + " )");
344 public void partInputChanged(IWorkbenchPartReference partRef) {
348 public void partHidden(IWorkbenchPartReference partRef) {
349 IWorkbenchPart part = partRef.getPart(false);
350 if (this.equals(part)) {
352 System.out.println(this + ": HID ( loader=" + loader + ", visible="
353 + getSite().getPage().isPartVisible(part) + " )");
359 public void partVisible(IWorkbenchPartReference partRef) {
360 IWorkbenchPart part = partRef.getPart(false);
361 if (this.equals(part)) {
363 System.out.println(this + ": MADE VISIBLE ( loader=" + loader + ", visible="
364 + getSite().getPage().isPartVisible(part) + " )");
365 createControlsIfNecessary(false);
369 private void createControlsIfNecessary(boolean forceContainerRepaint) {
370 // Cancel potential dispose before creating controls
371 reallyClearExisting = false;
372 if (loader == null) {
373 doCreateControls(true);
376 if (forceContainerRepaint) {
377 container.layout(true);
378 Point size = container.getSize();
379 container.redraw(0, 0, size.x, size.y, true);
385 // Can be used to cancel already scheduled dispose
386 volatile boolean reallyClearExisting = false;
388 Runnable clearExisting = new Runnable() {
392 if(!reallyClearExisting)
395 onInputChanged = null;
397 if (loader != null) {
401 if (container != null) {
403 final Composite oldContainer = container;
404 Display.getCurrent().asyncExec(new Runnable() {
407 if (!oldContainer.isDisposed())
408 oldContainer.dispose();
412 if (!container.isDisposed())
413 GridDataFactory.fillDefaults().exclude(true).applyTo(container);
423 private void clearExisting() {
424 Display.getCurrent().timerExec(TIME_BEFORE_DISPOSE_WHEN_HIDDEN, clearExisting);
426 // Do this after scheduling the runnable, because otherwise already scheduled runnable could
428 reallyClearExisting = true;
432 protected IPropertyPage getPropertyPage() {