]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/impl/ReadFactoryImpl.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / widgets / impl / ReadFactoryImpl.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 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.browsing.ui.swt.widgets.impl;\r
13 \r
14 import org.eclipse.jface.viewers.ISelection;\r
15 import org.simantics.db.ReadGraph;\r
16 import org.simantics.db.common.request.UnaryRead;\r
17 import org.simantics.db.common.utils.Logger;\r
18 import org.simantics.db.exception.DatabaseException;\r
19 import org.simantics.db.management.ISessionContext;\r
20 import org.simantics.db.procedure.Listener;\r
21 import org.simantics.utils.ReflectionUtils;\r
22 import org.simantics.utils.datastructures.Pair;\r
23 import org.simantics.utils.ui.ISelectionUtils;\r
24 \r
25 /**\r
26  * @author Antti Villberg\r
27  *\r
28  * @param <Input> expected and checked input type\r
29  * @param <Output> result type of factory\r
30  */\r
31 public abstract class ReadFactoryImpl<Input, Output> implements ReadFactory<Input, Output> {\r
32 \r
33     final Class<?> inputClass;\r
34     final boolean sync;\r
35 \r
36     protected ReadFactoryImpl() {\r
37         this(false);\r
38     }\r
39     \r
40     protected ReadFactoryImpl(boolean sync) {\r
41         inputClass = ReflectionUtils.getSingleParameterType(getClass());\r
42         this.sync = sync;\r
43     }\r
44 \r
45     public Object getIdentity(Object inputContents) {\r
46         return new Pair<Object, Class<?>>(inputContents, getClass());\r
47     }\r
48 \r
49     private Object getInputContents(Object input, Class<?> inputClass) {\r
50         if (inputClass.isInstance(input))\r
51             return input;\r
52         if (input instanceof ISelection)\r
53             return ISelectionUtils.filterSingleSelection(input, inputClass);\r
54         return null;\r
55     }\r
56 \r
57     @Override\r
58     public void listen(ISessionContext context, Object input, Listener<Output> listener) {\r
59         final Object inputContents = getInputContents(input, inputClass);\r
60         if (inputContents != null) {\r
61                 UnaryRead<Object, Output> read = new UnaryRead<Object, Output>(getIdentity(inputContents)) {\r
62                 @SuppressWarnings("unchecked")\r
63                 @Override\r
64                 public Output perform(ReadGraph graph) throws DatabaseException {\r
65                     return ReadFactoryImpl.this.perform(graph, (Input) inputContents);\r
66                 }\r
67                 @Override\r
68                 public String toString() {\r
69                         return getIdentity(inputContents).toString();\r
70                 }\r
71             };\r
72             if(sync)\r
73                                 try {\r
74                                         Output out = context.getSession().syncRequest(read, listener);\r
75                                         listener.execute(out);\r
76                                 } catch (DatabaseException e) {\r
77                                         Logger.defaultLogError(e);\r
78                                 }\r
79                         else\r
80                 context.getSession().asyncRequest(read, listener);\r
81         }\r
82     }\r
83 \r
84     abstract public Output perform(ReadGraph graph, Input input) throws DatabaseException;\r
85 \r
86 }\r