]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.swt/src/org/simantics/views/swt/ModelledWizard.java
Added utility for truncating collected history data
[simantics/platform.git] / bundles / org.simantics.views.swt / src / org / simantics / views / swt / ModelledWizard.java
1 package org.simantics.views.swt;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
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;
32
33 /**
34  * @author Tuukka Lehtonen
35  */
36 public class ModelledWizard extends Wizard implements IWorkbenchWizard, ModelledAction {
37
38         final String configurationURI;
39         
40         final private ArrayList<IWizardPage> pages = new ArrayList<IWizardPage>();
41         final private ArrayList<ModelledWizardPage> modelledPages = new ArrayList<ModelledWizardPage>();
42         
43         
44         private Function1<Object, Object> finishAction;
45         private Resource runtime;
46         
47     public ModelledWizard(String configurationURI) {
48         setNeedsProgressMonitor(true);
49         this.configurationURI = configurationURI;
50     }
51     
52     public List<ModelledWizardPage> getModelledPages() {
53         return modelledPages;
54     }
55
56     @Override
57     public void addPages() {
58         super.addPages();
59         for(IWizardPage page : pages) addPage(page);
60     }
61
62     @Override
63     public boolean performFinish() {
64         
65         finishAction.apply(this);
66         return true;
67         
68     }
69
70     public void loadPages(IWorkbenchSite site, ISessionContext context, final WidgetSupport support) throws DatabaseException {
71
72                 runtime = Simantics.getSession().sync(new WriteResultRequest<Resource>(Simantics.getSession().getService(VirtualGraph.class)) {
73             @Override
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);
79                                 return runtime;
80             }
81         });
82                 
83                 finishAction = Simantics.getSession().syncRequest(new Read<Function1<Object, Object>>() {
84                         @Override
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);
89                         }
90                 });
91         
92                 List<Resource> pageResources = Simantics.getSession().syncRequest(new Read<List<Resource>>() {
93
94                         @Override
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);
101                         }
102                         
103                 });
104
105                 for(final Resource page : pageResources) {
106
107                         ModelledWizardPage p = Simantics.getSession().syncRequest(new PossibleAdapter<ModelledWizardPage>(page, ModelledWizardPage.class));                     
108                         pages.add(p.create(context, support, runtime));
109                         modelledPages.add(p);
110                         
111                 }
112         
113     }
114     
115         @Override
116         public Runnable create(IWorkbenchSite site, ISessionContext context, final WidgetSupport support) throws DatabaseException {
117
118                 List<Resource> pageResources = Simantics.getSession().syncRequest(new Read<List<Resource>>() {
119
120                         @Override
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);
127                         }
128                         
129                 });
130
131                 runtime = Simantics.getSession().sync(new WriteResultRequest<Resource>(Simantics.getSession().getService(VirtualGraph.class)) {
132             @Override
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);
138                                 return runtime;
139             }
140         });
141                 
142                 for(final Resource page : pageResources) {
143
144                         ModelledWizardPage p = Simantics.getSession().syncRequest(new PossibleAdapter<ModelledWizardPage>(page, ModelledWizardPage.class));                     
145                         pages.add(p.create(context, support, runtime));
146                         
147                 }
148                 
149                 finishAction = Simantics.getSession().syncRequest(new Read<Function1<Object, Object>>() {
150                         @Override
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);
155                         }
156                 });
157                 
158                 return null;
159                 
160         }
161
162         @Override
163         public void init(IWorkbench workbench, IStructuredSelection selection) {
164                 
165                 WidgetSupportImpl support = new WidgetSupportImpl();
166                 try {
167                         loadPages(null, Simantics.getSessionContext(), support);
168                         support.finish();
169                         support.fireInput(Simantics.getSessionContext(), selection);
170                 } catch (DatabaseException e) {
171                         Logger.defaultLogError(e);
172                 }
173                 
174         }
175
176 }