]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.desktop.ui/src/org/simantics/desktop/ui/internal/ExportModel.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.desktop.ui / src / org / simantics / desktop / ui / internal / ExportModel.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.desktop.ui.internal;
13
14 import java.util.Collection;
15
16 import org.eclipse.core.commands.AbstractHandler;
17 import org.eclipse.core.commands.ExecutionEvent;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.StructuredSelection;
21 import org.eclipse.swt.widgets.Display;
22 import org.simantics.Simantics;
23 import org.simantics.browsing.ui.common.AdaptableHintContext;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.common.request.UniqueRead;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.layer0.SelectionHints;
29 import org.simantics.db.layer0.request.ProjectModels;
30 import org.simantics.modeling.ModelingUtils;
31 import org.simantics.utils.datastructures.hints.IHintContext;
32
33 public class ExportModel extends AbstractHandler {
34
35     @Override
36     public Object execute(ExecutionEvent event) throws ExecutionException {
37         
38         try {
39                 
40                         Resource lib = Simantics.sync(new UniqueRead<Resource>() {
41
42                                 @Override
43                                 public Resource perform(ReadGraph graph) throws DatabaseException {
44                                         Collection<Resource> models = graph.sync(new ProjectModels(Simantics.getProjectResource()));
45                                         if(models.size() == 1) return models.iterator().next();
46                                         return null;
47                                 }
48                                 
49                         });
50                         if(lib == null) return null;
51                         
52             IHintContext context = new AdaptableHintContext(SelectionHints.KEY_MAIN);
53             context.setHint(SelectionHints.KEY_MAIN, lib);
54
55                         IStructuredSelection sel = new StructuredSelection(context);
56                 ModelingUtils.openWizard(Display.getCurrent(), sel, "org.simantics.modeling.ui.modelExportWizard"); //$NON-NLS-1$
57                 } catch (DatabaseException e) {
58                         // TODO Auto-generated catch block
59                         e.printStackTrace();
60                 }
61         
62         return null;
63         
64     }
65
66 }