]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ModelledWizard.java
Use RuntimeEnvironmentRequest2 where applicable
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / ModelledWizard.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * 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.browsing.ui.swt;
13
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17
18 import org.eclipse.jface.wizard.IWizardPage;
19 import org.eclipse.jface.wizard.Wizard;
20 import org.eclipse.jface.wizard.WizardDialog;
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.ui.IWorkbenchSite;
23 import org.simantics.Simantics;
24 import org.simantics.browsing.ui.swt.stubs.BrowsingResource;
25 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
26 import org.simantics.databoard.Bindings;
27 import org.simantics.db.ReadGraph;
28 import org.simantics.db.Resource;
29 import org.simantics.db.common.primitiverequest.PossibleAdapter;
30 import org.simantics.db.common.utils.OrderedSetUtils;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.db.management.ISessionContext;
33 import org.simantics.db.request.Read;
34
35 /**
36  * @author Tuukka Lehtonen
37  */
38 public class ModelledWizard extends Wizard implements ModelledAction {
39
40         final Resource configuration;
41         
42         final private ArrayList<IWizardPage> pages = new ArrayList<IWizardPage>();
43         
44         private Runnable finishAction;
45         
46     public ModelledWizard(Resource configuration) {
47         setNeedsProgressMonitor(true);
48         this.configuration = configuration;
49     }
50
51     @Override
52     public void addPages() {
53         super.addPages();
54         for(IWizardPage page : pages) addPage(page);
55     }
56
57     @Override
58     public boolean performFinish() {
59         return true;
60     }
61
62         @Override
63         public Runnable create(IWorkbenchSite site, ISessionContext context, final WidgetSupport support) throws DatabaseException {
64
65                 String title = Simantics.getSession().syncRequest(new Read<String>() {
66
67                         @Override
68                         public String perform(ReadGraph graph) throws DatabaseException {
69                                 BrowsingResource br = BrowsingResource.getInstance(graph);
70                                 return graph.getPossibleRelatedValue(configuration, br.Wizard_Title, Bindings.STRING);
71                         }
72                         
73                 });
74                 
75                 setWindowTitle(title);
76                 
77                 List<Resource> pageResources = Simantics.getSession().syncRequest(new Read<List<Resource>>() {
78
79                         @Override
80                         public List<Resource> perform(ReadGraph graph) throws DatabaseException {
81                                 BrowsingResource br = BrowsingResource.getInstance(graph);
82                                 Resource pageList = graph.getPossibleObject(configuration, br.Wizard_Pages);
83                                 if(pageList == null) return Collections.emptyList();
84                                 return OrderedSetUtils.toList(graph, pageList);
85                         }
86                         
87                 });
88
89                 for(final Resource page : pageResources) {
90
91                         ModelledWizardPage p = Simantics.getSession().syncRequest(new PossibleAdapter<ModelledWizardPage>(page, ModelledWizardPage.class));                     
92                         pages.add(p.create(context, support));
93                         
94                 }
95                 
96                 ModelledAction finishAction = Simantics.getSession().syncRequest(new Read<ModelledAction>() {
97                         @Override
98                         public ModelledAction perform(ReadGraph graph) throws DatabaseException {
99                                 BrowsingResource BRO = BrowsingResource.getInstance(graph);
100                                 Resource actionResource = graph.getPossibleObject(configuration, BRO.Wizard_FinishAction);
101                                 return graph.adapt(actionResource, ModelledAction.class);
102                         }
103                 }); 
104                 
105                 this.finishAction = finishAction.create(null, null, support);
106                 
107                 return new Runnable() {
108
109                         @Override
110                         public void run() {
111                                 
112                                 WizardDialog dialog = new WizardDialog(Display.getCurrent().getActiveShell(), ModelledWizard.this);
113                             dialog.create();
114                             support.update();
115                             dialog.open();      
116                             
117                             if(ModelledWizard.this.finishAction != null) ModelledWizard.this.finishAction.run();
118                             
119                         }
120                         
121                 };
122                 
123         }
124
125 }