1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.modelBrowser.model;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.function.Supplier;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.simantics.browsing.ui.NodeContext;
22 import org.simantics.browsing.ui.common.node.DeleteException;
23 import org.simantics.browsing.ui.common.node.IModifiable;
24 import org.simantics.browsing.ui.common.node.IRefreshable;
25 import org.simantics.browsing.ui.content.Labeler.Modifier;
26 import org.simantics.browsing.ui.graph.impl.LabelModifier;
27 import org.simantics.browsing.ui.graph.impl.LabelerUtil;
28 import org.simantics.db.ReadGraph;
29 import org.simantics.db.Resource;
30 import org.simantics.db.Session;
31 import org.simantics.db.Statement;
32 import org.simantics.db.common.ResourceArray;
33 import org.simantics.db.common.request.ReadRequest;
34 import org.simantics.db.common.request.ResourceRead;
35 import org.simantics.db.exception.DatabaseException;
36 import org.simantics.db.procedure.Listener;
37 import org.simantics.db.request.Read;
38 import org.simantics.document.DocumentResource;
39 import org.simantics.modeling.ui.Activator;
40 import org.simantics.simulation.ontology.SimulationResource;
41 import org.simantics.utils.ui.ErrorLogger;
44 public class Experiment extends Node implements INode2, IPathNode, IRefreshable, IModifiable, IDisposable {
46 private static final String PENDING = "pending...";
48 private static final Supplier<Boolean> DEFAULT_IS_DISPOSED = () -> false;
50 Supplier<Boolean> isDisposed = DEFAULT_IS_DISPOSED;
54 String name = PENDING;
55 Collection<Object> children = Collections.emptyList();
57 Runnable childrenUpdater;
59 public Experiment(ReadGraph graph, Resource experiment) {
61 this.session = graph.getSession();
65 public int getCategory(Runnable updater, NodeContext context) {
70 public Collection<?> getChildren(final Runnable updater, NodeContext context) {
71 childrenUpdater = updater;
73 if (children != Collections.emptyList())
76 session.asyncRequest(new ReadRequest() {
78 public void run(ReadGraph graph) throws DatabaseException {
79 List<Object> result = new ArrayList<Object>();
81 for (Statement factory : findReportFactories(graph)) {
82 result.add(new ReportFactory(graph, factory));
85 } catch (DatabaseException e) {
95 public ImageDescriptor getImage(Runnable updater, NodeContext context) {
96 return Activator.EXPERIMENT_ICON;
100 public String getLabel(ReadGraph graph) throws DatabaseException {
101 String name = LabelerUtil.safeStringRepresentation(graph, resource);
102 Resource initialState = graph.getPossibleObject(resource, SimulationResource.getInstance(graph).HasInitialState);
103 if (initialState != null)
104 name += " (" + LabelerUtil.safeStringRepresentation(graph, initialState) + ")";
108 Read<String> labelRequest(Resource resource) {
109 return new ResourceRead<String>(resource) {
111 public String perform(ReadGraph graph) throws DatabaseException {
112 return getLabel(graph);
117 private class NameListener implements Listener<String> {
118 private final Object identity;
119 private final Resource resource;
121 public NameListener(Object identity, Resource resource) {
122 assert identity != null;
123 assert resource != null;
124 this.identity = identity;
125 this.resource = resource;
129 public int hashCode() {
130 return identity.hashCode() + 31 * resource.hashCode();
134 public boolean equals(Object obj) {
139 if (getClass() != obj.getClass())
141 NameListener other = (NameListener) obj;
142 return identity.equals(other.identity) && resource.equals(other.resource);
146 public void exception(Throwable t) {
147 ErrorLogger.defaultLogError(t);
150 public boolean isDisposed() {
151 return isDisposed.get();
154 public void execute(String result) {
161 public String getLabel(final Runnable updater, NodeContext context) {
162 nameUpdater = updater;
166 session.asyncRequest(labelRequest(resource), new NameListener(nameUpdater, resource));
171 public Modifier getModifier(String columnId) {
172 return new LabelModifier(session, resource) {
174 public void run(DatabaseException ex) {
185 public boolean hasChildren(Runnable updater, NodeContext context) {
186 return getChildren(updater, context).size() > 0;
189 @SuppressWarnings("rawtypes")
191 public Object getAdapter(Class adapter) {
192 return super.getAdapter(adapter);
196 public void refresh() {
201 public void refreshName() {
203 if (nameUpdater != null)
207 public void refreshChildren() {
208 this.children = Collections.emptyList();
209 if (childrenUpdater != null)
210 childrenUpdater.run();
213 Collection<Statement> findReportFactories(ReadGraph g) throws DatabaseException {
214 DocumentResource DOC = DocumentResource.getInstance(g);
215 return g.getStatements(resource, DOC.HasReportFactory);
219 public void handleDelete() throws DeleteException {
223 public void setPath(ResourceArray path) {
228 public ResourceArray getPath() {
232 public Resource getModel() {
233 return model.size() == 0 ? null : model.resources[0];
237 public void setDisposedCallable(Supplier<Boolean> isDisposed) {
238 this.isDisposed = isDisposed;