]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/GraphBackend.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / GraphBackend.java
diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/GraphBackend.java b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/GraphBackend.java
new file mode 100644 (file)
index 0000000..929d9ce
--- /dev/null
@@ -0,0 +1,216 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.spreadsheet.graph;\r
+\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.RequestProcessor;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.VirtualGraph;\r
+import org.simantics.db.common.procedure.adapter.AsyncListenerSupport;\r
+import org.simantics.db.common.procedure.adapter.ListenerSupport;\r
+import org.simantics.db.common.procedure.adapter.SyncListenerSupport;\r
+import org.simantics.db.common.processor.MergingDelayedWriteProcessor;\r
+import org.simantics.db.common.processor.MergingWriteOnlyProcessor;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.spreadsheet.Adaptable;\r
+import org.simantics.spreadsheet.CellEditor;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+public class GraphBackend implements Adaptable, ListenerSupport, AsyncListenerSupport, SyncListenerSupport {\r
+\r
+       final private RequestProcessor processor;\r
+\r
+       final private VirtualGraph virtualGraph;\r
+\r
+       private Resource model;\r
+//     private Resource spreadsheet;\r
+//     private Variable sheet;\r
+//     private ModelCellManager creator;\r
+       private CellEditor editor;\r
+       private boolean disposed = false;\r
+\r
+       final private MergingDelayedWriteProcessor delayedMerger;\r
+       final private MergingWriteOnlyProcessor writeOnlyMerger;\r
+       \r
+       public GraphBackend(RequestProcessor processor, VirtualGraph virtualGraph) {\r
+               this.processor = processor;\r
+               this.virtualGraph = virtualGraph;\r
+               delayedMerger = new MergingDelayedWriteProcessor(SimanticsUI.getSessionContext().getSession(), 5);\r
+               writeOnlyMerger = new MergingWriteOnlyProcessor(SimanticsUI.getSessionContext().getSession(), virtualGraph, 5);\r
+       }\r
+       \r
+       public RequestProcessor getModifier() {\r
+               return delayedMerger;\r
+       }\r
+\r
+       public RequestProcessor getWriteOnlyModifier() {\r
+               return writeOnlyMerger;\r
+       }\r
+\r
+       public void load(RequestProcessor proc, final Resource model, final Variable sheet) throws DatabaseException {\r
+\r
+//             final ITask task = ThreadLogger.getInstance().begin("GraphBackend.load");\r
+//             \r
+//             this.model = model;\r
+//\r
+//             processor.asyncRequest(new ReadRequest() {\r
+//\r
+//                     @Override\r
+//                     public void run(ReadGraph graph) throws DatabaseException {\r
+//\r
+//                             Layer0 l0 = Layer0.getInstance(graph);\r
+//\r
+//                             SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
+//\r
+//                             final Resource realization = sheet.getInterface(graph, Resource.class);\r
+//                             Resource config = graph.getPossibleObject(realization, L0X.Represents);\r
+//\r
+//                             // TODO : remove this and make the request persistent\r
+//                             processor.asyncRequest(new Rows(config), new CacheListener<RowsColumnsIndex>(GraphBackend.this));\r
+//\r
+//                             creator = new DefaultCellCreator(processor, config);\r
+//                             editor = new DefaultCellEditor(processor, config);\r
+//\r
+//                             graph.forObjectSet(config, sr.HasCell, new AsyncSetListenerDelegate<Resource>(GraphBackend.this) {\r
+//\r
+//                                     @Override\r
+//                                     public void add(AsyncReadGraph graph, final Resource cellResource) {\r
+//\r
+//                                             graph.forAdapted(cellResource, ServerCell.class, new ResourceAsyncListenerDelegate<ServerCell>(cellResource, GraphBackend.this) {\r
+//\r
+//                                                     @Override\r
+//                                                     public void execute(AsyncReadGraph graph, final ServerCell cell) {\r
+//\r
+//                                                             cell.register(graph.getSession(), delayedMerger, cellResource);\r
+//\r
+//                                                             graph.getSession().asyncRequest(new WriteRequest(virtualGraph) {\r
+//\r
+//                                                                     @Override\r
+//                                                                     public void perform(WriteGraph graph) throws DatabaseException {\r
+//\r
+//                                                                             Layer0 L0 = Layer0.getInstance(graph);\r
+//                                                                             SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
+//\r
+//                                                                             Map<Resource, Resource> map = graph.syncRequest(new MappedParts(realization));\r
+//                                                                             Resource resource = map.get(cellResource);\r
+//                                                                             Resource realizationType = graph.syncRequest(new RealizationType(cellResource, sr.Realization));\r
+//\r
+//                                                                             if(resource == null) {\r
+//                                                                                     resource = graph.newResource();\r
+//                                                                                     graph.claim(resource, L0.InstanceOf, null, realizationType);\r
+//                                                                                     graph.claim(resource, L0X.Represents, null, cellResource);\r
+//                                                                                     graph.addLiteral(resource, sr.Label, sr.LabelOf, L0.String, "", Bindings.STRING );\r
+//                                                                                     graph.addLiteral(resource, sr.Content, sr.ContentOf, L0.String, "", Bindings.STRING );\r
+//                                                                                     graph.claim(realization, L0.ConsistsOf, L0.PartOf, resource);\r
+//                                                                             }\r
+//\r
+//                                                                             final AdaptionService service = graph.peekService(AdaptionService.class);\r
+//\r
+//                                                                             class Modifier implements ResourceAdapter<StringModifier> {\r
+//\r
+//                                                                                     public void adapt(AsyncReadGraph graph, Resource source, Resource r, AsyncProcedure<StringModifier> procedure) {\r
+//\r
+//                                                                                             procedure.execute(graph, new StringModifier() {\r
+//\r
+//                                                                                                     @Override\r
+//                                                                                                     public String isValid(String newValue) {\r
+//                                                                                                             return null;\r
+//                                                                                                     }\r
+//\r
+//                                                                                                     @Override\r
+//                                                                                                     public void modify(WriteGraph graph, String value) throws DatabaseException {\r
+//\r
+//                                                                                                             StringModifier modifier = graph.adapt(cellResource, StringModifier.class);\r
+//                                                                                                             modifier.modify(graph, value);\r
+//\r
+//                                                                                                     }\r
+//\r
+//                                                                                             });\r
+//\r
+//                                                                                     }\r
+//\r
+//                                                                             };\r
+//\r
+//                                                                             service.addInstanceAdapter(resource, StringModifier.class, new Modifier());\r
+//\r
+//                                                                             cell.listen(GraphBackend.this, graph, resource);\r
+//\r
+//                                                                     }\r
+//\r
+//                                                             });\r
+//\r
+//                                                     }\r
+//\r
+//                                             });\r
+//\r
+//\r
+//                                     }\r
+//\r
+//                                     @Override\r
+//                                     public void remove(AsyncReadGraph graph, final Resource cellResource) {\r
+//\r
+//                                     }\r
+//\r
+//                             });\r
+//                             \r
+//                             task.finish();\r
+//\r
+//                     }\r
+//\r
+//             });\r
+\r
+       }\r
+\r
+       @Override\r
+       public <T> T getAdapter(Class<T> clazz) {\r
+\r
+/*             if(ModelCellManager.class == clazz) return (T)creator;\r
+               else if(CellEditor.class == clazz) return (T)editor;\r
+               else*/ return null;\r
+\r
+       }\r
+\r
+       @Override\r
+       public void exception(Throwable t) {\r
+               t.printStackTrace();\r
+       }\r
+\r
+       @Override\r
+       public boolean isDisposed() {\r
+               return disposed;\r
+       }\r
+\r
+       @Override\r
+       public void exception(AsyncReadGraph graph, Throwable t) {\r
+               t.printStackTrace();\r
+       }\r
+\r
+       @Override\r
+       public void exception(ReadGraph graph, Throwable t) {\r
+               t.printStackTrace();\r
+       }\r
+\r
+       public VirtualGraph getVirtualGraph() {\r
+               return virtualGraph;\r
+       }\r
+\r
+       public Resource getModel() {\r
+               return model;\r
+       }\r
+       \r
+       public void dispose() {\r
+               disposed = true;\r
+       }\r
+\r
+}\r