1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.browser.nodes;
\r
14 import java.util.Collection;
\r
16 import org.eclipse.jface.dialogs.MessageDialog;
\r
17 import org.eclipse.swt.SWT;
\r
18 import org.eclipse.swt.widgets.Shell;
\r
19 import org.eclipse.ui.PlatformUI;
\r
20 import org.simantics.browsing.ui.common.node.AbstractNode;
\r
21 import org.simantics.browsing.ui.common.node.DeleteException;
\r
22 import org.simantics.browsing.ui.common.node.IDeletableNode;
\r
23 import org.simantics.browsing.ui.common.node.IModifiableNode;
\r
24 import org.simantics.browsing.ui.content.Labeler.Modifier;
\r
25 import org.simantics.browsing.ui.graph.impl.LabelModifier;
\r
26 import org.simantics.db.ReadGraph;
\r
27 import org.simantics.db.Resource;
\r
28 import org.simantics.db.WriteGraph;
\r
29 import org.simantics.db.common.request.ObjectsWithType;
\r
30 import org.simantics.db.common.request.ReadRequest;
\r
31 import org.simantics.db.common.request.WriteRequest;
\r
32 import org.simantics.db.exception.DatabaseException;
\r
33 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
\r
34 import org.simantics.db.exception.ServiceException;
\r
35 import org.simantics.db.layer0.adapter.PasteHandler;
\r
36 import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;
\r
37 import org.simantics.db.layer0.util.RemoverUtil;
\r
38 import org.simantics.db.procedure.Listener;
\r
39 import org.simantics.db.request.Read;
\r
40 import org.simantics.issues.ontology.IssueResource;
\r
41 import org.simantics.layer0.Layer0;
\r
42 import org.simantics.modeling.ModelingResources;
\r
43 import org.simantics.simulation.ontology.SimulationResource;
\r
44 import org.simantics.structural.stubs.StructuralResource2;
\r
45 import org.simantics.sysdyn.ui.utils.ModuleTypeNameValidator;
\r
46 import org.simantics.ui.SimanticsUI;
\r
48 public class ModuleTypeNode extends AbstractNode<Resource> implements IDeletableNode, IModifiableNode {
\r
51 Listener<String> configurationNameSynchronizer;
\r
52 private boolean disposed = false;
\r
53 private Resource configuration;
\r
55 public ModuleTypeNode(Resource resource) {
\r
58 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
\r
61 public void run(ReadGraph graph) throws DatabaseException {
\r
62 ModelingResources mr = ModelingResources.getInstance(graph);
\r
63 StructuralResource2 sr2 = StructuralResource2.getInstance(graph);
\r
64 Resource type = graph.getPossibleObject(data, mr.SymbolToComponentType);
\r
65 configuration = graph.getPossibleObject(type, sr2.IsDefinedBy);
\r
69 // Not the best solution for name sync
\r
70 configurationNameSynchronizer = new Listener<String>() {
\r
73 public void execute(final String result) {
\r
77 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
80 public void perform(WriteGraph graph) throws DatabaseException {
\r
81 if(configuration != null)
\r
82 graph.claimLiteral(configuration, Layer0.getInstance(graph).HasLabel, result);
\r
88 public void exception(Throwable t) {
\r
89 t.printStackTrace();
\r
93 public boolean isDisposed() {
\r
98 SimanticsUI.getSession().asyncRequest(new Read<String>() {
\r
101 public String perform(ReadGraph graph) throws DatabaseException {
\r
102 Layer0 l0 = Layer0.getInstance(graph);
\r
103 ModelingResources mr = ModelingResources.getInstance(graph);
\r
104 Resource type = graph.getPossibleObject(data, mr.SymbolToComponentType);
\r
105 return (String) (type != null ? graph.getRelatedValue(type, l0.HasName) : null);
\r
108 }, configurationNameSynchronizer);
\r
113 public Modifier getModifier(String columnId) {
\r
114 Modifier modifier = null;
\r
116 modifier = SimanticsUI.getSession().syncRequest(new Read<Modifier>() {
\r
119 public Modifier perform(ReadGraph graph) throws ManyObjectsForFunctionalRelationException, ServiceException {
\r
120 ModelingResources mr = ModelingResources.getInstance(graph);
\r
121 Layer0 l0 = Layer0.getInstance(graph);
\r
122 Resource type = graph.getPossibleObject(data, mr.SymbolToComponentType);
\r
124 LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), type, l0.HasName) {
\r
126 public String isValid(String label) {
\r
127 if (!new ModuleTypeNameValidator().isValid(data, label))
\r
128 return "Not valid";
\r
138 } catch (DatabaseException e) {
\r
139 e.printStackTrace();
\r
146 public void delete() throws DeleteException {
\r
149 SimanticsUI.getSession().syncRequest(new WriteRequest() {
\r
152 public void perform(WriteGraph graph) throws DatabaseException{
\r
153 Layer0 l0 = Layer0.getInstance(graph);
\r
154 StructuralResource2 st = StructuralResource2.getInstance(graph);
\r
155 ModelingResources mr = ModelingResources.getInstance(graph);
\r
157 Resource type = graph.getPossibleObject(data, mr.SymbolToComponentType);
\r
158 Resource model = graph.getSingleObject(type, l0.PartOf);
\r
159 Resource modelConfiguration = graph.getSingleObject(model, SimulationResource.getInstance(graph).HasConfiguration);
\r
160 if (!graph.syncRequest(new ObjectsWithType(modelConfiguration, l0.ConsistsOf, type)).isEmpty()) {
\r
161 throw new ModuleDeleteException("The module is used at the model configuration");
\r
163 Collection<Resource> moduleTypes = graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, st.ComponentType));
\r
164 for(Resource r : moduleTypes) {
\r
165 Resource configuration = graph.getSingleObject(r, st.IsDefinedBy);
\r
166 if(!graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, type)).isEmpty()) {
\r
167 throw new ModuleDeleteException("The module is used at another module: " + graph.getRelatedValue(r, l0.HasName));
\r
171 IssueResource ISSUE = IssueResource.getInstance(graph);
\r
173 for(Resource issueSource : graph.syncRequest(new ObjectsWithType(type, l0.ConsistsOf, ISSUE.Sources_DependencyTracker))) {
\r
174 for(Resource issue : graph.syncRequest(new ObjectsWithType(issueSource, ISSUE.IssueSource_Manages, ISSUE.Issue))) {
\r
175 RemoverUtil.remove(graph, issue);
\r
178 RemoverUtil.remove(graph, type);
\r
181 } catch (ModuleDeleteException e) {
\r
182 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
\r
183 MessageDialog dialog = new MessageDialog(shell, "Unable to delete", null, e.message, SWT.ERROR,
\r
184 new String[] { "OK" }, 0);
\r
187 } catch (DatabaseException e) {
\r
188 e.printStackTrace();
\r
192 private class ModuleDeleteException extends DatabaseException {
\r
193 private static final long serialVersionUID = 4076002781765246919L;
\r
196 public ModuleDeleteException(String message) {
\r
197 this.message = message;
\r
201 @SuppressWarnings("rawtypes")
\r
203 public Object getAdapter(Class adapter) {
\r
204 if(PasteHandler.class == adapter && configuration != null)
\r
205 return new DefaultPasteHandler(configuration);
\r
206 return super.getAdapter(adapter);
\r