1 package org.simantics.spreadsheet.graph;
3 import java.util.ArrayList;
4 import java.util.Collections;
8 import org.eclipse.jface.layout.GridDataFactory;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.ModifyEvent;
11 import org.eclipse.swt.events.ModifyListener;
12 import org.eclipse.swt.events.SelectionAdapter;
13 import org.eclipse.swt.events.SelectionEvent;
14 import org.eclipse.swt.layout.GridData;
15 import org.eclipse.swt.layout.GridLayout;
16 import org.eclipse.swt.widgets.Combo;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.swt.widgets.Text;
20 import org.eclipse.ui.IWorkbenchSite;
21 import org.simantics.Simantics;
22 import org.simantics.browsing.ui.BuiltinKeys;
23 import org.simantics.browsing.ui.NodeContext;
24 import org.simantics.browsing.ui.graph.impl.SessionContextInputSource;
25 import org.simantics.browsing.ui.swt.GraphExplorerDialog2;
26 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;
27 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
28 import org.simantics.db.ReadGraph;
29 import org.simantics.db.Resource;
30 import org.simantics.db.common.request.PossibleIndexRoot;
31 import org.simantics.db.common.request.UniqueRead;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.layer0.request.IsLinkedTo;
34 import org.simantics.db.layer0.util.Layer0Utils;
35 import org.simantics.db.management.ISessionContext;
36 import org.simantics.utils.datastructures.Pair;
38 public class SaveSpreadsheetStateDialog extends GraphExplorerDialog2 {
40 private static final String SELECT_CUSTOM = "Select custom..";
42 private Combo parentSelector;
43 private Pair<String, Resource>[] pairs;
44 private String[] parents;
46 private Text stateName;
48 private String currentItem;
49 private String currentStateName;
51 private List<Resource> result;
53 public SaveSpreadsheetStateDialog(IWorkbenchSite site, String title, Pair<String, Resource>[] pairs) {
57 Resource book = pairs[0].second;
58 Resource context = pairs[1].second;
61 result = Simantics.getSession().syncRequest(new UniqueRead<List<Resource>>() {
64 public List<Resource> perform(ReadGraph graph) throws DatabaseException {
65 Resource bookINdex = graph.syncRequest(new PossibleIndexRoot(book));
66 Resource contextIndex = graph.syncRequest(new PossibleIndexRoot(context));
68 List<Resource> result = new ArrayList<>();
69 Set<Resource> indexRoots = Layer0Utils.listIndexRoots(graph);
70 for (Resource indexRoot : indexRoots) {
71 if (graph.syncRequest(new IsLinkedTo(indexRoot, bookINdex)) && graph.syncRequest(new IsLinkedTo(indexRoot, contextIndex))) {
72 result.add(indexRoot);
78 } catch (DatabaseException e) {
82 String[] newParents = new String[pairs.length + 1];
83 for (int i = 0; i < pairs.length; i++) {
84 newParents[i] = pairs[i].first;
86 newParents[pairs.length] = SELECT_CUSTOM;
87 this.parents = newParents;
91 public Set<String> getBrowseContexts() {
92 return Collections.singleton("http://www.simantics.org/Project-1.2/ProjectBrowseContext");
96 protected SessionContextInputSource getInputSource() {
97 return super.getInputSource();
101 protected void createControls(Composite body, ISessionContext context, WidgetSupport support) {
103 Composite upperComposite = new Composite(body, SWT.NONE);
104 GridLayout layout = new GridLayout(2, false);
105 upperComposite.setLayout(layout);
106 GridDataFactory.fillDefaults().grab(true, true).applyTo(upperComposite);
108 Composite explorerComposite = new Composite(body, SWT.NONE);
109 explorerComposite.setLayout(new GridLayout());
110 GridDataFactory.fillDefaults().grab(true, true).applyTo(explorerComposite);
112 Label parentLabel = new Label(upperComposite, SWT.NONE);
113 parentLabel.setText("Select parent: ");
115 parentSelector = new Combo(upperComposite, SWT.READ_ONLY);
116 parentSelector.setItems(parents);
117 parentSelector.select(0);
119 GridDataFactory.fillDefaults().grab(true, false).applyTo(parentSelector);
121 parentSelector.addSelectionListener(new SelectionAdapter() {
124 public void widgetSelected(SelectionEvent e) {
129 public void widgetDefaultSelected(SelectionEvent e) {
134 Label stateNameLabel = new Label(upperComposite, SWT.NONE);
135 stateNameLabel.setText("Name of the state: ");
136 GridDataFactory.fillDefaults().grab(false, false).applyTo(stateNameLabel);
138 stateName = new Text(upperComposite, SWT.NONE);
140 stateName.addModifyListener(new ModifyListener() {
143 public void modifyText(ModifyEvent e) {
144 currentStateName = stateName.getText();
148 GridDataFactory.fillDefaults().grab(true, false).applyTo(stateName);
150 super.createControls(explorerComposite, context, support);
155 private void hideOrShowGE() {
156 int selection = parentSelector.getSelectionIndex();
157 currentItem = parentSelector.getItem(selection);
159 GraphExplorerComposite composite = getGraphExplorerComposite();
160 GridData data = (GridData) composite.getLayoutData();
162 if (currentItem.equals(SELECT_CUSTOM))
163 data.exclude = false;
168 composite.setLayoutData(data);
169 composite.setVisible(!data.exclude);
170 composite.getParent().pack();
171 composite.getParent().layout();
172 composite.getParent().getParent().pack();
173 composite.getParent().getParent().layout();
174 composite.getParent().getParent().getParent().getParent().getParent();
175 composite.getShell().pack();
176 composite.getShell().layout();
177 // explorerComposite.pack();
178 // explorerComposite.getShell().layout(false);
184 public Object[] getSelection() {
185 if (currentItem.equals(SELECT_CUSTOM)) {
186 Object[] selection = super.getSelection();
187 NodeContext context = (NodeContext)selection[0];
188 Object obj = context.getConstant(BuiltinKeys.INPUT);
189 return new Object[] { Pair.make(obj, currentStateName) };
191 for (int i = 0; i < pairs.length; i++) {
192 Pair<String, Resource> p = pairs[i];
193 if (p.first.equals(currentItem)) {
194 return new Object[] {Pair.make(p.second, currentStateName) };