1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 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.handlers;
\r
14 import java.io.File;
\r
15 import java.io.IOException;
\r
17 import org.eclipse.core.commands.AbstractHandler;
\r
18 import org.eclipse.core.commands.ExecutionEvent;
\r
19 import org.eclipse.core.commands.ExecutionException;
\r
20 import org.eclipse.core.runtime.Platform;
\r
21 import org.eclipse.jface.viewers.ISelection;
\r
22 import org.eclipse.swt.SWT;
\r
23 import org.eclipse.swt.widgets.FileDialog;
\r
24 import org.eclipse.swt.widgets.Shell;
\r
25 import org.eclipse.ui.handlers.HandlerUtil;
\r
26 import org.simantics.databoard.Bindings;
\r
27 import org.simantics.databoard.Files;
\r
28 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
\r
29 import org.simantics.db.ReadGraph;
\r
30 import org.simantics.db.Resource;
\r
31 import org.simantics.db.WriteGraph;
\r
32 import org.simantics.db.WriteOnlyGraph;
\r
33 import org.simantics.db.common.request.ReadRequest;
\r
34 import org.simantics.db.common.request.WriteRequest;
\r
35 import org.simantics.db.exception.DatabaseException;
\r
36 import org.simantics.db.exception.ResourceNotFoundException;
\r
37 import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;
\r
38 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
\r
39 import org.simantics.db.request.Read;
\r
40 import org.simantics.graph.representation.Root;
\r
41 import org.simantics.graph.representation.TransferableGraph1;
\r
42 import org.simantics.layer0.Layer0;
\r
43 import org.simantics.layer0.utils.direct.GraphUtils;
\r
44 import org.simantics.sysdyn.SysdynResource;
\r
45 import org.simantics.sysdyn.manager.FunctionUtils;
\r
46 import org.simantics.ui.SimanticsUI;
\r
47 import org.simantics.ui.utils.ResourceAdaptionUtils;
\r
49 public class ImportFunctionLibrary extends AbstractHandler {
\r
52 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
53 Resource project = SimanticsUI.getProject().get();
\r
54 if(project == null) return null;
\r
56 ISelection sel = HandlerUtil.getCurrentSelection(event);
\r
57 final Resource functionLibrary = ResourceAdaptionUtils.toSingleResource(sel);
\r
58 if(functionLibrary == null) return null;
\r
60 Shell shell = HandlerUtil.getActiveShellChecked(event);
\r
61 FileDialog fd = new FileDialog(shell, SWT.OPEN);
\r
62 fd.setText("Import Function Library");
\r
63 fd.setFilterPath(Platform.getLocation().toOSString());
\r
64 String[] filterExt = {"*.tg"};
\r
65 fd.setFilterExtensions(filterExt);
\r
66 String selected = fd.open();
\r
67 if(selected == null) return null;
\r
69 TransferableGraph1 tg = null;
\r
71 tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));
\r
72 } catch (RuntimeBindingConstructionException e) {
\r
73 e.printStackTrace();
\r
74 } catch (IOException e) {
\r
75 e.printStackTrace();
\r
77 if(tg == null) return null;
\r
81 Boolean hasSharedOntologies;
\r
82 hasSharedOntologies = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {
\r
85 public Boolean perform(ReadGraph graph) throws DatabaseException {
\r
87 graph.getResource("http://SharedOntologies");
\r
88 } catch (ResourceNotFoundException e) {
\r
95 if(!hasSharedOntologies) {
\r
96 SimanticsUI.getSession().syncRequest(new WriteRequest() {
\r
99 public void perform(WriteGraph graph) throws DatabaseException {
\r
100 Layer0 l0 = Layer0.getInstance(graph);
\r
101 GraphUtils.create2(graph, l0.Library,
\r
102 l0.HasName, "SharedOntologies",
\r
103 l0.PartOf, graph.getResource("http:/"));
\r
108 } catch (DatabaseException e) {
\r
109 e.printStackTrace();
\r
114 SysdynFunctionLibraryImportAdvisor ia = new SysdynFunctionLibraryImportAdvisor(functionLibrary);
\r
116 DefaultPasteHandler.defaultExecute(tg, functionLibrary, ia);
\r
117 } catch (Exception e) {
\r
118 e.printStackTrace();
\r
121 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
\r
124 public void run(ReadGraph graph) throws DatabaseException {
\r
125 FunctionUtils.updateFunctionFileForLibrary(graph, functionLibrary);
\r
131 private class SysdynFunctionLibraryImportAdvisor extends DefaultPasteImportAdvisor {
\r
134 Resource model = null;
\r
136 public SysdynFunctionLibraryImportAdvisor(Resource library) {
\r
141 public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {
\r
142 if(root.type.equals(SysdynResource.URIs.SharedFunctionOntology)) {
\r
145 SysdynResource sr = SysdynResource.getInstance(graph);
\r
146 Layer0 l0 = Layer0.getInstance(graph);
\r
147 Resource r = library;
\r
148 while(!graph.isInstanceOf(r, sr.SysdynModel) && graph.isInstanceOf(r, l0.Ontology))
\r
149 r = graph.getSingleObject(r, l0.PartOf);
\r
150 if(graph.isInstanceOf(r, sr.SysdynModel))
\r
153 library = graph.getResource("http://SharedOntologies");
\r
154 } catch (ResourceNotFoundException e) {
\r
155 e.printStackTrace();
\r
161 public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {
\r
162 Layer0 l0 = graph.getService(Layer0.class);
\r
163 this.root = graph.newResource();
\r
164 graph.claim(library, l0.ConsistsOf, l0.PartOf, this.root);
\r
165 String name = root.name;
\r
166 String newName = nameMappings.get(name);
\r
167 graph.addLiteral(this.root, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);
\r
168 if(model != null && root.type.equals(SysdynResource.URIs.SharedFunctionOntology)) {
\r
169 graph.claim(model, l0.IsLinkedTo, l0.IsLinkedTo_Inverse, this.root);
\r