]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/AsyncReadGraphDataSource.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / AsyncReadGraphDataSource.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl;
13
14 import java.util.function.Consumer;
15
16 import org.simantics.browsing.ui.DataSource;
17 import org.simantics.db.AsyncReadGraph;
18 import org.simantics.db.AsyncRequestProcessor;
19 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
20 import org.simantics.db.common.request.AsyncReadRequest;
21 import org.simantics.db.service.LifecycleSupport;
22 import org.simantics.utils.ui.ErrorLogger;
23
24 public class AsyncReadGraphDataSource implements DataSource<AsyncReadGraph> {
25
26     final private AsyncRequestProcessor processor;
27     final private LifecycleSupport ls;
28
29     public AsyncReadGraphDataSource(AsyncRequestProcessor processor) {
30         this.processor = processor;
31         this.ls = processor.getService(LifecycleSupport.class);
32     }
33
34     @Override
35     public void schedule(final Consumer<AsyncReadGraph> callback) {
36         if(ls.isClosing() || ls.isClosed()) return;
37         processor.asyncRequest(new AsyncReadRequest() {
38             @Override
39             public void run(AsyncReadGraph graph) {
40                 if(ls.isClosing() || ls.isClosed()) return;
41                 callback.accept(graph);
42             }
43         }, new ProcedureAdapter<Object>() {
44             @Override
45             public void exception(Throwable t) {
46                 ErrorLogger.defaultLogError(t);
47             }
48         });
49     }
50
51     @Override
52     public Class<AsyncReadGraph> getProvidedClass() {
53         return AsyncReadGraph.class;
54     }
55
56 }