]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/model/Experiment.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / model / Experiment.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/model/Experiment.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/model/Experiment.java
new file mode 100644 (file)
index 0000000..74a232a
--- /dev/null
@@ -0,0 +1,241 @@
+/*******************************************************************************\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.modeling.ui.modelBrowser.model;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.List;\r
+import java.util.function.Supplier;\r
+\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.common.node.DeleteException;\r
+import org.simantics.browsing.ui.common.node.IModifiable;\r
+import org.simantics.browsing.ui.common.node.IRefreshable;\r
+import org.simantics.browsing.ui.content.Labeler.Modifier;\r
+import org.simantics.browsing.ui.graph.impl.LabelModifier;\r
+import org.simantics.browsing.ui.graph.impl.LabelerUtil;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.Statement;\r
+import org.simantics.db.common.ResourceArray;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.request.ResourceRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.procedure.Listener;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.document.DocumentResource;\r
+import org.simantics.modeling.ui.Activator;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+\r
+@Deprecated\r
+public class Experiment extends Node implements INode2, IPathNode, IRefreshable, IModifiable, IDisposable {\r
+\r
+    private static final String PENDING = "pending...";\r
+\r
+    private static final Supplier<Boolean> DEFAULT_IS_DISPOSED = () -> false;\r
+\r
+    Supplier<Boolean> isDisposed = DEFAULT_IS_DISPOSED;\r
+\r
+    ResourceArray model;\r
+    Session session;\r
+    String name = PENDING;\r
+    Collection<Object> children = Collections.emptyList();\r
+    Runnable nameUpdater;\r
+    Runnable childrenUpdater;\r
+\r
+    public Experiment(ReadGraph graph, Resource experiment) {\r
+        super(experiment);\r
+        this.session = graph.getSession();\r
+    }\r
+\r
+    @Override\r
+    public int getCategory(Runnable updater, NodeContext context) {\r
+        return 0;\r
+    }\r
+\r
+    @Override\r
+    public Collection<?> getChildren(final Runnable updater, NodeContext context) {\r
+        childrenUpdater = updater;\r
+\r
+        if (children != Collections.emptyList())\r
+            return children;\r
+\r
+        session.asyncRequest(new ReadRequest() {\r
+            @Override\r
+            public void run(ReadGraph graph) throws DatabaseException {\r
+                List<Object> result = new ArrayList<Object>();\r
+                try {\r
+                    for (Statement factory : findReportFactories(graph)) {\r
+                        result.add(new ReportFactory(graph, factory));\r
+                    }\r
+                    children = result;\r
+                } catch (DatabaseException e) {\r
+                    children = result;\r
+                }\r
+                updater.run();\r
+            }\r
+        });\r
+        return children;\r
+    }\r
+\r
+    @Override\r
+    public ImageDescriptor getImage(Runnable updater, NodeContext context) {\r
+        return Activator.EXPERIMENT_ICON;\r
+    }\r
+\r
+    @Override\r
+    public String getLabel(ReadGraph graph) throws DatabaseException {\r
+        String name = LabelerUtil.safeStringRepresentation(graph, resource);\r
+        Resource initialState = graph.getPossibleObject(resource, SimulationResource.getInstance(graph).HasInitialState);\r
+        if (initialState != null)\r
+            name += " (" + LabelerUtil.safeStringRepresentation(graph, initialState) + ")";\r
+        return name;\r
+    }\r
+\r
+    Read<String> labelRequest(Resource resource) {\r
+        return new ResourceRead<String>(resource) {\r
+            @Override\r
+            public String perform(ReadGraph graph) throws DatabaseException {\r
+                return getLabel(graph);\r
+            }\r
+        };\r
+    }\r
+\r
+    private class NameListener implements Listener<String> {\r
+        private final Object identity;\r
+        private final Resource resource;\r
+\r
+        public NameListener(Object identity, Resource resource) {\r
+            assert identity != null;\r
+            assert resource != null;\r
+            this.identity = identity;\r
+            this.resource = resource;\r
+        }\r
+\r
+        @Override\r
+        public int hashCode() {\r
+            return identity.hashCode() + 31 * resource.hashCode();\r
+        }\r
+\r
+        @Override\r
+        public boolean equals(Object obj) {\r
+            if (this == obj)\r
+                return true;\r
+            if (obj == null)\r
+                return false;\r
+            if (getClass() != obj.getClass())\r
+                return false;\r
+            NameListener other = (NameListener) obj;\r
+            return identity.equals(other.identity) && resource.equals(other.resource);\r
+        }\r
+\r
+        @Override\r
+        public void exception(Throwable t) {\r
+            ErrorLogger.defaultLogError(t);\r
+        }\r
+        @Override\r
+        public boolean isDisposed() {\r
+            return isDisposed.get();\r
+        }\r
+        @Override\r
+        public void execute(String result) {\r
+            name = result;\r
+            nameUpdater.run();\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public String getLabel(final Runnable updater, NodeContext context) {\r
+        nameUpdater = updater;\r
+        if (name != PENDING)\r
+            return name;\r
+\r
+        session.asyncRequest(labelRequest(resource), new NameListener(nameUpdater, resource));\r
+        return name;\r
+    }\r
+\r
+    @Override\r
+    public Modifier getModifier(String columnId) {\r
+        return new LabelModifier(session, resource) {\r
+            @Override\r
+            public void run(DatabaseException ex) {\r
+                if (ex == null) {\r
+                    refreshName();\r
+                } else {\r
+                    super.run(ex);\r
+                }\r
+            }\r
+        };\r
+    }\r
+\r
+    @Override\r
+    public boolean hasChildren(Runnable updater, NodeContext context) {\r
+        return getChildren(updater, context).size() > 0;\r
+    }\r
+\r
+    @SuppressWarnings("rawtypes")\r
+    @Override\r
+    public Object getAdapter(Class adapter) {\r
+        return super.getAdapter(adapter);\r
+    }\r
+\r
+    @Override\r
+    public void refresh() {\r
+        refreshName();\r
+        refreshChildren();\r
+    }\r
+\r
+    public void refreshName() {\r
+        this.name = PENDING;\r
+        if (nameUpdater != null)\r
+            nameUpdater.run();\r
+    }\r
+\r
+    public void refreshChildren() {\r
+        this.children = Collections.emptyList();\r
+        if (childrenUpdater != null)\r
+            childrenUpdater.run();\r
+    }\r
+\r
+    Collection<Statement> findReportFactories(ReadGraph g) throws DatabaseException {\r
+        DocumentResource DOC = DocumentResource.getInstance(g);\r
+        return g.getStatements(resource, DOC.HasReportFactory);\r
+    }\r
+\r
+    @Override\r
+    public void handleDelete() throws DeleteException {\r
+    }\r
+\r
+    @Override\r
+    public void setPath(ResourceArray path) {\r
+        this.model = path;\r
+    }\r
+\r
+    @Override\r
+    public ResourceArray getPath() {\r
+        return model;\r
+    }\r
+\r
+    public Resource getModel() {\r
+        return model.size() == 0 ? null : model.resources[0];\r
+    }\r
+\r
+    @Override\r
+    public void setDisposedCallable(Supplier<Boolean> isDisposed) {\r
+        this.isDisposed = isDisposed;\r
+    }\r
+\r
+}
\ No newline at end of file