]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SaveSpreadsheetStateDialog.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / SaveSpreadsheetStateDialog.java
1 package org.simantics.spreadsheet.graph;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.Set;
7
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;
37
38 public class SaveSpreadsheetStateDialog extends GraphExplorerDialog2 {
39
40     private static final String SELECT_CUSTOM = "Select custom..";
41     
42     private Combo parentSelector;
43     private Pair<String, Resource>[] pairs;
44     private String[] parents;
45
46     private Text stateName;
47
48     private String currentItem;
49     private String currentStateName;
50     
51     private List<Resource> result;
52     
53     public SaveSpreadsheetStateDialog(IWorkbenchSite site, String title, Pair<String, Resource>[] pairs) {
54         super(site, title);
55         this.pairs = pairs;
56         
57         Resource book = pairs[0].second;
58         Resource context = pairs[1].second;
59         
60         try {
61             result = Simantics.getSession().syncRequest(new UniqueRead<List<Resource>>() {
62
63                 @Override
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)); 
67                     
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);
73                         }
74                     }
75                     return result;
76                 }
77             });
78         } catch (DatabaseException e) {
79             e.printStackTrace();
80         }
81         
82         String[] newParents = new String[pairs.length + 1];
83         for (int i = 0; i < pairs.length; i++) {
84             newParents[i] = pairs[i].first;
85         }
86         newParents[pairs.length] = SELECT_CUSTOM;
87         this.parents = newParents;
88     }
89
90     @Override
91     public Set<String> getBrowseContexts() {
92         return Collections.singleton("http://www.simantics.org/Project-1.2/ProjectBrowseContext");
93     }
94     
95     @Override
96     protected SessionContextInputSource getInputSource() {
97         return super.getInputSource();
98     }
99
100     @Override
101     protected void createControls(Composite body, ISessionContext context, WidgetSupport support) {
102         
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);
107         
108         Composite explorerComposite = new Composite(body, SWT.NONE);
109         explorerComposite.setLayout(new GridLayout());
110         GridDataFactory.fillDefaults().grab(true, true).applyTo(explorerComposite);
111         
112         Label parentLabel = new Label(upperComposite, SWT.NONE);
113         parentLabel.setText("Select parent: ");
114         
115         parentSelector = new Combo(upperComposite, SWT.READ_ONLY);
116         parentSelector.setItems(parents);
117         parentSelector.select(0);
118         
119         GridDataFactory.fillDefaults().grab(true, false).applyTo(parentSelector);
120         
121         parentSelector.addSelectionListener(new SelectionAdapter() {
122             
123             @Override
124             public void widgetSelected(SelectionEvent e) {
125                 hideOrShowGE();
126             }
127             
128             @Override
129             public void widgetDefaultSelected(SelectionEvent e) {
130                 widgetSelected(e);
131             }
132         });
133         
134         Label stateNameLabel = new Label(upperComposite, SWT.NONE);
135         stateNameLabel.setText("Name of the state: ");
136         GridDataFactory.fillDefaults().grab(false, false).applyTo(stateNameLabel);
137         
138         stateName = new Text(upperComposite, SWT.NONE);
139         
140         stateName.addModifyListener(new ModifyListener() {
141             
142             @Override
143             public void modifyText(ModifyEvent e) {
144                 currentStateName = stateName.getText();
145             }
146         });
147         
148         GridDataFactory.fillDefaults().grab(true, false).applyTo(stateName);
149         
150         super.createControls(explorerComposite, context, support);
151         
152         hideOrShowGE();
153     }
154
155     private void hideOrShowGE() {
156         int selection = parentSelector.getSelectionIndex();
157         currentItem = parentSelector.getItem(selection);
158         
159         GraphExplorerComposite composite = getGraphExplorerComposite();
160         GridData data = (GridData) composite.getLayoutData();
161         
162         if (currentItem.equals(SELECT_CUSTOM))
163             data.exclude = false;
164         else
165             data.exclude = true;
166         
167          
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);
179     }
180     
181     
182     
183     @Override
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) };
190         } else {
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) };
195                 }
196             }
197         }
198         return null;
199     }
200     
201 }