1 package org.simantics.views.swt;
3 import java.util.ArrayList;
4 import java.util.Collections;
7 import org.eclipse.jface.viewers.IStructuredSelection;
8 import org.eclipse.jface.wizard.IWizardPage;
9 import org.eclipse.jface.wizard.Wizard;
10 import org.eclipse.ui.IWorkbench;
11 import org.eclipse.ui.IWorkbenchSite;
12 import org.eclipse.ui.IWorkbenchWizard;
13 import org.simantics.Simantics;
14 import org.simantics.browsing.ui.swt.ModelledAction;
15 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
16 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.VirtualGraph;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.primitiverequest.PossibleAdapter;
22 import org.simantics.db.common.request.WriteResultRequest;
23 import org.simantics.db.common.utils.ListUtils;
24 import org.simantics.db.common.utils.Logger;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.management.ISessionContext;
27 import org.simantics.db.request.Read;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.scenegraph.ontology.ScenegraphResources;
30 import org.simantics.scl.runtime.function.Function1;
31 import org.simantics.views.ontology.ViewsResources;
34 * @author Tuukka Lehtonen
36 public class ModelledWizard extends Wizard implements IWorkbenchWizard, ModelledAction {
38 final String configurationURI;
40 final private ArrayList<IWizardPage> pages = new ArrayList<IWizardPage>();
41 final private ArrayList<ModelledWizardPage> modelledPages = new ArrayList<ModelledWizardPage>();
44 private Function1<Object, Object> finishAction;
45 private Resource runtime;
47 public ModelledWizard(String configurationURI) {
48 setNeedsProgressMonitor(true);
49 this.configurationURI = configurationURI;
52 public List<ModelledWizardPage> getModelledPages() {
57 public void addPages() {
59 for(IWizardPage page : pages) addPage(page);
63 public boolean performFinish() {
65 finishAction.apply(this);
70 public void loadPages(IWorkbenchSite site, ISessionContext context, final WidgetSupport support) throws DatabaseException {
72 runtime = Simantics.getSession().sync(new WriteResultRequest<Resource>(Simantics.getSession().getService(VirtualGraph.class)) {
74 public Resource perform(WriteGraph graph) throws DatabaseException {
75 Layer0 L0 = Layer0.getInstance(graph);
76 ScenegraphResources SG = ScenegraphResources.getInstance(graph);
77 Resource runtime = graph.newResource();
78 graph.claim(runtime, L0.InstanceOf, null, SG.Runtime);
83 finishAction = Simantics.getSession().syncRequest(new Read<Function1<Object, Object>>() {
85 public Function1<Object, Object> perform(ReadGraph graph) throws DatabaseException {
86 ViewsResources VIEW = ViewsResources.getInstance(graph);
87 Resource configuration = graph.getResource(configurationURI);
88 return graph.getPossibleRelatedValue2(configuration, VIEW.Wizard_FinishAction);
92 List<Resource> pageResources = Simantics.getSession().syncRequest(new Read<List<Resource>>() {
95 public List<Resource> perform(ReadGraph graph) throws DatabaseException {
96 ViewsResources VIEW = ViewsResources.getInstance(graph);
97 Resource configuration = graph.getResource(configurationURI);
98 Resource pageList = graph.getPossibleObject(configuration, VIEW.Wizard_Pages);
99 if(pageList == null) return Collections.emptyList();
100 return ListUtils.toList(graph, pageList);
105 for(final Resource page : pageResources) {
107 ModelledWizardPage p = Simantics.getSession().syncRequest(new PossibleAdapter<ModelledWizardPage>(page, ModelledWizardPage.class));
108 pages.add(p.create(context, support, runtime));
109 modelledPages.add(p);
116 public Runnable create(IWorkbenchSite site, ISessionContext context, final WidgetSupport support) throws DatabaseException {
118 List<Resource> pageResources = Simantics.getSession().syncRequest(new Read<List<Resource>>() {
121 public List<Resource> perform(ReadGraph graph) throws DatabaseException {
122 ViewsResources VIEW = ViewsResources.getInstance(graph);
123 Resource configuration = graph.getResource(configurationURI);
124 Resource pageList = graph.getPossibleObject(configuration, VIEW.Wizard_Pages);
125 if(pageList == null) return Collections.emptyList();
126 return ListUtils.toList(graph, pageList);
131 runtime = Simantics.getSession().sync(new WriteResultRequest<Resource>(Simantics.getSession().getService(VirtualGraph.class)) {
133 public Resource perform(WriteGraph graph) throws DatabaseException {
134 Layer0 L0 = Layer0.getInstance(graph);
135 ScenegraphResources SG = ScenegraphResources.getInstance(graph);
136 Resource runtime = graph.newResource();
137 graph.claim(runtime, L0.InstanceOf, null, SG.Runtime);
142 for(final Resource page : pageResources) {
144 ModelledWizardPage p = Simantics.getSession().syncRequest(new PossibleAdapter<ModelledWizardPage>(page, ModelledWizardPage.class));
145 pages.add(p.create(context, support, runtime));
149 finishAction = Simantics.getSession().syncRequest(new Read<Function1<Object, Object>>() {
151 public Function1<Object, Object> perform(ReadGraph graph) throws DatabaseException {
152 ViewsResources VIEW = ViewsResources.getInstance(graph);
153 Resource configuration = graph.getResource(configurationURI);
154 return graph.getPossibleRelatedValue2(configuration, VIEW.Wizard_FinishAction);
163 public void init(IWorkbench workbench, IStructuredSelection selection) {
165 WidgetSupportImpl support = new WidgetSupportImpl();
167 loadPages(null, Simantics.getSessionContext(), support);
169 support.fireInput(Simantics.getSessionContext(), selection);
170 } catch (DatabaseException e) {
171 Logger.defaultLogError(e);