1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
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;
36 * @author Tuukka Lehtonen
38 public class ModelledWizard extends Wizard implements ModelledAction {
40 final Resource configuration;
42 final private ArrayList<IWizardPage> pages = new ArrayList<IWizardPage>();
44 private Runnable finishAction;
46 public ModelledWizard(Resource configuration) {
47 setNeedsProgressMonitor(true);
48 this.configuration = configuration;
52 public void addPages() {
54 for(IWizardPage page : pages) addPage(page);
58 public boolean performFinish() {
63 public Runnable create(IWorkbenchSite site, ISessionContext context, final WidgetSupport support) throws DatabaseException {
65 String title = Simantics.getSession().syncRequest(new Read<String>() {
68 public String perform(ReadGraph graph) throws DatabaseException {
69 BrowsingResource br = BrowsingResource.getInstance(graph);
70 return graph.getPossibleRelatedValue(configuration, br.Wizard_Title, Bindings.STRING);
75 setWindowTitle(title);
77 List<Resource> pageResources = Simantics.getSession().syncRequest(new Read<List<Resource>>() {
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);
89 for(final Resource page : pageResources) {
91 ModelledWizardPage p = Simantics.getSession().syncRequest(new PossibleAdapter<ModelledWizardPage>(page, ModelledWizardPage.class));
92 pages.add(p.create(context, support));
96 ModelledAction finishAction = Simantics.getSession().syncRequest(new Read<ModelledAction>() {
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);
105 this.finishAction = finishAction.create(null, null, support);
107 return new Runnable() {
112 WizardDialog dialog = new WizardDialog(Display.getCurrent().getActiveShell(), ModelledWizard.this);
117 if(ModelledWizard.this.finishAction != null) ModelledWizard.this.finishAction.run();