X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.views.swt%2Fsrc%2Forg%2Fsimantics%2Fviews%2Fswt%2FModelledEditor.java;fp=bundles%2Forg.simantics.views.swt%2Fsrc%2Forg%2Fsimantics%2Fviews%2Fswt%2FModelledEditor.java;h=2eaf6588e4080ad96ba3d5ea4481db45a59ebc3e;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.views.swt/src/org/simantics/views/swt/ModelledEditor.java b/bundles/org.simantics.views.swt/src/org/simantics/views/swt/ModelledEditor.java new file mode 100644 index 000000000..2eaf6588e --- /dev/null +++ b/bundles/org.simantics.views.swt/src/org/simantics/views/swt/ModelledEditor.java @@ -0,0 +1,133 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.views.swt; + +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.widgets.Composite; +import org.simantics.Simantics; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.VirtualGraph; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.request.WriteResultRequest; +import org.simantics.db.common.utils.Logger; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.ServiceNotFoundException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.request.Read; +import org.simantics.layer0.Layer0; +import org.simantics.scenegraph.ontology.ScenegraphResources; +import org.simantics.scl.runtime.function.Function3; +import org.simantics.ui.workbench.ResourceEditorPart2; +import org.simantics.utils.ui.jface.ActiveSelectionProvider; +import org.simantics.views.swt.client.base.SWTRoot; + +/** + * To use this class, first model your view contents in .pgraph files according + * to the Browsing.pgraph ontology. After that there are two ways to put your + * configuration to use by defining a new view extension: + *
    + *
  1. Set view extension class to + * org.simantics.browsing.ui.swt.ModelledView:configurationURI=ConfigURI + * , where ConfigURI is the URI of your view configuration.
  2. + *
  3. Extend this class and override at least {@link #configurationURI()} to + * define the URI from which the configuration for the view is found. Set view + * extension class to the created class.
  4. + *
+ * + * @author Antti Villberg + */ +abstract public class ModelledEditor extends ResourceEditorPart2 { + + public SWTRoot root; + private Composite base; + + abstract protected String configurationURI(); + + @Override + public void createPartControl(Composite parent) { + + try { + + final Variable variable = getResourceInput2().getVariable(); + + Resource runtime = Simantics.getSession().sync(new WriteResultRequest(Simantics.getSession().getService(VirtualGraph.class)) { + @Override + public Resource perform(WriteGraph graph) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + ScenegraphResources SG = ScenegraphResources.getInstance(graph); + Resource resource = graph.newResource(); + graph.claim(resource, L0.InstanceOf, null, SG.Runtime); + graph.claimLiteral(resource, SG.Runtime_HasVariable, variable.getURI(graph), Bindings.STRING); + return resource; + } + }); + + base = new Composite(parent, SWT.NONE); + base.setLayout(new FillLayout()); + + SWTViewLoaderProcess loader = new SWTViewLoaderProcess(null, null); + final Variable editorVariable = loader.getVariable(getSession(), configurationURI(), runtime); + + final Function3 onLoaded = getSession().syncRequest(new Read>() { + + @Override + public Function3 perform(ReadGraph graph) throws DatabaseException { + return editorVariable.getPossiblePropertyValue(graph, "onLoaded"); + } + + }); + + if(onLoaded != null) { + + Simantics.getSession().sync(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + onLoaded.apply(graph, editorVariable, variable); + } + }); + + } + + root = loader.load(getSession(), editorVariable); + root.createControls(base); + + } catch (ServiceNotFoundException e) { + Logger.defaultLogError(e); + } catch (DatabaseException e) { + Logger.defaultLogError(e); + } + + getSite().setSelectionProvider(new ActiveSelectionProvider() { + + @Override + public void setSelection(ISelection selection) { + super.setSelection(selection); + } + + }); + + } + + @Override + public void setFocus() { + if (root != null && root.getControl() != null && !root.getControl().isDisposed()) + root.getControl().setFocus(); + else if (!base.isDisposed()) + base.setFocus(); + } + +}