]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
e4645106910a8684544a0a9f3eb3c3f0352b0aa7
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.handlers.imports;\r
13 \r
14 import java.io.File;\r
15 \r
16 import org.eclipse.core.commands.AbstractHandler;\r
17 import org.eclipse.core.commands.ExecutionEvent;\r
18 import org.eclipse.core.commands.ExecutionException;\r
19 import org.eclipse.core.runtime.IProgressMonitor;\r
20 import org.eclipse.core.runtime.IStatus;\r
21 import org.eclipse.core.runtime.Platform;\r
22 import org.eclipse.core.runtime.jobs.Job;\r
23 import org.eclipse.jface.viewers.ISelection;\r
24 import org.eclipse.swt.SWT;\r
25 import org.eclipse.swt.widgets.FileDialog;\r
26 import org.eclipse.swt.widgets.Shell;\r
27 import org.eclipse.ui.handlers.HandlerUtil;\r
28 import org.simantics.DatabaseJob;\r
29 import org.simantics.browsing.ui.common.node.AbstractNode;\r
30 import org.simantics.db.Resource;\r
31 import org.simantics.sysdyn.ui.Activator;\r
32 import org.simantics.sysdyn.ui.utils.imports.ImportUtilsUI;\r
33 import org.simantics.utils.ui.AdaptionUtils;\r
34 \r
35 /**\r
36  * Imports modules from exported transferable graph files.\r
37  * \r
38  * @author Teemu Lempinen\r
39  *\r
40  */\r
41 public class ImportModuleHandler extends AbstractHandler {\r
42 \r
43 \r
44         @Override\r
45         public Object execute(ExecutionEvent event) throws ExecutionException {\r
46                 ISelection sel = HandlerUtil.getCurrentSelection(event);\r
47 \r
48                 @SuppressWarnings("unchecked")\r
49                 AbstractNode<Resource> node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class);\r
50                 if (node == null)\r
51                         return null;\r
52 \r
53                 final Resource model = node.data;\r
54 \r
55                 // Get imported transferable graph file using FileDialog\r
56                 Shell shell = HandlerUtil.getActiveShellChecked(event);\r
57                 FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
58                 fd.setText("Import Module");\r
59 \r
60                 String path = Activator.getDefault().getPreferenceStore().getString(ImportUtilsUI.IMPORTMODULETPATH);\r
61                 if(path.isEmpty() || !(new File(path).exists()))\r
62                         path = Platform.getLocation().toOSString();\r
63                 fd.setFilterPath(path);\r
64                 String[] filterExt = {"*.tg"};\r
65                 fd.setFilterExtensions(filterExt);\r
66                 final String selected = fd.open();\r
67                 if(selected == null) return null;\r
68                 \r
69                 Job job = new DatabaseJob("Import model") {\r
70 \r
71             @Override\r
72             protected IStatus run(IProgressMonitor monitor) {\r
73                 IStatus status = ImportUtilsUI.importModuleFile(model, selected, monitor);\r
74                 return status;\r
75             }\r
76         };\r
77         job.setUser(true);\r
78         job.schedule();\r
79 \r
80                 return null;\r
81         }\r
82         \r
83 }\r