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.sysdyn.ui.browser.nodes.FunctionsFolder;
\r
47 import org.simantics.ui.SimanticsUI;
\r
48 import org.simantics.ui.utils.AdaptionUtils;
\r
49 import org.simantics.ui.utils.ResourceAdaptionUtils;
\r
51 public class ImportFunctionLibrary extends AbstractHandler {
\r
54 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
55 ISelection sel = HandlerUtil.getCurrentSelection(event);
\r
56 Resource r = ResourceAdaptionUtils.toSingleResource(sel);
\r
58 FunctionsFolder mn = AdaptionUtils.adaptToSingle(sel, FunctionsFolder.class);
\r
61 if(r == null) return null;
\r
63 final Resource functionLibrary = r;
\r
65 Shell shell = HandlerUtil.getActiveShellChecked(event);
\r
66 FileDialog fd = new FileDialog(shell, SWT.OPEN);
\r
67 fd.setText("Import Function Library");
\r
68 fd.setFilterPath(Platform.getLocation().toOSString());
\r
69 String[] filterExt = {"*.tg"};
\r
70 fd.setFilterExtensions(filterExt);
\r
71 String selected = fd.open();
\r
72 if(selected == null) return null;
\r
74 TransferableGraph1 tg = null;
\r
76 tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));
\r
77 } catch (RuntimeBindingConstructionException e) {
\r
78 e.printStackTrace();
\r
79 } catch (IOException e) {
\r
80 e.printStackTrace();
\r
82 if(tg == null) return null;
\r
86 Boolean hasSharedOntologies;
\r
87 hasSharedOntologies = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {
\r
90 public Boolean perform(ReadGraph graph) throws DatabaseException {
\r
92 graph.getResource("http://SharedOntologies");
\r
93 } catch (ResourceNotFoundException e) {
\r
100 if(!hasSharedOntologies) {
\r
101 SimanticsUI.getSession().syncRequest(new WriteRequest() {
\r
104 public void perform(WriteGraph graph) throws DatabaseException {
\r
105 Layer0 l0 = Layer0.getInstance(graph);
\r
106 GraphUtils.create2(graph, l0.Library,
\r
107 l0.HasName, "SharedOntologies",
\r
108 l0.PartOf, graph.getResource("http:/"));
\r
113 } catch (DatabaseException e) {
\r
114 e.printStackTrace();
\r
119 SysdynFunctionLibraryImportAdvisor ia = new SysdynFunctionLibraryImportAdvisor(functionLibrary);
\r
121 DefaultPasteHandler.defaultExecute(tg, functionLibrary, ia);
\r
122 } catch (Exception e) {
\r
123 e.printStackTrace();
\r
126 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
\r
129 public void run(ReadGraph graph) throws DatabaseException {
\r
130 FunctionUtils.updateFunctionFileForLibrary(graph, functionLibrary);
\r
136 private class SysdynFunctionLibraryImportAdvisor extends DefaultPasteImportAdvisor {
\r
139 Resource model = null;
\r
141 public SysdynFunctionLibraryImportAdvisor(Resource library) {
\r
146 public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {
\r
147 if(root.type.equals(SysdynResource.URIs.SharedFunctionOntology)) {
\r
150 SysdynResource sr = SysdynResource.getInstance(graph);
\r
151 Layer0 l0 = Layer0.getInstance(graph);
\r
152 Resource r = library;
\r
153 while(!graph.isInstanceOf(r, sr.SysdynModel) && graph.isInstanceOf(r, l0.Ontology))
\r
154 r = graph.getSingleObject(r, l0.PartOf);
\r
155 if(graph.isInstanceOf(r, sr.SysdynModel))
\r
158 library = graph.getResource("http://SharedOntologies");
\r
159 } catch (ResourceNotFoundException e) {
\r
160 e.printStackTrace();
\r
166 public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {
\r
167 Layer0 l0 = graph.getService(Layer0.class);
\r
168 this.root = graph.newResource();
\r
169 graph.claim(library, l0.ConsistsOf, l0.PartOf, this.root);
\r
170 String name = root.name;
\r
171 String newName = nameMappings.get(name);
\r
172 graph.addLiteral(this.root, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);
\r
173 if(model != null && root.type.equals(SysdynResource.URIs.SharedFunctionOntology)) {
\r
174 graph.claim(model, l0.IsLinkedTo, l0.IsLinkedTo_Inverse, this.root);
\r