X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Fpdf%2FPDFDiagramExportWizard.java;h=9cb32a2a7bbb2f4ee27cbb2dd1b57ec6c6540228;hp=ba389f77d3cd149d96e8c4d3db788b06983edb8e;hb=e88be95edf1f80781646cfdf717ec1b663264179;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07 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 index ba389f77d..9cb32a2a7 100644 --- 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * Copyright (c) 2007, 2017 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 @@ -8,6 +8,7 @@ * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation + * Semantum Oy - initial selection handling improvements *******************************************************************************/ package org.simantics.modeling.ui.pdf; @@ -17,16 +18,15 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Deque; +import java.util.HashSet; 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; @@ -36,15 +36,16 @@ import org.eclipse.ui.IExportWizard; import org.eclipse.ui.IMemento; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.preferences.ScopedPreferenceStore; +import org.simantics.NameLabelMode; +import org.simantics.NameLabelUtil; 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.PossibleIndexRoot; 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; @@ -56,12 +57,17 @@ 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.strings.AlphanumComparator; import org.simantics.utils.ui.ErrorLogger; import org.simantics.utils.ui.ExceptionUtils; import org.simantics.utils.ui.workbench.StringMemento; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class PDFDiagramExportWizard extends Wizard implements IExportWizard { + private static final Logger LOGGER = LoggerFactory.getLogger(PDFDiagramExportWizard.class); + private static final int MAX_RECENT_EXPORT_PATHS = 10; private static final String TAG_PATH = "path"; @@ -70,9 +76,9 @@ public class PDFDiagramExportWizard extends Wizard implements IExportWizard { Deque recentExportPaths; boolean zoomToFit; - boolean attachTG, attachWiki; + boolean attachTG, attachWiki, addPageNumbers; - PDFExportPlan exportPlan; + PDFExportPlan exportPlan; private boolean readPreferences() { IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID); @@ -82,6 +88,7 @@ public class PDFDiagramExportWizard extends Wizard implements IExportWizard { 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); + addPageNumbers = store.getBoolean(Preferences.DIAGRAM_EXPORT_PDF_ADD_PAGE_NUMBERS); return true; } @@ -93,13 +100,14 @@ public class PDFDiagramExportWizard extends Wizard implements IExportWizard { 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)); + store.putValue(Preferences.DIAGRAM_EXPORT_PDF_ADD_PAGE_NUMBERS, String.valueOf(addPageNumbers)); if (store.needsSaving()) store.save(); } private Deque decodePaths(String recentPathsPref) { - Deque result = new LinkedList(); + Deque result = new LinkedList<>(); try { StringMemento sm = new StringMemento(recentPathsPref); for (IMemento m : sm.getChildren(TAG_PATH)) { @@ -137,7 +145,7 @@ public class PDFDiagramExportWizard extends Wizard implements IExportWizard { } private NamedResource toNamedResource(ReadGraph graph, Resource r) throws DatabaseException { - String name = graph.syncRequest(new GetName(r)); + String name = NameLabelUtil.modalName(graph, r, NameLabelMode.NAME_AND_LABEL); return new NamedResource(name, r); } @@ -145,7 +153,7 @@ public class PDFDiagramExportWizard extends Wizard implements IExportWizard { public void init(IWorkbench workbench, IStructuredSelection selection) { readPreferences(); - ISessionContext ctx = SimanticsUI.getSessionContext(); + ISessionContext ctx = Simantics.getSessionContext(); if (ctx == null) return; IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT); @@ -154,54 +162,45 @@ public class PDFDiagramExportWizard extends Wizard implements IExportWizard { exportPlan = new PDFExportPlan(ctx, recentExportPaths); exportPlan.project = project; - final Object selectedObject = selection.getFirstElement(); + exportPlan.initialSelection = selection; exportPlan.fitContentToPageMargins = zoomToFit; exportPlan.attachTG = attachTG; exportPlan.attachWiki = attachWiki; + exportPlan.addPageNumbers = addPageNumbers; // 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; - } - } + Set processed = new HashSet<>(); + List models = new ArrayList<>(); - 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 root : ontologies) { + if (processed.add(root)) + models.add( toNamedResource(graph, root) ); } - + 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) ); + if (processed.add(model)) + models.add( toNamedResource(graph, model) ); } - Collections.sort(models); + Collections.sort(models, AlphanumComparator.CASE_INSENSITIVE_COMPARATOR); exportPlan.selectableModels.addAll(models); - if (selection == null && !exportPlan.selectableModels.isEmpty()) { + + Resource selected = ResourceAdaptionUtils.toSingleResource(selection.getFirstElement()); + Resource indexRoot = selected != null ? graph.sync(new PossibleIndexRoot(selected)) : null; + if (indexRoot != null) + exportPlan.initialModelSelection = exportPlan.selection = toNamedResource(graph, indexRoot); + + if (exportPlan.selection == null && !exportPlan.selectableModels.isEmpty()) exportPlan.selection = exportPlan.selectableModels.get(0); - } } }); } catch (DatabaseException e) { - e.printStackTrace(); + LOGGER.error("Failed to initialize diagram PDF export wizard input data.", e); } } @@ -224,7 +223,7 @@ public class PDFDiagramExportWizard extends Wizard implements IExportWizard { recentExportPaths.addFirst(exportPlan.exportLocation.getAbsolutePath()); // Remove duplicates - Set dups = new TreeSet(String.CASE_INSENSITIVE_ORDER); + Set dups = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); for (Iterator it = recentExportPaths.iterator(); it.hasNext();) { String path = it.next(); if (!dups.add(path)) { @@ -238,6 +237,7 @@ public class PDFDiagramExportWizard extends Wizard implements IExportWizard { zoomToFit = exportPlan.fitContentToPageMargins; attachTG = exportPlan.attachTG; attachWiki = exportPlan.attachWiki; + addPageNumbers = exportPlan.addPageNumbers; writePreferences(); } catch (IOException e) { @@ -256,18 +256,13 @@ public class PDFDiagramExportWizard extends Wizard implements IExportWizard { 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(); - } + getContainer().run(true, true, monitor -> { + try { + DiagramPrinter.printToPdf(monitor, exportPlan, exportPlan.exportLocation.toString(), exportPlan.selectedNodes); + } catch (PdfException e) { + throw new InvocationTargetException(e); + } finally { + monitor.done(); } }); } catch (InvocationTargetException e) { @@ -278,7 +273,7 @@ public class PDFDiagramExportWizard extends Wizard implements IExportWizard { return false; } long end = System.currentTimeMillis(); - System.out.println("PDF export took " + ((end - start) * 1e-3) + " seconds."); + LOGGER.info("PDF export took " + ((end - start) * 1e-3) + " seconds."); return true; }