]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/impl/ReadFactoryImpl.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/impl/ReadFactoryImpl.java
new file mode 100644 (file)
index 0000000..ace741a
--- /dev/null
@@ -0,0 +1,86 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 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.browsing.ui.swt.widgets.impl;\r
+\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.common.request.UnaryRead;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.db.procedure.Listener;\r
+import org.simantics.utils.ReflectionUtils;\r
+import org.simantics.utils.datastructures.Pair;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ *\r
+ * @param <Input> expected and checked input type\r
+ * @param <Output> result type of factory\r
+ */\r
+public abstract class ReadFactoryImpl<Input, Output> implements ReadFactory<Input, Output> {\r
+\r
+    final Class<?> inputClass;\r
+    final boolean sync;\r
+\r
+    protected ReadFactoryImpl() {\r
+       this(false);\r
+    }\r
+    \r
+    protected ReadFactoryImpl(boolean sync) {\r
+       inputClass = ReflectionUtils.getSingleParameterType(getClass());\r
+       this.sync = sync;\r
+    }\r
+\r
+    public Object getIdentity(Object inputContents) {\r
+        return new Pair<Object, Class<?>>(inputContents, getClass());\r
+    }\r
+\r
+    private Object getInputContents(Object input, Class<?> inputClass) {\r
+        if (inputClass.isInstance(input))\r
+            return input;\r
+        if (input instanceof ISelection)\r
+            return ISelectionUtils.filterSingleSelection(input, inputClass);\r
+        return null;\r
+    }\r
+\r
+    @Override\r
+    public void listen(ISessionContext context, Object input, Listener<Output> listener) {\r
+        final Object inputContents = getInputContents(input, inputClass);\r
+        if (inputContents != null) {\r
+               UnaryRead<Object, Output> read = new UnaryRead<Object, Output>(getIdentity(inputContents)) {\r
+                @SuppressWarnings("unchecked")\r
+                @Override\r
+                public Output perform(ReadGraph graph) throws DatabaseException {\r
+                    return ReadFactoryImpl.this.perform(graph, (Input) inputContents);\r
+                }\r
+                @Override\r
+                public String toString() {\r
+                       return getIdentity(inputContents).toString();\r
+                }\r
+            };\r
+            if(sync)\r
+                               try {\r
+                                       Output out = context.getSession().syncRequest(read, listener);\r
+                                       listener.execute(out);\r
+                               } catch (DatabaseException e) {\r
+                                       Logger.defaultLogError(e);\r
+                               }\r
+                       else\r
+               context.getSession().asyncRequest(read, listener);\r
+        }\r
+    }\r
+\r
+    abstract public Output perform(ReadGraph graph, Input input) throws DatabaseException;\r
+\r
+}\r