]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/model/Experiment.java
Use Consumer interface instead of deprecated Callback interface
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / model / Experiment.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.modeling.ui.modelBrowser.model;
13
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;
19
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;
42
43 @Deprecated
44 public class Experiment extends Node implements INode2, IPathNode, IRefreshable, IModifiable, IDisposable {
45
46     private static final String PENDING = "pending...";
47
48     private static final Supplier<Boolean> DEFAULT_IS_DISPOSED = () -> false;
49
50     Supplier<Boolean> isDisposed = DEFAULT_IS_DISPOSED;
51
52     ResourceArray model;
53     Session session;
54     String name = PENDING;
55     Collection<Object> children = Collections.emptyList();
56     Runnable nameUpdater;
57     Runnable childrenUpdater;
58
59     public Experiment(ReadGraph graph, Resource experiment) {
60         super(experiment);
61         this.session = graph.getSession();
62     }
63
64     @Override
65     public int getCategory(Runnable updater, NodeContext context) {
66         return 0;
67     }
68
69     @Override
70     public Collection<?> getChildren(final Runnable updater, NodeContext context) {
71         childrenUpdater = updater;
72
73         if (children != Collections.emptyList())
74             return children;
75
76         session.asyncRequest(new ReadRequest() {
77             @Override
78             public void run(ReadGraph graph) throws DatabaseException {
79                 List<Object> result = new ArrayList<Object>();
80                 try {
81                     for (Statement factory : findReportFactories(graph)) {
82                         result.add(new ReportFactory(graph, factory));
83                     }
84                     children = result;
85                 } catch (DatabaseException e) {
86                     children = result;
87                 }
88                 updater.run();
89             }
90         });
91         return children;
92     }
93
94     @Override
95     public ImageDescriptor getImage(Runnable updater, NodeContext context) {
96         return Activator.EXPERIMENT_ICON;
97     }
98
99     @Override
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) + ")";
105         return name;
106     }
107
108     Read<String> labelRequest(Resource resource) {
109         return new ResourceRead<String>(resource) {
110             @Override
111             public String perform(ReadGraph graph) throws DatabaseException {
112                 return getLabel(graph);
113             }
114         };
115     }
116
117     private class NameListener implements Listener<String> {
118         private final Object identity;
119         private final Resource resource;
120
121         public NameListener(Object identity, Resource resource) {
122             assert identity != null;
123             assert resource != null;
124             this.identity = identity;
125             this.resource = resource;
126         }
127
128         @Override
129         public int hashCode() {
130             return identity.hashCode() + 31 * resource.hashCode();
131         }
132
133         @Override
134         public boolean equals(Object obj) {
135             if (this == obj)
136                 return true;
137             if (obj == null)
138                 return false;
139             if (getClass() != obj.getClass())
140                 return false;
141             NameListener other = (NameListener) obj;
142             return identity.equals(other.identity) && resource.equals(other.resource);
143         }
144
145         @Override
146         public void exception(Throwable t) {
147             ErrorLogger.defaultLogError(t);
148         }
149         @Override
150         public boolean isDisposed() {
151             return isDisposed.get();
152         }
153         @Override
154         public void execute(String result) {
155             name = result;
156             nameUpdater.run();
157         }
158     }
159
160     @Override
161     public String getLabel(final Runnable updater, NodeContext context) {
162         nameUpdater = updater;
163         if (name != PENDING)
164             return name;
165
166         session.asyncRequest(labelRequest(resource), new NameListener(nameUpdater, resource));
167         return name;
168     }
169
170     @Override
171     public Modifier getModifier(String columnId) {
172         return new LabelModifier(session, resource) {
173             @Override
174             public void accept(DatabaseException ex) {
175                 if (ex == null) {
176                     refreshName();
177                 } else {
178                     super.accept(ex);
179                 }
180             }
181         };
182     }
183
184     @Override
185     public boolean hasChildren(Runnable updater, NodeContext context) {
186         return getChildren(updater, context).size() > 0;
187     }
188
189     @SuppressWarnings("rawtypes")
190     @Override
191     public Object getAdapter(Class adapter) {
192         return super.getAdapter(adapter);
193     }
194
195     @Override
196     public void refresh() {
197         refreshName();
198         refreshChildren();
199     }
200
201     public void refreshName() {
202         this.name = PENDING;
203         if (nameUpdater != null)
204             nameUpdater.run();
205     }
206
207     public void refreshChildren() {
208         this.children = Collections.emptyList();
209         if (childrenUpdater != null)
210             childrenUpdater.run();
211     }
212
213     Collection<Statement> findReportFactories(ReadGraph g) throws DatabaseException {
214         DocumentResource DOC = DocumentResource.getInstance(g);
215         return g.getStatements(resource, DOC.HasReportFactory);
216     }
217
218     @Override
219     public void handleDelete() throws DeleteException {
220     }
221
222     @Override
223     public void setPath(ResourceArray path) {
224         this.model = path;
225     }
226
227     @Override
228     public ResourceArray getPath() {
229         return model;
230     }
231
232     public Resource getModel() {
233         return model.size() == 0 ? null : model.resources[0];
234     }
235
236     @Override
237     public void setDisposedCallable(Supplier<Boolean> isDisposed) {
238         this.isDisposed = isDisposed;
239     }
240
241 }