]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.swt/src/org/simantics/views/swt/ModelledEditor.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.views.swt / src / org / simantics / views / swt / ModelledEditor.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.views.swt;
13
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;
37
38 /**
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:
42  * <ol>
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>
49  * </ol>
50  * 
51  * @author Antti Villberg
52  */
53 abstract public class ModelledEditor extends ResourceEditorPart2 {
54
55         public SWTRoot root;
56     private Composite base;
57         
58     abstract protected String configurationURI();
59         
60     @Override
61     public void createPartControl(Composite parent) {
62         
63         try {
64                 
65                 final Variable variable = getResourceInput2().getVariable();
66                 
67             Resource runtime = Simantics.getSession().sync(new WriteResultRequest<Resource>(Simantics.getSession().getService(VirtualGraph.class)) {
68                 @Override
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);
75                         return resource;
76                 }
77             });
78             
79             base = new Composite(parent, SWT.NONE);
80             base.setLayout(new FillLayout());
81             
82                         SWTViewLoaderProcess loader = new SWTViewLoaderProcess(null, null);
83             final Variable editorVariable = loader.getVariable(getSession(), configurationURI(), runtime);
84
85                 final Function3<WriteGraph, Variable, Variable, Boolean> onLoaded = getSession().syncRequest(new Read<Function3<WriteGraph, Variable, Variable, Boolean>>() {
86
87                         @Override
88                         public Function3<WriteGraph, Variable, Variable, Boolean> perform(ReadGraph graph) throws DatabaseException {
89                                 return editorVariable.getPossiblePropertyValue(graph, "onLoaded");
90                         }
91
92                 });
93
94                 if(onLoaded != null) {
95
96                         Simantics.getSession().sync(new WriteRequest() {
97                     @Override
98                     public void perform(WriteGraph graph) throws DatabaseException {
99                         onLoaded.apply(graph, editorVariable, variable);
100                     }
101                 });
102
103                 }
104             
105                         root = loader.load(getSession(), editorVariable);
106                         root.createControls(base);
107             
108         } catch (ServiceNotFoundException e) {
109             Logger.defaultLogError(e);
110         } catch (DatabaseException e) {
111             Logger.defaultLogError(e);
112         }
113         
114         getSite().setSelectionProvider(new ActiveSelectionProvider() {
115                 
116                 @Override
117                 public void setSelection(ISelection selection) {
118                         super.setSelection(selection);
119                 }
120                 
121         });
122         
123     }
124
125     @Override
126     public void setFocus() {
127         if (root != null && root.getControl() != null && !root.getControl().isDisposed())
128                 root.getControl().setFocus();
129         else if (!base.isDisposed())
130             base.setFocus();
131     }
132     
133 }