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