1 package org.simantics.export.core;
4 import java.util.ArrayList;
7 import org.eclipse.core.runtime.Platform;
8 import org.eclipse.core.runtime.preferences.ConfigurationScope;
9 import org.eclipse.jface.viewers.IStructuredSelection;
10 import org.osgi.service.prefs.Preferences;
11 import org.simantics.databoard.Bindings;
12 import org.simantics.databoard.Databoard;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.Resource;
15 import org.simantics.db.Session;
16 import org.simantics.db.common.request.ReadRequest;
17 import org.simantics.db.exception.AssumptionException;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.exception.ServiceException;
20 import org.simantics.db.exception.ValidationException;
21 import org.simantics.db.layer0.request.ActiveModels;
22 import org.simantics.db.management.ISessionContext;
23 import org.simantics.export.core.manager.Content;
24 import org.simantics.project.IProject;
25 import org.simantics.project.ProjectKeys;
26 import org.simantics.utils.ui.AdaptionUtils;
28 public class ExportContext {
31 public ISessionContext sessionCtx;
32 public Session session;
33 public String project;
34 public List<String> activeModels;
37 /** Selected content */
38 public List<String> selection;
40 public ExportExtensionPoint eep;
42 public Preferences store;
43 public Databoard databoard;
45 public static ExportContext create(ISessionContext ctx, final IStructuredSelection selection) throws DatabaseException {
46 final ExportContext result = new ExportContext();
47 result.sessionCtx = ctx;
48 result.session = ctx.getSession();
49 result.selection = new ArrayList<String>();
50 result.activeModels = new ArrayList<String>();
51 result.workarea = Platform.getLocation().toFile();
52 result.store = ConfigurationScope.INSTANCE.getNode("org.simantics.export.core");
53 result.databoard = new Databoard();
55 result.session.syncRequest(new ReadRequest() {
56 public void run(ReadGraph graph) throws DatabaseException {
58 IProject iproject = result.sessionCtx.getHint( ProjectKeys.KEY_PROJECT );
59 Resource project = iproject==null?null:iproject.get();
60 if ( project != null ) {
61 result.project = graph.getURI(project);
64 for (Resource activeModel : graph.sync( new ActiveModels( project ) )) {
66 result.activeModels.add( graph.getURI( activeModel ) );
67 } catch (AssumptionException e) {
68 } catch (ValidationException e) {
69 } catch (ServiceException e) {
73 if ( selection != null ) {
74 for (Resource r : AdaptionUtils.adaptToCollection(selection, Resource.class)) {
76 result.selection.add( graph.getURI(r) );
77 } catch (AssumptionException e) {
78 } catch (ValidationException e) {
79 } catch (ServiceException e) {
87 //IExperiment experiment = ExperimentManager.getActiveExperiment();
88 // ISelection selection = HandlerUtil.getCurrentSelection(event);
89 // ImagesNode images = AdaptionUtils.adaptToSingle(selection, ImagesNode.class);
94 * Get preference store for a combination of selections
98 public Preferences store(List<Content> contents)
100 int hash = _hash(contents);
101 return store.node( "Selection-"+hash );
104 public static int _hash(List<Content> contents) {
106 for ( Content c : contents ) {
107 hash = 13*hash + c.url.hashCode();
108 hash = 13*hash + c.formatId.hashCode();