]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
9954ae65c07fc094e503e83f297a21dc6247362a
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 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.exports;\r
13 \r
14 import org.eclipse.core.commands.ExecutionEvent;\r
15 import org.eclipse.core.commands.ExecutionException;\r
16 import org.eclipse.jface.viewers.ISelection;\r
17 import org.eclipse.jface.viewers.IStructuredSelection;\r
18 import org.eclipse.ui.IWorkbenchPage;\r
19 import org.eclipse.ui.IWorkbenchPart;\r
20 import org.eclipse.ui.handlers.HandlerUtil;\r
21 import org.simantics.browsing.ui.common.node.AbstractNode;\r
22 import org.simantics.browsing.ui.platform.PropertyPageView;\r
23 import org.simantics.db.ReadGraph;\r
24 import org.simantics.db.Resource;\r
25 import org.simantics.db.exception.DatabaseException;\r
26 import org.simantics.db.layer0.request.PossibleModel;\r
27 import org.simantics.db.request.Read;\r
28 import org.simantics.modeling.ui.diagramEditor.DiagramEditor;\r
29 import org.simantics.sysdyn.ui.utils.SysdynWorkbenchUtils;\r
30 import org.simantics.ui.SimanticsUI;\r
31 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
32 import org.simantics.utils.ui.AdaptionUtils;\r
33 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
34 \r
35 /**\r
36  * Exports a selected model without asking the location.\r
37  * Model determination is based on any resource of the model.\r
38  * \r
39  * @author Tuomas Miettinen\r
40  *\r
41  */\r
42 public class ExportModelButtonHandler extends ExportModelHandler {\r
43 \r
44         @Override\r
45         public Object execute(ExecutionEvent event) throws ExecutionException {\r
46         \r
47                 status = WorkbenchUtils.getStatusLine( HandlerUtil.getActiveSite(event) );\r
48         \r
49                 final Resource model = determineModel(event);\r
50         if (model == null)\r
51                 return null;\r
52 \r
53         String selected = getAbsolutePath(model, event, false);\r
54         \r
55         if (selected != null) {\r
56                 createFile(model, selected);\r
57                 setExportStatus(model, selected);\r
58         }\r
59         \r
60         return null;\r
61         }\r
62 \r
63         @Override\r
64         protected Resource determineModel(ExecutionEvent event) {\r
65                 ISelection sel = HandlerUtil.getCurrentSelection(event);\r
66         if (sel == null) {\r
67                 // No selection, this is true e.g. in PropertyPageView\r
68                 IWorkbenchPart activePart = HandlerUtil.getActivePart(event);\r
69                 // In such a case get the selection the PropertyPageView point to.\r
70                 if (activePart instanceof PropertyPageView)\r
71                         sel = ((PropertyPageView)activePart).getLastSelection();\r
72         }\r
73         \r
74         // Get the Resource of the selection\r
75         Resource inputResource = ResourceAdaptionUtils.toSingleResource(sel);\r
76         if (inputResource == null) {\r
77                 // Coner case for when export is called when some folder in model browser is selected.\r
78             if (sel instanceof IStructuredSelection) {\r
79                 IStructuredSelection iss = (IStructuredSelection) sel;\r
80                 if (iss.size() == 1) {\r
81                         Object element = iss.getFirstElement();\r
82                         AbstractNode<?> a = AdaptionUtils.adaptToSingle(element, AbstractNode.class);\r
83                         if (a != null)\r
84                                 inputResource = (Resource)a.data;\r
85                 }\r
86             }\r
87         }\r
88         \r
89         // When the selection doesn't have a resource, use the currently active diagram.\r
90         if (inputResource == null) {\r
91                 IWorkbenchPage page = SysdynWorkbenchUtils.getActivePageOfEditor();\r
92                 DiagramEditor editor = (DiagramEditor)page.getActiveEditor();\r
93                 if (editor != null && editor instanceof DiagramEditor) {\r
94                 inputResource = editor.getInputResource();\r
95             } else {\r
96                 return null; \r
97             }\r
98         }\r
99         \r
100         // Now that we finally have determined which Resource is selected, we just need\r
101         // to get the model of that Resource.\r
102         Resource model;\r
103         final Resource resource = inputResource;\r
104                 try {\r
105                         model = SimanticsUI.getSession().syncRequest(new Read<Resource>() {\r
106 \r
107                                 @Override\r
108                                 public Resource perform(ReadGraph graph) throws DatabaseException {\r
109                                         return graph.sync(new PossibleModel(resource));\r
110                                 }\r
111                                 \r
112                         });\r
113                 if(model == null) return null;\r
114                 } catch (DatabaseException e1) {\r
115                         e1.printStackTrace();\r
116                         return null;\r
117                 }\r
118                 return model;\r
119         }\r
120         \r
121 }\r
122 \r