]> gerrit.simantics Code Review - simantics/platform.git/blob - modelBrowser/model/Model.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / modelBrowser / model / Model.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.io.File;
15 import java.util.ArrayList;
16 import java.util.Collection;
17
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.simantics.browsing.ui.common.node.IModifiable;
22 import org.simantics.browsing.ui.content.Labeler.Modifier;
23 import org.simantics.browsing.ui.graph.impl.LabelModifier;
24 import org.simantics.databoard.Bindings;
25 import org.simantics.db.ReadGraph;
26 import org.simantics.db.Resource;
27 import org.simantics.db.Session;
28 import org.simantics.db.exception.DatabaseException;
29 import org.simantics.layer0.Layer0;
30 import org.simantics.modeling.ui.Activator;
31 import org.simantics.simulation.ontology.SimulationResource;
32
33 @Deprecated
34 public class Model extends Node implements IModifiable {
35
36     final static boolean showFolders = true;
37
38     Resource configuration;
39     Session session;
40
41     public Model(ReadGraph g, Resource resource) throws DatabaseException {
42         super(resource);
43         this.configuration = g.getPossibleObject(resource, SimulationResource.getInstance(g).HasConfiguration);
44         this.session = g.getSession();
45     }
46
47     public File getModelDir() throws DatabaseException {
48         IPath location = Platform.getLocation();
49         File f = location.toFile();
50
51         String modelName =
52             (String) session.syncRequest(
53                     org.simantics.db.common.request.Queries.getRelatedValue(resource, Layer0.URIs.HasName, Bindings.STRING));
54
55         File modelDir = new File(f, modelName);
56         return modelDir;
57     }
58
59     @Override
60     public Collection<?> getChildren(ReadGraph g) throws DatabaseException {
61         Collection<Object> ret = new ArrayList<Object>(3);
62         if(configuration != null) {
63             ret.add(new Decorator(g.adapt(configuration, INode.class)) {
64                 @Override
65                 public String getLabel(ReadGraph g) {
66                     return "Configuration";
67                 }
68             });
69         }
70         ret.add(new Experiments(resource));
71         ret.add(new States(resource));
72 //        ret.add(new Queries(g, resource));
73         ret.add(new Charts(resource));
74         ret.add(new Subscriptions(resource));
75         ret.add(new Images(resource));
76 //        ret.add(new Spreadsheets(resource));
77         ret.add(new RelationViews(resource));
78
79         /*if(!showFolders) {
80                         ArrayList<Object> flatten = new ArrayList<Object>();
81                         for(INode n : ret)
82                                 flatten.addAll(n.getChildren(g));
83                         return flatten;
84                 }*/
85         return ret;
86     }
87
88     @Override
89     public ImageDescriptor getImage(ReadGraph g) {
90         return Activator.MODEL_ICON;
91     }
92
93     @Override
94     public boolean equals(Object obj) {
95         if(this == obj)
96             return true;
97         if(obj == null)
98             return false;
99         if(!getClass().equals(obj.getClass()))
100             return false;
101         Model other = (Model)obj;
102         if(configuration == null) {
103             return resource.equals(other.resource) && other.configuration == null;
104         } else {
105             return resource.equals(other.resource)
106             && configuration.equals(other.configuration);
107         }
108     }
109
110     @Override
111     public int hashCode() {
112         if(configuration == null) {
113             return getClass().hashCode()*31 + resource.hashCode();
114         } else {
115             return (getClass().hashCode()*31
116                     +  configuration.hashCode())*31 + resource.hashCode();
117         }
118     }
119
120     @Override
121     public Modifier getModifier(final Session session, String columnId) {
122         return new LabelModifier(session, resource);
123     }
124 }