1 /*******************************************************************************
2 * Copyright (c) 2007, 2017 Association for Decentralized Information Management
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
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;
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;
26 import java.util.TreeSet;
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;
67 public class PDFDiagramExportWizard extends Wizard implements IExportWizard {
69 private static final Logger LOGGER = LoggerFactory.getLogger(PDFDiagramExportWizard.class);
71 private static final int MAX_RECENT_EXPORT_PATHS = 10;
73 private static final String TAG_PATH = "path";
75 private static final String ATTR_NAME = "name";
77 Deque<String> recentExportPaths;
79 boolean attachTG, attachWiki, addPageNumbers;
81 PDFExportPlan exportPlan;
83 private boolean readPreferences() {
84 IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
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);
96 private void writePreferences() throws IOException {
97 IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
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));
105 if (store.needsSaving())
109 private Deque<String> decodePaths(String recentPathsPref) {
110 Deque<String> result = new LinkedList<>();
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())
118 } catch (IllegalArgumentException e) {
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);
129 return sm.toString();
132 public PDFDiagramExportWizard() {
133 setWindowTitle("Export Diagrams to PDF");
134 setNeedsProgressMonitor(true);
138 public void addPages() {
140 if (exportPlan != null) {
141 addPage(new PDFExportPage(exportPlan));
143 addPage(new NoProjectPage("Export Diagrams to PDF"));
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);
153 public void init(IWorkbench workbench, IStructuredSelection selection) {
156 ISessionContext ctx = SimanticsUI.getSessionContext();
159 IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
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;
171 // Get all model names
173 exportPlan.sessionContext.getSession().syncRequest(new ReadRequest() {
175 public void run(ReadGraph graph) throws DatabaseException {
176 Set<Resource> processed = new HashSet<>();
177 List<NamedResource> models = new ArrayList<>();
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) );
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) );
190 Collections.sort(models, AlphanumComparator.CASE_INSENSITIVE_COMPARATOR);
191 exportPlan.selectableModels.addAll(models);
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);
198 if (exportPlan.selection == null && !exportPlan.selectableModels.isEmpty())
199 exportPlan.selection = exportPlan.selectableModels.get(0);
202 } catch (DatabaseException e) {
203 LOGGER.error("Failed to initialize diagram PDF export wizard input data.", e);
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);
215 FileUtils.deleteAll(exportPlan.exportLocation);
216 } catch (IOException e) {
217 ExceptionUtils.logAndShowError(e);
223 recentExportPaths.addFirst(exportPlan.exportLocation.getAbsolutePath());
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)) {
234 if (recentExportPaths.size() > MAX_RECENT_EXPORT_PATHS)
235 recentExportPaths.pollLast();
237 zoomToFit = exportPlan.fitContentToPageMargins;
238 attachTG = exportPlan.attachTG;
239 attachWiki = exportPlan.attachWiki;
240 addPageNumbers = exportPlan.addPageNumbers;
243 } catch (IOException e) {
244 ErrorLogger.defaultLogError("Failed to write preferences", e);
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() {
251 public boolean select(Object toTest) {
252 Node n = (Node) toTest;
253 return exportPlan.selectedNodeSet.contains(n) && n.getDiagramResource() != null;
255 }, Node.CASE_INSENSITIVE_COMPARATOR);
257 long start = System.currentTimeMillis();
259 getContainer().run(true, true, monitor -> {
261 DiagramPrinter.printToPdf(monitor, exportPlan, exportPlan.exportLocation.toString(), exportPlan.selectedNodes);
262 } catch (PdfException e) {
263 throw new InvocationTargetException(e);
268 } catch (InvocationTargetException e) {
269 Throwable t = e.getTargetException();
270 ExceptionUtils.logAndShowError(t);
272 } catch (InterruptedException e) {
275 long end = System.currentTimeMillis();
276 LOGGER.info("PDF export took " + ((end - start) * 1e-3) + " seconds.");