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