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.jface.viewers.ISelection;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.layout.FillLayout;
17 import org.eclipse.swt.widgets.Composite;
18 import org.simantics.Simantics;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.VirtualGraph;
23 import org.simantics.db.WriteGraph;
24 import org.simantics.db.common.request.WriteRequest;
25 import org.simantics.db.common.request.WriteResultRequest;
26 import org.simantics.db.common.utils.Logger;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.exception.ServiceNotFoundException;
29 import org.simantics.db.layer0.variable.Variable;
30 import org.simantics.db.request.Read;
31 import org.simantics.layer0.Layer0;
32 import org.simantics.scenegraph.ontology.ScenegraphResources;
33 import org.simantics.scl.runtime.function.Function3;
34 import org.simantics.ui.workbench.ResourceEditorPart2;
35 import org.simantics.utils.ui.jface.ActiveSelectionProvider;
36 import org.simantics.views.swt.client.base.SWTRoot;
39 * To use this class, first model your view contents in .pgraph files according
40 * to the Browsing.pgraph ontology. After that there are two ways to put your
41 * configuration to use by defining a new view extension:
43 * <li>Set view extension class to
44 * <code>org.simantics.browsing.ui.swt.ModelledView:configurationURI=ConfigURI</code>
45 * , where ConfigURI is the URI of your view configuration.</li>
46 * <li>Extend this class and override at least {@link #configurationURI()} to
47 * define the URI from which the configuration for the view is found. Set view
48 * extension class to the created class.</li>
51 * @author Antti Villberg
53 abstract public class ModelledEditor extends ResourceEditorPart2 {
56 private Composite base;
58 abstract protected String configurationURI();
61 public void createPartControl(Composite parent) {
65 final Variable variable = getResourceInput2().getVariable();
67 Resource runtime = Simantics.getSession().sync(new WriteResultRequest<Resource>(Simantics.getSession().getService(VirtualGraph.class)) {
69 public Resource perform(WriteGraph graph) throws DatabaseException {
70 Layer0 L0 = Layer0.getInstance(graph);
71 ScenegraphResources SG = ScenegraphResources.getInstance(graph);
72 Resource resource = graph.newResource();
73 graph.claim(resource, L0.InstanceOf, null, SG.Runtime);
74 graph.claimLiteral(resource, SG.Runtime_HasVariable, variable.getURI(graph), Bindings.STRING);
79 base = new Composite(parent, SWT.NONE);
80 base.setLayout(new FillLayout());
82 SWTViewLoaderProcess loader = new SWTViewLoaderProcess(null, null);
83 final Variable editorVariable = loader.getVariable(getSession(), configurationURI(), runtime);
85 final Function3<WriteGraph, Variable, Variable, Boolean> onLoaded = getSession().syncRequest(new Read<Function3<WriteGraph, Variable, Variable, Boolean>>() {
88 public Function3<WriteGraph, Variable, Variable, Boolean> perform(ReadGraph graph) throws DatabaseException {
89 return editorVariable.getPossiblePropertyValue(graph, "onLoaded");
94 if(onLoaded != null) {
96 Simantics.getSession().sync(new WriteRequest() {
98 public void perform(WriteGraph graph) throws DatabaseException {
99 onLoaded.apply(graph, editorVariable, variable);
105 root = loader.load(getSession(), editorVariable);
106 root.createControls(base);
108 } catch (ServiceNotFoundException e) {
109 Logger.defaultLogError(e);
110 } catch (DatabaseException e) {
111 Logger.defaultLogError(e);
114 getSite().setSelectionProvider(new ActiveSelectionProvider() {
117 public void setSelection(ISelection selection) {
118 super.setSelection(selection);
126 public void setFocus() {
127 if (root != null && root.getControl() != null && !root.getControl().isDisposed())
128 root.getControl().setFocus();
129 else if (!base.isDisposed())