]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.export.core/src/org/simantics/export/core/ExportContext.java
Merge branch 'feature/funcwrite'
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / ExportContext.java
1 package org.simantics.export.core;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6
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;
27
28 public class ExportContext {
29
30         // Context data
31         public ISessionContext sessionCtx;
32         public Session session;
33         public String project;
34         public List<String> activeModels;
35         
36         // Other data
37         /** Selected content */
38         public List<String> selection;
39         
40         public ExportExtensionPoint eep;
41         public File workarea;
42         public Preferences store;
43         public Databoard databoard;
44         
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();
54                 
55         result.session.syncRequest(new ReadRequest() {
56             public void run(ReadGraph graph) throws DatabaseException {
57
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);
62                 }
63                 
64                 for (Resource activeModel : graph.sync( new ActiveModels( project ) )) {
65                         try { 
66                                         result.activeModels.add( graph.getURI( activeModel ) );
67                         } catch (AssumptionException e) {
68                         } catch (ValidationException e) {
69                         } catch (ServiceException e) {
70                         }
71                 }
72
73                 if ( selection != null ) {
74                         for (Resource r : AdaptionUtils.adaptToCollection(selection, Resource.class)) {
75                                 try {                                   
76                                         result.selection.add( graph.getURI(r) );
77                                 } catch (AssumptionException e) {
78                                 } catch (ValidationException e) {
79                                 } catch (ServiceException e) {
80                                 }
81                         }
82                 }
83                 
84             }
85         });
86                 
87                 //IExperiment experiment = ExperimentManager.getActiveExperiment();             
88 //        ISelection selection = HandlerUtil.getCurrentSelection(event);
89 //        ImagesNode images = AdaptionUtils.adaptToSingle(selection, ImagesNode.class);
90                 return result;
91         }
92
93         /**
94          * Get preference store for a combination of selections
95          * @param contents
96          * @return selections
97          */
98         public Preferences store(List<Content> contents)
99         {
100                 int hash = _hash(contents);             
101                 return store.node( "Selection-"+hash );         
102         }
103
104         public static int _hash(List<Content> contents) {
105                 int hash = 0x234234;
106                 for ( Content c : contents ) {
107                         hash = 13*hash + c.url.hashCode();
108                         hash = 13*hash + c.formatId.hashCode();
109                 }
110                 return hash;
111         }
112         
113 }