1 /*******************************************************************************
\r
2 * Copyright (c) 2010 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.exports;
\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
36 * Exports a selected model without asking the location.
\r
37 * Model determination is based on any resource of the model.
\r
39 * @author Tuomas Miettinen
\r
42 public class ExportModelButtonHandler extends ExportModelHandler {
\r
45 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
47 status = WorkbenchUtils.getStatusLine( HandlerUtil.getActiveSite(event) );
\r
49 final Resource model = determineModel(event);
\r
53 String selected = getAbsolutePath(model, event, false);
\r
55 if (selected != null) {
\r
56 createFile(model, selected);
\r
57 setExportStatus(model, selected);
\r
64 protected Resource determineModel(ExecutionEvent event) {
\r
65 ISelection sel = HandlerUtil.getCurrentSelection(event);
\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
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
84 inputResource = (Resource)a.data;
\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
100 // Now that we finally have determined which Resource is selected, we just need
\r
101 // to get the model of that Resource.
\r
103 final Resource resource = inputResource;
\r
105 model = SimanticsUI.getSession().syncRequest(new Read<Resource>() {
\r
108 public Resource perform(ReadGraph graph) throws DatabaseException {
\r
109 return graph.sync(new PossibleModel(resource));
\r
113 if(model == null) return null;
\r
114 } catch (DatabaseException e1) {
\r
115 e1.printStackTrace();
\r