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.common.utils.Logger;
40 import org.simantics.db.exception.DatabaseException;
41 import org.simantics.db.exception.ServiceNotFoundException;
42 import org.simantics.db.layer0.util.RemoverUtil;
43 import org.simantics.db.layer0.variable.Variable;
44 import org.simantics.db.management.ISessionContext;
45 import org.simantics.db.request.Read;
46 import org.simantics.layer0.Layer0;
47 import org.simantics.scenegraph.ontology.ScenegraphResources;
48 import org.simantics.scl.runtime.function.Function1;
49 import org.simantics.ui.workbench.IPropertyPage;
50 import org.simantics.utils.ui.jface.ActiveSelectionProvider;
51 import org.simantics.views.swt.client.base.SWTRoot;
54 * To use this class, first model your view contents in .pgraph files according
55 * to the Browsing.pgraph ontology. After that there are two ways to put your
56 * configuration to use by defining a new view extension:
58 * <li>Set view extension class to
59 * <code>org.simantics.browsing.ui.swt.ModelledView:configurationURI=ConfigURI</code>
60 * , where ConfigURI is the URI of your view configuration.</li>
61 * <li>Extend this class and override at least {@link #configurationURI()} to
62 * define the URI from which the configuration for the view is found. Set view
63 * extension class to the created class.</li>
66 * @author Antti Villberg
68 public class ModelledView extends SimanticsView implements IPartListener2 {
70 public static final int TIME_BEFORE_DISPOSE_WHEN_HIDDEN = 30000; // ms
72 private static final boolean DEBUG = false;
74 protected Resource runtime;
75 protected String configurationURI;
77 protected SWTRoot root;
79 protected Variable viewVariable;
81 protected Function1<Variable, Boolean> onInputChanged = null;
83 protected SWTViewLoaderProcess loader;
85 protected Composite body;
87 protected Composite container;
89 protected ModelledSupport support;
91 ActiveSelectionProvider selectionProvider = new ActiveSelectionProvider() {
93 public void setSelection(ISelection selection) {
94 super.setSelection(selection);
98 protected String configurationURI() {
99 return configurationURI;
103 protected WidgetSupportImpl createSupport() {
106 runtime = Simantics.getSession().sync(
107 new WriteResultRequest<Resource>(Simantics.getSession().getService(VirtualGraph.class)) {
109 public Resource perform(WriteGraph graph) throws DatabaseException {
110 Layer0 L0 = Layer0.getInstance(graph);
111 ScenegraphResources SG = ScenegraphResources.getInstance(graph);
112 Resource runtime = graph.newResource();
113 graph.claim(runtime, L0.InstanceOf, null, SG.Runtime);
117 } catch (ServiceNotFoundException e) {
118 Logger.defaultLogError(e);
119 } catch (DatabaseException e) {
120 Logger.defaultLogError(e);
123 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);
172 loader = new SWTViewLoaderProcess(this, getSite(), getClass().getSimpleName());
174 viewVariable = loader.getVariable(Simantics.getSession(), configurationURI(), runtime);
176 onInputChanged = Simantics.getSession().syncRequest(new Read<Function1<Variable, Boolean>>() {
179 public Function1<Variable, Boolean> perform(ReadGraph graph) throws DatabaseException {
180 return viewVariable.getPossiblePropertyValue(graph, "onInputChanged");
185 root = loader.load(Simantics.getSession(), viewVariable);
186 root.createControls(container);
187 root.getControl().addListener(SWT.Dispose, new Listener() {
189 final SWTViewLoaderProcess oldLoader = ModelledView.this.loader;
192 public void handleEvent(Event event) {
194 if (oldLoader != null && !oldLoader.isDisposed())
203 getSite().setSelectionProvider(selectionProvider);
205 } catch (DatabaseException e) {
208 Logger.defaultLogError(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 (ServiceNotFoundException e) {
295 Logger.defaultLogError(e);
296 } catch (DatabaseException e) {
297 Logger.defaultLogError(e);
302 public void partActivated(IWorkbenchPartReference partRef) {
304 IWorkbenchPart part = partRef.getPart(false);
305 if (this.equals(part)) {
306 System.out.println(this + ": ACTIVATED ( loader=" + loader + ", visible="
307 + getSite().getPage().isPartVisible(part) + " )");
313 public void partBroughtToTop(IWorkbenchPartReference partRef) {
315 IWorkbenchPart part = partRef.getPart(false);
316 if (this.equals(part)) {
317 System.out.println(this + ": BROUGHT TO TOP ( loader=" + loader + ", visible="
318 + getSite().getPage().isPartVisible(part) + " )");
324 public void partClosed(IWorkbenchPartReference partRef) {
326 IWorkbenchPart part = partRef.getPart(false);
327 if (this.equals(part)) {
328 System.out.println(this + ": CLOSED ( loader=" + loader + ", visible="
329 + getSite().getPage().isPartVisible(part) + " )");
335 public void partDeactivated(IWorkbenchPartReference partRef) {
336 IWorkbenchPart part = partRef.getPart(false);
337 if (this.equals(part)) {
339 System.out.println(this + ": DEACTIVATED ( loader=" + loader + ", visible="
340 + getSite().getPage().isPartVisible(part) + " )");
346 public void partOpened(IWorkbenchPartReference partRef) {
348 IWorkbenchPart part = partRef.getPart(false);
349 if (this.equals(part)) {
350 System.out.println(this + ": OPENED ( loader=" + loader + ", visible="
351 + getSite().getPage().isPartVisible(part) + " )");
357 public void partInputChanged(IWorkbenchPartReference partRef) {
361 public void partHidden(IWorkbenchPartReference partRef) {
362 IWorkbenchPart part = partRef.getPart(false);
363 if (this.equals(part)) {
365 System.out.println(this + ": HID ( loader=" + loader + ", visible="
366 + getSite().getPage().isPartVisible(part) + " )");
372 public void partVisible(IWorkbenchPartReference partRef) {
373 IWorkbenchPart part = partRef.getPart(false);
374 if (this.equals(part)) {
376 System.out.println(this + ": MADE VISIBLE ( loader=" + loader + ", visible="
377 + getSite().getPage().isPartVisible(part) + " )");
378 createControlsIfNecessary(false);
382 private void createControlsIfNecessary(boolean forceContainerRepaint) {
383 // Cancel potential dispose before creating controls
384 reallyClearExisting = false;
385 if (loader == null) {
386 doCreateControls(true);
389 if (forceContainerRepaint) {
390 container.layout(true);
391 Point size = container.getSize();
392 container.redraw(0, 0, size.x, size.y, true);
398 // Can be used to cancel already scheduled dispose
399 volatile boolean reallyClearExisting = false;
401 Runnable clearExisting = new Runnable() {
405 if(!reallyClearExisting)
408 onInputChanged = null;
410 if (loader != null) {
414 if (container != null) {
416 final Composite oldContainer = container;
417 Display.getCurrent().asyncExec(new Runnable() {
420 if (!oldContainer.isDisposed())
421 oldContainer.dispose();
425 if (!container.isDisposed())
426 GridDataFactory.fillDefaults().exclude(true).applyTo(container);
436 private void clearExisting() {
437 Display.getCurrent().timerExec(TIME_BEFORE_DISPOSE_WHEN_HIDDEN, clearExisting);
439 // Do this after scheduling the runnable, because otherwise already scheduled runnable could
441 reallyClearExisting = true;
445 protected IPropertyPage getPropertyPage() {