]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/pdf/PDFDiagramExportWizard.java
Remove usage of deprecated SimanticsUI-methods
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / pdf / PDFDiagramExportWizard.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2017 Association for Decentralized Information Management
3  * in 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  *     Semantum Oy - initial selection handling improvements
12  *******************************************************************************/
13 package org.simantics.modeling.ui.pdf;
14
15 import java.io.IOException;
16 import java.lang.reflect.InvocationTargetException;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Deque;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Set;
26 import java.util.TreeSet;
27
28 import org.eclipse.core.runtime.preferences.InstanceScope;
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.jface.preference.IPersistentPreferenceStore;
31 import org.eclipse.jface.preference.IPreferenceStore;
32 import org.eclipse.jface.viewers.IFilter;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.jface.wizard.Wizard;
35 import org.eclipse.ui.IExportWizard;
36 import org.eclipse.ui.IMemento;
37 import org.eclipse.ui.IWorkbench;
38 import org.eclipse.ui.preferences.ScopedPreferenceStore;
39 import org.simantics.NameLabelMode;
40 import org.simantics.NameLabelUtil;
41 import org.simantics.Simantics;
42 import org.simantics.db.ReadGraph;
43 import org.simantics.db.Resource;
44 import org.simantics.db.common.NamedResource;
45 import org.simantics.db.common.request.ObjectsWithType;
46 import org.simantics.db.common.request.PossibleIndexRoot;
47 import org.simantics.db.common.request.ReadRequest;
48 import org.simantics.db.exception.DatabaseException;
49 import org.simantics.db.management.ISessionContext;
50 import org.simantics.layer0.Layer0;
51 import org.simantics.modeling.requests.Node;
52 import org.simantics.modeling.ui.Activator;
53 import org.simantics.modeling.ui.utils.NoProjectPage;
54 import org.simantics.project.IProject;
55 import org.simantics.project.ProjectKeys;
56 import org.simantics.simulation.ontology.SimulationResource;
57 import org.simantics.ui.SimanticsUI;
58 import org.simantics.ui.utils.ResourceAdaptionUtils;
59 import org.simantics.utils.FileUtils;
60 import org.simantics.utils.strings.AlphanumComparator;
61 import org.simantics.utils.ui.ErrorLogger;
62 import org.simantics.utils.ui.ExceptionUtils;
63 import org.simantics.utils.ui.workbench.StringMemento;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66
67 public class PDFDiagramExportWizard extends Wizard implements IExportWizard {
68
69     private static final Logger LOGGER = LoggerFactory.getLogger(PDFDiagramExportWizard.class);
70
71     private static final int    MAX_RECENT_EXPORT_PATHS = 10;
72
73     private static final String TAG_PATH  = "path";
74
75     private static final String ATTR_NAME = "name";
76
77     Deque<String>               recentExportPaths;
78     boolean                     zoomToFit;
79     boolean                     attachTG, attachWiki, addPageNumbers;
80
81     PDFExportPlan               exportPlan;
82
83     private boolean readPreferences() {
84         IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
85
86         String recentPathsPref = store.getString(Preferences.DIAGRAM_EXPORT_PDF_PATH);
87         recentExportPaths = decodePaths(recentPathsPref);
88         zoomToFit = store.getBoolean(Preferences.DIAGRAM_EXPORT_PDF_ZOOM_TO_FIT);
89         attachTG =  store.getBoolean(Preferences.DIAGRAM_EXPORT_PDF_ATTACH_TG);
90         attachWiki =  store.getBoolean(Preferences.DIAGRAM_EXPORT_PDF_ATTACH_WIKI);
91         addPageNumbers =  store.getBoolean(Preferences.DIAGRAM_EXPORT_PDF_ADD_PAGE_NUMBERS);
92
93         return true;
94     }
95
96     private void writePreferences() throws IOException {
97         IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
98
99         store.putValue(Preferences.DIAGRAM_EXPORT_PDF_PATH, encodePaths(recentExportPaths));
100         store.putValue(Preferences.DIAGRAM_EXPORT_PDF_ZOOM_TO_FIT, String.valueOf(zoomToFit));
101         store.putValue(Preferences.DIAGRAM_EXPORT_PDF_ATTACH_TG, String.valueOf(attachTG));
102         store.putValue(Preferences.DIAGRAM_EXPORT_PDF_ATTACH_WIKI, String.valueOf(attachWiki));
103         store.putValue(Preferences.DIAGRAM_EXPORT_PDF_ADD_PAGE_NUMBERS, String.valueOf(addPageNumbers));
104
105         if (store.needsSaving())
106             store.save();
107     }
108
109     private Deque<String> decodePaths(String recentPathsPref) {
110         Deque<String> result = new LinkedList<>();
111         try {
112             StringMemento sm = new StringMemento(recentPathsPref);
113             for (IMemento m : sm.getChildren(TAG_PATH)) {
114                 String name = m.getString(ATTR_NAME);
115                 if (name != null && !name.isEmpty())
116                     result.add(name);
117             }
118         } catch (IllegalArgumentException e) {
119         }
120         return result;
121     }
122
123     private String encodePaths(Deque<String> recentPaths) {
124         StringMemento sm = new StringMemento();
125         for (String path : recentPaths) {
126             IMemento m = sm.createChild(TAG_PATH);
127             m.putString(ATTR_NAME, path);
128         }
129         return sm.toString();
130     }
131
132     public PDFDiagramExportWizard() {
133         setWindowTitle("Export Diagrams to PDF");
134         setNeedsProgressMonitor(true);
135     }
136
137     @Override
138     public void addPages() {
139         super.addPages();
140         if (exportPlan != null) {
141             addPage(new PDFExportPage(exportPlan));
142         } else {
143             addPage(new NoProjectPage("Export Diagrams to PDF"));
144         }
145     }
146
147     private NamedResource toNamedResource(ReadGraph graph, Resource r) throws DatabaseException {
148         String name = NameLabelUtil.modalName(graph, r, NameLabelMode.NAME_AND_LABEL);
149         return new NamedResource(name, r);
150     }
151
152     @Override
153     public void init(IWorkbench workbench, IStructuredSelection selection) {
154         readPreferences();
155
156         ISessionContext ctx = Simantics.getSessionContext();
157         if (ctx == null)
158             return;
159         IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
160         if (project == null)
161             return;
162
163         exportPlan = new PDFExportPlan(ctx, recentExportPaths);
164         exportPlan.project = project;
165         exportPlan.initialSelection = selection;
166         exportPlan.fitContentToPageMargins = zoomToFit;
167         exportPlan.attachTG = attachTG;
168         exportPlan.attachWiki = attachWiki;
169         exportPlan.addPageNumbers = addPageNumbers;
170         
171         // Get all model names
172         try {
173             exportPlan.sessionContext.getSession().syncRequest(new ReadRequest() {
174                 @Override
175                 public void run(ReadGraph graph) throws DatabaseException {
176                     Set<Resource> processed = new HashSet<>();
177                     List<NamedResource> models = new ArrayList<>();
178
179                     Collection<Resource> ontologies = Simantics.applySCL("Simantics/SharedOntologies", "traverseSharedOntologies", graph, graph.getRootLibrary());
180                     for (Resource root : ontologies) {
181                         if (processed.add(root))
182                             models.add( toNamedResource(graph, root) );
183                     }
184
185                     for (Resource model : graph.syncRequest(new ObjectsWithType(exportPlan.project.get(),
186                             Layer0.getInstance(graph).ConsistsOf, SimulationResource.getInstance(graph).Model))) {
187                         if (processed.add(model))
188                             models.add( toNamedResource(graph, model) );
189                     }
190                     Collections.sort(models, AlphanumComparator.CASE_INSENSITIVE_COMPARATOR);
191                     exportPlan.selectableModels.addAll(models);
192
193                     Resource selected = ResourceAdaptionUtils.toSingleResource(selection.getFirstElement());
194                     Resource indexRoot = selected != null ? graph.sync(new PossibleIndexRoot(selected)) : null;
195                     if (indexRoot != null)
196                         exportPlan.initialModelSelection = exportPlan.selection = toNamedResource(graph, indexRoot);
197
198                     if (exportPlan.selection == null && !exportPlan.selectableModels.isEmpty())
199                         exportPlan.selection = exportPlan.selectableModels.get(0);
200                 }
201             });
202         } catch (DatabaseException e) {
203             LOGGER.error("Failed to initialize diagram PDF export wizard input data.", e);
204         }
205     }
206
207     @Override
208     public boolean performFinish() {
209         if (exportPlan.exportLocation.exists()) {
210             boolean confirmed = MessageDialog.openConfirm(getShell(), "Overwrite", "Are you sure you want to overwrite " + exportPlan.exportLocation);
211             if (!confirmed)
212                 return false;
213
214             try {
215                 FileUtils.deleteAll(exportPlan.exportLocation);
216             } catch (IOException e) {
217                 ExceptionUtils.logAndShowError(e);
218                 return false;
219             }
220         }
221
222         try {
223             recentExportPaths.addFirst(exportPlan.exportLocation.getAbsolutePath());
224
225             // Remove duplicates
226             Set<String> dups = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
227             for (Iterator<String> it = recentExportPaths.iterator(); it.hasNext();) {
228                 String path = it.next();
229                 if (!dups.add(path)) {
230                     it.remove();
231                 }
232             }
233
234             if (recentExportPaths.size() > MAX_RECENT_EXPORT_PATHS)
235                 recentExportPaths.pollLast();
236
237             zoomToFit = exportPlan.fitContentToPageMargins;
238             attachTG = exportPlan.attachTG;
239             attachWiki = exportPlan.attachWiki;
240             addPageNumbers = exportPlan.addPageNumbers;
241
242             writePreferences();
243         } catch (IOException e) {
244             ErrorLogger.defaultLogError("Failed to write preferences", e);
245         }
246
247         // Make sure that the diagrams are printed in the same order as the user
248         // saw them in the wizard.
249         exportPlan.selectedNodes = exportPlan.nodes.depthFirstFlatten(new IFilter() {
250             @Override
251             public boolean select(Object toTest) {
252                 Node n = (Node) toTest;
253                 return exportPlan.selectedNodeSet.contains(n) && n.getDiagramResource() != null;
254             }
255         }, Node.CASE_INSENSITIVE_COMPARATOR);
256
257         long start = System.currentTimeMillis();
258         try {
259             getContainer().run(true, true, monitor -> {
260                 try {
261                     DiagramPrinter.printToPdf(monitor, exportPlan, exportPlan.exportLocation.toString(), exportPlan.selectedNodes);
262                 } catch (PdfException e) {
263                     throw new InvocationTargetException(e);
264                 } finally {
265                     monitor.done();
266                 }
267             });
268         } catch (InvocationTargetException e) {
269             Throwable t = e.getTargetException();
270             ExceptionUtils.logAndShowError(t);
271             return false;
272         } catch (InterruptedException e) {
273             return false;
274         }
275         long end = System.currentTimeMillis();
276         LOGGER.info("PDF export took " + ((end - start) * 1e-3) + " seconds.");
277
278         return true;
279     }
280
281 }