--- /dev/null
+package org.simantics.spreadsheet.ui.editor;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.TreeMap;\r
+\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.ui.contexts.IContextService;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.UnaryRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.request.VariableName;\r
+import org.simantics.db.layer0.request.VariableRead;\r
+import org.simantics.db.layer0.request.VariableRepresents;\r
+import org.simantics.db.layer0.request.VariableURI;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.selectionview.StandardPropertyPage;\r
+import org.simantics.spreadsheet.graph.GraphUI;\r
+import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;\r
+import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
+import org.simantics.spreadsheet.ui.Spreadsheet;\r
+import org.simantics.spreadsheet.util.SpreadsheetUtils;\r
+import org.simantics.ui.workbench.IPropertyPage;\r
+import org.simantics.ui.workbench.ResourceEditorPart2;\r
+import org.simantics.utils.ui.LayoutUtils;\r
+import org.simantics.utils.ui.jface.ActiveSelectionProvider;\r
+\r
+public class SpreadsheetEditor2 extends ResourceEditorPart2 {\r
+\r
+ public static final boolean EXCEL = false;\r
+ public static final String EDITOR_ID = "org.simantics.spreadsheet.ui.editor2";\r
+\r
+ private Spreadsheet spreadsheet;\r
+ private final ActiveSelectionProvider selectionProvider = new ActiveSelectionProvider();\r
+ private ExcelLink excelLink;\r
+ private GraphUI ui;\r
+\r
+ public Spreadsheet getSpreadsheet() {\r
+ return spreadsheet;\r
+ }\r
+\r
+ @Override\r
+ public void createPartControl(Composite parent) {\r
+\r
+ // Create UI\r
+ parent.setLayout(LayoutUtils.createNoBorderGridLayout(1));\r
+ parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));\r
+\r
+ ui = new GraphUI(getSession());\r
+\r
+ try {\r
+ \r
+ Variable bookVariable = getSession().syncRequest(new Read<Variable>() {\r
+\r
+ @Override\r
+ public Variable perform(ReadGraph graph) throws DatabaseException {\r
+ \r
+ Resource book = getResourceInput2().getResource();\r
+ Variable withContext = SpreadsheetUtils.getBookVariable(graph, book); \r
+ return withContext; \r
+\r
+ }\r
+\r
+ });\r
+ \r
+// getSession().syncRequest(new VariableRead<Variable>(bookVariable) {\r
+//\r
+// @Override\r
+// public Variable perform(ReadGraph graph) throws DatabaseException {\r
+// SpreadsheetGraphUtils.fullSynchronization(graph, variable);\r
+// return variable;\r
+// }\r
+//\r
+// });\r
+ \r
+ Variable sheetVariable = getSession().syncRequest(new VariableRead<Variable>(bookVariable) {\r
+\r
+ @Override\r
+ public Variable perform(ReadGraph graph) throws DatabaseException {\r
+ \r
+ SpreadsheetResource SR = SpreadsheetResource.getInstance(graph);\r
+ Collection<Variable> children = variable.getChildren(graph);\r
+ if(children.isEmpty()) return null;\r
+ \r
+ TreeMap<String,Variable> names = new TreeMap<String,Variable>();\r
+ for(Variable child : children) {\r
+ Resource type = child.getPossibleType(graph, SR.Spreadsheet);\r
+ if(type != null)\r
+ names.put(child.getName(graph), child);\r
+ }\r
+ \r
+ return names.firstEntry().getValue();\r
+\r
+ }\r
+\r
+ });\r
+ \r
+ String uri = getSession().syncRequest(new VariableURI(sheetVariable));\r
+ \r
+ Resource book = getSession().syncRequest(new VariableRepresents(bookVariable));\r
+ Resource sheet = getSession().syncRequest(new VariableRepresents(sheetVariable));\r
+ String sheetName = getSession().syncRequest(new VariableName(sheetVariable));\r
+ String bookName = getSession().syncRequest(new VariableName(bookVariable));\r
+\r
+ spreadsheet = new Spreadsheet(parent, SWT.NONE, ui, selectionProvider);\r
+ spreadsheet.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));\r
+ \r
+ excelLink = new ExcelLink(getSession(), sheet, spreadsheet.getClientModel(), book, bookName, sheetName, uri);\r
+\r
+ if (GraphUI.DEBUG)\r
+ System.out.println("Opening SpreadsheetEditor2 with uri: " + uri);\r
+ ui.load(sheetVariable, spreadsheet.getClientInterface());\r
+ \r
+ } catch (DatabaseException e) {\r
+ \r
+ e.printStackTrace();\r
+ return;\r
+ \r
+ }\r
+\r
+ spreadsheet.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));\r
+ spreadsheet.defaultInitializeUI();\r
+\r
+ getSite().setSelectionProvider(selectionProvider);\r
+\r
+ // Start tracking editor input validity. \r
+ activateValidation();\r
+ \r
+ IContextService contexts = (IContextService)getSite().getService(IContextService.class);\r
+ contexts.activateContext("org.simantics.spreadsheet.ui.context");\r
+\r
+ }\r
+\r
+ @Override\r
+ public void setFocus() {\r
+ if (spreadsheet != null)\r
+ spreadsheet.setFocus();\r
+ }\r
+\r
+ @Override\r
+ public Object getAdapter(Class adapter) {\r
+ if (adapter == IPropertyPage.class)\r
+ return new StandardPropertyPage(getSite(), Collections.singleton(SpreadsheetResource.URIs.BrowseContext));\r
+ if (adapter == GraphUI.class)\r
+ return ui;\r
+ if (adapter == Spreadsheet.class)\r
+ return getSpreadsheet();\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public void dispose() {\r
+\r
+ if(EXCEL)\r
+ excelLink.dispose();\r
+ \r
+// backend.dispose();\r
+\r
+// tableModel.dispose();\r
+ ui.dispose();\r
+ super.dispose();\r
+\r
+ }\r
+\r
+}
\ No newline at end of file