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.common.ErrorLogger;
31 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
32 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;
33 import org.simantics.db.ReadGraph;
34 import org.simantics.db.Resource;
35 import org.simantics.db.VirtualGraph;
36 import org.simantics.db.WriteGraph;
37 import org.simantics.db.common.request.WriteRequest;
38 import org.simantics.db.common.request.WriteResultRequest;
39 import org.simantics.db.exception.DatabaseException;
40 import org.simantics.db.exception.RuntimeDatabaseException;
41 import org.simantics.db.layer0.util.RemoverUtil;
42 import org.simantics.db.layer0.variable.Variable;
43 import org.simantics.db.management.ISessionContext;
44 import org.simantics.db.request.Read;
45 import org.simantics.layer0.Layer0;
46 import org.simantics.scenegraph.ontology.ScenegraphResources;
47 import org.simantics.scl.runtime.function.Function1;
48 import org.simantics.ui.workbench.IPropertyPage;
49 import org.simantics.utils.ui.jface.ActiveSelectionProvider;
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 ActiveSelectionProvider selectionProvider = new ActiveSelectionProvider() {
96 public void setSelection(ISelection selection) {
97 super.setSelection(selection);
101 protected String configurationURI() {
102 return configurationURI;
106 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 (RuntimeDatabaseException | DatabaseException e) {
121 LOGGER.error("Failed to initialize modelled widget support runtime scenegraph", e);
124 support = new ModelledSupport(this);
130 public void fireInput() {
131 if (onInputChanged != null)
132 onInputChanged.apply(viewVariable);
136 public void setInitializationData(IConfigurationElement cfig, String propertyName, Object data) {
137 super.setInitializationData(cfig, propertyName, data);
138 if (data instanceof String) {
139 String[] parameters = ((String) data).split(";");
141 for (String parameter : parameters) {
142 String[] keyValue = parameter.split("=");
143 if (keyValue.length > 2) {
144 ErrorLogger.defaultLogWarning("Invalid parameter '" + parameter + ". Complete view argument: "
148 String key = keyValue[0];
149 String value = keyValue.length > 1 ? keyValue[1] : "";
151 if ("configurationURI".equals(key)) {
152 configurationURI = value;
158 protected void doCreateControls(boolean load) {
160 System.out.println(this + " doCreateControls(" + load + ")");
162 if (container == null) {
163 GridLayoutFactory.fillDefaults().applyTo(body);
164 container = new Composite(body, SWT.NONE);
165 GridDataFactory.fillDefaults().grab(true, true).applyTo(container);
166 GridLayoutFactory.fillDefaults().applyTo(container);
173 loader = new SWTViewLoaderProcess(this, getSite(), getClass().getSimpleName());
175 viewVariable = loader.getVariable(Simantics.getSession(), configurationURI(), runtime);
177 onInputChanged = Simantics.getSession().syncRequest(new Read<Function1<Variable, Boolean>>() {
180 public Function1<Variable, Boolean> perform(ReadGraph graph) throws DatabaseException {
181 return viewVariable.getPossiblePropertyValue(graph, "onInputChanged");
186 root = loader.load(Simantics.getSession(), viewVariable);
187 root.createControls(container);
188 root.getControl().addListener(SWT.Dispose, new Listener() {
190 final SWTViewLoaderProcess oldLoader = ModelledView.this.loader;
193 public void handleEvent(Event event) {
195 if (oldLoader != null && !oldLoader.isDisposed())
204 getSite().setSelectionProvider(selectionProvider);
206 } catch (DatabaseException e) {
208 LOGGER.error("Failed to create modelled controls", e);
217 protected void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {
220 // Only create controls if the part is TRULY visible.
221 // Fast view parts seem to cause calls to createPartControl even
222 // when the part is hidden in reality
223 boolean visible = site.getPage().isPartVisible(this);
225 System.out.println(this + ": createControls( visible=" + site.getPage().isPartVisible(this) + " )");
226 doCreateControls(true);
228 getSite().setSelectionProvider(selectionProvider);
229 getSite().getPage().addPartListener(this);
233 protected void inputChanged(IWorkbenchPart provider, Object input) {
234 // Do not accept selections from self
235 if (provider == this)
237 applySessionContext(getSessionContext());
241 public void setFocus() {
242 if (root != null && !root.isNodeDisposed())
246 public void setVisible(boolean value) {
247 if (root != null && !root.isNodeDisposed())
248 root.setVisible(value);
252 public void dispose() {
254 disposeRuntime(runtime);
256 IWorkbenchSite site = getSite();
258 IWorkbenchPage page = site.getPage();
260 page.removePartListener(this);
268 if (loader != null) {
272 if (support != null) {
281 protected void disposeRuntime(Resource runtime) {
282 final Resource rt = this.runtime;
288 Simantics.getSession().sync(new WriteRequest(Simantics.getSession().getService(VirtualGraph.class)) {
290 public void perform(WriteGraph graph) throws DatabaseException {
291 RemoverUtil.remove(graph, rt);
294 } catch (RuntimeDatabaseException | DatabaseException e) {
295 LOGGER.error("Failed to dispose of the modelled widget support runtime scenegraph", e);
300 public void partActivated(IWorkbenchPartReference partRef) {
302 IWorkbenchPart part = partRef.getPart(false);
303 if (this.equals(part)) {
304 System.out.println(this + ": ACTIVATED ( loader=" + loader + ", visible="
305 + getSite().getPage().isPartVisible(part) + " )");
311 public void partBroughtToTop(IWorkbenchPartReference partRef) {
313 IWorkbenchPart part = partRef.getPart(false);
314 if (this.equals(part)) {
315 System.out.println(this + ": BROUGHT TO TOP ( loader=" + loader + ", visible="
316 + getSite().getPage().isPartVisible(part) + " )");
322 public void partClosed(IWorkbenchPartReference partRef) {
324 IWorkbenchPart part = partRef.getPart(false);
325 if (this.equals(part)) {
326 System.out.println(this + ": CLOSED ( loader=" + loader + ", visible="
327 + getSite().getPage().isPartVisible(part) + " )");
333 public void partDeactivated(IWorkbenchPartReference partRef) {
334 IWorkbenchPart part = partRef.getPart(false);
335 if (this.equals(part)) {
337 System.out.println(this + ": DEACTIVATED ( loader=" + loader + ", visible="
338 + getSite().getPage().isPartVisible(part) + " )");
344 public void partOpened(IWorkbenchPartReference partRef) {
346 IWorkbenchPart part = partRef.getPart(false);
347 if (this.equals(part)) {
348 System.out.println(this + ": OPENED ( loader=" + loader + ", visible="
349 + getSite().getPage().isPartVisible(part) + " )");
355 public void partInputChanged(IWorkbenchPartReference partRef) {
359 public void partHidden(IWorkbenchPartReference partRef) {
360 IWorkbenchPart part = partRef.getPart(false);
361 if (this.equals(part)) {
363 System.out.println(this + ": HID ( loader=" + loader + ", visible="
364 + getSite().getPage().isPartVisible(part) + " )");
370 public void partVisible(IWorkbenchPartReference partRef) {
371 IWorkbenchPart part = partRef.getPart(false);
372 if (this.equals(part)) {
374 System.out.println(this + ": MADE VISIBLE ( loader=" + loader + ", visible="
375 + getSite().getPage().isPartVisible(part) + " )");
376 createControlsIfNecessary(false);
380 private void createControlsIfNecessary(boolean forceContainerRepaint) {
381 // Cancel potential dispose before creating controls
382 reallyClearExisting = false;
383 if (loader == null) {
384 doCreateControls(true);
387 if (forceContainerRepaint) {
388 container.layout(true);
389 Point size = container.getSize();
390 container.redraw(0, 0, size.x, size.y, true);
396 // Can be used to cancel already scheduled dispose
397 volatile boolean reallyClearExisting = false;
399 Runnable clearExisting = new Runnable() {
403 if(!reallyClearExisting)
406 onInputChanged = null;
408 if (loader != null) {
412 if (container != null) {
414 final Composite oldContainer = container;
415 Display.getCurrent().asyncExec(new Runnable() {
418 if (!oldContainer.isDisposed())
419 oldContainer.dispose();
423 if (!container.isDisposed())
424 GridDataFactory.fillDefaults().exclude(true).applyTo(container);
434 private void clearExisting() {
435 Display.getCurrent().timerExec(TIME_BEFORE_DISPOSE_WHEN_HIDDEN, clearExisting);
437 // Do this after scheduling the runnable, because otherwise already scheduled runnable could
439 reallyClearExisting = true;
443 protected IPropertyPage getPropertyPage() {