]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.spreadsheet.graph;\r
13 \r
14 import org.simantics.db.AsyncReadGraph;\r
15 import org.simantics.db.ReadGraph;\r
16 import org.simantics.db.RequestProcessor;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.VirtualGraph;\r
19 import org.simantics.db.common.procedure.adapter.AsyncListenerSupport;\r
20 import org.simantics.db.common.procedure.adapter.ListenerSupport;\r
21 import org.simantics.db.common.procedure.adapter.SyncListenerSupport;\r
22 import org.simantics.db.common.processor.MergingDelayedWriteProcessor;\r
23 import org.simantics.db.common.processor.MergingWriteOnlyProcessor;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.db.layer0.variable.Variable;\r
26 import org.simantics.spreadsheet.Adaptable;\r
27 import org.simantics.spreadsheet.CellEditor;\r
28 import org.simantics.ui.SimanticsUI;\r
29 \r
30 public class GraphBackend implements Adaptable, ListenerSupport, AsyncListenerSupport, SyncListenerSupport {\r
31 \r
32         final private RequestProcessor processor;\r
33 \r
34         final private VirtualGraph virtualGraph;\r
35 \r
36         private Resource model;\r
37 //      private Resource spreadsheet;\r
38 //      private Variable sheet;\r
39 //      private ModelCellManager creator;\r
40         private CellEditor editor;\r
41         private boolean disposed = false;\r
42 \r
43         final private MergingDelayedWriteProcessor delayedMerger;\r
44         final private MergingWriteOnlyProcessor writeOnlyMerger;\r
45         \r
46         public GraphBackend(RequestProcessor processor, VirtualGraph virtualGraph) {\r
47                 this.processor = processor;\r
48                 this.virtualGraph = virtualGraph;\r
49                 delayedMerger = new MergingDelayedWriteProcessor(SimanticsUI.getSessionContext().getSession(), 5);\r
50                 writeOnlyMerger = new MergingWriteOnlyProcessor(SimanticsUI.getSessionContext().getSession(), virtualGraph, 5);\r
51         }\r
52         \r
53         public RequestProcessor getModifier() {\r
54                 return delayedMerger;\r
55         }\r
56 \r
57         public RequestProcessor getWriteOnlyModifier() {\r
58                 return writeOnlyMerger;\r
59         }\r
60 \r
61         public void load(RequestProcessor proc, final Resource model, final Variable sheet) throws DatabaseException {\r
62 \r
63 //              final ITask task = ThreadLogger.getInstance().begin("GraphBackend.load");\r
64 //              \r
65 //              this.model = model;\r
66 //\r
67 //              processor.asyncRequest(new ReadRequest() {\r
68 //\r
69 //                      @Override\r
70 //                      public void run(ReadGraph graph) throws DatabaseException {\r
71 //\r
72 //                              Layer0 l0 = Layer0.getInstance(graph);\r
73 //\r
74 //                              SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
75 //\r
76 //                              final Resource realization = sheet.getInterface(graph, Resource.class);\r
77 //                              Resource config = graph.getPossibleObject(realization, L0X.Represents);\r
78 //\r
79 //                              // TODO : remove this and make the request persistent\r
80 //                              processor.asyncRequest(new Rows(config), new CacheListener<RowsColumnsIndex>(GraphBackend.this));\r
81 //\r
82 //                              creator = new DefaultCellCreator(processor, config);\r
83 //                              editor = new DefaultCellEditor(processor, config);\r
84 //\r
85 //                              graph.forObjectSet(config, sr.HasCell, new AsyncSetListenerDelegate<Resource>(GraphBackend.this) {\r
86 //\r
87 //                                      @Override\r
88 //                                      public void add(AsyncReadGraph graph, final Resource cellResource) {\r
89 //\r
90 //                                              graph.forAdapted(cellResource, ServerCell.class, new ResourceAsyncListenerDelegate<ServerCell>(cellResource, GraphBackend.this) {\r
91 //\r
92 //                                                      @Override\r
93 //                                                      public void execute(AsyncReadGraph graph, final ServerCell cell) {\r
94 //\r
95 //                                                              cell.register(graph.getSession(), delayedMerger, cellResource);\r
96 //\r
97 //                                                              graph.getSession().asyncRequest(new WriteRequest(virtualGraph) {\r
98 //\r
99 //                                                                      @Override\r
100 //                                                                      public void perform(WriteGraph graph) throws DatabaseException {\r
101 //\r
102 //                                                                              Layer0 L0 = Layer0.getInstance(graph);\r
103 //                                                                              SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
104 //\r
105 //                                                                              Map<Resource, Resource> map = graph.syncRequest(new MappedParts(realization));\r
106 //                                                                              Resource resource = map.get(cellResource);\r
107 //                                                                              Resource realizationType = graph.syncRequest(new RealizationType(cellResource, sr.Realization));\r
108 //\r
109 //                                                                              if(resource == null) {\r
110 //                                                                                      resource = graph.newResource();\r
111 //                                                                                      graph.claim(resource, L0.InstanceOf, null, realizationType);\r
112 //                                                                                      graph.claim(resource, L0X.Represents, null, cellResource);\r
113 //                                                                                      graph.addLiteral(resource, sr.Label, sr.LabelOf, L0.String, "", Bindings.STRING );\r
114 //                                                                                      graph.addLiteral(resource, sr.Content, sr.ContentOf, L0.String, "", Bindings.STRING );\r
115 //                                                                                      graph.claim(realization, L0.ConsistsOf, L0.PartOf, resource);\r
116 //                                                                              }\r
117 //\r
118 //                                                                              final AdaptionService service = graph.peekService(AdaptionService.class);\r
119 //\r
120 //                                                                              class Modifier implements ResourceAdapter<StringModifier> {\r
121 //\r
122 //                                                                                      public void adapt(AsyncReadGraph graph, Resource source, Resource r, AsyncProcedure<StringModifier> procedure) {\r
123 //\r
124 //                                                                                              procedure.execute(graph, new StringModifier() {\r
125 //\r
126 //                                                                                                      @Override\r
127 //                                                                                                      public String isValid(String newValue) {\r
128 //                                                                                                              return null;\r
129 //                                                                                                      }\r
130 //\r
131 //                                                                                                      @Override\r
132 //                                                                                                      public void modify(WriteGraph graph, String value) throws DatabaseException {\r
133 //\r
134 //                                                                                                              StringModifier modifier = graph.adapt(cellResource, StringModifier.class);\r
135 //                                                                                                              modifier.modify(graph, value);\r
136 //\r
137 //                                                                                                      }\r
138 //\r
139 //                                                                                              });\r
140 //\r
141 //                                                                                      }\r
142 //\r
143 //                                                                              };\r
144 //\r
145 //                                                                              service.addInstanceAdapter(resource, StringModifier.class, new Modifier());\r
146 //\r
147 //                                                                              cell.listen(GraphBackend.this, graph, resource);\r
148 //\r
149 //                                                                      }\r
150 //\r
151 //                                                              });\r
152 //\r
153 //                                                      }\r
154 //\r
155 //                                              });\r
156 //\r
157 //\r
158 //                                      }\r
159 //\r
160 //                                      @Override\r
161 //                                      public void remove(AsyncReadGraph graph, final Resource cellResource) {\r
162 //\r
163 //                                      }\r
164 //\r
165 //                              });\r
166 //                              \r
167 //                              task.finish();\r
168 //\r
169 //                      }\r
170 //\r
171 //              });\r
172 \r
173         }\r
174 \r
175         @Override\r
176         public <T> T getAdapter(Class<T> clazz) {\r
177 \r
178 /*              if(ModelCellManager.class == clazz) return (T)creator;\r
179                 else if(CellEditor.class == clazz) return (T)editor;\r
180                 else*/ return null;\r
181 \r
182         }\r
183 \r
184         @Override\r
185         public void exception(Throwable t) {\r
186                 t.printStackTrace();\r
187         }\r
188 \r
189         @Override\r
190         public boolean isDisposed() {\r
191                 return disposed;\r
192         }\r
193 \r
194         @Override\r
195         public void exception(AsyncReadGraph graph, Throwable t) {\r
196                 t.printStackTrace();\r
197         }\r
198 \r
199         @Override\r
200         public void exception(ReadGraph graph, Throwable t) {\r
201                 t.printStackTrace();\r
202         }\r
203 \r
204         public VirtualGraph getVirtualGraph() {\r
205                 return virtualGraph;\r
206         }\r
207 \r
208         public Resource getModel() {\r
209                 return model;\r
210         }\r
211         \r
212         public void dispose() {\r
213                 disposed = true;\r
214         }\r
215 \r
216 }\r