--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.pdf;\r
+\r
+import java.io.IOException;\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.Deque;\r
+import java.util.Iterator;\r
+import java.util.LinkedList;\r
+import java.util.List;\r
+import java.util.Set;\r
+import java.util.TreeSet;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.preferences.InstanceScope;\r
+import org.eclipse.jface.dialogs.MessageDialog;\r
+import org.eclipse.jface.operation.IRunnableWithProgress;\r
+import org.eclipse.jface.preference.IPersistentPreferenceStore;\r
+import org.eclipse.jface.preference.IPreferenceStore;\r
+import org.eclipse.jface.viewers.IFilter;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.wizard.Wizard;\r
+import org.eclipse.ui.IExportWizard;\r
+import org.eclipse.ui.IMemento;\r
+import org.eclipse.ui.IWorkbench;\r
+import org.eclipse.ui.preferences.ScopedPreferenceStore;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.graph.impl.request.GetName;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.NamedResource;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.request.ActiveModels;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.requests.Node;\r
+import org.simantics.modeling.ui.Activator;\r
+import org.simantics.modeling.ui.utils.NoProjectPage;\r
+import org.simantics.project.IProject;\r
+import org.simantics.project.ProjectKeys;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.ResourceAdaptionUtils;\r
+import org.simantics.utils.FileUtils;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
+import org.simantics.utils.ui.workbench.StringMemento;\r
+\r
+public class PDFDiagramExportWizard extends Wizard implements IExportWizard {\r
+\r
+ private static final int MAX_RECENT_EXPORT_PATHS = 10;\r
+\r
+ private static final String TAG_PATH = "path";\r
+\r
+ private static final String ATTR_NAME = "name";\r
+\r
+ Deque<String> recentExportPaths;\r
+ boolean zoomToFit;\r
+ boolean attachTG, attachWiki;\r
+\r
+ PDFExportPlan exportPlan;\r
+\r
+ private boolean readPreferences() {\r
+ IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);\r
+\r
+ String recentPathsPref = store.getString(Preferences.DIAGRAM_EXPORT_PDF_PATH);\r
+ recentExportPaths = decodePaths(recentPathsPref);\r
+ zoomToFit = store.getBoolean(Preferences.DIAGRAM_EXPORT_PDF_ZOOM_TO_FIT);\r
+ attachTG = store.getBoolean(Preferences.DIAGRAM_EXPORT_PDF_ATTACH_TG);\r
+ attachWiki = store.getBoolean(Preferences.DIAGRAM_EXPORT_PDF_ATTACH_WIKI);\r
+\r
+ return true;\r
+ }\r
+\r
+ private void writePreferences() throws IOException {\r
+ IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);\r
+\r
+ store.putValue(Preferences.DIAGRAM_EXPORT_PDF_PATH, encodePaths(recentExportPaths));\r
+ store.putValue(Preferences.DIAGRAM_EXPORT_PDF_ZOOM_TO_FIT, String.valueOf(zoomToFit));\r
+ store.putValue(Preferences.DIAGRAM_EXPORT_PDF_ATTACH_TG, String.valueOf(attachTG));\r
+ store.putValue(Preferences.DIAGRAM_EXPORT_PDF_ATTACH_WIKI, String.valueOf(attachWiki));\r
+\r
+ if (store.needsSaving())\r
+ store.save();\r
+ }\r
+\r
+ private Deque<String> decodePaths(String recentPathsPref) {\r
+ Deque<String> result = new LinkedList<String>();\r
+ try {\r
+ StringMemento sm = new StringMemento(recentPathsPref);\r
+ for (IMemento m : sm.getChildren(TAG_PATH)) {\r
+ String name = m.getString(ATTR_NAME);\r
+ if (name != null && !name.isEmpty())\r
+ result.add(name);\r
+ }\r
+ } catch (IllegalArgumentException e) {\r
+ }\r
+ return result;\r
+ }\r
+\r
+ private String encodePaths(Deque<String> recentPaths) {\r
+ StringMemento sm = new StringMemento();\r
+ for (String path : recentPaths) {\r
+ IMemento m = sm.createChild(TAG_PATH);\r
+ m.putString(ATTR_NAME, path);\r
+ }\r
+ return sm.toString();\r
+ }\r
+\r
+ public PDFDiagramExportWizard() {\r
+ setWindowTitle("Export Diagrams to PDF");\r
+ setNeedsProgressMonitor(true);\r
+ }\r
+\r
+ @Override\r
+ public void addPages() {\r
+ super.addPages();\r
+ if (exportPlan != null) {\r
+ addPage(new PDFExportPage(exportPlan));\r
+ } else {\r
+ addPage(new NoProjectPage("Export Diagrams to PDF"));\r
+ }\r
+ }\r
+\r
+ private NamedResource toNamedResource(ReadGraph graph, Resource r) throws DatabaseException {\r
+ String name = graph.syncRequest(new GetName(r));\r
+ return new NamedResource(name, r);\r
+ }\r
+\r
+ @Override\r
+ public void init(IWorkbench workbench, IStructuredSelection selection) {\r
+ readPreferences();\r
+\r
+ ISessionContext ctx = SimanticsUI.getSessionContext();\r
+ if (ctx == null)\r
+ return;\r
+ IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);\r
+ if (project == null)\r
+ return;\r
+\r
+ exportPlan = new PDFExportPlan(ctx, recentExportPaths);\r
+ exportPlan.project = project;\r
+ final Object selectedObject = selection.getFirstElement();\r
+ exportPlan.fitContentToPageMargins = zoomToFit;\r
+ exportPlan.attachTG = attachTG;\r
+ exportPlan.attachWiki = attachWiki;\r
+ \r
+ // Get all model names\r
+ try {\r
+ exportPlan.sessionContext.getSession().syncRequest(new ReadRequest() {\r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ Resource selection = ResourceAdaptionUtils.toSingleResource(selectedObject);\r
+ if (selection != null) {\r
+ //exportModel.selection = new NamedResource(name + " (input selection)", selection);\r
+ exportPlan.selection = toNamedResource(graph, selection);\r
+ exportPlan.selectableModels.add(exportPlan.selection);\r
+ } else {\r
+ for (Resource activeModel : graph.syncRequest(new ActiveModels(exportPlan.project.get()))) {\r
+ selection = activeModel;\r
+ exportPlan.selection = toNamedResource(graph, activeModel);\r
+ exportPlan.selectableModels.add( exportPlan.selection );\r
+ break;\r
+ }\r
+ }\r
+\r
+ List<NamedResource> models = new ArrayList<NamedResource>();\r
+ \r
+ Collection<Resource> ontologies = Simantics.applySCL("Simantics/SharedOntologies", "traverseSharedOntologies", graph, graph.getRootLibrary());\r
+ for (Resource model : ontologies) {\r
+ if (model.equals(selection))\r
+ continue;\r
+ models.add( toNamedResource(graph, model) );\r
+ }\r
+ \r
+ for (Resource model : graph.syncRequest(new ObjectsWithType(exportPlan.project.get(),\r
+ Layer0.getInstance(graph).ConsistsOf, SimulationResource.getInstance(graph).Model))) {\r
+ if (model.equals(selection))\r
+ continue;\r
+ models.add( toNamedResource(graph, model) );\r
+ }\r
+ Collections.sort(models);\r
+ exportPlan.selectableModels.addAll(models);\r
+ if (selection == null && !exportPlan.selectableModels.isEmpty()) {\r
+ exportPlan.selection = exportPlan.selectableModels.get(0);\r
+ }\r
+ }\r
+ });\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public boolean performFinish() {\r
+ if (exportPlan.exportLocation.exists()) {\r
+ boolean confirmed = MessageDialog.openConfirm(getShell(), "Overwrite", "Are you sure you want to overwrite " + exportPlan.exportLocation);\r
+ if (!confirmed)\r
+ return false;\r
+\r
+ try {\r
+ FileUtils.deleteAll(exportPlan.exportLocation);\r
+ } catch (IOException e) {\r
+ ExceptionUtils.logAndShowError(e);\r
+ return false;\r
+ }\r
+ }\r
+\r
+ try {\r
+ recentExportPaths.addFirst(exportPlan.exportLocation.getAbsolutePath());\r
+\r
+ // Remove duplicates\r
+ Set<String> dups = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);\r
+ for (Iterator<String> it = recentExportPaths.iterator(); it.hasNext();) {\r
+ String path = it.next();\r
+ if (!dups.add(path)) {\r
+ it.remove();\r
+ }\r
+ }\r
+\r
+ if (recentExportPaths.size() > MAX_RECENT_EXPORT_PATHS)\r
+ recentExportPaths.pollLast();\r
+\r
+ zoomToFit = exportPlan.fitContentToPageMargins;\r
+ attachTG = exportPlan.attachTG;\r
+ attachWiki = exportPlan.attachWiki;\r
+\r
+ writePreferences();\r
+ } catch (IOException e) {\r
+ ErrorLogger.defaultLogError("Failed to write preferences", e);\r
+ }\r
+\r
+ // Make sure that the diagrams are printed in the same order as the user\r
+ // saw them in the wizard.\r
+ exportPlan.selectedNodes = exportPlan.nodes.depthFirstFlatten(new IFilter() {\r
+ @Override\r
+ public boolean select(Object toTest) {\r
+ Node n = (Node) toTest;\r
+ return exportPlan.selectedNodeSet.contains(n) && n.getDiagramResource() != null;\r
+ }\r
+ }, Node.CASE_INSENSITIVE_COMPARATOR);\r
+\r
+ long start = System.currentTimeMillis();\r
+ try {\r
+ getContainer().run(true, true, new IRunnableWithProgress() {\r
+ @Override\r
+ public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
+ try {\r
+ // Print pdf\r
+ DiagramPrinter.printToPdf(monitor, exportPlan, exportPlan.exportLocation.toString(), exportPlan.selectedNodes, exportPlan.sessionContext);\r
+ \r
+ } catch (PdfException e) {\r
+ throw new InvocationTargetException(e);\r
+ } finally {\r
+ monitor.done();\r
+ }\r
+ }\r
+ });\r
+ } catch (InvocationTargetException e) {\r
+ Throwable t = e.getTargetException();\r
+ ExceptionUtils.logAndShowError(t);\r
+ return false;\r
+ } catch (InterruptedException e) {\r
+ return false;\r
+ }\r
+ long end = System.currentTimeMillis();\r
+ System.out.println("PDF export took " + ((end - start) * 1e-3) + " seconds.");\r
+\r
+ return true;\r
+ }\r
+\r
+}\r