1 package org.simantics.spreadsheet.ui.editor;
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.TreeMap;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.layout.GridData;
9 import org.eclipse.swt.widgets.Composite;
10 import org.eclipse.ui.contexts.IContextService;
11 import org.simantics.databoard.Bindings;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.common.request.UnaryRead;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.request.VariableName;
17 import org.simantics.db.layer0.request.VariableRead;
18 import org.simantics.db.layer0.request.VariableRepresents;
19 import org.simantics.db.layer0.request.VariableURI;
20 import org.simantics.db.layer0.variable.Variable;
21 import org.simantics.db.request.Read;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.selectionview.StandardPropertyPage;
24 import org.simantics.spreadsheet.graph.GraphUI;
25 import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;
26 import org.simantics.spreadsheet.resource.SpreadsheetResource;
27 import org.simantics.spreadsheet.ui.Spreadsheet;
28 import org.simantics.spreadsheet.util.SpreadsheetUtils;
29 import org.simantics.ui.workbench.IPropertyPage;
30 import org.simantics.ui.workbench.ResourceEditorPart2;
31 import org.simantics.utils.ui.LayoutUtils;
32 import org.simantics.utils.ui.jface.ActiveSelectionProvider;
34 public class SpreadsheetEditor2 extends ResourceEditorPart2 {
36 public static final boolean EXCEL = false;
37 public static final String EDITOR_ID = "org.simantics.spreadsheet.ui.editor2";
39 private Spreadsheet spreadsheet;
40 private final ActiveSelectionProvider selectionProvider = new ActiveSelectionProvider();
41 private ExcelLink excelLink;
44 public Spreadsheet getSpreadsheet() {
49 public void createPartControl(Composite parent) {
52 parent.setLayout(LayoutUtils.createNoBorderGridLayout(1));
53 parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
55 ui = new GraphUI(getSession());
59 Variable bookVariable = getSession().syncRequest(new Read<Variable>() {
62 public Variable perform(ReadGraph graph) throws DatabaseException {
64 Resource book = getResourceInput2().getResource();
65 Variable withContext = SpreadsheetUtils.getBookVariable(graph, book);
72 // getSession().syncRequest(new VariableRead<Variable>(bookVariable) {
75 // public Variable perform(ReadGraph graph) throws DatabaseException {
76 // SpreadsheetGraphUtils.fullSynchronization(graph, variable);
82 Variable sheetVariable = getSession().syncRequest(new VariableRead<Variable>(bookVariable) {
85 public Variable perform(ReadGraph graph) throws DatabaseException {
87 SpreadsheetResource SR = SpreadsheetResource.getInstance(graph);
88 Collection<Variable> children = variable.getChildren(graph);
89 if(children.isEmpty()) return null;
91 TreeMap<String,Variable> names = new TreeMap<String,Variable>();
92 for(Variable child : children) {
93 Resource type = child.getPossibleType(graph, SR.Spreadsheet);
95 names.put(child.getName(graph), child);
98 return names.firstEntry().getValue();
104 String uri = getSession().syncRequest(new VariableURI(sheetVariable));
106 Resource book = getSession().syncRequest(new VariableRepresents(bookVariable));
107 Resource sheet = getSession().syncRequest(new VariableRepresents(sheetVariable));
108 String sheetName = getSession().syncRequest(new VariableName(sheetVariable));
109 String bookName = getSession().syncRequest(new VariableName(bookVariable));
111 spreadsheet = new Spreadsheet(parent, SWT.NONE, ui, selectionProvider);
112 spreadsheet.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
114 excelLink = new ExcelLink(getSession(), sheet, spreadsheet.getClientModel(), book, bookName, sheetName, uri);
117 System.out.println("Opening SpreadsheetEditor2 with uri: " + uri);
118 ui.load(sheetVariable, spreadsheet.getClientInterface());
120 } catch (DatabaseException e) {
127 spreadsheet.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
128 spreadsheet.defaultInitializeUI();
130 getSite().setSelectionProvider(selectionProvider);
132 // Start tracking editor input validity.
133 activateValidation();
135 IContextService contexts = (IContextService)getSite().getService(IContextService.class);
136 contexts.activateContext("org.simantics.spreadsheet.ui.context");
141 public void setFocus() {
142 if (spreadsheet != null)
143 spreadsheet.setFocus();
147 public Object getAdapter(Class adapter) {
148 if (adapter == IPropertyPage.class)
149 return new StandardPropertyPage(getSite(), Collections.singleton(SpreadsheetResource.URIs.BrowseContext));
150 if (adapter == GraphUI.class)
152 if (adapter == Spreadsheet.class)
153 return getSpreadsheet();
158 public void dispose() {
163 // backend.dispose();
165 // tableModel.dispose();