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.imports;
\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.MessageBox;
\r
25 import org.eclipse.swt.widgets.Shell;
\r
26 import org.eclipse.ui.handlers.HandlerUtil;
\r
27 import org.simantics.browsing.ui.common.node.AbstractNode;
\r
28 import org.simantics.databoard.Bindings;
\r
29 import org.simantics.databoard.Files;
\r
30 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
\r
31 import org.simantics.db.Resource;
\r
32 import org.simantics.db.WriteGraph;
\r
33 import org.simantics.db.common.request.WriteRequest;
\r
34 import org.simantics.db.common.utils.NameUtils;
\r
35 import org.simantics.db.exception.DatabaseException;
\r
36 import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;
\r
37 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
\r
38 import org.simantics.graph.db.MissingDependencyException;
\r
39 import org.simantics.graph.representation.TransferableGraph1;
\r
40 import org.simantics.layer0.Layer0;
\r
41 import org.simantics.sysdyn.SysdynResource;
\r
42 import org.simantics.sysdyn.ui.Activator;
\r
43 import org.simantics.ui.SimanticsUI;
\r
44 import org.simantics.ui.utils.AdaptionUtils;
\r
47 * Imports modules from exported transferable graph files.
\r
49 * @author Teemu Lempinen
\r
52 public class ImportModuleHandler extends AbstractHandler {
\r
54 public static String IMPORTMODULETPATH = "IMPORT_MODULE_PATH";
\r
57 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
58 ISelection sel = HandlerUtil.getCurrentSelection(event);
\r
60 @SuppressWarnings("unchecked")
\r
61 AbstractNode<Resource> node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class);
\r
65 final Resource model = node.data;
\r
67 // Get imported transferable graph file using FileDialog
\r
68 final Shell shell = HandlerUtil.getActiveShellChecked(event);
\r
69 FileDialog fd = new FileDialog(shell, SWT.OPEN);
\r
70 fd.setText("Import Module");
\r
72 String path = Activator.getDefault().getPreferenceStore().getString(IMPORTMODULETPATH);
\r
73 if(path.isEmpty() || !(new File(path).exists()))
\r
74 path = Platform.getLocation().toOSString();
\r
75 fd.setFilterPath(path);
\r
76 String[] filterExt = {"*.tg"};
\r
77 fd.setFilterExtensions(filterExt);
\r
78 String selected = fd.open();
\r
79 if(selected == null) return null;
\r
81 Activator.getDefault().getPreferenceStore().setValue(IMPORTMODULETPATH, (new File(selected)).getParent());
\r
83 // Get the transferable graph from file
\r
84 TransferableGraph1 tg = null;
\r
86 tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));
\r
87 } catch (RuntimeBindingConstructionException e) {
\r
88 e.printStackTrace();
\r
89 } catch (IOException e) {
\r
90 MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);
\r
91 mb.setText("Error");
\r
92 mb.setMessage("The imported file is not of type: Module Type");
\r
96 if(tg == null) return null;
\r
99 // Import the module to model
\r
100 DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(model);
\r
102 DefaultPasteHandler.defaultExecute(tg, model, ia);
\r
103 } catch (MissingDependencyException e) {
\r
104 e.printStackTrace();
\r
105 } catch (Exception e) {
\r
106 e.printStackTrace();
\r
109 final Resource root = ia.getRoot();
\r
111 // Check that the imported file actually was a module. Display error message otherwise.
\r
112 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
115 public void perform(WriteGraph graph) throws DatabaseException {
\r
116 if(!graph.isInheritedFrom(root, SysdynResource.getInstance(graph).Module)) {
\r
117 Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf);
\r
118 String type = "...";
\r
119 if(instanceOf != null)
\r
120 type = NameUtils.getSafeName(graph, instanceOf);
\r
122 Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits);
\r
123 if(inheritedFrom != null)
\r
124 type = NameUtils.getSafeName(graph, inheritedFrom);
\r
126 final String ft = type;
\r
127 graph.deny(root, Layer0.getInstance(graph).PartOf);
\r
129 shell.getDisplay().asyncExec(new Runnable() {
\r
132 public void run() {
\r
133 MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);
\r
134 mb.setText("Error");
\r
135 mb.setMessage("The imported file is not of type: System Dynamics Module (" + ft +")");
\r