]> gerrit.simantics Code Review - simantics/platform.git/blob
2a60f8095b812820e3b59d9f58c793db6c75a50a
[simantics/platform.git] /
1 package org.simantics.modeling.template2d.ui.export;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.Set;
7
8 import org.eclipse.core.runtime.IProgressMonitor;
9 import org.osgi.service.prefs.Preferences;
10 import org.simantics.databoard.binding.mutable.Variant;
11 import org.simantics.databoard.type.RecordType;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.adapter.CopyHandler;
16 import org.simantics.db.layer0.util.ClipboardUtils;
17 import org.simantics.db.layer0.util.SimanticsClipboard;
18 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
19 import org.simantics.db.layer0.util.SimanticsClipboardImpl;
20 import org.simantics.db.layer0.util.SimanticsKeys;
21 import org.simantics.db.request.Read;
22 import org.simantics.export.core.ExportContext;
23 import org.simantics.export.core.error.ExportException;
24 import org.simantics.export.core.intf.ExportClass;
25 import org.simantics.export.core.manager.Content;
26 import org.simantics.export.core.manager.TransferableGraphWriter;
27 import org.simantics.export.core.util.ExportQueries;
28 import org.simantics.graph.representation.TransferableGraph1;
29 import org.simantics.utils.datastructures.MapList;
30
31 public class DrawingTemplateExporter implements ExportClass {
32
33         @Override
34         public RecordType options(ExportContext context, Collection<String> content) throws ExportException {
35                 // TODO Auto-generated method stub
36                 return null;
37         }
38
39         @Override
40         public void fillDefaultPrefs(ExportContext ctx, Variant options) throws ExportException {
41                 // TODO Auto-generated method stub
42         }
43
44         @Override
45         public void savePref(Variant options, Preferences contentScopeNode, Preferences workbenchScopeNode) throws ExportException {
46                 // TODO Auto-generated method stub
47         }
48
49         @Override
50         public void loadPref(Variant options, Preferences contentScopeNode, Preferences workbenchScopeNode) throws ExportException {
51                 // TODO Auto-generated method stub
52         }
53
54         @Override
55         public void export(List<Content> contents, 
56                         Object writer_,
57                         ExportContext context, 
58                         Variant options,
59                         IProgressMonitor monitor, 
60                         MapList<Content, Content> attachmentMap
61                         ) throws ExportException {
62                                 
63                 final TransferableGraphWriter writer = (TransferableGraphWriter) writer_;
64                 
65         try {
66                 List<Resource> resources = context.session.syncRequest( ExportQueries.toResources2(contents) );
67                 for ( final Resource model : resources ) {
68                     // TODO: figure out a way to make the TG go directly into a file
69                     // instead of having it all in memory at once.
70                     SimanticsClipboard clipboard = context.session.syncRequest(new Read<SimanticsClipboard>() {
71                         @Override
72                         public SimanticsClipboard perform(ReadGraph graph) throws DatabaseException {
73                             CopyHandler ch = graph.adapt( model, CopyHandler.class );
74                             SimanticsClipboardImpl clipboard = new SimanticsClipboardImpl();
75                             ch.copyToClipboard(graph, clipboard);
76                             return clipboard;
77                         }
78                     });
79                     for (Set<Representation> object : clipboard.getContents()) {
80                         TransferableGraph1 tg = ClipboardUtils.accept(object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);                    
81                         writer.writeFile(TransferableGraph1.BINDING, tg);
82                     }
83                 }
84
85         } catch (DatabaseException e) {
86                 throw new ExportException(e);
87                 }
88
89         }
90
91         @Override
92         public List<String> validate(String contentUri, ExportContext context, Variant options) {
93                 return Collections.emptyList();
94         }
95
96 }